From f27799c3f2d36cee21175937ace6f5c4178d2d30 Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Thu, 31 May 2018 21:03:25 +0300 Subject: [PATCH 1/4] Fix: soft-delete does not sync original. --- .../Database/Eloquent/SoftDeletes.php | 2 ++ ...baseEloquentSoftDeletesIntegrationTest.php | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/SoftDeletes.php b/src/Illuminate/Database/Eloquent/SoftDeletes.php index d8b736306813..dfc53c6c9803 100644 --- a/src/Illuminate/Database/Eloquent/SoftDeletes.php +++ b/src/Illuminate/Database/Eloquent/SoftDeletes.php @@ -77,6 +77,8 @@ protected function runSoftDelete() } $query->update($columns); + + $this->syncOriginal(); } /** diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 140a1a1d5013..88d859fcd001 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -4,7 +4,6 @@ use Illuminate\Support\Carbon; use PHPUnit\Framework\TestCase; -use Illuminate\Database\Connection; use Illuminate\Pagination\Paginator; use Illuminate\Database\Query\Builder; use Illuminate\Database\Eloquent\SoftDeletes; @@ -254,6 +253,37 @@ public function testFirstOrCreate() $this->assertCount(3, SoftDeletesTestUser::withTrashed()->get()); } + /** + * @throws \Exception + */ + public function testUpdateModelAfterSoftDeleting() + { + $now = Carbon::now(); + $this->createUsers(); + + /** @var SoftDeletesTestUser $userModel */ + $userModel = SoftDeletesTestUser::find(2); + $userModel->delete(); + $this->assertEquals($now->toDateTimeString(), $userModel->getOriginal('deleted_at')); + $this->assertNull(SoftDeletesTestUser::find(2)); + $this->assertEquals($userModel, SoftDeletesTestUser::withTrashed()->find(2)); + } + + /** + * @throws \Exception + */ + public function testRestoreAfterSoftDelete() + { + $this->createUsers(); + + /** @var SoftDeletesTestUser $userModel */ + $userModel = SoftDeletesTestUser::find(2); + $userModel->delete(); + $userModel->restore(); + + $this->assertEquals($userModel->id, SoftDeletesTestUser::find(2)->id); + } + public function testUpdateOrCreate() { $this->createUsers(); From 10c549f382c123264bfdd2aecd04124f3831aef1 Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Thu, 31 May 2018 21:19:43 +0300 Subject: [PATCH 2/4] fix mock data. --- tests/Database/DatabaseSoftDeletingTraitTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Database/DatabaseSoftDeletingTraitTest.php b/tests/Database/DatabaseSoftDeletingTraitTest.php index ca076439ca3d..ecd45d93600b 100644 --- a/tests/Database/DatabaseSoftDeletingTraitTest.php +++ b/tests/Database/DatabaseSoftDeletingTraitTest.php @@ -102,4 +102,8 @@ public function getUpdatedAtColumn() { return defined('static::UPDATED_AT') ? static::UPDATED_AT : 'updated_at'; } + + public function syncOriginal() + { + } } From f8706fc7a54f04a171708c36bc0bf7494c080678 Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Fri, 1 Jun 2018 19:16:39 +0300 Subject: [PATCH 3/4] run syncOriginal in soft-deleting, only if `$query->update($columns)` is updated something. --- src/Illuminate/Database/Eloquent/SoftDeletes.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/SoftDeletes.php b/src/Illuminate/Database/Eloquent/SoftDeletes.php index dfc53c6c9803..6dbbe68c4a06 100644 --- a/src/Illuminate/Database/Eloquent/SoftDeletes.php +++ b/src/Illuminate/Database/Eloquent/SoftDeletes.php @@ -76,9 +76,11 @@ protected function runSoftDelete() $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time); } - $query->update($columns); + $updated = $query->update($columns); - $this->syncOriginal(); + if ($updated) { + $this->syncOriginal(); + } } /** From bbc9de883c95a3342bf76f11be7be7057b2bd589 Mon Sep 17 00:00:00 2001 From: Tetiana Blindaruk Date: Fri, 1 Jun 2018 19:42:44 +0300 Subject: [PATCH 4/4] [soft deleting] add testSoftDeleteAfterRestoring. --- ...abaseEloquentSoftDeletesIntegrationTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 88d859fcd001..df1dd1f1846a 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -284,6 +284,24 @@ public function testRestoreAfterSoftDelete() $this->assertEquals($userModel->id, SoftDeletesTestUser::find(2)->id); } + /** + * @throws \Exception + */ + public function testSoftDeleteAfterRestoring() + { + $this->createUsers(); + + /** @var SoftDeletesTestUser $userModel */ + $userModel = SoftDeletesTestUser::withTrashed()->find(1); + $userModel->restore(); + $this->assertEquals($userModel->deleted_at, SoftDeletesTestUser::find(1)->deleted_at); + $this->assertEquals($userModel->getOriginal('deleted_at'), SoftDeletesTestUser::find(1)->deleted_at); + $userModel->delete(); + $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 testUpdateOrCreate() { $this->createUsers();