Skip to content

Commit

Permalink
1.) added: bugfix to SortableTrait for deleting
Browse files Browse the repository at this point in the history
2.) added: bugfix and proper test coverage for deleting
3.) added: changes to config to avoid issues with old version package users

Signed-off-by: Oskars Germovs <[email protected]>
  • Loading branch information
Faks committed Nov 24, 2024
1 parent f53e5a6 commit f88f73d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions config/eloquent-sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
* Define if the models should sort when updating.
* When true, the package will automatically update the order of models when one is updated.
*/
'sort_when_updating' => true,
'sort_when_updating' => false,

/*
* Define if the models should sort when deleting.
* When true, the package will automatically update the order of models when one is deleted.
*/
'sort_when_deleting' => true,
'sort_when_deleting' => false,

/*
* Define if the timestamps should be ignored when sorting.
Expand Down
2 changes: 1 addition & 1 deletion src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public static function setMassNewOrder(
}

$caseStatement = collect($getSortables)->reduce(function (string $carry, int $id) use (&$incrementOrder) {
$incrementOrder++;
$carry .= "WHEN {$id} THEN {$incrementOrder} ";
$incrementOrder++;
return $carry;
}, '');

Expand Down
32 changes: 25 additions & 7 deletions tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function it_can_get_the_highest_order_number()
public function it_can_get_the_highest_order_number_with_trashed_models()
{
$this->setUpSoftDeletes();
config()->set('eloquent-sortable.sort_when_deleting', false);

DummyWithSoftDeletes::first()->delete();

Expand Down Expand Up @@ -504,18 +505,35 @@ public function it_does_not_update_order_when_sortables_is_not_set_on_update()
}

/** @test */
public function it_updates_order_when_sortables_property_is_set_on_delete()
public function it_reorders_models_when_a_model_is_deleted()
{
$modelToDelete = Dummy::first();
$remainingModels = Dummy::where('id', '!=', $modelToDelete->id)->pluck('id');
// Use initial data from SQLite

$newOrder = $remainingModels->shuffle()->toArray();
$modelToDelete->sortables = $newOrder;
// Store the original order before deletion
$originalOrder = Dummy::orderBy('order_column')->pluck('order_column')->toArray();

// Ensure the config is set to reorder on delete
config()->set('eloquent-sortable.sort_when_deleting', true);
$this->assertTrue(config('eloquent-sortable.sort_when_deleting'), 'sort_when_deleting should be true');

// Act: Delete one model (e.g., the one with order_column = 1)
$modelToDelete = Dummy::query()->find(1);
$modelToDelete->delete();

foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
$this->assertEquals($newOrder[$i], $dummy->id);
// Fetch the new order after deletion
$newOrder = Dummy::orderBy('order_column')->pluck('order_column')->toArray();

// Assert: The new order should not match the original order
$this->assertNotEquals($newOrder, $originalOrder, 'The models should have been reordered after deletion.');

// Additional Assert: Ensure `order_column` values are gapless and sequential
$remainingModels = Dummy::orderBy('order_column')->get();
foreach ($remainingModels as $index => $model) {
$this->assertSame(
$index + 1,
(int)$model->order_column,
"The order_column value should be sequential and gapless after deletion."
);
}
}

Expand Down

0 comments on commit f88f73d

Please sign in to comment.