Skip to content

Commit 43a72fb

Browse files
committed
Default chapter templates: Added tests, extracted repo logic
- 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
1 parent 4137cf9 commit 43a72fb

File tree

5 files changed

+375
-244
lines changed

5 files changed

+375
-244
lines changed

app/Entities/Repos/BaseRepo.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace BookStack\Entities\Repos;
44

55
use BookStack\Activity\TagRepo;
6+
use BookStack\Entities\Models\Book;
7+
use BookStack\Entities\Models\Chapter;
68
use BookStack\Entities\Models\Entity;
79
use BookStack\Entities\Models\HasCoverImage;
810
use BookStack\Entities\Models\HasHtmlDescription;
11+
use BookStack\Entities\Models\Page;
912
use BookStack\Exceptions\ImageUploadException;
1013
use BookStack\References\ReferenceStore;
1114
use BookStack\References\ReferenceUpdater;
@@ -104,6 +107,33 @@ public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $remov
104107
}
105108
}
106109

110+
/**
111+
* Update the default page template used for this item.
112+
* Checks that, if changing, the provided value is a valid template and the user
113+
* has visibility of the provided page template id.
114+
*/
115+
public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
116+
{
117+
$changing = $templateId !== intval($entity->default_template_id);
118+
if (!$changing) {
119+
return;
120+
}
121+
122+
if ($templateId === 0) {
123+
$entity->default_template_id = null;
124+
$entity->save();
125+
return;
126+
}
127+
128+
$templateExists = Page::query()->visible()
129+
->where('template', '=', true)
130+
->where('id', '=', $templateId)
131+
->exists();
132+
133+
$entity->default_template_id = $templateExists ? $templateId : null;
134+
$entity->save();
135+
}
136+
107137
protected function updateDescription(Entity $entity, array $input): void
108138
{
109139
if (!in_array(HasHtmlDescription::class, class_uses($entity))) {

app/Entities/Repos/BookRepo.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function create(array $input): Book
8686
$book = new Book();
8787
$this->baseRepo->create($book, $input);
8888
$this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
89-
$this->updateBookDefaultTemplate($book, intval($input['default_template_id'] ?? null));
89+
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
9090
Activity::add(ActivityType::BOOK_CREATE, $book);
9191

9292
return $book;
@@ -100,7 +100,7 @@ public function update(Book $book, array $input): Book
100100
$this->baseRepo->update($book, $input);
101101

102102
if (array_key_exists('default_template_id', $input)) {
103-
$this->updateBookDefaultTemplate($book, intval($input['default_template_id']));
103+
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
104104
}
105105

106106
if (array_key_exists('image', $input)) {
@@ -112,33 +112,6 @@ public function update(Book $book, array $input): Book
112112
return $book;
113113
}
114114

115-
/**
116-
* Update the default page template used for this book.
117-
* Checks that, if changing, the provided value is a valid template and the user
118-
* has visibility of the provided page template id.
119-
*/
120-
protected function updateBookDefaultTemplate(Book $book, int $templateId): void
121-
{
122-
$changing = $templateId !== intval($book->default_template_id);
123-
if (!$changing) {
124-
return;
125-
}
126-
127-
if ($templateId === 0) {
128-
$book->default_template_id = null;
129-
$book->save();
130-
return;
131-
}
132-
133-
$templateExists = Page::query()->visible()
134-
->where('template', '=', true)
135-
->where('id', '=', $templateId)
136-
->exists();
137-
138-
$book->default_template_id = $templateExists ? $templateId : null;
139-
$book->save();
140-
}
141-
142115
/**
143116
* Update the given book's cover image, or clear it.
144117
*

app/Entities/Repos/ChapterRepo.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use BookStack\Entities\Models\Book;
77
use BookStack\Entities\Models\Page;
88
use BookStack\Entities\Models\Chapter;
9-
use BookStack\Entities\Models\Entity;
109
use BookStack\Entities\Tools\BookContents;
1110
use BookStack\Entities\Tools\TrashCan;
1211
use BookStack\Exceptions\MoveOperationException;
@@ -47,7 +46,7 @@ public function create(array $input, Book $parentBook): Chapter
4746
$chapter->book_id = $parentBook->id;
4847
$chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
4948
$this->baseRepo->create($chapter, $input);
50-
$this->updateChapterDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
49+
$this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
5150
Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
5251

5352
return $chapter;
@@ -61,7 +60,7 @@ public function update(Chapter $chapter, array $input): Chapter
6160
$this->baseRepo->update($chapter, $input);
6261

6362
if (array_key_exists('default_template_id', $input)) {
64-
$this->updateChapterDefaultTemplate($chapter, intval($input['default_template_id']));
63+
$this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id']));
6564
}
6665

6766
Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
@@ -108,33 +107,6 @@ public function move(Chapter $chapter, string $parentIdentifier): Book
108107
return $parent;
109108
}
110109

111-
/**
112-
* Update the default page template used for this chapter.
113-
* Checks that, if changing, the provided value is a valid template and the user
114-
* has visibility of the provided page template id.
115-
*/
116-
protected function updateChapterDefaultTemplate(Chapter $chapter, int $templateId): void
117-
{
118-
$changing = $templateId !== intval($chapter->default_template_id);
119-
if (!$changing) {
120-
return;
121-
}
122-
123-
if ($templateId === 0) {
124-
$chapter->default_template_id = null;
125-
$chapter->save();
126-
return;
127-
}
128-
129-
$templateExists = Page::query()->visible()
130-
->where('template', '=', true)
131-
->where('id', '=', $templateId)
132-
->exists();
133-
134-
$chapter->default_template_id = $templateExists ? $templateId : null;
135-
$chapter->save();
136-
}
137-
138110
/**
139111
* Find a page parent entity via an identifier string in the format:
140112
* {type}:{id}

tests/Entity/BookDefaultTemplateTest.php

Lines changed: 0 additions & 185 deletions
This file was deleted.

0 commit comments

Comments
 (0)