From 1730c276ed41b6a730b1c9e847e2972a0d3a58e9 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Sun, 26 Jan 2020 01:21:08 -0700 Subject: [PATCH] Fixed file and parent dir links not working properly --- app/ViewFunctions/ParentDir.php | 4 +++- app/resources/views/components/file.twig | 2 +- tests/ViewFunctions/ParentDirTest.php | 25 ++++++++++++++++++++++++ tests/ViewFunctions/ParentTest.php | 16 --------------- 4 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 tests/ViewFunctions/ParentDirTest.php delete mode 100644 tests/ViewFunctions/ParentTest.php diff --git a/app/ViewFunctions/ParentDir.php b/app/ViewFunctions/ParentDir.php index e59366f..a90bb48 100644 --- a/app/ViewFunctions/ParentDir.php +++ b/app/ViewFunctions/ParentDir.php @@ -18,8 +18,10 @@ class ParentDir extends ViewFunction */ public function __invoke(string $path) { - return Collection::make( + $parentDir = dirname($_SERVER['SCRIPT_NAME']) . '/' . Collection::make( explode('/', $path) )->filter()->slice(0, -1)->implode('/'); + + return '/' . ltrim($parentDir, '/'); } } diff --git a/app/resources/views/components/file.twig b/app/resources/views/components/file.twig index ab6cb76..abdc818 100644 --- a/app/resources/views/components/file.twig +++ b/app/resources/views/components/file.twig @@ -1,5 +1,5 @@
diff --git a/tests/ViewFunctions/ParentDirTest.php b/tests/ViewFunctions/ParentDirTest.php new file mode 100644 index 0000000..a640d16 --- /dev/null +++ b/tests/ViewFunctions/ParentDirTest.php @@ -0,0 +1,25 @@ +container, $this->config); + + $this->assertEquals('/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 ParentDir($this->container, $this->config); + + $this->assertEquals('/some/dir/foo/bar', $parentDir('foo/bar/baz')); + } +} diff --git a/tests/ViewFunctions/ParentTest.php b/tests/ViewFunctions/ParentTest.php deleted file mode 100644 index 6f6c0f4..0000000 --- a/tests/ViewFunctions/ParentTest.php +++ /dev/null @@ -1,16 +0,0 @@ -container, $this->config); - - $this->assertEquals('foo/bar', $parentDir('foo/bar/baz')); - } -}