diff --git a/app/Models/Component.php b/app/Models/Component.php index a23df90..acc8beb 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -135,19 +135,16 @@ public function links() public function title() { - $pieces = []; - if (count($this->xpath('did/unitdate')) > 0) { - $pieces = array_merge( - $pieces, - $this->xpath('did/unittitle'), - $this->xpath('did/unitdate') - ); - } else { - $pieces = array_merge($pieces, $this->xpath('did/unittitle')); - } + $pieces = array_merge( + $this->xpath('did/unittitle'), + $this->xpath('did/unitdate') + ); $segments = []; foreach ($pieces as $piece) { - $segments[] = fa_render($piece); + $segment = fa_render($piece); + if ($segment !== '') { + $segments[] = $segment; + } } return implode(', ', $segments); } diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php new file mode 100644 index 0000000..28fafc2 --- /dev/null +++ b/tests/ComponentTest.php @@ -0,0 +1,60 @@ +newInstanceWithoutConstructor(); + $component->xml = new SimpleXMLElement('' . $did . ''); + return $component->title(); + } + + public function testTitleJoinsTheUnittitleAndUnitdate(): void + { + $this->assertSame('Letters, 1906', $this->title('Letters1906')); + } + + public function testTitleIsJustTheUnittitleWithoutAUnitdate(): void + { + $this->assertSame('Letters', $this->title('Letters')); + } + + public function testTitleIsJustTheUnitdateWithoutAUnittitle(): void + { + $this->assertSame('1980 March', $this->title('1980 March')); + } + + public function testEmptyUnittitleLeavesNoLeadingComma(): void + { + $this->assertSame('1906', $this->title('1906')); + } + + public function testWhitespaceOnlyUnittitleLeavesNoLeadingComma(): void + { + $this->assertSame('1906', $this->title(' 1906')); + } + + public function testEmptyUnitdateLeavesNoTrailingComma(): void + { + $this->assertSame('Letters', $this->title('Letters')); + } + + public function testEveryUnitdateIsListed(): void + { + $this->assertSame( + 'Headhouse ruins, 1998, undated', + $this->title( + 'Headhouse ruins' + . '1998undated' + ) + ); + } +}