Skip to content

Commit

Permalink
Only mark link as broken when it matches the configured status (#45)
Browse files Browse the repository at this point in the history
* Only mark link as broken when it matches the configured status

* Fix styling

---------

Co-authored-by: Baspa <Baspa@users.noreply.github.com>
  • Loading branch information
Baspa and Baspa authored Sep 27, 2023
1 parent b4e29ac commit 0a1437f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
11 changes: 11 additions & 0 deletions config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,15 @@
|
*/
'javascript' => false,

/*
|--------------------------------------------------------------------------
| Check specific options
|--------------------------------------------------------------------------
|
*/
'broken_link_check' => [
// Add status codes that should be considered as broken links. Empty array means all status codes starting with a 4, 5 or 0.
'status_codes' => [],
],
];
8 changes: 1 addition & 7 deletions src/Checks/Content/BrokenLinkCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ public function validateContent(Crawler $crawler): bool
return $link;
})
->filter(function ($link) {
$statusCode = (string) getRemoteStatus($link);

if (str_starts_with($statusCode, '4') || str_starts_with($statusCode, '5') || $statusCode === '0') {
return $link;
}

return false;
return isBrokenLink($link) ? $link : false;
})->map(function ($link) {
return [
'url' => $link,
Expand Down
6 changes: 5 additions & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ function isBrokenLink(string $url): bool
{
$statusCode = (string) getRemoteStatus($url);

if (str_starts_with($statusCode, '4') || str_starts_with($statusCode, '5') || $statusCode === '0') {
if (! empty(config('seo.broken_link_check.status_codes'))) {
return in_array($statusCode, config('seo.broken_link_check.status_codes'));
}

if (str_starts_with($statusCode, '4') || str_starts_with($statusCode, '5') || $statusCode == '0') {
return true;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Checks/Content/BrokenLinkCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,18 @@

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

it('can check if link is broken by checking on configured status codes', function () {
$check = new BrokenLinkCheck();
$crawler = new Crawler();

config(['seo.broken_link_check.status_codes' => ['403']]);

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

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

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

0 comments on commit 0a1437f

Please sign in to comment.