Skip to content

Commit cb36221

Browse files
authored
Check if a relative url is used when performing broken link checks (#17)
* Check if relative url and try to build an absolute url * Fix styling * Add tests * Fix styling Co-authored-by: Baspa <[email protected]>
1 parent e651f36 commit cb36221

File tree

6 files changed

+56
-12
lines changed

6 files changed

+56
-12
lines changed

src/Checks/Content/BrokenImageCheck.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function validateContent(Crawler $crawler): bool
4747
}
4848

4949
$content = collect($content)->filter(fn ($value) => $value !== null)
50+
->map(fn ($link) => addBaseIfRelativeUrl($link, $this->url))
5051
->filter(fn ($link) => isBrokenLink($link))->toArray();
5152

5253
$this->actualValue = $content;

src/Checks/Content/BrokenLinkCheck.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ public function validateContent(Crawler $crawler): bool
4646
return true;
4747
}
4848

49-
$content = collect($content)->filter(fn ($value) => $value !== null)->filter(function ($link) {
50-
// Filter out all links that are mailto, tel or have a file extension
51-
if (preg_match('/^mailto:/msi', $link) ||
52-
preg_match('/^tel:/msi', $link) ||
53-
preg_match('/\.[a-z]{2,4}$/msi', $link) ||
54-
filter_var($link, FILTER_VALIDATE_URL) === false
55-
) {
56-
return false;
57-
}
58-
59-
return $link;
60-
})->filter(fn ($link) => isBrokenLink($link))->toArray();
49+
$content = collect($content)->filter(fn ($value) => $value !== null)
50+
->map(fn ($link) => addBaseIfRelativeUrl($link, $this->url))
51+
->filter(function ($link) {
52+
// Filter out all links that are mailto, tel or have a file extension
53+
if (preg_match('/^mailto:/msi', $link) ||
54+
preg_match('/^tel:/msi', $link) ||
55+
preg_match('/\.[a-z]{2,4}$/msi', $link) ||
56+
filter_var($link, FILTER_VALIDATE_URL) === false
57+
) {
58+
return false;
59+
}
60+
61+
return $link;
62+
})
63+
->filter(fn ($link) => isBrokenLink($link))->toArray();
6164

6265
$this->actualValue = $content;
6366

src/Seo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Seo
1919
*/
2020
public ProgressBar|null $progress;
2121

22+
public string $url;
23+
2224
public function __construct(
2325
protected Http $http,
2426
protected Collection $successful,
@@ -29,6 +31,7 @@ public function __construct(
2931
public function check(string $url, ProgressBar|null $progress = null): SeoScore
3032
{
3133
$this->progress = $progress;
34+
$this->url = $url;
3235

3336
try {
3437
$response = $this->visitPage(url: $url);
@@ -66,6 +69,7 @@ private function runChecks(Response $response): void
6669
'checks' => $checks,
6770
'progress' => $this->progress,
6871
'crawler' => $crawler,
72+
'url' => $this->url,
6973
])
7074
->through($checks->keys()->toArray())
7175
->then(function ($data) {

src/Traits/PerformCheck.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
trait PerformCheck
88
{
9+
public string|null $url = null;
10+
911
public function __invoke(array $data, Closure $next)
1012
{
13+
$this->url = $data['url'] ?? null;
14+
1115
if (! in_array('exit', $data)) {
1216
$result = $this->check($data['response'], $data['crawler']);
1317
}

src/helpers.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,22 @@ function bytesToHumanReadable(int $bytes): string
135135
return round($bytes / (1000 ** $i), 2).' '.$units[$i];
136136
}
137137
}
138+
139+
if (! function_exists('addBaseIfRelativeUrl')) {
140+
function addBaseIfRelativeUrl(string $url, string|null $checkedUrl = null): string
141+
{
142+
if (! Str::startsWith($url, '/')) {
143+
return $url;
144+
}
145+
146+
if (config('app.url')) {
147+
return config('app.url').$url;
148+
}
149+
150+
if ($checkedUrl) {
151+
return $checkedUrl.$url;
152+
}
153+
154+
return $url;
155+
}
156+
}

tests/Checks/Content/BrokenLinkCheckTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,16 @@
4242

4343
$this->assertTrue($check->check(Http::get('vormkracht10.nl'), $crawler));
4444
});
45+
46+
it('can run the broken link check on a relative url', function () {
47+
$check = new BrokenLinkCheck();
48+
$crawler = new Crawler();
49+
50+
Http::fake([
51+
'vormkracht10.nl' => Http::response('<html><head></head><body><a href="/404">Vormkracht10</a></body></html>', 200),
52+
]);
53+
54+
$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body());
55+
56+
$this->assertFalse($check->check(Http::get('vormkracht10.nl'), $crawler));
57+
});

0 commit comments

Comments
 (0)