Skip to content

Commit

Permalink
Also check links do not start with mailto and tel
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrhymes committed Jan 23, 2024
1 parent 7693c22 commit 200cae1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Jobs/CheckModelForBrokenLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function handle()
foreach ($anchorTags as $anchorTag) {
$href = $anchorTag->getAttribute('href');

if (Str::startsWith($href, ['mailto', 'tel'])) {
continue;
}

$link = new Link;
$link->url = ! Str::startsWith($href, ['http://', 'https://']) && $this->base
? $this->base.$href
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/AvoidTelMailtoLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@
expect($this->post->fresh()->brokenLinks)->toHaveCount(0);

Http::assertNothingSent();

$this->assertDatabaseCount('broken_links', 0);
});

it('does not check tel and mailto links when base is added', function () {
CheckModelForBrokenLinks::dispatch($this->post, ['content'], 'https://this-is-a-relative-link.com/');

Http::assertNothingSent();

$this->assertDatabaseCount('broken_links', 0);
});
10 changes: 5 additions & 5 deletions tests/Feature/RelativeLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
$this->post = Post::factory()
->create([
'content' => '
<a href="/relative">Temporary redirect link</a>',
<a href="relative">Temporary redirect link</a>',
]);
});

it('records relative urls using base url', function () {
CheckModelForBrokenLinks::dispatch($this->post, ['content'], 'https://this-is-a-relative-link.com');
CheckModelForBrokenLinks::dispatch($this->post, ['content'], 'https://this-is-a-relative-link.com/');

$this->assertDatabaseHas('broken_links', [
'linkable_id' => $this->post->id,
Expand All @@ -30,7 +30,7 @@
});

it('records relative urls using base url using the facade', function () {
LinkChecker::checkForBrokenLinks($this->post, ['content'], 'https://this-is-a-relative-link.com');
LinkChecker::checkForBrokenLinks($this->post, ['content'], 'https://this-is-a-relative-link.com/');

$this->assertDatabaseHas('broken_links', [
'linkable_id' => $this->post->id,
Expand All @@ -47,7 +47,7 @@
$this->assertDatabaseHas('broken_links', [
'linkable_id' => $this->post->id,
'linkable_type' => 'ChrisRhymes\LinkChecker\Test\Models\Post',
'broken_link' => '/relative',
'broken_link' => 'relative',
'link_text' => 'Temporary redirect link',
'exception_message' => 'cURL error 6: Could not resolve host: relative (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)',
]);
Expand All @@ -59,7 +59,7 @@
$this->assertDatabaseHas('broken_links', [
'linkable_id' => $this->post->id,
'linkable_type' => 'ChrisRhymes\LinkChecker\Test\Models\Post',
'broken_link' => '/relative',
'broken_link' => 'relative',
'link_text' => 'Temporary redirect link',
'exception_message' => 'cURL error 6: Could not resolve host: relative (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)',
]);
Expand Down

0 comments on commit 200cae1

Please sign in to comment.