Added test & update to prevent page creation w/ empty slug

Caused by changes to page repo in reference work,
This adds back in the slug generate although at a more central place.
Adds a test case to cover the problematic scenario.
This commit is contained in:
Dan Brown 2022-09-01 12:53:34 +01:00
parent f092c97748
commit 34c63e1c30
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 29 additions and 1 deletions

View File

@ -56,7 +56,7 @@ class BaseRepo
$entity->fill($input);
$entity->updated_by = user()->id;
if ($entity->isDirty('name')) {
if ($entity->isDirty('name') || empty($entity->slug)) {
$entity->refreshSlug();
}

View File

@ -204,4 +204,32 @@ class PageDraftTest extends TestCase
$draft->refresh();
$this->assertStringContainsString('href="https://example.com"', $draft->html);
}
public function test_slug_generated_on_draft_publish_to_page_when_no_name_change()
{
/** @var Book $book */
$book = Book::query()->first();
$this->asEditor()->get($book->getUrl('/create-page'));
/** @var Page $draft */
$draft = Page::query()->where('draft', '=', true)->where('book_id', '=', $book->id)->firstOrFail();
$this->put('/ajax/page/' . $draft->id . '/save-draft', [
'name' => 'My page',
'markdown' => "Update test",
])->assertOk();
$draft->refresh();
$this->assertEmpty($draft->slug);
$this->post($draft->getUrl(), [
'name' => 'My page',
'markdown' => "# My markdown page"
]);
$this->assertDatabaseHas('pages', [
'id' => $draft->id,
'draft' => false,
'slug' => 'my-page',
]);
}
}