Skip to content

Commit 22682dd

Browse files
authored
fix audited relations duplicity on same model (#943)
1 parent 49001f7 commit 22682dd

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

src/Auditable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ private function dispatchRelationAuditEvent($relationName, $event, $old, $new)
850850
$this->auditEvent = $event;
851851
$this->isCustomEvent = true;
852852
Event::dispatch(AuditCustom::class, [$this]);
853+
$this->auditCustomOld = $this->auditCustomNew = [];
853854
$this->isCustomEvent = false;
854855
}
855856

tests/Functional/AuditingTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,40 @@ public function itWillAuditSync()
667667
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
668668
}
669669

670+
/**
671+
* @test
672+
* @return void
673+
*/
674+
public function itWillAuditSyncIndividually()
675+
{
676+
Article::disableAuditing();
677+
$user = factory(User::class)->create();
678+
$category = factory(Category::class)->create();
679+
$article = factory(Article::class)->create();
680+
Article::enableAuditing();
681+
682+
$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
683+
$article->auditSync('users', [$user->getKey()]);
684+
$article->auditSync('categories', [$category->getKey()]);
685+
$audits = $article->audits()->get();
686+
$auditFirst = $audits->first();
687+
$auditLast = $audits->last();
688+
689+
$this->assertSame($no_of_audits_before + 2, $audits->count());
690+
$this->assertSame($user->getKey(), $article->users()->first()->getKey());
691+
$this->assertSame($category->getKey(), $article->categories()->first()->getKey());
692+
693+
$this->assertArrayHasKey('users', $auditFirst->new_values);
694+
$this->assertArrayHasKey('users', $auditFirst->old_values);
695+
$this->assertArrayNotHasKey('categories', $auditFirst->new_values);
696+
$this->assertArrayNotHasKey('categories', $auditFirst->old_values);
697+
698+
$this->assertArrayHasKey('categories', $auditLast->new_values);
699+
$this->assertArrayHasKey('categories', $auditLast->old_values);
700+
$this->assertArrayNotHasKey('users', $auditLast->new_values);
701+
$this->assertArrayNotHasKey('users', $auditLast->old_values);
702+
}
703+
670704
/**
671705
* @test
672706
* @return void

tests/Models/Article.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function __construct(array $attributes = [])
5555
parent::__construct($attributes);
5656
}
5757

58+
public function users()
59+
{
60+
return $this->morphToMany(User::class, 'model', 'model_has_users');
61+
}
62+
5863
public function categories()
5964
{
6065
return $this->morphToMany(Category::class, 'model', 'model_has_categories');

tests/database/migrations/0000_00_00_000002_create_users_test_table.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public function up()
2121
$table->string('email')->unique();
2222
$table->timestamps();
2323
});
24+
25+
Schema::create('model_has_users', function (Blueprint $table) {
26+
$table->string('model_type');
27+
$table->unsignedBigInteger('model_id');
28+
$table->unsignedBigInteger('user_id');
29+
$table->timestamps();
30+
});
2431
}
2532

2633
/**
@@ -30,6 +37,7 @@ public function up()
3037
*/
3138
public function down()
3239
{
40+
Schema::drop('model_has_users');
3341
Schema::drop('users');
3442
}
3543
}

tests/database/migrations/0000_00_00_000003_create_categories_test_table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function up()
2121

2222
Schema::create('model_has_categories', function (Blueprint $table) {
2323
$table->string('model_type');
24-
$table->string('pivot_type')->nullable();
25-
$table->unsignedBigInteger('category_id');
2624
$table->unsignedBigInteger('model_id');
25+
$table->unsignedBigInteger('category_id');
26+
$table->string('pivot_type')->nullable();
2727
$table->timestamps();
2828
});
2929
}
@@ -35,6 +35,7 @@ public function up()
3535
*/
3636
public function down()
3737
{
38+
Schema::drop('model_has_categories');
3839
Schema::drop('categories');
3940
}
4041
}

0 commit comments

Comments
 (0)