Skip to content

Commit

Permalink
Avoid duplicate data on detach, attach, sync (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Oct 10, 2023
1 parent 7adc295 commit cca0fbe
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,8 @@ public function auditSyncWithoutDetaching(string $relationName, $ids, $columns =
*/
private function dispatchRelationAuditEvent($relationName, $event, $old, $new)
{
$this->auditCustomOld[$relationName] = $old->toArray();
$this->auditCustomNew[$relationName] = $new->toArray();
$this->auditCustomOld[$relationName] = $old->diff($new)->toArray();
$this->auditCustomNew[$relationName] = $new->diff($old)->toArray();

if (
empty($this->auditCustomOld[$relationName]) &&
Expand Down
66 changes: 61 additions & 5 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class AuditingTest extends AuditingTestCase
{
use WithFaker;


/**
* @test
*/
Expand Down Expand Up @@ -586,18 +585,20 @@ public function itWillAuditModelsWhenValuesAreEmpty()
* @test
* @return void
*/
public function itWillAuditCustomEventData()
public function itWillAuditAttach()
{
$firstCategory = factory(Category::class)->create();
$secondCategory = factory(Category::class)->create();
$article = factory(Article::class)->create();

$article->auditAttach('categories', $firstCategory);
$article->auditAttach('categories', $secondCategory);
$lastAttachedArticle = \end($article->audits->last()->getModified()['categories']['new']);
$lastArticleAudit = $article->audits->last()->getModified()['categories'];

$this->assertSame($firstCategory->name, $article->categories->first()->name);
$this->assertSame($secondCategory->name, $lastAttachedArticle['name']);
$this->assertSame(0, count($lastArticleAudit['old']));
$this->assertSame(1, count($lastArticleAudit['new']));
$this->assertSame($secondCategory->name, $lastArticleAudit['new'][0]['name']);
}

/**
Expand Down Expand Up @@ -733,6 +734,61 @@ public function itWillNotAuditSyncWhenSkippingEmptyValuesAndNoChangesMade()
$this->assertSame($no_of_audits_before, $no_of_audits_after);
}

/**
* @test
* @return void
*/
public function itWillNotAuditAttachWhenSkippingEmptyValuesAndNoChangesMade()
{
$this->app['config']->set('audit.empty_values', false);

$firstCategory = factory(Category::class)->create();
$article = factory(Article::class)->create();

$article->categories()->attach($firstCategory);

$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
$categoryBefore = $article->categories()->first()->getKey();

$article->auditAttach('categories', [$firstCategory->getKey()]);

$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
$categoryAfter = $article->categories()->first()->getKey();

$this->assertSame($firstCategory->getKey(), $categoryBefore);
$this->assertSame($firstCategory->getKey(), $categoryAfter);
$this->assertSame($categoryBefore, $categoryAfter);
$this->assertSame($no_of_audits_before, $no_of_audits_after);
}

/**
* @test
* @return void
*/
public function itWillNotAuditDetachWhenSkippingEmptyValuesAndNoChangesMade()
{
$this->app['config']->set('audit.empty_values', false);

$firstCategory = factory(Category::class)->create();
$secondCategory = factory(Category::class)->create();
$article = factory(Article::class)->create();

$article->categories()->attach($firstCategory);

$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
$categoryBefore = $article->categories()->first()->getKey();

$article->auditDetach('categories', [$secondCategory->getKey()]);

$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
$categoryAfter = $article->categories()->first()->getKey();

$this->assertSame($firstCategory->getKey(), $categoryBefore);
$this->assertSame($firstCategory->getKey(), $categoryAfter);
$this->assertSame($categoryBefore, $categoryAfter);
$this->assertSame($no_of_audits_before, $no_of_audits_after);
}

/**
* @test
* @return void
Expand Down

0 comments on commit cca0fbe

Please sign in to comment.