From 806b3ab208c67a96cea5e8e96354e16b53658669 Mon Sep 17 00:00:00 2001 From: Neal Date: Wed, 5 Aug 2026 15:39:44 -0400 Subject: [PATCH] Add catalog_url for redirects to exploreuk-web-app --- app/Models/Component.php | 17 +++++++++++++++++ tests/CatalogUrlTest.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/CatalogUrlTest.php diff --git a/app/Models/Component.php b/app/Models/Component.php index a23df90..236a6c9 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -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; @@ -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 = diff --git a/tests/CatalogUrlTest.php b/tests/CatalogUrlTest.php new file mode 100644 index 0000000..b41c95b --- /dev/null +++ b/tests/CatalogUrlTest.php @@ -0,0 +1,36 @@ +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') + ); + } +}