Skip to content

Commit 6c0746f

Browse files
authored
fix: fix custom morph prefix on user, with tests (#773)
1 parent f200377 commit 6c0746f

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

src/Audit.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public function auditable()
4444
*/
4545
public function user()
4646
{
47-
return $this->morphTo();
47+
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');
48+
49+
return $this->morphTo(__FUNCTION__, $morphPrefix . '_type', $morphPrefix . '_id');
4850
}
4951

5052
/**

tests/AuditingTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function getEnvironmentSetUp($app)
2727
// Audit
2828
$app['config']->set('audit.drivers.database.table', 'audit_testing');
2929
$app['config']->set('audit.drivers.database.connection', 'testing');
30-
$app['config']->set('audit.user.morph_prefix', 'user');
30+
$app['config']->set('audit.user.morph_prefix', 'prefix');
3131
$app['config']->set('audit.user.resolver', UserResolver::class);
3232
$app['config']->set('audit.user.guards', [
3333
'web',

tests/Unit/AuditableTest.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ public function itReturnsTheAuditData()
403403

404404
$this->assertCount(11, $auditData = $model->toAudit());
405405

406+
$morphPrefix = config('audit.user.morph_prefix', 'user');
406407
self::Assert()::assertArraySubset([
407408
'old_values' => [],
408409
'new_values' => [
@@ -411,15 +412,15 @@ public function itReturnsTheAuditData()
411412
'reviewed' => 1,
412413
'published_at' => $now->toDateTimeString(),
413414
],
414-
'event' => 'created',
415-
'auditable_id' => null,
416-
'auditable_type' => Article::class,
417-
'user_id' => null,
418-
'user_type' => null,
419-
'url' => 'console',
420-
'ip_address' => '127.0.0.1',
421-
'user_agent' => 'Symfony',
422-
'tags' => null,
415+
'event' => 'created',
416+
'auditable_id' => null,
417+
'auditable_type' => Article::class,
418+
$morphPrefix . '_id' => null,
419+
$morphPrefix . '_type' => null,
420+
'url' => 'console',
421+
'ip_address' => '127.0.0.1',
422+
'user_agent' => 'Symfony',
423+
'tags' => null,
423424
], $auditData, true);
424425
}
425426

@@ -462,6 +463,7 @@ public function itReturnsTheAuditDataIncludingUserAttributes(
462463

463464
$this->assertCount(11, $auditData = $model->toAudit());
464465

466+
$morphPrefix = config('audit.user.morph_prefix', 'user');
465467
self::Assert()::assertArraySubset([
466468
'old_values' => [],
467469
'new_values' => [
@@ -470,15 +472,15 @@ public function itReturnsTheAuditDataIncludingUserAttributes(
470472
'reviewed' => 1,
471473
'published_at' => $now->toDateTimeString(),
472474
],
473-
'event' => 'created',
474-
'auditable_id' => null,
475-
'auditable_type' => Article::class,
476-
'user_id' => $id,
477-
'user_type' => $type,
478-
'url' => 'console',
479-
'ip_address' => '127.0.0.1',
480-
'user_agent' => 'Symfony',
481-
'tags' => null,
475+
'event' => 'created',
476+
'auditable_id' => null,
477+
'auditable_type' => Article::class,
478+
$morphPrefix . '_id' => $id,
479+
$morphPrefix . '_type' => $type,
480+
'url' => 'console',
481+
'ip_address' => '127.0.0.1',
482+
'user_agent' => 'Symfony',
483+
'tags' => null,
482484
], $auditData, true);
483485
}
484486

@@ -544,21 +546,22 @@ public function itExcludesAttributesFromTheAuditDataWhenInStrictMode()
544546

545547
$this->assertCount(11, $auditData = $model->toAudit());
546548

549+
$morphPrefix = config('audit.user.morph_prefix', 'user');
547550
self::Assert()::assertArraySubset([
548551
'old_values' => [],
549552
'new_values' => [
550553
'title' => 'How To Audit Eloquent Models',
551554
'content' => 'First step: install the laravel-auditing package.',
552555
],
553-
'event' => 'created',
554-
'auditable_id' => null,
555-
'auditable_type' => Article::class,
556-
'user_id' => null,
557-
'user_type' => null,
558-
'url' => 'console',
559-
'ip_address' => '127.0.0.1',
560-
'user_agent' => 'Symfony',
561-
'tags' => null,
556+
'event' => 'created',
557+
'auditable_id' => null,
558+
'auditable_type' => Article::class,
559+
$morphPrefix . '_id' => null,
560+
$morphPrefix . '_type' => null,
561+
'url' => 'console',
562+
'ip_address' => '127.0.0.1',
563+
'user_agent' => 'Symfony',
564+
'tags' => null,
562565
], $auditData, true);
563566
}
564567

tests/database/factories/AuditFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
*/
1414

1515
$factory->define(Audit::class, function (Faker $faker) {
16+
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');
17+
1618
return [
17-
'user_id' => function () {
19+
$morphPrefix . '_id' => function () {
1820
return factory(User::class)->create()->id;
1921
},
20-
'user_type' => User::class,
22+
$morphPrefix . '_type' => User::class,
2123
'event' => 'updated',
2224
'auditable_id' => function () {
2325
return factory(Article::class)->create()->id;

0 commit comments

Comments
 (0)