Skip to content

Commit 624a56f

Browse files
[6.x] Fix CP Nav active state when trailing slashes are enforced (#14363)
1 parent 0b82eee commit 624a56f

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/CP/Navigation/NavItem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ protected function generateActivePatternForCpUrl($url)
149149
$cpUrl = url(config('statamic.cp.route')).'/';
150150

151151
$relativeUrl = str_replace($cpUrl, '', URL::removeQueryAndFragment($url));
152+
$relativeUrl = rtrim($relativeUrl, '/');
152153

153154
return $relativeUrl.'(/(.*)?|$)';
154155
}

tests/CP/Navigation/ActiveNavItemTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Statamic\Facades;
1313
use Statamic\Facades\Blink;
1414
use Statamic\Facades\CP\Nav;
15+
use Statamic\Facades\URL;
1516
use Statamic\Facades\User;
1617
use Tests\FakesRoles;
1718
use Tests\PreventSavingStacheItemsToDisk;
@@ -35,6 +36,13 @@ public function setUp(): void
3536
Facades\Form::shouldReceive('all')->andReturn(collect());
3637
}
3738

39+
public function tearDown(): void
40+
{
41+
URL::enforceTrailingSlashes(false);
42+
43+
parent::tearDown();
44+
}
45+
3846
protected function resolveApplicationConfiguration($app)
3947
{
4048
parent::resolveApplicationConfiguration($app);
@@ -408,6 +416,34 @@ public function it_resolves_extension_children_closure_and_can_check_when_parent
408416
$this->assertTrue($this->getItemByDisplay($seoPro->children(), 'Section Defaults')->isActive());
409417
}
410418

419+
#[Test]
420+
public function it_properly_handles_is_active_checks_when_trailing_slashes_are_enforced()
421+
{
422+
URL::enforceTrailingSlashes();
423+
424+
Nav::clearCachedUrls();
425+
426+
$parent = Nav::create('parent')
427+
->section('test')
428+
->url('http://localhost/cp/parent')
429+
->children([
430+
$hello = Nav::create('hello')->url('http://localhost/cp/hello'),
431+
$world = Nav::create('world')->url('http://localhost/cp/world'),
432+
]);
433+
434+
// Test active status with trailing slash in request URL
435+
Request::swap(Request::create('http://localhost/cp/hello/'));
436+
$this->assertTrue($parent->isActive());
437+
$this->assertTrue($hello->isActive());
438+
$this->assertFalse($world->isActive());
439+
440+
// Test active status on descendant with trailing slash
441+
Request::swap(Request::create('http://localhost/cp/hello/nested/path/'));
442+
$this->assertTrue($parent->isActive());
443+
$this->assertTrue($hello->isActive());
444+
$this->assertFalse($world->isActive());
445+
}
446+
411447
#[Test]
412448
public function it_properly_handles_various_edge_cases_when_checking_is_active_on_descendants_of_nav_children()
413449
{

0 commit comments

Comments
 (0)