Skip to content

Commit

Permalink
Allow detection of changes during forceDelete on model using SoftDele…
Browse files Browse the repository at this point in the history
…tes (#267)
  • Loading branch information
bte2 authored and freekmurze committed Sep 28, 2017
1 parent faec486 commit ae0de19
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Traits/DetectsChanges.php
Expand Up @@ -53,7 +53,8 @@ public function attributeValuesToBeLogged(string $processingEvent): array
return []; return [];
} }


$properties['attributes'] = static::logChanges($this->exists ? $this->fresh() : $this); $tmp = $this;
$properties['attributes'] = static::logChanges($this->exists ? $tmp->fresh() ?? $this : $this);


if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') {
$nullProperties = array_fill_keys(array_keys($properties['attributes']), null); $nullProperties = array_fill_keys(array_keys($properties['attributes']), null);
Expand Down
41 changes: 41 additions & 0 deletions tests/DetectsChangesTest.php
Expand Up @@ -6,6 +6,7 @@
use Spatie\Activitylog\Test\Models\User; use Spatie\Activitylog\Test\Models\User;
use Spatie\Activitylog\Test\Models\Article; use Spatie\Activitylog\Test\Models\Article;
use Spatie\Activitylog\Traits\LogsActivity; use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\SoftDeletes;


class DetectsChangesTest extends TestCase class DetectsChangesTest extends TestCase
{ {
Expand Down Expand Up @@ -266,6 +267,46 @@ public function it_will_store_the_values_when_deleting_the_model()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()); $this->assertEquals($expectedChanges, $this->getLastActivity()->changes());
} }


/** @test */
public function it_will_store_the_values_when_deleting_the_model_with_softdeletes()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['name', 'text'];

use LogsActivity, SoftDeletes;
};

$article = new $articleClass();
$article->name = 'my name';
$article->save();

$article->delete();

$expectedChanges = collect([
'attributes' => [
'name' => 'my name',
'text' => null,
],
]);

$this->assertEquals('deleted', $this->getLastActivity()->description);
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes());

$article->forceDelete();

$expectedChanges = collect([
'attributes' => [
'name' => 'my name',
],
]);

$activities = $article->activity;

$this->assertCount(3, $activities);
$this->assertEquals('deleted', $this->getLastActivity()->description);
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes());
}

/** @test */ /** @test */
public function it_can_store_the_changes_of_array_casted_properties() public function it_can_store_the_changes_of_array_casted_properties()
{ {
Expand Down
7 changes: 7 additions & 0 deletions tests/LogsActivityTest.php
Expand Up @@ -121,6 +121,13 @@ public function it_will_log_the_deletion_of_a_model_with_softdeletes()
$this->assertEquals(get_class($this->article), $this->getLastActivity()->subject_type); $this->assertEquals(get_class($this->article), $this->getLastActivity()->subject_type);
$this->assertEquals($article->id, $this->getLastActivity()->subject_id); $this->assertEquals($article->id, $this->getLastActivity()->subject_id);
$this->assertEquals('deleted', $this->getLastActivity()->description); $this->assertEquals('deleted', $this->getLastActivity()->description);

$article->forceDelete();

$this->assertCount(3, Activity::all());

$this->assertEquals('deleted', $this->getLastActivity()->description);
$this->assertNull($article->fresh());
} }


/** @test */ /** @test */
Expand Down

0 comments on commit ae0de19

Please sign in to comment.