Skip to content

Commit

Permalink
Check if a relative url is used when performing broken link checks (#17)
Browse files Browse the repository at this point in the history
* Check if relative url and try to build an absolute url

* Fix styling

* Add tests

* Fix styling

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa committed Jan 13, 2023
1 parent e651f36 commit cb36221
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Checks/Content/BrokenImageCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function validateContent(Crawler $crawler): bool
}

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

$this->actualValue = $content;
Expand Down
27 changes: 15 additions & 12 deletions src/Checks/Content/BrokenLinkCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,21 @@ public function validateContent(Crawler $crawler): bool
return true;
}

$content = collect($content)->filter(fn ($value) => $value !== null)->filter(function ($link) {
// Filter out all links that are mailto, tel or have a file extension
if (preg_match('/^mailto:/msi', $link) ||
preg_match('/^tel:/msi', $link) ||
preg_match('/\.[a-z]{2,4}$/msi', $link) ||
filter_var($link, FILTER_VALIDATE_URL) === false
) {
return false;
}

return $link;
})->filter(fn ($link) => isBrokenLink($link))->toArray();
$content = collect($content)->filter(fn ($value) => $value !== null)
->map(fn ($link) => addBaseIfRelativeUrl($link, $this->url))
->filter(function ($link) {
// Filter out all links that are mailto, tel or have a file extension
if (preg_match('/^mailto:/msi', $link) ||
preg_match('/^tel:/msi', $link) ||
preg_match('/\.[a-z]{2,4}$/msi', $link) ||
filter_var($link, FILTER_VALIDATE_URL) === false
) {
return false;
}

return $link;
})
->filter(fn ($link) => isBrokenLink($link))->toArray();

$this->actualValue = $content;

Expand Down
4 changes: 4 additions & 0 deletions src/Seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Seo
*/
public ProgressBar|null $progress;

public string $url;

public function __construct(
protected Http $http,
protected Collection $successful,
Expand All @@ -29,6 +31,7 @@ public function __construct(
public function check(string $url, ProgressBar|null $progress = null): SeoScore
{
$this->progress = $progress;
$this->url = $url;

try {
$response = $this->visitPage(url: $url);
Expand Down Expand Up @@ -66,6 +69,7 @@ private function runChecks(Response $response): void
'checks' => $checks,
'progress' => $this->progress,
'crawler' => $crawler,
'url' => $this->url,
])
->through($checks->keys()->toArray())
->then(function ($data) {
Expand Down
4 changes: 4 additions & 0 deletions src/Traits/PerformCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

trait PerformCheck
{
public string|null $url = null;

public function __invoke(array $data, Closure $next)
{
$this->url = $data['url'] ?? null;

if (! in_array('exit', $data)) {
$result = $this->check($data['response'], $data['crawler']);
}
Expand Down
19 changes: 19 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,22 @@ function bytesToHumanReadable(int $bytes): string
return round($bytes / (1000 ** $i), 2).' '.$units[$i];
}
}

if (! function_exists('addBaseIfRelativeUrl')) {
function addBaseIfRelativeUrl(string $url, string|null $checkedUrl = null): string
{
if (! Str::startsWith($url, '/')) {
return $url;
}

if (config('app.url')) {
return config('app.url').$url;
}

if ($checkedUrl) {
return $checkedUrl.$url;
}

return $url;
}
}
13 changes: 13 additions & 0 deletions tests/Checks/Content/BrokenLinkCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,16 @@

$this->assertTrue($check->check(Http::get('vormkracht10.nl'), $crawler));
});

it('can run the broken link check on a relative url', function () {
$check = new BrokenLinkCheck();
$crawler = new Crawler();

Http::fake([
'vormkracht10.nl' => Http::response('<html><head></head><body><a href="/404">Vormkracht10</a></body></html>', 200),
]);

$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body());

$this->assertFalse($check->check(Http::get('vormkracht10.nl'), $crawler));
});

0 comments on commit cb36221

Please sign in to comment.