From 34c63e1c303c60f6ca9e149f61b5665d0108f248 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Thu, 1 Sep 2022 12:53:34 +0100 Subject: [PATCH] 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. --- app/Entities/Repos/BaseRepo.php | 2 +- tests/Entity/PageDraftTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/Entities/Repos/BaseRepo.php b/app/Entities/Repos/BaseRepo.php index cfde7fe1c..747d1b176 100644 --- a/app/Entities/Repos/BaseRepo.php +++ b/app/Entities/Repos/BaseRepo.php @@ -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(); } diff --git a/tests/Entity/PageDraftTest.php b/tests/Entity/PageDraftTest.php index 0b44e5f0b..6fd9a7f70 100644 --- a/tests/Entity/PageDraftTest.php +++ b/tests/Entity/PageDraftTest.php @@ -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', + ]); + } }