Permalink
Browse files

Allow detection of changes during forceDelete on model using SoftDele…

…tes (#267)
  • Loading branch information...
bte2 authored and freekmurze committed Sep 28, 2017
1 parent faec486 commit ae0de19001d191306666c4dfe8ff46faf260934c
Showing with 50 additions and 1 deletion.
  1. +2 −1 src/Traits/DetectsChanges.php
  2. +41 −0 tests/DetectsChangesTest.php
  3. +7 −0 tests/LogsActivityTest.php
@@ -53,7 +53,8 @@ public function attributeValuesToBeLogged(string $processingEvent): array
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') {
$nullProperties = array_fill_keys(array_keys($properties['attributes']), null);
@@ -6,6 +6,7 @@
use Spatie\Activitylog\Test\Models\User;
use Spatie\Activitylog\Test\Models\Article;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\SoftDeletes;
class DetectsChangesTest extends TestCase
{
@@ -266,6 +267,46 @@ public function it_will_store_the_values_when_deleting_the_model()
$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 */
public function it_can_store_the_changes_of_array_casted_properties()
{
@@ -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($article->id, $this->getLastActivity()->subject_id);
$this->assertEquals('deleted', $this->getLastActivity()->description);
$article->forceDelete();
$this->assertCount(3, Activity::all());
$this->assertEquals('deleted', $this->getLastActivity()->description);
$this->assertNull($article->fresh());
}
/** @test */

0 comments on commit ae0de19

Please sign in to comment.