From 75fc0f64fa4e5072e299d2f57527150a59dbdd49 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Fri, 1 May 2026 00:27:26 -0700 Subject: [PATCH] added test cases, remove activeClass from output if no target set --- src/View/Helper/ActiveLinkHelper.php | 2 ++ .../View/Helper/ActiveLinkHelperTest.php | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/View/Helper/ActiveLinkHelper.php b/src/View/Helper/ActiveLinkHelper.php index 93c009c..bfb2693 100644 --- a/src/View/Helper/ActiveLinkHelper.php +++ b/src/View/Helper/ActiveLinkHelper.php @@ -42,6 +42,8 @@ class ActiveLinkHelper extends Helper { public function link(array|string $title, array|string|null $url = null, array $options = []): string { $currentUrl = $options['current'] ?? Router::parseRequest($this->getView()->getRequest()); if (!array_key_exists('target', $options) || !$currentUrl) { + unset($options['activeClass']); + return $this->Html->link($title, $url, $options); } diff --git a/tests/TestCase/View/Helper/ActiveLinkHelperTest.php b/tests/TestCase/View/Helper/ActiveLinkHelperTest.php index 124e8cd..ff12d42 100644 --- a/tests/TestCase/View/Helper/ActiveLinkHelperTest.php +++ b/tests/TestCase/View/Helper/ActiveLinkHelperTest.php @@ -25,6 +25,10 @@ class ActiveLinkHelperTest extends TestCase { '/', ['controller' => 'Tests', 'action' => 'index'], ); + $routes->get( + '/home', + ['controller' => 'Tests', 'action' => 'index'], + ); $routes->get( '/admin/users', ['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index'], @@ -39,6 +43,20 @@ class ActiveLinkHelperTest extends TestCase { /** * @return void + */ + public function testLinkNoTargetSet(): void { + $request = new ServerRequest(['url' => '/']); + $view = new View($request); + $helper = new ActiveLinkHelper($view); + + $result = $helper->link('goto', '/', [ + 'activeClass' => 'awesome-active-class', + ]); + $this->assertStringNotContainsString('awesome-active-class', $result); + } + + /** + * @return void */ public function testLinkMatchesString(): void { $request = new ServerRequest(['url' => '/']); @@ -50,6 +68,19 @@ class ActiveLinkHelperTest extends TestCase { 'activeClass' => 'awesome-active-class', ]); $this->assertStringContainsString('awesome-active-class', $result); + $this->assertStringNotContainsString('activeClass', $result); + + $request = new ServerRequest(['url' => '/home']); + $view = new View($request); + $helper = new ActiveLinkHelper($view); + + $result = $helper->link('goto', '/', [ + 'target' => '/', + 'activeClass' => 'awesome-active-class', + ]); + $this->assertStringContainsString('awesome-active-class', $result); + $this->assertStringNotContainsString('activeClass', $result); + } /**