Skip to content

Commit

Permalink
Merge pull request #1238 from hydephp/bugfixes
Browse files Browse the repository at this point in the history
Update pretty relative index links to rewrite to `./` instead of `/`
  • Loading branch information
caendesilva authored Mar 8, 2023
2 parents 03d3235 + d5f89a2 commit d15496b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
2 changes: 1 addition & 1 deletion _pages/404.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Sorry, the page you are looking for could not be found.
</p>

<a href="{{ config('hyde.url') ?? '/' }}">
<a href="{{ config('hyde.url') ?? './' }}">
<button
class="bg-transparent text-grey-darkest font-bold uppercase tracking-wide py-3 px-6 border-2 border-grey-light hover:border-grey rounded-lg">
Go Home
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/resources/views/pages/404.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Sorry, the page you are looking for could not be found.
</p>

<a href="{{ config('hyde.url') ?? '/' }}">
<a href="{{ config('hyde.url') ?? './' }}">
<button
class="bg-transparent text-grey-darkest font-bold uppercase tracking-wide py-3 px-6 border-2 border-grey-light hover:border-grey rounded-lg">
Go Home
Expand Down
4 changes: 4 additions & 0 deletions packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function relativeLink(string $destination): string
}
$route .= $this->formatLink($destination);

if (Config::getBool('hyde.pretty_urls', false) === true && $route === '/') {
return './';
}

return str_replace('//', '/', $route);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/framework/tests/Unit/BreadcrumbsComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testCanGenerateBreadcrumbsForIndexPageWithPrettyUrls()
self::mockConfig(['hyde.pretty_urls' => true]);
$this->mockPage(new MarkdownPage('index'));

$this->assertSame(['/' => 'Home'], (new BreadcrumbsComponent())->breadcrumbs);
$this->assertSame(['./' => 'Home'], (new BreadcrumbsComponent())->breadcrumbs);
}

public function testCanGenerateBreadcrumbsForNestedPageWithPrettyUrls()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,99 +5,116 @@
namespace Hyde\Framework\Testing\Unit\Foundation;

use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\RenderData;
use Hyde\Testing\InteractsWithPages;
use Hyde\Testing\UnitTestCase;
use Illuminate\Support\Facades\View;
use Illuminate\View\Factory;
use Mockery;

/**
* @covers \Hyde\Foundation\Kernel\Hyperlinks::relativeLink
*/
class HyperlinkFileHelperRelativeLinkTest extends TestCase
class HyperlinkFileHelperRelativeLinkTest extends UnitTestCase
{
public function test_helper_returns_string_as_is_if_current_is_not_set()
use InteractsWithPages;

protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

protected function setUp(): void
{
Render::swap(new RenderData());
View::swap(Mockery::mock(Factory::class)->makePartial());
}

public function testHelperReturnsStringAsIsIfCurrentIsNotSet()
{
$this->assertEquals('foo/bar.html', Hyde::relativeLink('foo/bar.html'));
}

public function test_helper_injects_proper_number_of_doubles_slash()
public function testHelperInjectsProperNumberOfDoublesSlash()
{
$this->mockCurrentPage('foo/bar.html');
$this->assertEquals('../foo.html', Hyde::relativeLink('foo.html'));
}

public function test_helper_injects_proper_number_of_doubles_slash_for_deeply_nested_paths()
public function testHelperInjectsProperNumberOfDoublesSlashForDeeplyNestedPaths()
{
$this->mockCurrentPage('foo/bar/baz/qux.html');
$this->assertEquals('../../../foo.html', Hyde::relativeLink('foo.html'));
}

public function test_helper_handles_destination_without_file_extension()
public function testHelperHandlesDestinationWithoutFileExtension()
{
$this->mockCurrentPage('foo/bar.html');
$this->assertEquals('../foo', Hyde::relativeLink('foo'));
}

public function test_helper_handles_current_without_file_extension()
public function testHelperHandlesCurrentWithoutFileExtension()
{
$this->mockCurrentPage('foo/bar');
$this->assertEquals('../foo.html', Hyde::relativeLink('foo.html'));
}

public function test_helper_handles_case_without_any_file_extensions()
public function testHelperHandlesCaseWithoutAnyFileExtensions()
{
$this->mockCurrentPage('foo/bar');
$this->assertEquals('../foo', Hyde::relativeLink('foo'));
}

public function test_helper_handles_case_with_mixed_file_extensions()
public function testHelperHandlesCaseWithMixedFileExtensions()
{
$this->mockCurrentPage('foo/bar.md');
$this->assertEquals('../foo.md', Hyde::relativeLink('foo.md'));
$this->mockCurrentPage('foo/bar.txt');
$this->assertEquals('../foo.txt', Hyde::relativeLink('foo.txt'));
}

public function test_helper_handles_different_file_extensions()
public function testHelperHandlesDifferentFileExtensions()
{
$this->mockCurrentPage('foo/bar');
$this->assertEquals('../foo.png', Hyde::relativeLink('foo.png'));
$this->assertEquals('../foo.css', Hyde::relativeLink('foo.css'));
$this->assertEquals('../foo.js', Hyde::relativeLink('foo.js'));
}

public function test_helper_returns_pretty_url_if_enabled_and_destination_is_a_html_file()
public function testHelperReturnsPrettyUrlIfEnabledAndDestinationIsAHtmlFile()
{
config(['hyde.pretty_urls' => true]);
self::mockConfig(['hyde.pretty_urls' => true]);
$this->mockCurrentPage('foo/bar.html');
$this->assertEquals('../foo', Hyde::relativeLink('foo.html'));
}

public function test_helper_method_does_not_require_current_path_to_be_html_to_use_pretty_urls()
public function testHelperMethodDoesNotRequireCurrentPathToBeHtmlToUsePrettyUrls()
{
config(['hyde.pretty_urls' => true]);
self::mockConfig(['hyde.pretty_urls' => true]);
$this->mockCurrentPage('foo/bar');
$this->assertEquals('../foo', Hyde::relativeLink('foo.html'));
}

public function test_helper_returns_does_not_return_pretty_url_if_when_enabled_but_and_destination_is_not_a_html_file()
public function testHelperReturnsDoesNotReturnPrettyUrlIfWhenEnabledButAndDestinationIsNotAHtmlFile()
{
config(['hyde.pretty_urls' => true]);
self::mockConfig(['hyde.pretty_urls' => true]);
$this->mockCurrentPage('foo/bar.html');
$this->assertEquals('../foo.png', Hyde::relativeLink('foo.png'));
}

public function test_helper_rewrites_index_when_using_pretty_urls()
public function testHelperRewritesIndexWhenUsingPrettyUrls()
{
config(['hyde.pretty_urls' => true]);
self::mockConfig(['hyde.pretty_urls' => true]);
$this->mockCurrentPage('foo.html');
$this->assertEquals('/', Hyde::relativeLink('index.html'));
$this->assertEquals('./', Hyde::relativeLink('index.html'));
$this->mockCurrentPage('foo/bar.html');
$this->assertEquals('../', Hyde::relativeLink('index.html'));
$this->mockCurrentPage('foo/bar/baz.html');
$this->assertEquals('../../', Hyde::relativeLink('index.html'));
}

public function test_helper_does_not_rewrite_index_when_not_using_pretty_urls()
public function testHelperDoesNotRewriteIndexWhenNotUsingPrettyUrls()
{
config(['hyde.pretty_urls' => false]);
self::mockConfig(['hyde.pretty_urls' => false]);
$this->mockCurrentPage('foo.html');
$this->assertEquals('index.html', Hyde::relativeLink('index.html'));
$this->mockCurrentPage('foo/bar.html');
Expand All @@ -106,9 +123,9 @@ public function test_helper_does_not_rewrite_index_when_not_using_pretty_urls()
$this->assertEquals('../../index.html', Hyde::relativeLink('index.html'));
}

public function test_helper_rewrites_documentation_page_index_when_using_pretty_urls()
public function testHelperRewritesDocumentationPageIndexWhenUsingPrettyUrls()
{
config(['hyde.pretty_urls' => true]);
self::mockConfig(['hyde.pretty_urls' => true]);
$this->mockCurrentPage('foo.html');
$this->assertEquals('docs/', Hyde::relativeLink('docs/index.html'));
$this->mockCurrentPage('docs.html');
Expand All @@ -119,9 +136,9 @@ public function test_helper_rewrites_documentation_page_index_when_using_pretty_
$this->assertEquals('../docs/', Hyde::relativeLink('docs/index.html'));
}

public function test_helper_does_not_rewrite_documentation_page_index_when_not_using_pretty_urls()
public function testHelperDoesNotRewriteDocumentationPageIndexWhenNotUsingPrettyUrls()
{
config(['hyde.pretty_urls' => false]);
self::mockConfig(['hyde.pretty_urls' => false]);
$this->mockCurrentPage('foo.html');
$this->assertEquals('docs/index.html', Hyde::relativeLink('docs/index.html'));
$this->mockCurrentPage('docs.html');
Expand All @@ -132,7 +149,7 @@ public function test_helper_does_not_rewrite_documentation_page_index_when_not_u
$this->assertEquals('../docs/index.html', Hyde::relativeLink('docs/index.html'));
}

public function test_helper_does_not_rewrite_already_processed_links()
public function testHelperDoesNotRewriteAlreadyProcessedLinks()
{
$this->assertEquals('../foo', Hyde::relativeLink('../foo'));
}
Expand Down

0 comments on commit d15496b

Please sign in to comment.