From bdd90f386788b1ce06d7be06eacdc9beedabe5a0 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Mon, 13 Apr 2020 11:58:21 -0700 Subject: [PATCH] Fixed broken parent directory links for directories named '0' --- app/src/ViewFunctions/ParentUrl.php | 10 ++++++++-- tests/ViewFunctions/ParentUrlTest.php | 28 ++++++--------------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/app/src/ViewFunctions/ParentUrl.php b/app/src/ViewFunctions/ParentUrl.php index d0772e0..50a0c25 100644 --- a/app/src/ViewFunctions/ParentUrl.php +++ b/app/src/ViewFunctions/ParentUrl.php @@ -35,8 +35,14 @@ class ParentUrl extends ViewFunction function (string $segment): string { return rawurlencode($segment); } - )->filter()->slice(0, -1)->implode($this->directorySeparator); + )->filter(function ($value): bool { + return $value !== null; + })->slice(0, -1)->implode($this->directorySeparator); - return empty($parentDir) ? '.' : sprintf('?dir=%s', $parentDir); + if ($parentDir === '') { + return '.'; + } + + return sprintf('?dir=%s', $parentDir); } } diff --git a/tests/ViewFunctions/ParentUrlTest.php b/tests/ViewFunctions/ParentUrlTest.php index 2671866..abe856f 100644 --- a/tests/ViewFunctions/ParentUrlTest.php +++ b/tests/ViewFunctions/ParentUrlTest.php @@ -7,41 +7,25 @@ use Tests\TestCase; class ParentUrlTest extends TestCase { - public function test_it_can_get_the_parent_directory_when_one_level_deep(): void + public function test_it_can_get_the_parent_directory(): void { $parentDir = new ParentUrl; $this->assertEquals('.', $parentDir('foo')); - } - - public function test_it_can_get_the_parent_directory_when_two_levels_deep(): void - { - $parentDir = new ParentUrl; - $this->assertEquals('?dir=foo', $parentDir('foo/bar')); - } - - public function test_it_can_get_the_parent_directory_when_three_levels_deep(): void - { - $parentDir = new ParentUrl; - - $this->assertEquals('?dir=foo/bar', $parentDir('foo/bar/baz')); - } - - public function test_it_can_get_the_parent_directory_from_a_path_in_a_subdirectory(): void - { - $_SERVER['SCRIPT_NAME'] = '/some/dir/index.php'; - - $parentDir = new ParentUrl; - $this->assertEquals('?dir=foo/bar', $parentDir('foo/bar/baz')); + $this->assertEquals('?dir=foo/0', $parentDir('foo/0/bar')); + $this->assertEquals('?dir=0', $parentDir('0/bar')); } public function test_it_can_get_the_parent_directory_with_back_slashes(): void { $parentDir = new ParentUrl('\\'); + $this->assertEquals('?dir=foo', $parentDir('foo\bar')); $this->assertEquals('?dir=foo\bar', $parentDir('foo\bar\baz')); + $this->assertEquals('?dir=foo\0', $parentDir('foo\0\bar')); + $this->assertEquals('?dir=0', $parentDir('0\bar')); } public function test_parent_url_segments_are_url_encoded(): void