Skip to content

Commit

Permalink
Order Item#authors based on author field
Browse files Browse the repository at this point in the history
  • Loading branch information
eronisko committed Mar 14, 2024
1 parent 1c378e5 commit 210736f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
26 changes: 11 additions & 15 deletions app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Chelout\RelationshipEvents\Concerns\HasBelongsToManyEvents;
use ElasticScoutDriverPlus\Searchable;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Date;
Expand Down Expand Up @@ -379,21 +380,16 @@ public function getAuthorsWithoutAuthority()

public function getAuthorsWithAuthoritiesAttribute()
{
$authorities = $this
->authorities
->map(fn ($authority) => (object) [
'name' => $authority->name,
'authority' => $authority
]);

$authors = $this
->getAuthorsWithoutAuthority()
->map(fn ($author) => (object) [
'name' => $author,
'authority' => null
]);

return $authorities->concat($authors);
return app(AuthorityMatcher::class)
->matchAll($this, onlyExisting: true)
->map(function (EloquentCollection $authorities, string $author) {
$authority = $authorities->first();
return (object) [
'name' => $authority->name ?? $author,
'authority' => $authority,
];
})
->values();
}

public function getUniqueAuthorsWithAuthorityNames()
Expand Down
20 changes: 12 additions & 8 deletions tests/Models/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public function testMergedAuthorityNamesAndAuthors()

$data = $item->getIndexedData('sk');
$this->assertCount(2, $data['author']);
$this->assertEquals('Boudník, Vladimír', $data['author'][0]);
$this->assertEquals('Philips Wouwerman', $data['author'][1]);
$this->assertEquals('Philips Wouwerman', $data['author'][0]);
$this->assertEquals('Boudník, Vladimír', $data['author'][1]);
}

public function testEmptyAuthorityName()
Expand Down Expand Up @@ -186,14 +186,18 @@ public function testAuthorsWithAuthoritiesAttribute()
$data = $item->authors_with_authorities;
$this->assertCount(3, $data);

$this->assertEquals('Boudník, Vladimír', $data[0]->name);
$this->assertInstanceOf(Authority::class, $data[0]->authority);
$this->assertEquals($authority->id, $data[0]->authority->id);
$this->assertEquals('Philips Wouwerman', $data[0]->name);
$this->assertEquals(null, $data[0]->authority);

$this->assertEquals('Philips Wouwerman', $data[1]->name);
$this->assertEquals(null, $data[1]->authority);
$this->assertEquals('Boudník, Vladimír', $data[1]->name);
$this->assertInstanceOf(Authority::class, $data[1]->authority);
$this->assertEquals($authority->id, $data[1]->authority->id);

$this->markTestSkipped('should list in the order of the author field');
// Order of the authors is preserved
$this->assertEquals(
['Philips Wouwerman', 'Boudník, Vladimír', 'Mikuláš Galanda'],
$data->pluck('name')->toArray()
);
}

protected function createFreeItem()
Expand Down

0 comments on commit 210736f

Please sign in to comment.