From 210736f6960dd9b2d57cd378936b0c40f196de85 Mon Sep 17 00:00:00 2001 From: Ernest Walzel Date: Thu, 14 Mar 2024 11:06:20 +0100 Subject: [PATCH] Order Item#authors based on author field --- app/Item.php | 26 +++++++++++--------------- tests/Models/ItemTest.php | 20 ++++++++++++-------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/Item.php b/app/Item.php index 884f50aff..611ebd058 100644 --- a/app/Item.php +++ b/app/Item.php @@ -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; @@ -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() diff --git a/tests/Models/ItemTest.php b/tests/Models/ItemTest.php index 7acbbad29..4482528e1 100644 --- a/tests/Models/ItemTest.php +++ b/tests/Models/ItemTest.php @@ -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() @@ -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()