Skip to content

Commit c824699

Browse files
committed
Fix #330, Create helper to make pretty URLs if enabled
1 parent a17d449 commit c824699

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/Concerns/Internal/FileHelpers.php

+15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ public static function vendorPath(string $path = ''): string
7373
return static::path('vendor/hyde/framework/'.trim($path, '/\\'));
7474
}
7575

76+
/**
77+
* Format a link to an HTML file, allowing for pretty URLs, if enabled.
78+
* @see \Tests\Unit\HydeFileHelperForPageLinksCanCreatePrettyUrlsTest
79+
*/
80+
public static function pageLink(string $destination): string
81+
{
82+
if (config('hyde.prettyUrls', false) === true) {
83+
if (str_ends_with($destination, '.html')) {
84+
return substr($destination, 0, -5);
85+
}
86+
}
87+
88+
return $destination;
89+
}
90+
7691
/**
7792
* Inject the proper number of `../` before the links in Blade templates.
7893
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Hyde\Framework\Hyde;
6+
use Tests\TestCase;
7+
8+
/**
9+
* Class HydeFileHelperForPageLinksCanCreatePrettyUrlsTest.
10+
*
11+
* @covers \Hyde\Framework\Concerns\Internal\FileHelpers
12+
*/
13+
class HydeFileHelperForPageLinksCanCreatePrettyUrlsTest extends TestCase
14+
{
15+
public function test_helper_returns_string_as_is_if_pretty_urls_is_not_true()
16+
{
17+
config(['hyde.prettyUrls' => false]);
18+
19+
$this->assertEquals('foo/bar.html', Hyde::pageLink('foo/bar.html'));
20+
}
21+
22+
public function test_helper_returns_pretty_url_if_pretty_urls_is_true()
23+
{
24+
config(['hyde.prettyUrls' => true]);
25+
26+
$this->assertEquals('foo/bar', Hyde::pageLink('foo/bar.html'));
27+
}
28+
29+
public function test_non_pretty_urls_is_default_value_when_config_is_not_set()
30+
{
31+
config(['hyde.prettyUrls' => null]);
32+
33+
$this->assertEquals('foo/bar.html', Hyde::pageLink('foo/bar.html'));
34+
}
35+
36+
public function test_helper_respects_absolute_urls()
37+
{
38+
config(['hyde.prettyUrls' => false]);
39+
$this->assertEquals('/foo/bar.html', Hyde::pageLink('/foo/bar.html'));
40+
}
41+
42+
public function test_helper_respects_pretty_absolute_urls()
43+
{
44+
config(['hyde.prettyUrls' => true]);
45+
$this->assertEquals('/foo/bar', Hyde::pageLink('/foo/bar.html'));
46+
}
47+
48+
public function test_helper_respects_relative_urls()
49+
{
50+
config(['hyde.prettyUrls' => false]);
51+
$this->assertEquals('../foo/bar.html', Hyde::pageLink('../foo/bar.html'));
52+
}
53+
54+
public function test_helper_respects_pretty_relative_urls()
55+
{
56+
config(['hyde.prettyUrls' => true]);
57+
$this->assertEquals('../foo/bar', Hyde::pageLink('../foo/bar.html'));
58+
}
59+
60+
public function test_non_html_links_are_not_modified()
61+
{
62+
config(['hyde.prettyUrls' => true]);
63+
$this->assertEquals('/foo/bar.jpg', Hyde::pageLink('/foo/bar.jpg'));
64+
}
65+
66+
public function test_helper_respects_absolute_urls_with_pretty_urls_enabled()
67+
{
68+
config(['hyde.prettyUrls' => true]);
69+
$this->assertEquals('/foo/bar.jpg', Hyde::pageLink('/foo/bar.jpg'));
70+
}
71+
}

0 commit comments

Comments
 (0)