Skip to content
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
17 changes: 17 additions & 0 deletions app/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public function __construct(protected $id, private $component_id)
}
}

public static function catalogUrl($dao)
{
$dao = trim((string) ($dao ?? ''));
if ($dao === '') {
return null;
}
return '/catalog/' . rawurlencode($dao);
}

public function links()
{
global $g_minter;
Expand Down Expand Up @@ -126,6 +135,14 @@ public function links()
break;
}
}
// The dao describes the whole $link_raw record. Iterating $link's
// keys avoids re-deriving the image/image_overflow choice above.
$catalog_url = self::catalogUrl($link_raw['dao'] ?? null);
if ($catalog_url !== null) {
foreach (array_keys($link) as $field) {
$link[$field]['catalog_url'] = $catalog_url;
}
}
$links[] = $link;
}
$this->has_image_overflow =
Expand Down
36 changes: 36 additions & 0 deletions tests/CatalogUrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tests\Unit;

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

class CatalogUrlTest extends TestCase
{
public function testPageLevelDaoIsPassedThroughVerbatim(): void
{
$this->assertSame(
'/catalog/xt7x3f4knz7q_1_1',
Component::catalogUrl('xt7x3f4knz7q_1_1')
);
}

public function testMissingDaoYieldsNull(): void
{
$this->assertNull(Component::catalogUrl(null));
}

public function testEmptyDaoYieldsNullRatherThanBareCatalogPath(): void
{
$this->assertNull(Component::catalogUrl(''));
$this->assertNull(Component::catalogUrl(' '));
}

public function testDaoIsUrlEncoded(): void
{
$this->assertSame(
'/catalog/odd%20id%3F1',
Component::catalogUrl('odd id?1')
);
}
}