Skip to content

Commit 24fc129

Browse files
authored
[8.x] Fixed SoftDeletes force deletion sets "exists" property to false only when deletion succeeded (#39987)
* SoftDelete::performDeleteOnModel() sets "exists" property to false only when succeeded * Fix lint
1 parent a9d70e6 commit 24fc129

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/Illuminate/Database/Eloquent/SoftDeletes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public function forceDelete()
6464
protected function performDeleteOnModel()
6565
{
6666
if ($this->forceDeleting) {
67-
$this->exists = false;
68-
69-
return $this->setKeysForSaveQuery($this->newModelQuery())->forceDelete();
67+
return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () {
68+
$this->exists = false;
69+
});
7070
}
7171

7272
return $this->runSoftDelete();

tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Pagination\CursorPaginator;
1212
use Illuminate\Pagination\Paginator;
1313
use Illuminate\Support\Carbon;
14+
use Mockery;
1415
use PHPUnit\Framework\TestCase;
1516

1617
class DatabaseEloquentSoftDeletesIntegrationTest extends TestCase
@@ -189,6 +190,42 @@ public function testForceDeleteActuallyDeletesRecords()
189190
$this->assertEquals(1, $users->first()->id);
190191
}
191192

193+
public function testForceDeleteUpdateExistsProperty()
194+
{
195+
$this->createUsers();
196+
$user = SoftDeletesTestUser::find(2);
197+
198+
$this->assertTrue($user->exists);
199+
200+
$user->forceDelete();
201+
202+
$this->assertFalse($user->exists);
203+
}
204+
205+
public function testForceDeleteDoesntUpdateExistsPropertyIfFailed()
206+
{
207+
$user = new class() extends SoftDeletesTestUser
208+
{
209+
public $exists = true;
210+
211+
public function newModelQuery()
212+
{
213+
return Mockery::spy(parent::newModelQuery(), function (Mockery\MockInterface $mock) {
214+
$mock->shouldReceive('forceDelete')->andThrow(new \Exception());
215+
});
216+
}
217+
};
218+
219+
$this->assertTrue($user->exists);
220+
221+
try {
222+
$user->forceDelete();
223+
} catch (\Exception $exception) {
224+
}
225+
226+
$this->assertTrue($user->exists);
227+
}
228+
192229
public function testRestoreRestoresRecords()
193230
{
194231
$this->createUsers();

0 commit comments

Comments
 (0)