Skip to content

Commit

Permalink
don't log force delete events (#6)
Browse files Browse the repository at this point in the history
* ignore timestamp changes

* fire FORCE_DELETE event type when non soft deletes are occurring

* adding tests for force delete checks
  • Loading branch information
Tom Schlick committed Aug 16, 2019
1 parent c92aed1 commit 466d391
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class EventType
const UPDATED = 2;
const DELETED = 3;
const RESTORED = 4;
const FORCE_DELETED = 5;
}
11 changes: 10 additions & 1 deletion src/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ public static function recordChanges(int $event_type, $model) : void
case EventType::RESTORED:
$changes = $model->getChanges();
break;
case EventType::FORCE_DELETED:
return; // if force deleted we want to stop execution here as there would be nothing to correlate records to
break;
}

collect($changes)
->except(config('model-auditlog.global_ignored_fields'))
->except([$model->getKeyName()]) // Ignore the current model's primary key
->except([
$model->getKeyName(), // Ignore the current model's primary key
'created_at',
'updated_at',
'date_created',
'date_modified',
])
->each(function ($change, $key) use ($event_type, $model) {
$log = new static();
$log->event_type = $event_type;
Expand Down
10 changes: 9 additions & 1 deletion src/Observers/AuditLogObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ public function updated($model) : void
*/
public function deleted($model) : void
{
/*
* If a model is hard deleting, either via a force delete or that model does not implement
* the SoftDeletes trait we should tag it as such so logging doesn't occur down the pipe.
*/
if (! method_exists($model, 'isForceDeleting') || $model->isForceDeleting()) {
$event = EventType::FORCE_DELETED;
}

$this->getAuditLogModel($model)
->recordChanges(EventType::DELETED, $model);
->recordChanges($event ?? EventType::DELETED, $model);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Fakes/Models/NonSoftDeletePost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace OrisIntel\AuditLog\Tests\Fakes\Models;

use Illuminate\Database\Eloquent\Model;
use OrisIntel\AuditLog\Traits\AuditLoggable;

class NonSoftDeletePost extends Model
{
use AuditLoggable;

protected $guarded = [];

protected $table = 'posts';
}
12 changes: 12 additions & 0 deletions tests/Fakes/Models/NonSoftDeletePostAuditLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace OrisIntel\AuditLog\Tests\Fakes\Models;

use OrisIntel\AuditLog\Models\BaseModel;

class NonSoftDeletePostAuditLog extends BaseModel
{
public $timestamps = false;

public $table = 'posts_auditlog';
}
33 changes: 33 additions & 0 deletions tests/PostModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Collection;
use OrisIntel\AuditLog\EventType;
use OrisIntel\AuditLog\Tests\Fakes\Models\NonSoftDeletePost;
use OrisIntel\AuditLog\Tests\Fakes\Models\Post;
use OrisIntel\AuditLog\Tests\Fakes\Models\PostAuditLog;

Expand Down Expand Up @@ -92,6 +93,38 @@ public function deleting_a_post_triggers_a_revision()
$this->assertNotEmpty($last->field_value_new);
}

/** @test */
public function force_deleting_a_post_does_not_trigger_a_revision()
{
/** @var Post $post */
$post = Post::create([
'title' => 'Test',
'posted_at' => '2019-04-05 12:00:00',
]);

$this->assertEquals(2, $post->auditLogs()->count());

$post->forceDelete();

$this->assertEquals(2, $post->auditLogs()->count());
}

/** @test */
public function deleting_a_non_soft_deleting_post_does_not_trigger_a_revision()
{
/** @var Post $post */
$post = NonSoftDeletePost::create([
'title' => 'Test',
'posted_at' => '2019-04-05 12:00:00',
]);

$this->assertEquals(2, $post->auditLogs()->count());

$post->forceDelete();

$this->assertEquals(2, $post->auditLogs()->count());
}

/** @test */
public function restoring_a_post_triggers_a_revision()
{
Expand Down

0 comments on commit 466d391

Please sign in to comment.