Skip to content

Commit

Permalink
[8.x] Fixed SoftDeletes force deletion sets "exists" property to fals…
Browse files Browse the repository at this point in the history
…e only when deletion succeeded (laravel#39987)

* SoftDelete::performDeleteOnModel() sets "exists" property to false only when succeeded

* Fix lint
  • Loading branch information
sancherie authored Dec 13, 2021
1 parent a9d70e6 commit 24fc129
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public function forceDelete()
protected function performDeleteOnModel()
{
if ($this->forceDeleting) {
$this->exists = false;

return $this->setKeysForSaveQuery($this->newModelQuery())->forceDelete();
return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () {
$this->exists = false;
});
}

return $this->runSoftDelete();
Expand Down
37 changes: 37 additions & 0 deletions tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Carbon;
use Mockery;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentSoftDeletesIntegrationTest extends TestCase
Expand Down Expand Up @@ -189,6 +190,42 @@ public function testForceDeleteActuallyDeletesRecords()
$this->assertEquals(1, $users->first()->id);
}

public function testForceDeleteUpdateExistsProperty()
{
$this->createUsers();
$user = SoftDeletesTestUser::find(2);

$this->assertTrue($user->exists);

$user->forceDelete();

$this->assertFalse($user->exists);
}

public function testForceDeleteDoesntUpdateExistsPropertyIfFailed()
{
$user = new class() extends SoftDeletesTestUser
{
public $exists = true;

public function newModelQuery()
{
return Mockery::spy(parent::newModelQuery(), function (Mockery\MockInterface $mock) {
$mock->shouldReceive('forceDelete')->andThrow(new \Exception());
});
}
};

$this->assertTrue($user->exists);

try {
$user->forceDelete();
} catch (\Exception $exception) {
}

$this->assertTrue($user->exists);
}

public function testRestoreRestoresRecords()
{
$this->createUsers();
Expand Down

0 comments on commit 24fc129

Please sign in to comment.