Skip to content

Commit 10c43e7

Browse files
Merge pull request #707 from mitchdesign/master
Add option to skip auditSync when sync has no changes
2 parents 16f5eff + d3f1f65 commit 10c43e7

File tree

4 files changed

+136
-10
lines changed

4 files changed

+136
-10
lines changed

config/audit.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,19 @@
9696
|
9797
| Some events may be empty on purpose. Use allowed_empty_values to exclude
9898
| those from the empty values check. For example when auditing
99-
| model retrieved events which will never have new and old values
99+
| model retrieved events which will never have new and old values.
100+
|
101+
| When using empty_values => true, you can still exclude certain events
102+
| with empty values by specifying them in disallowed_empty_values.
100103
|
101104
*/
102105

103106
'empty_values' => true,
104107
'allowed_empty_values' => [
105108
'retrieved'
106109
],
110+
'disallowed_empty_values' => [
111+
],
107112

108113
/*
109114
|--------------------------------------------------------------------------

src/Auditable.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ public function auditDetach(string $relationName, $ids = null, $touch = true)
671671
* @param $relationName
672672
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
673673
* @param bool $detaching
674+
* @param bool $skipUnchanged
674675
* @return array
675676
* @throws AuditingException
676677
*/
@@ -681,22 +682,33 @@ public function auditSync($relationName, $ids, $detaching = true)
681682
}
682683

683684
$this->auditEvent = 'sync';
684-
$this->isCustomEvent = true;
685+
685686
$this->auditCustomOld = [
686687
$relationName => $this->{$relationName}()->get()->isEmpty() ? [] : $this->{$relationName}()->get()->toArray()
687688
];
689+
688690
$changes = $this->{$relationName}()->sync($ids, $detaching);
689-
$this->auditCustomNew = [
690-
$relationName => $this->{$relationName}()->get()->isEmpty() ? [] : $this->{$relationName}()->get()->toArray()
691-
];
691+
692+
if (collect($changes)->flatten()->isEmpty()) {
693+
$this->auditCustomOld = [];
694+
$this->auditCustomNew = [];
695+
} else {
696+
$this->auditCustomNew = [
697+
$relationName => $this->{$relationName}()->get()->isEmpty() ? [] : $this->{$relationName}()->get()->toArray()
698+
];
699+
}
700+
701+
$this->isCustomEvent = true;
692702
Event::dispatch(AuditCustom::class, [$this]);
693703
$this->isCustomEvent = false;
704+
694705
return $changes;
695706
}
696707

697708
/**
698709
* @param string $relationName
699710
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
711+
* @param bool $skipUnchanged
700712
* @return array
701713
* @throws AuditingException
702714
*/

src/Auditor.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ public function execute(Auditable $model)
6767
return;
6868
}
6969

70-
// If we want to avoid storing Audits with empty old_values & new_values, return null here.
71-
if (!Config::get('audit.empty_values')) {
70+
// Check if we want to avoid storing empty values
71+
$allowEmpty = Config::get('audit.empty_values');
72+
$explicitAllowEmpty = in_array($model->getAuditEvent(), Config::get('audit.allowed_empty_values', []));
73+
$explicitDisallowEmpty = in_array($model->getAuditEvent(), Config::get('audit.disallowed_empty_values', []));
74+
75+
if ($explicitDisallowEmpty || (!$allowEmpty && !$explicitAllowEmpty)) {
7276
if (
7377
empty($model->toAudit()['new_values']) &&
74-
empty($model->toAudit()['old_values']) &&
75-
!in_array($model->getAuditEvent(), Config::get('audit.allowed_empty_values'))
78+
empty($model->toAudit()['old_values'])
7679
) {
7780
return;
7881
}

tests/Functional/AuditingTest.php

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AuditingTest extends AuditingTestCase
2323
{
2424
use WithFaker;
2525

26-
26+
2727
/**
2828
* @test
2929
*/
@@ -576,6 +576,112 @@ public function itWillAuditCustomEventData()
576576
);
577577
}
578578

579+
/**
580+
* @test
581+
* @return void
582+
*/
583+
public function itWillAuditSync()
584+
{
585+
$firstCategory = factory(Category::class)->create();
586+
$secondCategory = factory(Category::class)->create();
587+
$article = factory(Article::class)->create();
588+
589+
$article->categories()->attach($firstCategory);
590+
591+
$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
592+
$categoryBefore = $article->categories()->first()->getKey();
593+
594+
$article->auditSync('categories', [$secondCategory->getKey()]);
595+
596+
$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
597+
$categoryAfter = $article->categories()->first()->getKey();
598+
599+
$this->assertSame($firstCategory->getKey(), $categoryBefore);
600+
$this->assertSame($secondCategory->getKey(), $categoryAfter);
601+
$this->assertNotSame($categoryBefore, $categoryAfter);
602+
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
603+
}
604+
605+
/**
606+
* @test
607+
* @return void
608+
*/
609+
public function itWillAuditSyncWithoutChanges()
610+
{
611+
$firstCategory = factory(Category::class)->create();
612+
$article = factory(Article::class)->create();
613+
614+
$article->categories()->attach($firstCategory);
615+
616+
$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
617+
$categoryBefore = $article->categories()->first()->getKey();
618+
619+
$article->auditSync('categories', [$firstCategory->getKey()]);
620+
621+
$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
622+
$categoryAfter = $article->categories()->first()->getKey();
623+
624+
$this->assertSame($firstCategory->getKey(), $categoryBefore);
625+
$this->assertSame($firstCategory->getKey(), $categoryAfter);
626+
$this->assertSame($categoryBefore, $categoryAfter);
627+
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
628+
}
629+
630+
/**
631+
* @test
632+
* @return void
633+
*/
634+
public function itWillAuditSyncWhenSkippingEmptyValues()
635+
{
636+
$this->app['config']->set('audit.empty_values', false);
637+
638+
$firstCategory = factory(Category::class)->create();
639+
$secondCategory = factory(Category::class)->create();
640+
$article = factory(Article::class)->create();
641+
642+
$article->categories()->attach($firstCategory);
643+
644+
$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
645+
$categoryBefore = $article->categories()->first()->getKey();
646+
647+
$article->auditSync('categories', [$secondCategory->getKey()]);
648+
649+
$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
650+
$categoryAfter = $article->categories()->first()->getKey();
651+
652+
$this->assertSame($firstCategory->getKey(), $categoryBefore);
653+
$this->assertSame($secondCategory->getKey(), $categoryAfter);
654+
$this->assertNotSame($categoryBefore, $categoryAfter);
655+
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
656+
}
657+
658+
/**
659+
* @test
660+
* @return void
661+
*/
662+
public function itWillNotAuditSyncWhenSkippingEmptyValuesAndNoChangesMade()
663+
{
664+
$this->app['config']->set('audit.empty_values', false);
665+
666+
$firstCategory = factory(Category::class)->create();
667+
$article = factory(Article::class)->create();
668+
669+
$article->categories()->attach($firstCategory);
670+
671+
$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
672+
$categoryBefore = $article->categories()->first()->getKey();
673+
674+
$article->auditSync('categories', [$firstCategory->getKey()]);
675+
676+
$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
677+
$categoryAfter = $article->categories()->first()->getKey();
678+
679+
$this->assertSame($firstCategory->getKey(), $categoryBefore);
680+
$this->assertSame($firstCategory->getKey(), $categoryAfter);
681+
$this->assertSame($categoryBefore, $categoryAfter);
682+
$this->assertSame($no_of_audits_before, $no_of_audits_after);
683+
}
684+
579685
/**
580686
* @test
581687
* @return void

0 commit comments

Comments
 (0)