From 0a1437f63dca1c94795637831c4b34c04cdabdc9 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 27 Sep 2023 12:19:10 +0200 Subject: [PATCH] Only mark link as broken when it matches the configured status (#45) * Only mark link as broken when it matches the configured status * Fix styling --------- Co-authored-by: Baspa --- config/seo.php | 11 +++++++++++ src/Checks/Content/BrokenLinkCheck.php | 8 +------- src/helpers.php | 6 +++++- tests/Checks/Content/BrokenLinkCheckTest.php | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/config/seo.php b/config/seo.php index 682c6a64..1f4b3b8b 100644 --- a/config/seo.php +++ b/config/seo.php @@ -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' => [], + ], ]; diff --git a/src/Checks/Content/BrokenLinkCheck.php b/src/Checks/Content/BrokenLinkCheck.php index fae119ff..7efd41f6 100644 --- a/src/Checks/Content/BrokenLinkCheck.php +++ b/src/Checks/Content/BrokenLinkCheck.php @@ -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, diff --git a/src/helpers.php b/src/helpers.php index c4572e90..efb74644 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -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; } diff --git a/tests/Checks/Content/BrokenLinkCheckTest.php b/tests/Checks/Content/BrokenLinkCheckTest.php index d23d55a5..f542c0a5 100644 --- a/tests/Checks/Content/BrokenLinkCheckTest.php +++ b/tests/Checks/Content/BrokenLinkCheckTest.php @@ -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('Vormkracht10', 200), + ]); + + $crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); + + $this->assertTrue($check->check(Http::get('vormkracht10.nl/admin/dashboard'), $crawler)); +});