From 8fa15cfb6b5d50fa3ee0f91f458cda1f489736af Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Wed, 26 Feb 2020 11:08:56 -0700 Subject: [PATCH] Fixed url view function improperly stripping leading dotslash on Windows --- app/src/ViewFunctions/Url.php | 4 ++-- tests/ViewFunctions/UrlTest.php | 31 ++++++++++++++++++------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/src/ViewFunctions/Url.php b/app/src/ViewFunctions/Url.php index e415f6a..1bb8b4d 100644 --- a/app/src/ViewFunctions/Url.php +++ b/app/src/ViewFunctions/Url.php @@ -16,12 +16,12 @@ class Url extends ViewFunction */ public function __invoke(string $path = '/'): string { - $path = preg_replace('/^[.\/]+/', '', $path); + $path = preg_replace('/^.?(\/|\\\)+/', '', $path); if (is_file($path)) { return $path; } - return empty($path) ? '' : sprintf('?dir=%s', ltrim($path, './')); + return empty($path) ? '' : sprintf('?dir=%s', $path); } } diff --git a/tests/ViewFunctions/UrlTest.php b/tests/ViewFunctions/UrlTest.php index d0255fe..801e770 100644 --- a/tests/ViewFunctions/UrlTest.php +++ b/tests/ViewFunctions/UrlTest.php @@ -7,32 +7,37 @@ use Tests\TestCase; class UrlTest extends TestCase { - public function test_it_can_return_a_directory_url(): void + public function test_it_can_return_a_url_for_a_directory(): void { $url = new Url($this->container, $this->config); + // Forward slashes $this->assertEquals('', $url('/')); + $this->assertEquals('', $url('./')); $this->assertEquals('?dir=some/path', $url('some/path')); + $this->assertEquals('?dir=some/path', $url('./some/path')); + $this->assertEquals('?dir=some/path', $url('./some/path')); $this->assertEquals('?dir=some/file.test', $url('some/file.test')); + $this->assertEquals('?dir=some/file.test', $url('./some/file.test')); + + // Back slashes + $this->assertEquals('', $url('\\')); + $this->assertEquals('', $url('.\\')); + $this->assertEquals('?dir=some\path', $url('some\path')); + $this->assertEquals('?dir=some\path', $url('.\some\path')); + $this->assertEquals('?dir=some\file.test', $url('some\file.test')); + $this->assertEquals('?dir=some\file.test', $url('.\some\file.test')); } - public function test_it_can_return_a_directory_url_in_a_subdirectory(): void - { - $_SERVER['SCRIPT_NAME'] = '/some/dir/index.php'; - - $url = new Url($this->container, $this->config); - - $this->assertEquals('', $url('/')); - $this->assertEquals('?dir=some/path', $url('some/path')); - $this->assertEquals('?dir=some/file.test', $url('some/file.test')); - } - - public function test_it_can_return_a_file_url(): void + public function test_it_can_return_a_url_for_a_file(): void { chdir($this->filePath('.')); $url = new Url($this->container, $this->config); + $this->assertEquals('README.md', $url('README.md')); + $this->assertEquals('README.md', $url('./README.md')); $this->assertEquals('subdir/alpha.scss', $url('subdir/alpha.scss')); + $this->assertEquals('subdir/alpha.scss', $url('./subdir/alpha.scss')); } }