Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync matched authorities without pivot data #919

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/Harvest/Importers/GmuhkItemImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ public function import(array $row, Progress $result) {
return $authorities->count() === 1;
})
->map->first()
->mapWithKeys(
fn(Authority $authority, $author) => [
$authority->id => ['role' => AuthorityMatcher::parse($author)['alt_name']],
]
);
->mapWithKeys(function(Authority $authority, $author) {
$pivotData = [];
if ($role = AuthorityMatcher::parse($author)['alt_name']) {
$pivotData['role'] = $role;
}

return [$authority->id => $pivotData];
});

$item->syncMatchedAuthorities($matches);

Expand Down
4 changes: 3 additions & 1 deletion app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,9 @@ public function syncMatchedAuthorities(\Illuminate\Support\Collection|array $ids

// Update existing pivot data
foreach ($idsWithPivotData as $id => $pivotData) {
$this->authorities()->updateExistingPivot($id, $pivotData);
if ($pivotData) {
$this->authorities()->updateExistingPivot($id, $pivotData);
}
}

// Create new authorities
Expand Down
36 changes: 36 additions & 0 deletions tests/Models/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,40 @@ public function testSyncMatchedAuthoritiesAddsNew()
'automatically_matched' => true,
]);
}

public function testSyncMatchedAuthoritiesCreatesWithoutPivotData()
{
$item = Item::factory()->create();
$authority = Authority::factory()->create();

$item->syncMatchedAuthorities([
$authority->id => [],
]);

$this->assertDatabaseHas('authority_item', [
'authority_id' => $authority->id,
'item_id' => $item->id,
'role' => null,
'automatically_matched' => true,
]);
}

public function testSyncMatchedAuthoritiesUpdatesWithoutPivotData()
{
$item = Item::factory()->create();
$authority = Authority::factory()->create();
$item->authorities()->sync([
$authority->id => ['role' => 'author'],
]);

$item->syncMatchedAuthorities([
$authority->id => [],
]);

$this->assertDatabaseHas('authority_item', [
'authority_id' => $authority->id,
'item_id' => $item->id,
'role' => 'author',
]);
}
}