Skip to content

Commit c89896d

Browse files
Fix AuditCustom event not respecting audit.enabled and audit.console config
1 parent c516bdf commit c89896d

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/Contracts/Auditable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public function getAuditEvent();
3535
*/
3636
public function getAuditEvents(): array;
3737

38+
/**
39+
* Determine whether auditing is enabled.
40+
*/
41+
public static function isAuditingEnabled(): bool;
42+
3843
/**
3944
* Is the model ready for auditing?
4045
*/

src/Listeners/RecordCustomAudit.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class RecordCustomAudit
99
{
1010
public function handle(AuditCustom $event): void
1111
{
12+
if (! $event->model::isAuditingEnabled()) {
13+
return;
14+
}
15+
1216
Auditor::execute($event->model);
1317
}
1418
}

tests/Functional/AuditingTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,41 @@ public function test_can_audit_any_custom_event(): void
946946
]);
947947
}
948948

949+
public function test_custom_event_does_not_audit_when_auditing_is_disabled(): void
950+
{
951+
config(['audit.enabled' => false]);
952+
953+
$article = Article::factory()->create();
954+
$article->auditEvent = 'whateverYouWant';
955+
$article->isCustomEvent = true;
956+
$article->auditCustomOld = ['customExample' => 'Anakin Skywalker'];
957+
$article->auditCustomNew = ['customExample' => 'Darth Vader'];
958+
959+
$auditCountBefore = Audit::where('auditable_type', Article::class)->count();
960+
961+
Event::dispatch(new AuditCustom($article));
962+
963+
$this->assertSame($auditCountBefore, Audit::where('auditable_type', Article::class)->count());
964+
}
965+
966+
public function test_custom_event_does_not_audit_when_running_in_console_without_console_flag(): void
967+
{
968+
App::shouldReceive('runningInConsole')->andReturn(true);
969+
config(['audit.enabled' => true, 'audit.console' => false]);
970+
971+
$article = Article::factory()->create();
972+
$article->auditEvent = 'whateverYouWant';
973+
$article->isCustomEvent = true;
974+
$article->auditCustomOld = ['customExample' => 'Anakin Skywalker'];
975+
$article->auditCustomNew = ['customExample' => 'Darth Vader'];
976+
977+
$auditCountBefore = Audit::where('auditable_type', Article::class)->count();
978+
979+
Event::dispatch(new AuditCustom($article));
980+
981+
$this->assertSame($auditCountBefore, Audit::where('auditable_type', Article::class)->count());
982+
}
983+
949984
public function test_can_audit_custom_audit_model_implementation(): void
950985
{
951986
$audit = null;

0 commit comments

Comments
 (0)