From cca0fbe3accbc44ebd2439d9c67d3135cadd6b64 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Tue, 10 Oct 2023 15:32:49 -0500 Subject: [PATCH] Avoid duplicate data on detach, attach, sync (#873) --- src/Auditable.php | 4 +- tests/Functional/AuditingTest.php | 66 ++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/Auditable.php b/src/Auditable.php index 77fddf30..46b7b328 100644 --- a/src/Auditable.php +++ b/src/Auditable.php @@ -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]) && diff --git a/tests/Functional/AuditingTest.php b/tests/Functional/AuditingTest.php index 1f787c33..3d776964 100644 --- a/tests/Functional/AuditingTest.php +++ b/tests/Functional/AuditingTest.php @@ -23,7 +23,6 @@ class AuditingTest extends AuditingTestCase { use WithFaker; - /** * @test */ @@ -586,7 +585,7 @@ public function itWillAuditModelsWhenValuesAreEmpty() * @test * @return void */ - public function itWillAuditCustomEventData() + public function itWillAuditAttach() { $firstCategory = factory(Category::class)->create(); $secondCategory = factory(Category::class)->create(); @@ -594,10 +593,12 @@ public function itWillAuditCustomEventData() $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']); } /** @@ -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