Skip to content

Commit

Permalink
fix audited relations duplicity on same model (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored May 30, 2024
1 parent 49001f7 commit 22682dd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ private function dispatchRelationAuditEvent($relationName, $event, $old, $new)
$this->auditEvent = $event;
$this->isCustomEvent = true;
Event::dispatch(AuditCustom::class, [$this]);
$this->auditCustomOld = $this->auditCustomNew = [];
$this->isCustomEvent = false;
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,40 @@ public function itWillAuditSync()
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
}

/**
* @test
* @return void
*/
public function itWillAuditSyncIndividually()
{
Article::disableAuditing();
$user = factory(User::class)->create();
$category = factory(Category::class)->create();
$article = factory(Article::class)->create();
Article::enableAuditing();

$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
$article->auditSync('users', [$user->getKey()]);
$article->auditSync('categories', [$category->getKey()]);
$audits = $article->audits()->get();
$auditFirst = $audits->first();
$auditLast = $audits->last();

$this->assertSame($no_of_audits_before + 2, $audits->count());
$this->assertSame($user->getKey(), $article->users()->first()->getKey());
$this->assertSame($category->getKey(), $article->categories()->first()->getKey());

$this->assertArrayHasKey('users', $auditFirst->new_values);
$this->assertArrayHasKey('users', $auditFirst->old_values);
$this->assertArrayNotHasKey('categories', $auditFirst->new_values);
$this->assertArrayNotHasKey('categories', $auditFirst->old_values);

$this->assertArrayHasKey('categories', $auditLast->new_values);
$this->assertArrayHasKey('categories', $auditLast->old_values);
$this->assertArrayNotHasKey('users', $auditLast->new_values);
$this->assertArrayNotHasKey('users', $auditLast->old_values);
}

/**
* @test
* @return void
Expand Down
5 changes: 5 additions & 0 deletions tests/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function __construct(array $attributes = [])
parent::__construct($attributes);
}

public function users()
{
return $this->morphToMany(User::class, 'model', 'model_has_users');
}

public function categories()
{
return $this->morphToMany(Category::class, 'model', 'model_has_categories');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public function up()
$table->string('email')->unique();
$table->timestamps();
});

Schema::create('model_has_users', function (Blueprint $table) {
$table->string('model_type');
$table->unsignedBigInteger('model_id');
$table->unsignedBigInteger('user_id');
$table->timestamps();
});
}

/**
Expand All @@ -30,6 +37,7 @@ public function up()
*/
public function down()
{
Schema::drop('model_has_users');
Schema::drop('users');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function up()

Schema::create('model_has_categories', function (Blueprint $table) {
$table->string('model_type');
$table->string('pivot_type')->nullable();
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('model_id');
$table->unsignedBigInteger('category_id');
$table->string('pivot_type')->nullable();
$table->timestamps();
});
}
Expand All @@ -35,6 +35,7 @@ public function up()
*/
public function down()
{
Schema::drop('model_has_categories');
Schema::drop('categories');
}
}

0 comments on commit 22682dd

Please sign in to comment.