]> BookStack Code Mirror - bookstack/commitdiff
Default chapter templates: Added tests, extracted repo logic
authorDan Brown <redacted>
Thu, 1 Feb 2024 12:51:47 +0000 (12:51 +0000)
committerDan Brown <redacted>
Thu, 1 Feb 2024 12:51:47 +0000 (12:51 +0000)
- Updated existing book tests to be generic to all default templates,
  and updated with chapter testing.
- Extracted repeated logic in the Book/Chapter repos to be shared in the
  BaseRepo.

Review of #4750

app/Entities/Repos/BaseRepo.php
app/Entities/Repos/BookRepo.php
app/Entities/Repos/ChapterRepo.php
tests/Entity/BookDefaultTemplateTest.php [deleted file]
tests/Entity/DefaultTemplateTest.php [new file with mode: 0644]

index 27bf00161867b1c7d530005a49a6b3d5281045b5..17208ae032a196b6780da187b675c7e24967c854 100644 (file)
@@ -3,9 +3,12 @@
 namespace BookStack\Entities\Repos;
 
 use BookStack\Activity\TagRepo;
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Models\Entity;
 use BookStack\Entities\Models\HasCoverImage;
 use BookStack\Entities\Models\HasHtmlDescription;
+use BookStack\Entities\Models\Page;
 use BookStack\Exceptions\ImageUploadException;
 use BookStack\References\ReferenceStore;
 use BookStack\References\ReferenceUpdater;
@@ -104,6 +107,33 @@ class BaseRepo
         }
     }
 
+    /**
+     * Update the default page template used for this item.
+     * Checks that, if changing, the provided value is a valid template and the user
+     * has visibility of the provided page template id.
+     */
+    public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
+    {
+        $changing = $templateId !== intval($entity->default_template_id);
+        if (!$changing) {
+            return;
+        }
+
+        if ($templateId === 0) {
+            $entity->default_template_id = null;
+            $entity->save();
+            return;
+        }
+
+        $templateExists = Page::query()->visible()
+            ->where('template', '=', true)
+            ->where('id', '=', $templateId)
+            ->exists();
+
+        $entity->default_template_id = $templateExists ? $templateId : null;
+        $entity->save();
+    }
+
     protected function updateDescription(Entity $entity, array $input): void
     {
         if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
index 03e1118b12280c9c67ef93de3a96d8d74fa98632..bf765b22d158f43524c4b51f077d5f5a8f109430 100644 (file)
@@ -86,7 +86,7 @@ class BookRepo
         $book = new Book();
         $this->baseRepo->create($book, $input);
         $this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
-        $this->updateBookDefaultTemplate($book, intval($input['default_template_id'] ?? null));
+        $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
         Activity::add(ActivityType::BOOK_CREATE, $book);
 
         return $book;
@@ -100,7 +100,7 @@ class BookRepo
         $this->baseRepo->update($book, $input);
 
         if (array_key_exists('default_template_id', $input)) {
-            $this->updateBookDefaultTemplate($book, intval($input['default_template_id']));
+            $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
         }
 
         if (array_key_exists('image', $input)) {
@@ -112,33 +112,6 @@ class BookRepo
         return $book;
     }
 
-    /**
-     * Update the default page template used for this book.
-     * Checks that, if changing, the provided value is a valid template and the user
-     * has visibility of the provided page template id.
-     */
-    protected function updateBookDefaultTemplate(Book $book, int $templateId): void
-    {
-        $changing = $templateId !== intval($book->default_template_id);
-        if (!$changing) {
-            return;
-        }
-
-        if ($templateId === 0) {
-            $book->default_template_id = null;
-            $book->save();
-            return;
-        }
-
-        $templateExists = Page::query()->visible()
-            ->where('template', '=', true)
-            ->where('id', '=', $templateId)
-            ->exists();
-
-        $book->default_template_id = $templateExists ? $templateId : null;
-        $book->save();
-    }
-
     /**
      * Update the given book's cover image, or clear it.
      *
index 9534a406082346307c7658db71c6c65ad73ab65e..50b554d68dc585b4d1f3b42b16c8021936721c8e 100644 (file)
@@ -6,7 +6,6 @@ use BookStack\Activity\ActivityType;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Models\Chapter;
-use BookStack\Entities\Models\Entity;
 use BookStack\Entities\Tools\BookContents;
 use BookStack\Entities\Tools\TrashCan;
 use BookStack\Exceptions\MoveOperationException;
@@ -47,7 +46,7 @@ class ChapterRepo
         $chapter->book_id = $parentBook->id;
         $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
         $this->baseRepo->create($chapter, $input);
-        $this->updateChapterDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
+        $this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
         Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
 
         return $chapter;
@@ -61,7 +60,7 @@ class ChapterRepo
         $this->baseRepo->update($chapter, $input);
 
         if (array_key_exists('default_template_id', $input)) {
-            $this->updateChapterDefaultTemplate($chapter, intval($input['default_template_id']));
+            $this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id']));
         }
 
         Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
@@ -108,33 +107,6 @@ class ChapterRepo
         return $parent;
     }
 
-    /**
-     * Update the default page template used for this chapter.
-     * Checks that, if changing, the provided value is a valid template and the user
-     * has visibility of the provided page template id.
-     */
-    protected function updateChapterDefaultTemplate(Chapter $chapter, int $templateId): void
-    {
-        $changing = $templateId !== intval($chapter->default_template_id);
-        if (!$changing) {
-            return;
-        }
-
-        if ($templateId === 0) {
-            $chapter->default_template_id = null;
-            $chapter->save();
-            return;
-        }
-
-        $templateExists = Page::query()->visible()
-            ->where('template', '=', true)
-            ->where('id', '=', $templateId)
-            ->exists();
-
-        $chapter->default_template_id = $templateExists ? $templateId : null;
-        $chapter->save();
-    }
-
     /**
      * Find a page parent entity via an identifier string in the format:
      * {type}:{id}
diff --git a/tests/Entity/BookDefaultTemplateTest.php b/tests/Entity/BookDefaultTemplateTest.php
deleted file mode 100644 (file)
index d4cd5b2..0000000
+++ /dev/null
@@ -1,185 +0,0 @@
-<?php
-
-namespace Tests\Entity;
-
-use BookStack\Entities\Models\Book;
-use BookStack\Entities\Models\Page;
-use Tests\TestCase;
-
-class BookDefaultTemplateTest extends TestCase
-{
-    public function test_creating_book_with_default_template()
-    {
-        $templatePage = $this->entities->templatePage();
-        $details = [
-            'name' => 'My book with default template',
-            'default_template_id' => $templatePage->id,
-        ];
-
-        $this->asEditor()->post('/books', $details);
-        $this->assertDatabaseHas('books', $details);
-    }
-
-    public function test_updating_book_with_default_template()
-    {
-        $book = $this->entities->book();
-        $templatePage = $this->entities->templatePage();
-
-        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => strval($templatePage->id)]);
-        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => $templatePage->id]);
-
-        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => '']);
-        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => null]);
-    }
-
-    public function test_default_template_cannot_be_set_if_not_a_template()
-    {
-        $book = $this->entities->book();
-        $page = $this->entities->page();
-        $this->assertFalse($page->template);
-
-        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => $page->id]);
-        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => null]);
-    }
-
-    public function test_default_template_cannot_be_set_if_not_have_access()
-    {
-        $book = $this->entities->book();
-        $templatePage = $this->entities->templatePage();
-        $this->permissions->disableEntityInheritedPermissions($templatePage);
-
-        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => $templatePage->id]);
-        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => null]);
-    }
-
-    public function test_inaccessible_default_template_can_be_set_if_unchanged()
-    {
-        $templatePage = $this->entities->templatePage();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-        $this->permissions->disableEntityInheritedPermissions($templatePage);
-
-        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => $templatePage->id]);
-        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => $templatePage->id]);
-    }
-
-    public function test_default_page_template_option_shows_on_book_form()
-    {
-        $templatePage = $this->entities->templatePage();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-
-        $resp = $this->asEditor()->get($book->getUrl('/edit'));
-        $this->withHtml($resp)->assertElementExists('input[name="default_template_id"][value="' . $templatePage->id . '"]');
-    }
-
-    public function test_default_page_template_option_only_shows_template_name_if_visible()
-    {
-        $templatePage = $this->entities->templatePage();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-
-        $resp = $this->asEditor()->get($book->getUrl('/edit'));
-        $this->withHtml($resp)->assertElementContains('#template-control a.text-page', "#{$templatePage->id}, {$templatePage->name}");
-
-        $this->permissions->disableEntityInheritedPermissions($templatePage);
-
-        $resp = $this->asEditor()->get($book->getUrl('/edit'));
-        $this->withHtml($resp)->assertElementNotContains('#template-control a.text-page', "#{$templatePage->id}, {$templatePage->name}");
-        $this->withHtml($resp)->assertElementContains('#template-control a.text-page', "#{$templatePage->id}");
-    }
-
-    public function test_creating_book_page_uses_default_template()
-    {
-        $templatePage = $this->entities->templatePage();
-        $templatePage->forceFill(['html' => '<p>My template page</p>', 'markdown' => '# My template page'])->save();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-
-        $this->asEditor()->get($book->getUrl('/create-page'));
-        $latestPage = $book->pages()
-            ->where('draft', '=', true)
-            ->where('template', '=', false)
-            ->latest()->first();
-
-        $this->assertEquals('<p>My template page</p>', $latestPage->html);
-        $this->assertEquals('# My template page', $latestPage->markdown);
-    }
-
-    public function test_creating_chapter_page_uses_default_template()
-    {
-        $templatePage = $this->entities->templatePage();
-        $templatePage->forceFill(['html' => '<p>My template page in chapter</p>', 'markdown' => '# My template page in chapter'])->save();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-        $chapter = $book->chapters()->first();
-
-        $this->asEditor()->get($chapter->getUrl('/create-page'));
-        $latestPage = $chapter->pages()
-            ->where('draft', '=', true)
-            ->where('template', '=', false)
-            ->latest()->first();
-
-        $this->assertEquals('<p>My template page in chapter</p>', $latestPage->html);
-        $this->assertEquals('# My template page in chapter', $latestPage->markdown);
-    }
-
-    public function test_creating_book_page_as_guest_uses_default_template()
-    {
-        $templatePage = $this->entities->templatePage();
-        $templatePage->forceFill(['html' => '<p>My template page</p>', 'markdown' => '# My template page'])->save();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-        $guest = $this->users->guest();
-
-        $this->permissions->makeAppPublic();
-        $this->permissions->grantUserRolePermissions($guest, ['page-create-all', 'page-update-all']);
-
-        $resp = $this->post($book->getUrl('/create-guest-page'), [
-            'name' => 'My guest page with template'
-        ]);
-        $latestPage = $book->pages()
-            ->where('draft', '=', false)
-            ->where('template', '=', false)
-            ->where('created_by', '=', $guest->id)
-            ->latest()->first();
-
-        $this->assertEquals('<p>My template page</p>', $latestPage->html);
-        $this->assertEquals('# My template page', $latestPage->markdown);
-    }
-
-    public function test_creating_book_page_does_not_use_template_if_not_visible()
-    {
-        $templatePage = $this->entities->templatePage();
-        $templatePage->forceFill(['html' => '<p>My template page</p>', 'markdown' => '# My template page'])->save();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-        $this->permissions->disableEntityInheritedPermissions($templatePage);
-
-        $this->asEditor()->get($book->getUrl('/create-page'));
-        $latestPage = $book->pages()
-            ->where('draft', '=', true)
-            ->where('template', '=', false)
-            ->latest()->first();
-
-        $this->assertEquals('', $latestPage->html);
-        $this->assertEquals('', $latestPage->markdown);
-    }
-
-    public function test_template_page_delete_removes_book_template_usage()
-    {
-        $templatePage = $this->entities->templatePage();
-        $book = $this->bookUsingDefaultTemplate($templatePage);
-
-        $book->refresh();
-        $this->assertEquals($templatePage->id, $book->default_template_id);
-
-        $this->asEditor()->delete($templatePage->getUrl());
-        $this->asAdmin()->post('/settings/recycle-bin/empty');
-
-        $book->refresh();
-        $this->assertEquals(null, $book->default_template_id);
-    }
-
-    protected function bookUsingDefaultTemplate(Page $page): Book
-    {
-        $book = $this->entities->book();
-        $book->default_template_id = $page->id;
-        $book->save();
-
-        return $book;
-    }
-}
diff --git a/tests/Entity/DefaultTemplateTest.php b/tests/Entity/DefaultTemplateTest.php
new file mode 100644 (file)
index 0000000..5369a54
--- /dev/null
@@ -0,0 +1,341 @@
+<?php
+
+namespace Tests\Entity;
+
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Models\Page;
+use Tests\TestCase;
+
+class DefaultTemplateTest extends TestCase
+{
+    public function test_creating_book_with_default_template()
+    {
+        $templatePage = $this->entities->templatePage();
+        $details = [
+            'name' => 'My book with default template',
+            'default_template_id' => $templatePage->id,
+        ];
+
+        $this->asEditor()->post('/books', $details);
+        $this->assertDatabaseHas('books', $details);
+    }
+
+    public function test_creating_chapter_with_default_template()
+    {
+        $templatePage = $this->entities->templatePage();
+        $book = $this->entities->book();
+        $details = [
+            'name' => 'My chapter with default template',
+            'default_template_id' => $templatePage->id,
+        ];
+
+        $this->asEditor()->post($book->getUrl('/create-chapter'), $details);
+        $this->assertDatabaseHas('chapters', $details);
+    }
+
+    public function test_updating_book_with_default_template()
+    {
+        $book = $this->entities->book();
+        $templatePage = $this->entities->templatePage();
+
+        $this->asEditor()->put($book->getUrl(), ['name' => $book->name, 'default_template_id' => strval($templatePage->id)]);
+        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => $templatePage->id]);
+
+        $this->asEditor()->put($book->getUrl(), ['name' => $book->name, 'default_template_id' => '']);
+        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => null]);
+    }
+
+    public function test_updating_chapter_with_default_template()
+    {
+        $chapter = $this->entities->chapter();
+        $templatePage = $this->entities->templatePage();
+
+        $this->asEditor()->put($chapter->getUrl(), ['name' => $chapter->name, 'default_template_id' => strval($templatePage->id)]);
+        $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'default_template_id' => $templatePage->id]);
+
+        $this->asEditor()->put($chapter->getUrl(), ['name' => $chapter->name, 'default_template_id' => '']);
+        $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'default_template_id' => null]);
+    }
+
+    public function test_default_book_template_cannot_be_set_if_not_a_template()
+    {
+        $book = $this->entities->book();
+        $page = $this->entities->page();
+        $this->assertFalse($page->template);
+
+        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => $page->id]);
+        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => null]);
+    }
+
+    public function test_default_chapter_template_cannot_be_set_if_not_a_template()
+    {
+        $chapter = $this->entities->chapter();
+        $page = $this->entities->page();
+        $this->assertFalse($page->template);
+
+        $this->asEditor()->put("/chapters/{$chapter->slug}", ['name' => $chapter->name, 'default_template_id' => $page->id]);
+        $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'default_template_id' => null]);
+    }
+
+
+    public function test_default_book_template_cannot_be_set_if_not_have_access()
+    {
+        $book = $this->entities->book();
+        $templatePage = $this->entities->templatePage();
+        $this->permissions->disableEntityInheritedPermissions($templatePage);
+
+        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => $templatePage->id]);
+        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => null]);
+    }
+
+    public function test_default_chapter_template_cannot_be_set_if_not_have_access()
+    {
+        $chapter = $this->entities->chapter();
+        $templatePage = $this->entities->templatePage();
+        $this->permissions->disableEntityInheritedPermissions($templatePage);
+
+        $this->asEditor()->put("/chapters/{$chapter->slug}", ['name' => $chapter->name, 'default_template_id' => $templatePage->id]);
+        $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'default_template_id' => null]);
+    }
+
+    public function test_inaccessible_book_default_template_can_be_set_if_unchanged()
+    {
+        $templatePage = $this->entities->templatePage();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+        $this->permissions->disableEntityInheritedPermissions($templatePage);
+
+        $this->asEditor()->put("/books/{$book->slug}", ['name' => $book->name, 'default_template_id' => $templatePage->id]);
+        $this->assertDatabaseHas('books', ['id' => $book->id, 'default_template_id' => $templatePage->id]);
+    }
+
+    public function test_inaccessible_chapter_default_template_can_be_set_if_unchanged()
+    {
+        $templatePage = $this->entities->templatePage();
+        $chapter = $this->chapterUsingDefaultTemplate($templatePage);
+        $this->permissions->disableEntityInheritedPermissions($templatePage);
+
+        $this->asEditor()->put("/chapters/{$chapter->slug}", ['name' => $chapter->name, 'default_template_id' => $templatePage->id]);
+        $this->assertDatabaseHas('chapters', ['id' => $chapter->id, 'default_template_id' => $templatePage->id]);
+    }
+
+    public function test_default_page_template_option_shows_on_book_form()
+    {
+        $templatePage = $this->entities->templatePage();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+
+        $resp = $this->asEditor()->get($book->getUrl('/edit'));
+        $this->withHtml($resp)->assertElementExists('input[name="default_template_id"][value="' . $templatePage->id . '"]');
+    }
+
+    public function test_default_page_template_option_shows_on_chapter_form()
+    {
+        $templatePage = $this->entities->templatePage();
+        $chapter = $this->chapterUsingDefaultTemplate($templatePage);
+
+        $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
+        $this->withHtml($resp)->assertElementExists('input[name="default_template_id"][value="' . $templatePage->id . '"]');
+    }
+
+    public function test_book_default_page_template_option_only_shows_template_name_if_visible()
+    {
+        $templatePage = $this->entities->templatePage();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+
+        $resp = $this->asEditor()->get($book->getUrl('/edit'));
+        $this->withHtml($resp)->assertElementContains('#template-control a.text-page', "#{$templatePage->id}, {$templatePage->name}");
+
+        $this->permissions->disableEntityInheritedPermissions($templatePage);
+
+        $resp = $this->asEditor()->get($book->getUrl('/edit'));
+        $this->withHtml($resp)->assertElementNotContains('#template-control a.text-page', "#{$templatePage->id}, {$templatePage->name}");
+        $this->withHtml($resp)->assertElementContains('#template-control a.text-page', "#{$templatePage->id}");
+    }
+
+    public function test_chapter_default_page_template_option_only_shows_template_name_if_visible()
+    {
+        $templatePage = $this->entities->templatePage();
+        $chapter = $this->chapterUsingDefaultTemplate($templatePage);
+
+        $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
+        $this->withHtml($resp)->assertElementContains('#template-control a.text-page', "#{$templatePage->id}, {$templatePage->name}");
+
+        $this->permissions->disableEntityInheritedPermissions($templatePage);
+
+        $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
+        $this->withHtml($resp)->assertElementNotContains('#template-control a.text-page', "#{$templatePage->id}, {$templatePage->name}");
+        $this->withHtml($resp)->assertElementContains('#template-control a.text-page', "#{$templatePage->id}");
+    }
+
+    public function test_creating_book_page_uses_book_default_template()
+    {
+        $templatePage = $this->entities->templatePage();
+        $templatePage->forceFill(['html' => '<p>My template page</p>', 'markdown' => '# My template page'])->save();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+
+        $this->asEditor()->get($book->getUrl('/create-page'));
+        $latestPage = $book->pages()
+            ->where('draft', '=', true)
+            ->where('template', '=', false)
+            ->latest()->first();
+
+        $this->assertEquals('<p>My template page</p>', $latestPage->html);
+        $this->assertEquals('# My template page', $latestPage->markdown);
+    }
+
+    public function test_creating_chapter_page_uses_chapter_default_template()
+    {
+        $templatePage = $this->entities->templatePage();
+        $templatePage->forceFill(['html' => '<p>My chapter template page</p>', 'markdown' => '# My chapter template page'])->save();
+        $chapter = $this->chapterUsingDefaultTemplate($templatePage);
+
+        $this->asEditor()->get($chapter->getUrl('/create-page'));
+        $latestPage = $chapter->pages()
+            ->where('draft', '=', true)
+            ->where('template', '=', false)
+            ->latest()->first();
+
+        $this->assertEquals('<p>My chapter template page</p>', $latestPage->html);
+        $this->assertEquals('# My chapter template page', $latestPage->markdown);
+    }
+
+    public function test_creating_chapter_page_uses_book_default_template_if_no_chapter_template_set()
+    {
+        $templatePage = $this->entities->templatePage();
+        $templatePage->forceFill(['html' => '<p>My template page in chapter</p>', 'markdown' => '# My template page in chapter'])->save();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+        $chapter = $book->chapters()->first();
+
+        $this->asEditor()->get($chapter->getUrl('/create-page'));
+        $latestPage = $chapter->pages()
+            ->where('draft', '=', true)
+            ->where('template', '=', false)
+            ->latest()->first();
+
+        $this->assertEquals('<p>My template page in chapter</p>', $latestPage->html);
+        $this->assertEquals('# My template page in chapter', $latestPage->markdown);
+    }
+
+    public function test_creating_chapter_page_uses_chapter_template_instead_of_book_template()
+    {
+        $bookTemplatePage = $this->entities->templatePage();
+        $bookTemplatePage->forceFill(['html' => '<p>My book template</p>', 'markdown' => '# My book template'])->save();
+        $book = $this->bookUsingDefaultTemplate($bookTemplatePage);
+
+        $chapterTemplatePage = $this->entities->templatePage();
+        $chapterTemplatePage->forceFill(['html' => '<p>My chapter template</p>', 'markdown' => '# My chapter template'])->save();
+        $chapter = $book->chapters()->first();
+        $chapter->default_template_id = $chapterTemplatePage->id;
+        $chapter->save();
+
+        $this->asEditor()->get($chapter->getUrl('/create-page'));
+        $latestPage = $chapter->pages()
+            ->where('draft', '=', true)
+            ->where('template', '=', false)
+            ->latest()->first();
+
+        $this->assertEquals('<p>My chapter template</p>', $latestPage->html);
+        $this->assertEquals('# My chapter template', $latestPage->markdown);
+    }
+
+    public function test_creating_page_as_guest_uses_default_template()
+    {
+        $templatePage = $this->entities->templatePage();
+        $templatePage->forceFill(['html' => '<p>My template page</p>', 'markdown' => '# My template page'])->save();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+        $chapter = $this->chapterUsingDefaultTemplate($templatePage);
+        $guest = $this->users->guest();
+
+        $this->permissions->makeAppPublic();
+        $this->permissions->grantUserRolePermissions($guest, ['page-create-all', 'page-update-all']);
+
+        $this->post($book->getUrl('/create-guest-page'), [
+            'name' => 'My guest page with template'
+        ]);
+        $latestBookPage = $book->pages()
+            ->where('draft', '=', false)
+            ->where('template', '=', false)
+            ->where('created_by', '=', $guest->id)
+            ->latest()->first();
+
+        $this->assertEquals('<p>My template page</p>', $latestBookPage->html);
+        $this->assertEquals('# My template page', $latestBookPage->markdown);
+
+        $this->post($chapter->getUrl('/create-guest-page'), [
+            'name' => 'My guest page with template'
+        ]);
+        $latestChapterPage = $chapter->pages()
+            ->where('draft', '=', false)
+            ->where('template', '=', false)
+            ->where('created_by', '=', $guest->id)
+            ->latest()->first();
+
+        $this->assertEquals('<p>My template page</p>', $latestChapterPage->html);
+        $this->assertEquals('# My template page', $latestChapterPage->markdown);
+    }
+
+    public function test_templates_not_used_if_not_visible()
+    {
+        $templatePage = $this->entities->templatePage();
+        $templatePage->forceFill(['html' => '<p>My template page</p>', 'markdown' => '# My template page'])->save();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+        $chapter = $this->chapterUsingDefaultTemplate($templatePage);
+
+        $this->permissions->disableEntityInheritedPermissions($templatePage);
+
+        $this->asEditor()->get($book->getUrl('/create-page'));
+        $latestBookPage = $book->pages()
+            ->where('draft', '=', true)
+            ->where('template', '=', false)
+            ->latest()->first();
+
+        $this->assertEquals('', $latestBookPage->html);
+        $this->assertEquals('', $latestBookPage->markdown);
+
+        $this->asEditor()->get($chapter->getUrl('/create-page'));
+        $latestChapterPage = $chapter->pages()
+            ->where('draft', '=', true)
+            ->where('template', '=', false)
+            ->latest()->first();
+
+        $this->assertEquals('', $latestChapterPage->html);
+        $this->assertEquals('', $latestChapterPage->markdown);
+    }
+
+    public function test_template_page_delete_removes_template_usage()
+    {
+        $templatePage = $this->entities->templatePage();
+        $book = $this->bookUsingDefaultTemplate($templatePage);
+        $chapter = $this->chapterUsingDefaultTemplate($templatePage);
+
+        $book->refresh();
+        $this->assertEquals($templatePage->id, $book->default_template_id);
+        $this->assertEquals($templatePage->id, $chapter->default_template_id);
+
+        $this->asEditor()->delete($templatePage->getUrl());
+        $this->asAdmin()->post('/settings/recycle-bin/empty');
+
+        $book->refresh();
+        $chapter->refresh();
+        $this->assertEquals(null, $book->default_template_id);
+        $this->assertEquals(null, $chapter->default_template_id);
+    }
+
+    protected function bookUsingDefaultTemplate(Page $page): Book
+    {
+        $book = $this->entities->book();
+        $book->default_template_id = $page->id;
+        $book->save();
+
+        return $book;
+    }
+
+    protected function chapterUsingDefaultTemplate(Page $page): Chapter
+    {
+        $chapter = $this->entities->chapter();
+        $chapter->default_template_id = $page->id;
+        $chapter->save();
+
+        return $chapter;
+    }
+}