3 namespace Tests\Helpers;
5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\Page;
11 use BookStack\Entities\Repos\BookRepo;
12 use BookStack\Entities\Repos\BookshelfRepo;
13 use BookStack\Entities\Repos\ChapterRepo;
14 use BookStack\Entities\Repos\PageRepo;
15 use Illuminate\Database\Eloquent\Builder;
18 * Class to provider and action entity models for common test case
19 * operations. Tracks handled models and only returns fresh models.
20 * Does not dedupe against nested/child/parent models.
25 * @var array<string, int[]>
27 protected array $fetchCache = [
35 * Get an un-fetched page from the system.
37 public function page(callable $queryFilter = null): Page
39 /** @var Page $page */
40 $page = Page::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['page'])->first();
41 $this->addToCache($page);
45 public function pageWithinChapter(): Page
47 return $this->page(fn(Builder $query) => $query->whereHas('chapter')->with('chapter'));
50 public function pageNotWithinChapter(): Page
52 return $this->page(fn(Builder $query) => $query->where('chapter_id', '=', 0));
56 * Get an un-fetched chapter from the system.
58 public function chapter(callable $queryFilter = null): Chapter
60 /** @var Chapter $chapter */
61 $chapter = Chapter::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['chapter'])->first();
62 $this->addToCache($chapter);
66 public function chapterHasPages(): Chapter
68 return $this->chapter(fn(Builder $query) => $query->whereHas('pages'));
72 * Get an un-fetched book from the system.
74 public function book(callable $queryFilter = null): Book
76 /** @var Book $book */
77 $book = Book::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['book'])->first();
78 $this->addToCache($book);
83 * Get a book that has chapters and pages assigned.
85 public function bookHasChaptersAndPages(): Book
87 return $this->book(function (Builder $query) {
88 $query->has('chapters')->has('pages')->with(['chapters', 'pages']);
93 * Get an un-fetched shelf from the system.
95 public function shelf(callable $queryFilter = null): Bookshelf
97 /** @var Bookshelf $shelf */
98 $shelf = Bookshelf::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['bookshelf'])->first();
99 $this->addToCache($shelf);
104 * Get all entity types from the system.
105 * @return array{page: Page, chapter: Chapter, book: Book, bookshelf: Bookshelf}
107 public function all(): array
110 'page' => $this->page(),
111 'chapter' => $this->chapter(),
112 'book' => $this->book(),
113 'bookshelf' => $this->shelf(),
117 public function updatePage(Page $page, array $data): Page
119 $this->addToCache($page);
120 return app()->make(PageRepo::class)->update($page, $data);
124 * Create a book to page chain of entities that belong to a specific user.
125 * @return array{book: Book, chapter: Chapter, page: Page}
127 public function createChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array
129 if (empty($updaterUser)) {
130 $updaterUser = $creatorUser;
133 $userAttrs = ['created_by' => $creatorUser->id, 'owned_by' => $creatorUser->id, 'updated_by' => $updaterUser->id];
134 /** @var Book $book */
135 $book = Book::factory()->create($userAttrs);
136 $chapter = Chapter::factory()->create(array_merge(['book_id' => $book->id], $userAttrs));
137 $page = Page::factory()->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
139 $book->rebuildPermissions();
140 $this->addToCache([$page, $chapter, $book]);
142 return compact('book', 'chapter', 'page');
146 * Create and return a new bookshelf.
148 public function newShelf(array $input = ['name' => 'test shelf', 'description' => 'My new test shelf']): Bookshelf
150 $shelf = app(BookshelfRepo::class)->create($input, []);
151 $this->addToCache($shelf);
156 * Create and return a new book.
158 public function newBook(array $input = ['name' => 'test book', 'description' => 'My new test book']): Book
160 $book = app(BookRepo::class)->create($input);
161 $this->addToCache($book);
166 * Create and return a new test chapter.
168 public function newChapter(array $input, Book $book): Chapter
170 $chapter = app(ChapterRepo::class)->create($input, $book);
171 $this->addToCache($chapter);
176 * Create and return a new test page.
178 public function newPage(array $input = ['name' => 'test page', 'html' => 'My new test page']): Page
180 $book = $this->book();
181 $pageRepo = app(PageRepo::class);
182 $draftPage = $pageRepo->getNewDraftPage($book);
183 $this->addToCache($draftPage);
184 return $pageRepo->publishDraft($draftPage, $input);
188 * @param Entity|Entity[] $entities
190 protected function addToCache($entities): void
192 if (!is_array($entities)) {
193 $entities = [$entities];
196 foreach ($entities as $entity) {
197 $this->fetchCache[$entity->getType()][] = $entity->id;