diff --git a/app/Item.php b/app/Item.php index 5692c4783..60361409e 100644 --- a/app/Item.php +++ b/app/Item.php @@ -779,8 +779,9 @@ public function getImageUrlAttribute() return sprintf('%s%s', config('app.url'), $this->getImagePath()); } - public function syncMatchedAuthorities(\Illuminate\Support\Collection $idsWithPivotData) + public function syncMatchedAuthorities(\Illuminate\Support\Collection|array $idsWithPivotData) { + $idsWithPivotData = collect($idsWithPivotData); // Detach automatically-matched authorities that no longer match $this->authorities() ->wherePivot('automatically_matched', true) diff --git a/tests/Models/ItemTest.php b/tests/Models/ItemTest.php index 6fdea5a51..b129d0569 100644 --- a/tests/Models/ItemTest.php +++ b/tests/Models/ItemTest.php @@ -187,43 +187,75 @@ protected function createFreeItem() ]); } - public function testSyncMatchedAuthorities() + public function testSyncMatchedAuthoritiesUpdatesExisting() { - $authorities = Authority::factory() - ->count(5) + $item = Item::factory()->create(); + [$authority1, $authority2] = Authority::factory() + ->count(2) ->create(); + $item->authorities()->sync([ + $authority1->id => ['role' => 'author', 'automatically_matched' => true], + $authority2->id => ['role' => 'author', 'automatically_matched' => false], + ]); + + $item->syncMatchedAuthorities([ + $authority1->id => ['role' => 'new-role-1'], + $authority2->id => ['role' => 'new-role-2'], + ]); + + $this->assertDatabaseHas('authority_item', [ + 'authority_id' => $authority1->id, + 'item_id' => $item->id, + 'role' => 'new-role-1', + 'automatically_matched' => true, + ]); + $this->assertDatabaseHas('authority_item', [ + 'authority_id' => $authority2->id, + 'item_id' => $item->id, + 'role' => 'new-role-2', + 'automatically_matched' => false, + ]); + } + + public function testSyncMatchedAuthoritiesDeletesOnlyAutomaticallyMatched() + { $item = Item::factory()->create(); + [$authority1, $authority2] = Authority::factory() + ->count(2) + ->create(); $item->authorities()->sync([ - // present in matched authorities - $authorities[0]->id => ['role' => 'author', 'automatically_matched' => true], - $authorities[1]->id => ['role' => 'author', 'automatically_matched' => false], - // missing from matched authorities - $authorities[2]->id => ['role' => 'author', 'automatically_matched' => true], - $authorities[3]->id => ['role' => 'author', 'automatically_matched' => false], - ]); - - $item->syncMatchedAuthorities( - collect([ - $authorities[0]->id => ['role' => 'after'], - $authorities[1]->id => ['role' => 'after'], - $authorities[4]->id => ['role' => 'after'], - ]) - ); + $authority1->id => ['automatically_matched' => true, 'role' => 'author'], + $authority2->id => ['automatically_matched' => false, 'role' => 'author'], + ]); - $item->refresh(); - $this->assertCount(4, $item->authorities); - $this->assertEquals( - [ - $authorities[0]->id => ['role' => 'after', 'automatically_matched' => true], - $authorities[1]->id => ['role' => 'after', 'automatically_matched' => false], - $authorities[3]->id => ['role' => 'author', 'automatically_matched' => false], - $authorities[4]->id => ['role' => 'after', 'automatically_matched' => true], - ], - $item->authorities - ->pluck('pivot') - ->keyBy('authority_id') - ->map->only(['role', 'automatically_matched']) - ->toArray() - ); + $item->syncMatchedAuthorities([]); + + $this->assertDatabaseMissing('authority_item', [ + 'authority_id' => $authority1->id, + 'item_id' => $item->id, + ]); + $this->assertDatabaseHas('authority_item', [ + 'authority_id' => $authority2->id, + 'item_id' => $item->id, + 'role' => 'author', + 'automatically_matched' => false, + ]); + } + + public function testSyncMatchedAuthoritiesAddsNew() + { + $item = Item::factory()->create(); + $authority = Authority::factory()->create(); + + $item->syncMatchedAuthorities([ + $authority->id => ['role' => 'author'], + ]); + + $this->assertDatabaseHas('authority_item', [ + 'authority_id' => $authority->id, + 'item_id' => $item->id, + 'role' => 'author', + 'automatically_matched' => true, + ]); } }