Skip to content

Commit

Permalink
Check if the field is a url and check that
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrhymes committed Aug 24, 2023
1 parent 93c224f commit e346e52
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 26 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Link Checker for Laravel

A package that will check for broken links in the HTML of a specified model's fields.
A package that will check for broken links in the specified model's fields. It will check both URL fields and fields containing HTML.

![Downloads](https://img.shields.io/packagist/dt/chrisrhymes/link-checker.svg)
![Downloads](https://img.shields.io/github/stars/chrisrhymes/link-checker.svg)
Expand Down Expand Up @@ -56,7 +56,7 @@ php artisan vendor:publish --provider="ChrisRhymes\LinkChecker\ServiceProvider"

## Usage

Then you can check if the model has broken links in the html in a specific field.
Then you can check if the model has broken links in the specified field(s).

```php
use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
Expand All @@ -65,10 +65,10 @@ use ChrisRhymes\LinkChecker\Facades\LinkChecker;
$post = Post::first();

// Dispatch the job directly
CheckModelForBrokenLinks::dispatch($post, ['content']);
CheckModelForBrokenLinks::dispatch($post, ['content', 'url']);

// Or using the facade
LinkChecker::checkForBrokenLinks($post, ['content']);
LinkChecker::checkForBrokenLinks($post, ['content', 'url']);
```

This will queue a job to get the links from the model, which will then queue a job to check each link it finds.
Expand Down
11 changes: 11 additions & 0 deletions src/Jobs/CheckModelForBrokenLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class CheckModelForBrokenLinks implements ShouldQueue
{
Expand Down Expand Up @@ -48,6 +49,16 @@ public function handle()
})
->filter()
->each(function ($field) {
if (Str::isUrl($field)) {
$link = new Link;
$link->url = $field;
$link->text = $field;

$this->links[] = $link;

return;
}

try {
$doc = new DOMDocument();
$doc->loadHTML($field);
Expand Down
1 change: 1 addition & 0 deletions tests/Database/Factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function definition()
return [
'title' => $this->faker->words(3, true),
'content' => $this->faker->text(),
'url' => $this->faker->url(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function up()
$table->bigIncrements('id');
$table->string('title');
$table->text('content');
$table->string('url')->nullable();
$table->timestamps();
});
}
Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/AvoidTelMailtoLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function () {
Http::fake();

Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/CheckForBrokenLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Models\BrokenLink;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Queue;

uses(RefreshDatabase::class);

beforeEach(function () {
Http::fake([
'https://this-is-broken.com' => Http::response(null, 404),
Expand Down
66 changes: 66 additions & 0 deletions tests/Feature/ChecksUrlFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Models\BrokenLink;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Support\Facades\Http;

beforeEach(function () {
Http::fake([
'https://this-is-broken.com' => Http::response(null, 404),
]);

$this->post = Post::factory()
->create([
'content' => '<a href="https://this-is-broken.com">Broken link</a>',
'url' => 'https://this-is-broken.com',
]);
});

it('checks a url not in html', function () {
CheckModelForBrokenLinks::dispatch($this->post, ['url']);

$this->assertDatabaseHas('broken_links', [
'linkable_id' => $this->post->id,
'linkable_type' => 'ChrisRhymes\LinkChecker\Test\Models\Post',
'broken_link' => 'https://this-is-broken.com',
'link_text' => 'https://this-is-broken.com',
'exception_message' => '404 Status Code',
]);
});

it('checks for broken links in both specified fields', function () {
CheckModelForBrokenLinks::dispatch($this->post, ['content', 'url']);

expect(BrokenLink::count())
->toEqual(2);

$this->assertDatabaseHas('broken_links', [
'linkable_id' => $this->post->id,
'linkable_type' => 'ChrisRhymes\LinkChecker\Test\Models\Post',
'broken_link' => 'https://this-is-broken.com',
'link_text' => 'https://this-is-broken.com',
'exception_message' => '404 Status Code',
]);

$this->assertDatabaseHas('broken_links', [
'linkable_id' => $this->post->id,
'linkable_type' => 'ChrisRhymes\LinkChecker\Test\Models\Post',
'broken_link' => 'https://this-is-broken.com',
'link_text' => 'Broken link',
'exception_message' => '404 Status Code',
]);
});

it('ignores invalid url value', function () {
$postWithInvalidUrl = Post::factory()->create([
'url' => 'not a link',
]);

CheckModelForBrokenLinks::dispatch($postWithInvalidUrl, ['url']);

Http::assertNothingSent();

expect(BrokenLink::count())
->toEqual(0);
});
3 changes: 0 additions & 3 deletions tests/Feature/CustomUserAgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function () {
Http::fake();

Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/HasBrokenLinksTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

beforeEach(function () {
$this->post = Post::factory()
Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/RateLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Models\BrokenLink;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function () {
Http::fake([
'*' => Http::response(null, 404),
Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function () {
Http::fake([
'https://this-is-a-temporary-redirect.com' => Http::response(null, 302),
Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/VerifySSLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
use ChrisRhymes\LinkChecker\Jobs\CheckModelForBrokenLinks;
use ChrisRhymes\LinkChecker\Models\BrokenLink;
use ChrisRhymes\LinkChecker\Test\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function () {
Http::fake([
'*' => Http::response(null, 404),
Expand Down
5 changes: 4 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
|
*/

uses(ChrisRhymes\LinkChecker\Test\TestCase::class)->in('Feature');
uses(
ChrisRhymes\LinkChecker\Test\TestCase::class,
Illuminate\Foundation\Testing\RefreshDatabase::class
)->in('Feature');

/*
|--------------------------------------------------------------------------
Expand Down

0 comments on commit e346e52

Please sign in to comment.