Skip to content

Commit

Permalink
Added auditSyncWithPivotValues method (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored May 28, 2024
1 parent c22b1e3 commit 5dd4036
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
36 changes: 32 additions & 4 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace OwenIt\Auditing;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
Expand Down Expand Up @@ -754,7 +756,7 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c

/**
* @param string $relationName
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
* @param Collection|Model|array $ids
* @param bool $detaching
* @param array $columns
* @param \Closure|null $callback
Expand Down Expand Up @@ -787,7 +789,7 @@ public function auditSync(string $relationName, $ids, $detaching = true, $column

/**
* @param string $relationName
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
* @param Collection|Model|array $ids
* @param array $columns
* @param \Closure|null $callback
* @return array
Expand All @@ -800,11 +802,37 @@ public function auditSyncWithoutDetaching(string $relationName, $ids, $columns =
return $this->auditSync($relationName, $ids, false, $columns, $callback);
}

/**
* @param string $relationName
* @param Collection|Model|array $ids
* @param array $values
* @param bool $detaching
* @param array $columns
* @param \Closure|null $callback
* @return array
*/
public function auditSyncWithPivotValues(string $relationName, $ids, array $values, bool $detaching = true, $columns = ['*'], $callback = null)
{
$this->validateRelationshipMethodExistence($relationName, 'syncWithPivotValues');

if ($ids instanceof Model) {
$ids = $ids->getKey();
} elseif ($ids instanceof \Illuminate\Database\Eloquent\Collection) {
$ids = $ids->isEmpty() ? [] : $ids->pluck($ids->first()->getKeyName())->toArray();
} elseif ($ids instanceof Collection) {
$ids = $ids->toArray();
}

return $this->auditSync($relationName, collect(Arr::wrap($ids))->mapWithKeys(function ($id) use ($values) {
return [$id => $values];
}), $detaching, $columns, $callback);
}

/**
* @param string $relationName
* @param string $event
* @param \Illuminate\Support\Collection $old
* @param \Illuminate\Support\Collection $new
* @param Collection $old
* @param Collection $new
* @return void
*/
private function dispatchRelationAuditEvent($relationName, $event, $old, $new)
Expand Down
48 changes: 45 additions & 3 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,49 @@ public function itWillAuditSync()
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
}

/**
* @test
* @return void
*/
public function itWillAuditSyncWithPivotValues()
{
if (version_compare($this->app->version(), '8.0.0', '<')) {
$this->markTestSkipped('This test is only for Laravel 8.0.0+');
}

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

$article->categories()->attach([$firstCategory->getKey() => [ 'pivot_type' => 'PIVOT_1' ]]);

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

$article->auditSyncWithPivotValues(
'categories',
$secondCategory,
[ 'pivot_type' => 'PIVOT_1' ]
);

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

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

$this->assertSame(
"{$secondCategory->getKey()}",
$article->categories()->pluck('id')->join(',')
);

$this->assertSame(
$secondCategory->getKey(),
$article->categories()->wherePivot('pivot_type', 'PIVOT_1')->first()->getKey()
);
}

/**
* @test
* @return void
Expand Down Expand Up @@ -697,7 +740,6 @@ function ($categories) { return $categories->wherePivot('pivot_type', 'PIVOT_1')

$this->assertSame($firstCategory->getKey(), $categoryBefore);
$this->assertSame($secondCategory->getKey(), $categoryAfter);
$this->assertNotSame($categoryBefore, $categoryAfter);
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);

$this->assertSame(
Expand Down Expand Up @@ -994,12 +1036,12 @@ public function canAuditAnyCustomEvent()
* @return void
*/
public function canAuditCustomAuditModelImplementation()
{
{
$audit = null;
Event::listen(Audited::class, function ($event) use (&$audit) {
$audit = $event->audit;
});

$article = new ArticleCustomAuditMorph();
$article->title = $this->faker->unique()->sentence;
$article->content = $this->faker->unique()->paragraph(6);
Expand Down

0 comments on commit 5dd4036

Please sign in to comment.