Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only mark link as broken when it matches the configured status #45

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
});