3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
11 class SortTest extends TestCase
13 public function test_drafts_do_not_show_up()
16 $pageRepo = app(PageRepo::class);
17 $book = $this->entities->book();
18 $draft = $pageRepo->getNewDraftPage($book);
20 $resp = $this->get($book->getUrl());
21 $resp->assertSee($draft->name);
23 $resp = $this->get($book->getUrl() . '/sort');
24 $resp->assertDontSee($draft->name);
27 public function test_page_move_into_book()
29 $page = $this->entities->page();
30 $currentBook = $page->book;
31 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
33 $resp = $this->asEditor()->get($page->getUrl('/move'));
34 $resp->assertSee('Move Page');
36 $movePageResp = $this->put($page->getUrl('/move'), [
37 'entity_selection' => 'book:' . $newBook->id,
41 $movePageResp->assertRedirect($page->getUrl());
42 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
44 $newBookResp = $this->get($newBook->getUrl());
45 $newBookResp->assertSee('moved page');
46 $newBookResp->assertSee($page->name);
49 public function test_page_move_into_chapter()
51 $page = $this->entities->page();
52 $currentBook = $page->book;
53 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
54 $newChapter = $newBook->chapters()->first();
56 $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
57 'entity_selection' => 'chapter:' . $newChapter->id,
61 $movePageResp->assertRedirect($page->getUrl());
62 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
64 $newChapterResp = $this->get($newChapter->getUrl());
65 $newChapterResp->assertSee($page->name);
68 public function test_page_move_from_chapter_to_book()
70 $oldChapter = Chapter::query()->first();
71 $page = $oldChapter->pages()->first();
72 $newBook = Book::query()->where('id', '!=', $oldChapter->book_id)->first();
74 $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
75 'entity_selection' => 'book:' . $newBook->id,
79 $movePageResp->assertRedirect($page->getUrl());
80 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
81 $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
83 $newBookResp = $this->get($newBook->getUrl());
84 $newBookResp->assertSee($page->name);
87 public function test_page_move_requires_create_permissions_on_parent()
89 $page = $this->entities->page();
90 $currentBook = $page->book;
91 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
92 $editor = $this->getEditor();
94 $this->entities->setPermissions($newBook, ['view', 'update', 'delete'], $editor->roles->all());
96 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
97 'entity_selection' => 'book:' . $newBook->id,
99 $this->assertPermissionError($movePageResp);
101 $this->entities->setPermissions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles->all());
102 $movePageResp = $this->put($page->getUrl('/move'), [
103 'entity_selection' => 'book:' . $newBook->id,
107 $movePageResp->assertRedirect($page->getUrl());
109 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
112 public function test_page_move_requires_delete_permissions()
114 $page = $this->entities->page();
115 $currentBook = $page->book;
116 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
117 $editor = $this->getEditor();
119 $this->entities->setPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
120 $this->entities->setPermissions($page, ['view', 'update', 'create'], $editor->roles->all());
122 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
123 'entity_selection' => 'book:' . $newBook->id,
125 $this->assertPermissionError($movePageResp);
126 $pageView = $this->get($page->getUrl());
127 $pageView->assertDontSee($page->getUrl('/move'));
129 $this->entities->setPermissions($page, ['view', 'update', 'create', 'delete'], $editor->roles->all());
130 $movePageResp = $this->put($page->getUrl('/move'), [
131 'entity_selection' => 'book:' . $newBook->id,
135 $movePageResp->assertRedirect($page->getUrl());
136 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
139 public function test_chapter_move()
141 $chapter = $this->entities->chapter();
142 $currentBook = $chapter->book;
143 $pageToCheck = $chapter->pages->first();
144 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
146 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
147 $chapterMoveResp->assertSee('Move Chapter');
149 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
150 'entity_selection' => 'book:' . $newBook->id,
153 $chapter = Chapter::query()->find($chapter->id);
154 $moveChapterResp->assertRedirect($chapter->getUrl());
155 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
157 $newBookResp = $this->get($newBook->getUrl());
158 $newBookResp->assertSee('moved chapter');
159 $newBookResp->assertSee($chapter->name);
161 $pageToCheck = Page::query()->find($pageToCheck->id);
162 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
163 $pageCheckResp = $this->get($pageToCheck->getUrl());
164 $pageCheckResp->assertSee($newBook->name);
167 public function test_chapter_move_requires_delete_permissions()
169 $chapter = $this->entities->chapter();
170 $currentBook = $chapter->book;
171 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
172 $editor = $this->getEditor();
174 $this->entities->setPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
175 $this->entities->setPermissions($chapter, ['view', 'update', 'create'], $editor->roles->all());
177 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
178 'entity_selection' => 'book:' . $newBook->id,
180 $this->assertPermissionError($moveChapterResp);
181 $pageView = $this->get($chapter->getUrl());
182 $pageView->assertDontSee($chapter->getUrl('/move'));
184 $this->entities->setPermissions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles->all());
185 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
186 'entity_selection' => 'book:' . $newBook->id,
189 $chapter = Chapter::query()->find($chapter->id);
190 $moveChapterResp->assertRedirect($chapter->getUrl());
191 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
194 public function test_chapter_move_requires_create_permissions_in_new_book()
196 $chapter = $this->entities->chapter();
197 $currentBook = $chapter->book;
198 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
199 $editor = $this->getEditor();
201 $this->entities->setPermissions($newBook, ['view', 'update', 'delete'], [$editor->roles->first()]);
202 $this->entities->setPermissions($chapter, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
204 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
205 'entity_selection' => 'book:' . $newBook->id,
207 $this->assertPermissionError($moveChapterResp);
209 $this->entities->setPermissions($newBook, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
210 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
211 'entity_selection' => 'book:' . $newBook->id,
214 $chapter = Chapter::query()->find($chapter->id);
215 $moveChapterResp->assertRedirect($chapter->getUrl());
216 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
219 public function test_chapter_move_changes_book_for_deleted_pages_within()
221 /** @var Chapter $chapter */
222 $chapter = Chapter::query()->whereHas('pages')->first();
223 $currentBook = $chapter->book;
224 $pageToCheck = $chapter->pages->first();
225 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
227 $pageToCheck->delete();
229 $this->asEditor()->put($chapter->getUrl('/move'), [
230 'entity_selection' => 'book:' . $newBook->id,
233 $pageToCheck->refresh();
234 $this->assertEquals($newBook->id, $pageToCheck->book_id);
237 public function test_book_sort_page_shows()
239 $bookToSort = $this->entities->book();
241 $resp = $this->asAdmin()->get($bookToSort->getUrl());
242 $this->withHtml($resp)->assertElementExists('a[href="' . $bookToSort->getUrl('/sort') . '"]');
244 $resp = $this->get($bookToSort->getUrl('/sort'));
245 $resp->assertStatus(200);
246 $resp->assertSee($bookToSort->name);
249 public function test_book_sort()
251 $oldBook = $this->entities->book();
252 $chapterToMove = $this->entities->newChapter(['name' => 'chapter to move'], $oldBook);
253 $newBook = $this->entities->newBook(['name' => 'New sort book']);
254 $pagesToMove = Page::query()->take(5)->get();
256 // Create request data
259 'id' => $chapterToMove->id,
261 'parentChapter' => false,
263 'book' => $newBook->id,
266 foreach ($pagesToMove as $index => $page) {
270 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
272 'book' => $newBook->id,
276 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
277 $sortResp->assertRedirect($newBook->getUrl());
278 $sortResp->assertStatus(302);
279 $this->assertDatabaseHas('chapters', [
280 'id' => $chapterToMove->id,
281 'book_id' => $newBook->id,
284 $this->assertTrue($newBook->chapters()->count() === 1);
285 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
287 $checkPage = $pagesToMove[1];
288 $checkResp = $this->get($checkPage->refresh()->getUrl());
289 $checkResp->assertSee($newBook->name);
292 public function test_book_sort_makes_no_changes_if_new_chapter_does_not_align_with_new_book()
294 $page = $this->entities->pageWithinChapter();
295 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
300 'parentChapter' => $otherChapter->id,
302 'book' => $page->book_id,
304 $this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
306 $this->assertDatabaseHas('pages', [
307 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
311 public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_chapter()
313 $page = $this->entities->pageWithinChapter();
314 /** @var Chapter $otherChapter */
315 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
316 $this->entities->setPermissions($otherChapter);
321 'parentChapter' => $otherChapter->id,
323 'book' => $otherChapter->book_id,
325 $this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
327 $this->assertDatabaseHas('pages', [
328 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
332 public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_book()
334 $page = $this->entities->pageWithinChapter();
335 /** @var Chapter $otherChapter */
336 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
337 $editor = $this->getEditor();
338 $this->entities->setPermissions($otherChapter->book, ['update', 'delete'], [$editor->roles()->first()]);
343 'parentChapter' => $otherChapter->id,
345 'book' => $otherChapter->book_id,
347 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
349 $this->assertDatabaseHas('pages', [
350 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
354 public function test_book_sort_makes_no_changes_if_no_update_or_create_permissions_on_new_chapter()
356 $page = $this->entities->pageWithinChapter();
357 /** @var Chapter $otherChapter */
358 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
359 $editor = $this->getEditor();
360 $this->entities->setPermissions($otherChapter, ['view', 'delete'], [$editor->roles()->first()]);
365 'parentChapter' => $otherChapter->id,
367 'book' => $otherChapter->book_id,
369 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
371 $this->assertDatabaseHas('pages', [
372 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
376 public function test_book_sort_makes_no_changes_if_no_update_permissions_on_moved_item()
378 $page = $this->entities->pageWithinChapter();
379 /** @var Chapter $otherChapter */
380 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
381 $editor = $this->getEditor();
382 $this->entities->setPermissions($page, ['view', 'delete'], [$editor->roles()->first()]);
387 'parentChapter' => $otherChapter->id,
389 'book' => $otherChapter->book_id,
391 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
393 $this->assertDatabaseHas('pages', [
394 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
398 public function test_book_sort_makes_no_changes_if_no_delete_permissions_on_moved_item()
400 $page = $this->entities->pageWithinChapter();
401 /** @var Chapter $otherChapter */
402 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
403 $editor = $this->getEditor();
404 $this->entities->setPermissions($page, ['view', 'update'], [$editor->roles()->first()]);
409 'parentChapter' => $otherChapter->id,
411 'book' => $otherChapter->book_id,
413 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
415 $this->assertDatabaseHas('pages', [
416 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
420 public function test_book_sort_item_returns_book_content()
422 $bookToSort = $this->entities->book();
423 $firstPage = $bookToSort->pages[0];
424 $firstChapter = $bookToSort->chapters[0];
426 $resp = $this->asAdmin()->get($bookToSort->getUrl() . '/sort-item');
428 // Ensure book details are returned
429 $resp->assertSee($bookToSort->name);
430 $resp->assertSee($firstPage->name);
431 $resp->assertSee($firstChapter->name);
434 public function test_pages_in_book_show_sorted_by_priority()
436 $book = $this->entities->bookHasChaptersAndPages();
437 $book->chapters()->forceDelete();
438 /** @var Page[] $pages */
439 $pages = $book->pages()->where('chapter_id', '=', 0)->take(2)->get();
440 $book->pages()->whereNotIn('id', $pages->pluck('id'))->delete();
442 $resp = $this->asEditor()->get($book->getUrl());
443 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[0]->name);
444 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[1]->name);
446 $pages[0]->forceFill(['priority' => 10])->save();
447 $pages[1]->forceFill(['priority' => 5])->save();
449 $resp = $this->asEditor()->get($book->getUrl());
450 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[1]->name);
451 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[0]->name);