Skip to content

Commit

Permalink
Merge pull request #35 from richdynamix/feature/canonical-url-override
Browse files Browse the repository at this point in the history
Add canonical_url to SEO Data object for override
  • Loading branch information
ralphjsmit authored Apr 22, 2023
2 parents a89eef3 + f744830 commit 3fdd02b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions database/migrations/create_seo_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ return new class extends Migration
$table->string('image')->nullable();
$table->string('author')->nullable();
$table->string('robots')->nullable();
$table->string('canonical_url')->nullable();

$table->timestamps();
});
Expand Down
2 changes: 2 additions & 0 deletions src/Models/SEO.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function model(): MorphTo
public function prepareForUsage(): SEOData
{
if ( method_exists($this->model, 'getDynamicSEOData') ) {
/** @var SEOData $overrides */
$overrides = $this->model->getDynamicSEOData();
}

Expand All @@ -45,6 +46,7 @@ public function prepareForUsage(): SEOData
type: $overrides->type ?? null,
locale: $overrides->locale ?? null,
robots: $overrides->robots ?? $this->robots,
canonical_url: $overrides->canonical_url ?? $this->canonical_url,
);
}
}
1 change: 1 addition & 0 deletions src/Support/SEOData.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
public ?string $favicon = null,
public ?string $locale = null,
public ?string $robots = null,
public ?string $canonical_url = null,
) {
if ( $this->locale === null ) {
$this->locale = Str::of(app()->getLocale())->lower()->kebab();
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/CanonicalTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function initialize(SEOData $SEOData = null): static
$collection = new static();

if ( config('seo.canonical_link') ) {
$collection->push(new LinkTag('canonical', $SEOData->url));
$collection->push(new LinkTag('canonical', $SEOData->canonical_url ?? $SEOData->url));
}

return $collection;
Expand Down
54 changes: 54 additions & 0 deletions tests/Feature/Tags/CanonicalTagTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use RalphJSmit\Laravel\SEO\Tests\Fixtures\Page;

use function Pest\Laravel\get;

Expand All @@ -18,3 +21,54 @@
->assertDontSee('rel="canonical"', false);
});

it('can display the model level canonical url if set in database', function () {
config()->set('seo.canonical_link', true);

$page = Page::create();

$page->seo->update([
'canonical_url' => 'https://example.com/canonical/url/test',
]);

$page->refresh();

get(route('seo.test-page', ['page' => $page]))
->assertSee('<link rel="canonical" href="https://example.com/canonical/url/test">', false);
});

it('can display the model level canonical url if set on override', function () {
config()->set('seo.canonical_link', true);

$page = Page::create();

$page::$overrides = [
'canonical_url' => 'https://example.com/canonical/url/test',
];

$page->refresh();

get(route('seo.test-page', ['page' => $page]))
->assertSee('<link rel="canonical" href="https://example.com/canonical/url/test">', false);
});

it('will not break if no canonical_url column exists in seo table', function () {
// New seo.canonical_url column was added in https://github.com/ralphjsmit/laravel-seo/pull/35.
config()->set('seo.canonical_link', true);

$page = Page::create();

expect(Schema::hasColumn('seo', 'canonical_url'))
->toBeTrue();

Schema::table('seo', function (Blueprint $table) {
$table->dropColumn('canonical_url');
});

expect(Schema::hasColumn('seo', 'canonical_url'))
->toBeFalse();

$page->refresh();

get(route('seo.test-page', ['page' => $page]))
->assertOk();
});

0 comments on commit 3fdd02b

Please sign in to comment.