Skip to content

Commit

Permalink
[6.x] Fix model restoring right after soft deleting it (#31719)
Browse files Browse the repository at this point in the history
* fix(eloquent) Fix model restoring after soft deleting it

* Code review - remove comments
  • Loading branch information
danielsuguimoto authored Mar 6, 2020
1 parent 2460f09 commit 898f24e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ protected function runSoftDelete()
}

$query->update($columns);

$this->syncOriginalAttributes(array_keys($columns));
}

/**
Expand Down
17 changes: 14 additions & 3 deletions tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ public function testUpdateModelAfterSoftDeleting()
/** @var SoftDeletesTestUser $userModel */
$userModel = SoftDeletesTestUser::find(2);
$userModel->delete();
$userModel->syncOriginal();
$this->assertEquals($now->toDateTimeString(), $userModel->getOriginal('deleted_at'));
$this->assertNull(SoftDeletesTestUser::find(2));
$this->assertEquals($userModel, SoftDeletesTestUser::withTrashed()->find(2));
Expand All @@ -285,7 +284,6 @@ public function testRestoreAfterSoftDelete()
/** @var SoftDeletesTestUser $userModel */
$userModel = SoftDeletesTestUser::find(2);
$userModel->delete();
$userModel->syncOriginal();
$userModel->restore();

$this->assertEquals($userModel->id, SoftDeletesTestUser::find(2)->id);
Expand All @@ -304,12 +302,25 @@ public function testSoftDeleteAfterRestoring()
$this->assertEquals($userModel->deleted_at, SoftDeletesTestUser::find(1)->deleted_at);
$this->assertEquals($userModel->getOriginal('deleted_at'), SoftDeletesTestUser::find(1)->deleted_at);
$userModel->delete();
$userModel->syncOriginal();
$this->assertNull(SoftDeletesTestUser::find(1));
$this->assertEquals($userModel->deleted_at, SoftDeletesTestUser::withTrashed()->find(1)->deleted_at);
$this->assertEquals($userModel->getOriginal('deleted_at'), SoftDeletesTestUser::withTrashed()->find(1)->deleted_at);
}

public function testModifyingBeforeSoftDeletingAndRestoring()
{
$this->createUsers();

/** @var SoftDeletesTestUser $userModel */
$userModel = SoftDeletesTestUser::find(2);
$userModel->email = 'foo@bar.com';
$userModel->delete();
$userModel->restore();

$this->assertEquals($userModel->id, SoftDeletesTestUser::find(2)->id);
$this->assertSame('foo@bar.com', SoftDeletesTestUser::find(2)->email);
}

public function testUpdateOrCreate()
{
$this->createUsers();
Expand Down
4 changes: 4 additions & 0 deletions tests/Database/DatabaseSoftDeletingTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function testDeleteSetsSoftDeletedColumn()
'deleted_at' => 'date-time',
'updated_at' => 'date-time',
]);
$model->shouldReceive('syncOriginalAttributes')->once()->with([
'deleted_at',
'updated_at',
]);
$model->delete();

$this->assertInstanceOf(Carbon::class, $model->deleted_at);
Expand Down

0 comments on commit 898f24e

Please sign in to comment.