From 69fe2c1481b07487b930d56304a65dadae7fe83c Mon Sep 17 00:00:00 2001 From: Steven Richardson Date: Thu, 13 Apr 2023 10:39:28 +0100 Subject: [PATCH 1/4] Add canonical_url to SEO Data object for override --- database/migrations/create_seo_table.php.stub | 1 + src/Models/SEO.php | 1 + src/Support/SEOData.php | 1 + src/Tags/CanonicalTag.php | 2 +- tests/Feature/Tags/CanonicalTagTest.php | 16 ++++++++++++++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/database/migrations/create_seo_table.php.stub b/database/migrations/create_seo_table.php.stub index 08d8c17..c0f8dd4 100644 --- a/database/migrations/create_seo_table.php.stub +++ b/database/migrations/create_seo_table.php.stub @@ -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(); }); diff --git a/src/Models/SEO.php b/src/Models/SEO.php index 75f526e..8dac5af 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -45,6 +45,7 @@ public function prepareForUsage(): SEOData type: $overrides->type ?? null, locale: $overrides->locale ?? null, robots: $overrides->robots ?? $this->robots, + canonical_url: $overrides->canonical_url ?? $this->url, ); } } \ No newline at end of file diff --git a/src/Support/SEOData.php b/src/Support/SEOData.php index c88dfc7..f0121bf 100644 --- a/src/Support/SEOData.php +++ b/src/Support/SEOData.php @@ -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(); diff --git a/src/Tags/CanonicalTag.php b/src/Tags/CanonicalTag.php index 10771d9..82c00bb 100644 --- a/src/Tags/CanonicalTag.php +++ b/src/Tags/CanonicalTag.php @@ -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; diff --git a/tests/Feature/Tags/CanonicalTagTest.php b/tests/Feature/Tags/CanonicalTagTest.php index 3f9a0ff..3e9f31f 100644 --- a/tests/Feature/Tags/CanonicalTagTest.php +++ b/tests/Feature/Tags/CanonicalTagTest.php @@ -2,6 +2,8 @@ use Illuminate\Support\Str; +use RalphJSmit\Laravel\SEO\Tests\Fixtures\Page; + use function Pest\Laravel\get; it('can display the canonical URL if allowed', function () { @@ -18,3 +20,17 @@ ->assertDontSee('rel="canonical"', false); }); +it('can display the model level canonical url if set', 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('', false); +}); \ No newline at end of file From 85c5b8ec9780c0a36df87332dee92627280c6eb4 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:03:51 +0200 Subject: [PATCH 2/4] Update src/Models/SEO.php --- src/Models/SEO.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/SEO.php b/src/Models/SEO.php index 8dac5af..f3837e7 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -45,7 +45,7 @@ public function prepareForUsage(): SEOData type: $overrides->type ?? null, locale: $overrides->locale ?? null, robots: $overrides->robots ?? $this->robots, - canonical_url: $overrides->canonical_url ?? $this->url, + canonical_url: $overrides->canonical_url ?? $this->canonical_url, ); } } \ No newline at end of file From 3161d329f3e4a05987b9e6f4621509a1cb1ee072 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:05:03 +0200 Subject: [PATCH 3/4] Add test for DB column --- src/Models/SEO.php | 1 + tests/Feature/Tags/CanonicalTagTest.php | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Models/SEO.php b/src/Models/SEO.php index f3837e7..23e26c2 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -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(); } diff --git a/tests/Feature/Tags/CanonicalTagTest.php b/tests/Feature/Tags/CanonicalTagTest.php index 3e9f31f..b974fcc 100644 --- a/tests/Feature/Tags/CanonicalTagTest.php +++ b/tests/Feature/Tags/CanonicalTagTest.php @@ -1,7 +1,6 @@ assertDontSee('rel="canonical"', false); }); -it('can display the model level canonical url if set', function () { +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('', false); +}); + +it('can display the model level canonical url if set on override', function () { config()->set('seo.canonical_link', true); $page = Page::create(); From f74483084dafc59f00d8e4e21c05df899b1b2982 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:06:56 +0200 Subject: [PATCH 4/4] Add test for BC --- tests/Feature/Tags/CanonicalTagTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Feature/Tags/CanonicalTagTest.php b/tests/Feature/Tags/CanonicalTagTest.php index b974fcc..3324e1f 100644 --- a/tests/Feature/Tags/CanonicalTagTest.php +++ b/tests/Feature/Tags/CanonicalTagTest.php @@ -1,5 +1,7 @@ $page])) ->assertSee('', 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(); }); \ No newline at end of file