added test cases, remove activeClass from output if no target set
All checks were successful
CI / testsuite (sqlite, 8.2, ) (push) Successful in 12m46s
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Successful in 12m35s
CI / testsuite (sqlite, 8.4, ) (push) Successful in 10m20s
CI / Coding Standard & Static Analysis (push) Successful in 13m51s
CI / testsuite (mysql, 8.2, ) (push) Successful in 5m9s
CI / testsuite (mysql, 8.4, ) (push) Successful in 6m20s
CI / testsuite (pgsql, 8.2, ) (push) Successful in 5m16s
CI / testsuite (pgsql, 8.4, ) (push) Successful in 5m51s

This commit is contained in:
2026-05-01 00:27:26 -07:00
parent a70733db9d
commit 75fc0f64fa
2 changed files with 33 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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);
}
/**