Skip to content

Commit e1e381e

Browse files
fix: re-index state array after block deletion to prevent undefined k… (#21)
* fix: re-index state array after block deletion to prevent undefined key error * ci: update php version in ci * fix: testbench package issue * fix: update test bench version to working one' --------- Co-authored-by: GigaGiorgadze <giga.giorgadze.11@gmail.com>
1 parent c9ba789 commit e1e381e

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup PHP
1717
uses: shivammathur/setup-php@v2
1818
with:
19-
php-version: '8.1'
19+
php-version: '8.3'
2020
coverage: none
2121

2222
- name: Install composer dependencies

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"laravel/pint": "^1.0",
3232
"nunomaduro/collision": "^7.9||^8.0",
3333
"nunomaduro/larastan": "^2.0.1||^3.0",
34-
"orchestra/testbench": "^10.0.0||^9.0||^8.35",
34+
"orchestra/testbench": "^10.0.0||^9.16||^8.35",
3535
"pestphp/pest": "^3.1||^2.0",
3636
"pestphp/pest-plugin-arch": "^3.0||^2.0",
3737
"pestphp/pest-plugin-laravel": "^3.0||^2.0",
@@ -78,4 +78,4 @@
7878
},
7979
"minimum-stability": "dev",
8080
"prefer-stable": true
81-
}
81+
}

src/Components/Forms/Actions/DeletePageBuilderBlockAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected function setUp(): void
4343
$items = $component->getState();
4444
unset($items[$arguments['index']]);
4545

46-
$component->state($items);
46+
$component->state(array_values($items));
4747

4848
$action->sendSuccessNotification();
4949

tests/Forms/PageBuilderTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,49 @@
122122
]);
123123
});
124124

125+
it('can delete multiple blocks sequentially without undefined array key error', function () {
126+
$blocks = PageBuilderBlock::factory()->count(2)->sequence(
127+
['order' => 0],
128+
['order' => 1],
129+
)->create([
130+
'block_type' => ViewBlock::class,
131+
'page_builder_blockable_id' => $this->page->id,
132+
'page_builder_blockable_type' => Page::class,
133+
'data' => [
134+
'image' => 'https://example.com/image.jpg',
135+
'hero_button' => [
136+
'text' => 'Test',
137+
'url' => 'https://example.com',
138+
],
139+
],
140+
]);
141+
142+
$component = livewire(TestComponentWithPageBuilderRenderedUsingViews::class);
143+
144+
// Delete the first block (index 0)
145+
$component
146+
->mountFormComponentAction('website_content', 'delete', ['index' => 0, 'item' => $blocks->get(0)->id])
147+
->callMountedFormComponentAction()
148+
->assertHasNoErrors()
149+
->assertFormSet([
150+
'website_content' => [
151+
[
152+
'id' => $blocks->get(1)->id,
153+
],
154+
],
155+
]);
156+
157+
// Delete the second block — now also at index 0 after re-indexing.
158+
// This used to throw "Undefined array key 0".
159+
$component
160+
->mountFormComponentAction('website_content', 'delete', ['index' => 0, 'item' => $blocks->get(1)->id])
161+
->callMountedFormComponentAction()
162+
->assertHasNoErrors()
163+
->assertFormSet([
164+
'website_content' => [],
165+
]);
166+
});
167+
125168
it('can reorder existing blocks', function () {
126169
$blocks = PageBuilderBlock::factory()->count(3)->sequence(
127170
[

0 commit comments

Comments
 (0)