Fixed incorrect file link URLs

This commit is contained in:
Chris Kankiewicz
2020-02-06 09:31:29 -07:00
parent 68d89ad0e0
commit aae4a25f27
2 changed files with 15 additions and 2 deletions

View File

@@ -18,6 +18,10 @@ class Url extends ViewFunction
{
$path = preg_replace('/^[.\/]+/', '', $path);
if (is_file($path)) {
return $path;
}
return empty($path) ? '' : sprintf('?dir=%s', ltrim($path, './'));
}
}

View File

@@ -7,7 +7,7 @@ use Tests\TestCase;
class UrlTest extends TestCase
{
public function test_it_can_return_a_url(): void
public function test_it_can_return_a_directory_url(): void
{
$url = new Url($this->container, $this->config);
@@ -16,7 +16,7 @@ class UrlTest extends TestCase
$this->assertEquals('?dir=some/file.test', $url('some/file.test'));
}
public function test_it_can_return_a_url_in_a_subdirectory(): void
public function test_it_can_return_a_directory_url_in_a_subdirectory(): void
{
$_SERVER['SCRIPT_NAME'] = '/some/dir/index.php';
@@ -26,4 +26,13 @@ class UrlTest extends TestCase
$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
{
chdir($this->filePath('.'));
$url = new Url($this->container, $this->config);
$this->assertEquals('README.md', $url('README.md'));
$this->assertEquals('subdir/alpha.scss', $url('subdir/alpha.scss'));
}
}