Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ trait Auditable
*/
public static function bootAuditable()
{
if (App::getFacadeRoot() && static::isAuditingEnabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikn69 I can't remember why the App::getFacadeRoot() check is required but I definitely remember needing it at some point, does that ring any bells for you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but that will in some cases make audits not happen at all (because when the app is not spun up, but the model gets booted, it will never observe)

The tests succeed, even OutsideOfAppContextTest

Copy link
Contributor

@erikn69 erikn69 Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something is being reversed; I believe it was perhaps for performance reasons, to avoid always registering observers, I don't remember.

It seems it's always been like this: #134

static::observe(new AuditableObserver);
}
static::observe(new AuditableObserver);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/AuditableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ protected function dispatchAudit(Auditable $model): void
if (! $model->readyForAuditing()) {
return;
}
$modelClass = $model::class;
if (method_exists($modelClass, 'isAuditingEnabled') && ! $modelClass::isAuditingEnabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like one check has been replaced by two, is this one definitely needed? should there be a separate test to pick it up?

return;
}

// @phpstan-ignore method.notFound
$model->preloadResolverData();
Expand Down
4 changes: 4 additions & 0 deletions src/Listeners/ProcessDispatchAudit.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function withDelay(DispatchAudit $event): int

public function handle(DispatchAudit $event): void
{
$modelClass = $event->model::class;
if (method_exists($modelClass, 'isAuditingEnabled') && ! $modelClass::isAuditingEnabled()) {
return;
}
Auditor::execute($event->model);
}
}
33 changes: 33 additions & 0 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1268,4 +1268,37 @@ public function test_it_works_when_config_allowed_array_value_is_false(): void
'tags' => null,
], $auditData, true);
}

public function test_it_works_when_enabling_after_model_boot(): void
{
$this->app['config']->set('audit.enabled', true);

$this->app['config']->set('audit.console', false);
Article::clearBootedModels(); // Make sure the model is booted again
Article::disableAuditing();

$model = tap(new Article([
'title' => 'How To Audit Eloquent Models',
'content' => fake()->text(),
'reviewed' => fake()->boolean(),
]), fn (Article $article) => $article->save()); // constructor will boot trait
$this->assertFalse(Article::isAuditingEnabled(), 'Because console audit is disabled, the model is not auditing');
$this->assertTrue(Article::getEventDispatcher()->hasListeners(\sprintf('eloquent.created: %s', Article::class)));

$this->assertCount(0, $model->audits, 'If auditing is enabled, the model should have an audit after creating');

$this->app['config']->set('audit.console', true);
Article::enableAuditing();

$model = tap(new Article([
'title' => 'How To Audit Eloquent Models',
'content' => fake()->text(),
'reviewed' => fake()->boolean(),
]), fn (Article $article) => $article->save()); // will not boot again, because booting only happens once

$this->assertCount(1, $model->audits, 'If auditing is enabled, the model should have an audit after creating');

$this->assertTrue(Article::isAuditingEnabled(), 'Because console audit is enabled, the model is auditing');
$this->assertTrue(Article::getEventDispatcher()->hasListeners(\sprintf('eloquent.created: %s', Article::class)), 'The model should have a listener for created');
}
}