Skip to content

Commit 466d391

Browse files
author
Tom Schlick
authored
don't log force delete events (#6)
* ignore timestamp changes * fire FORCE_DELETE event type when non soft deletes are occurring * adding tests for force delete checks
1 parent c92aed1 commit 466d391

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

src/EventType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class EventType
88
const UPDATED = 2;
99
const DELETED = 3;
1010
const RESTORED = 4;
11+
const FORCE_DELETED = 5;
1112
}

src/Models/BaseModel.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@ public static function recordChanges(int $event_type, $model) : void
3131
case EventType::RESTORED:
3232
$changes = $model->getChanges();
3333
break;
34+
case EventType::FORCE_DELETED:
35+
return; // if force deleted we want to stop execution here as there would be nothing to correlate records to
36+
break;
3437
}
3538

3639
collect($changes)
3740
->except(config('model-auditlog.global_ignored_fields'))
38-
->except([$model->getKeyName()]) // Ignore the current model's primary key
41+
->except([
42+
$model->getKeyName(), // Ignore the current model's primary key
43+
'created_at',
44+
'updated_at',
45+
'date_created',
46+
'date_modified',
47+
])
3948
->each(function ($change, $key) use ($event_type, $model) {
4049
$log = new static();
4150
$log->event_type = $event_type;

src/Observers/AuditLogObserver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@ public function updated($model) : void
3030
*/
3131
public function deleted($model) : void
3232
{
33+
/*
34+
* If a model is hard deleting, either via a force delete or that model does not implement
35+
* the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe.
36+
*/
37+
if (! method_exists($model, 'isForceDeleting') || $model->isForceDeleting()) {
38+
$event = EventType::FORCE_DELETED;
39+
}
40+
3341
$this->getAuditLogModel($model)
34-
->recordChanges(EventType::DELETED, $model);
42+
->recordChanges($event ?? EventType::DELETED, $model);
3543
}
3644

3745
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace OrisIntel\AuditLog\Tests\Fakes\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use OrisIntel\AuditLog\Traits\AuditLoggable;
7+
8+
class NonSoftDeletePost extends Model
9+
{
10+
use AuditLoggable;
11+
12+
protected $guarded = [];
13+
14+
protected $table = 'posts';
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace OrisIntel\AuditLog\Tests\Fakes\Models;
4+
5+
use OrisIntel\AuditLog\Models\BaseModel;
6+
7+
class NonSoftDeletePostAuditLog extends BaseModel
8+
{
9+
public $timestamps = false;
10+
11+
public $table = 'posts_auditlog';
12+
}

tests/PostModelTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Testing\DatabaseTransactions;
66
use Illuminate\Support\Collection;
77
use OrisIntel\AuditLog\EventType;
8+
use OrisIntel\AuditLog\Tests\Fakes\Models\NonSoftDeletePost;
89
use OrisIntel\AuditLog\Tests\Fakes\Models\Post;
910
use OrisIntel\AuditLog\Tests\Fakes\Models\PostAuditLog;
1011

@@ -92,6 +93,38 @@ public function deleting_a_post_triggers_a_revision()
9293
$this->assertNotEmpty($last->field_value_new);
9394
}
9495

96+
/** @test */
97+
public function force_deleting_a_post_does_not_trigger_a_revision()
98+
{
99+
/** @var Post $post */
100+
$post = Post::create([
101+
'title' => 'Test',
102+
'posted_at' => '2019-04-05 12:00:00',
103+
]);
104+
105+
$this->assertEquals(2, $post->auditLogs()->count());
106+
107+
$post->forceDelete();
108+
109+
$this->assertEquals(2, $post->auditLogs()->count());
110+
}
111+
112+
/** @test */
113+
public function deleting_a_non_soft_deleting_post_does_not_trigger_a_revision()
114+
{
115+
/** @var Post $post */
116+
$post = NonSoftDeletePost::create([
117+
'title' => 'Test',
118+
'posted_at' => '2019-04-05 12:00:00',
119+
]);
120+
121+
$this->assertEquals(2, $post->auditLogs()->count());
122+
123+
$post->forceDelete();
124+
125+
$this->assertEquals(2, $post->auditLogs()->count());
126+
}
127+
95128
/** @test */
96129
public function restoring_a_post_triggers_a_revision()
97130
{

0 commit comments

Comments
 (0)