Skip to content

Commit

Permalink
Add default SEO Model so that ->prepareForUsage() will always work
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphjsmit committed Jun 1, 2022
1 parent b9f9425 commit e0dabf4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Support/HasSEO.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ protected static function bootHasSEO(): void

public function seo(): MorphOne
{
return $this->morphOne(config('seo.model'), 'model');
return $this->morphOne(config('seo.model'), 'model')->withDefault();
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Support/SEODataTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

use RalphJSmit\Laravel\SEO\Models\SEO;
use RalphJSmit\Laravel\SEO\Tests\Fixtures\Page;

use function Pest\Laravel\assertDatabaseCount;

beforeEach(function () {
copy(__DIR__ . '/../../Fixtures/images/test-image.jpg', public_path('test-image.jpg'));
});
Expand All @@ -17,4 +20,25 @@

$this->assertEquals(1451, $SEOData->imageMeta()->width);
$this->assertEquals(258, $SEOData->imageMeta()->height);
});

it('can allow to prepareForUsage without a model in the database', function () {
$page = Page::create();

$page->seo->delete();

$page->refresh();

$this->assertNull($page->seo->prepareForUsage()->title);

$page::$overrides = [
'title' => 'Test Title',
];

$this->assertSame('Test Title', $page->seo->prepareForUsage()->title);

// Touch the page so that it is saved again. The default SEO model shouldn't be saved, but discarded.
$page->touch();

assertDatabaseCount(SEO::class, 0);
});

0 comments on commit e0dabf4

Please sign in to comment.