Skip to content
Merged
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
19 changes: 8 additions & 11 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
60 changes: 60 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Tests\Unit;

use App\Models\Component;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SimpleXMLElement;

class ComponentTest extends TestCase
{
private function title(string $did): string
{
// The full Component.php constructor needs an XML file so make a small fixture instead
$component = (new ReflectionClass(Component::class))->newInstanceWithoutConstructor();
$component->xml = new SimpleXMLElement('<c id="ref1" level="item"><did>' . $did . '</did></c>');
return $component->title();
}

public function testTitleJoinsTheUnittitleAndUnitdate(): void
{
$this->assertSame('Letters, 1906', $this->title('<unittitle>Letters</unittitle><unitdate>1906</unitdate>'));
}

public function testTitleIsJustTheUnittitleWithoutAUnitdate(): void
{
$this->assertSame('Letters', $this->title('<unittitle>Letters</unittitle>'));
}

public function testTitleIsJustTheUnitdateWithoutAUnittitle(): void
{
$this->assertSame('1980 March', $this->title('<unitdate>1980 March</unitdate>'));
}

public function testEmptyUnittitleLeavesNoLeadingComma(): void
{
$this->assertSame('1906', $this->title('<unittitle/><unitdate>1906</unitdate>'));
}

public function testWhitespaceOnlyUnittitleLeavesNoLeadingComma(): void
{
$this->assertSame('1906', $this->title('<unittitle> </unittitle><unitdate>1906</unitdate>'));
}

public function testEmptyUnitdateLeavesNoTrailingComma(): void
{
$this->assertSame('Letters', $this->title('<unittitle>Letters</unittitle><unitdate/>'));
}

public function testEveryUnitdateIsListed(): void
{
$this->assertSame(
'Headhouse ruins, 1998, undated',
$this->title(
'<unittitle>Headhouse ruins</unittitle>'
. '<unitdate type="inclusive">1998</unitdate><unitdate>undated</unitdate>'
)
);
}
}
Loading