3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\Repos\PageRepo;
8 class SortTest extends TestCase
12 public function setUp(): void
15 $this->book = Book::first();
18 public function test_drafts_do_not_show_up()
21 $pageRepo = app(PageRepo::class);
22 $draft = $pageRepo->getNewDraftPage($this->book);
24 $resp = $this->get($this->book->getUrl());
25 $resp->assertSee($draft->name);
27 $resp = $this->get($this->book->getUrl() . '/sort');
28 $resp->assertDontSee($draft->name);
31 public function test_page_move_into_book()
33 $page = Page::first();
34 $currentBook = $page->book;
35 $newBook = Book::where('id', '!=', $currentBook->id)->first();
37 $resp = $this->asEditor()->get($page->getUrl('/move'));
38 $resp->assertSee('Move Page');
40 $movePageResp = $this->put($page->getUrl('/move'), [
41 'entity_selection' => 'book:' . $newBook->id
43 $page = Page::find($page->id);
45 $movePageResp->assertRedirect($page->getUrl());
46 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
48 $newBookResp = $this->get($newBook->getUrl());
49 $newBookResp->assertSee('moved page');
50 $newBookResp->assertSee($page->name);
53 public function test_page_move_into_chapter()
55 $page = Page::first();
56 $currentBook = $page->book;
57 $newBook = Book::where('id', '!=', $currentBook->id)->first();
58 $newChapter = $newBook->chapters()->first();
60 $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
61 'entity_selection' => 'chapter:' . $newChapter->id
63 $page = Page::find($page->id);
65 $movePageResp->assertRedirect($page->getUrl());
66 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
68 $newChapterResp = $this->get($newChapter->getUrl());
69 $newChapterResp->assertSee($page->name);
72 public function test_page_move_from_chapter_to_book()
74 $oldChapter = Chapter::first();
75 $page = $oldChapter->pages()->first();
76 $newBook = Book::where('id', '!=', $oldChapter->book_id)->first();
78 $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
79 'entity_selection' => 'book:' . $newBook->id
81 $page = Page::find($page->id);
83 $movePageResp->assertRedirect($page->getUrl());
84 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
85 $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
87 $newBookResp = $this->get($newBook->getUrl());
88 $newBookResp->assertSee($page->name);
91 public function test_page_move_requires_create_permissions_on_parent()
93 $page = Page::first();
94 $currentBook = $page->book;
95 $newBook = Book::where('id', '!=', $currentBook->id)->first();
96 $editor = $this->getEditor();
98 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
100 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
101 'entity_selection' => 'book:' . $newBook->id
103 $this->assertPermissionError($movePageResp);
105 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles);
106 $movePageResp = $this->put($page->getUrl('/move'), [
107 'entity_selection' => 'book:' . $newBook->id
110 $page = Page::find($page->id);
111 $movePageResp->assertRedirect($page->getUrl());
113 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
116 public function test_page_move_requires_delete_permissions()
118 $page = Page::first();
119 $currentBook = $page->book;
120 $newBook = Book::where('id', '!=', $currentBook->id)->first();
121 $editor = $this->getEditor();
123 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
124 $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
126 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
127 'entity_selection' => 'book:' . $newBook->id
129 $this->assertPermissionError($movePageResp);
130 $pageView = $this->get($page->getUrl());
131 $pageView->assertDontSee($page->getUrl('/move'));
133 $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles);
134 $movePageResp = $this->put($page->getUrl('/move'), [
135 'entity_selection' => 'book:' . $newBook->id
138 $page = Page::find($page->id);
139 $movePageResp->assertRedirect($page->getUrl());
140 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
143 public function test_chapter_move()
145 $chapter = Chapter::first();
146 $currentBook = $chapter->book;
147 $pageToCheck = $chapter->pages->first();
148 $newBook = Book::where('id', '!=', $currentBook->id)->first();
150 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
151 $chapterMoveResp->assertSee('Move Chapter');
153 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
154 'entity_selection' => 'book:' . $newBook->id
157 $chapter = Chapter::find($chapter->id);
158 $moveChapterResp->assertRedirect($chapter->getUrl());
159 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
161 $newBookResp = $this->get($newBook->getUrl());
162 $newBookResp->assertSee('moved chapter');
163 $newBookResp->assertSee($chapter->name);
165 $pageToCheck = Page::find($pageToCheck->id);
166 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
167 $pageCheckResp = $this->get($pageToCheck->getUrl());
168 $pageCheckResp->assertSee($newBook->name);
171 public function test_chapter_move_requires_delete_permissions()
173 $chapter = Chapter::first();
174 $currentBook = $chapter->book;
175 $newBook = Book::where('id', '!=', $currentBook->id)->first();
176 $editor = $this->getEditor();
178 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
179 $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
181 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
182 'entity_selection' => 'book:' . $newBook->id
184 $this->assertPermissionError($moveChapterResp);
185 $pageView = $this->get($chapter->getUrl());
186 $pageView->assertDontSee($chapter->getUrl('/move'));
188 $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles);
189 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
190 'entity_selection' => 'book:' . $newBook->id
193 $chapter = Chapter::find($chapter->id);
194 $moveChapterResp->assertRedirect($chapter->getUrl());
195 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
198 public function test_book_sort()
200 $oldBook = Book::query()->first();
201 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
202 $newBook = $this->newBook(['name' => 'New sort book']);
203 $pagesToMove = Page::query()->take(5)->get();
205 // Create request data
208 'id' => $chapterToMove->id,
210 'parentChapter' => false,
212 'book' => $newBook->id
215 foreach ($pagesToMove as $index => $page) {
219 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
221 'book' => $newBook->id
225 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
226 $sortResp->assertRedirect($newBook->getUrl());
227 $sortResp->assertStatus(302);
228 $this->assertDatabaseHas('chapters', [
229 'id' => $chapterToMove->id,
230 'book_id' => $newBook->id,
233 $this->assertTrue($newBook->chapters()->count() === 1);
234 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
236 $checkPage = $pagesToMove[1];
237 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
238 $checkResp->assertSee($newBook->name);
241 public function test_page_copy()
243 $page = Page::first();
244 $currentBook = $page->book;
245 $newBook = Book::where('id', '!=', $currentBook->id)->first();
247 $resp = $this->asEditor()->get($page->getUrl('/copy'));
248 $resp->assertSee('Copy Page');
250 $movePageResp = $this->post($page->getUrl('/copy'), [
251 'entity_selection' => 'book:' . $newBook->id,
252 'name' => 'My copied test page'
254 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
256 $movePageResp->assertRedirect($pageCopy->getUrl());
257 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
260 public function test_page_copy_with_no_destination()
262 $page = Page::first();
263 $currentBook = $page->book;
265 $resp = $this->asEditor()->get($page->getUrl('/copy'));
266 $resp->assertSee('Copy Page');
268 $movePageResp = $this->post($page->getUrl('/copy'), [
269 'name' => 'My copied test page'
272 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
274 $movePageResp->assertRedirect($pageCopy->getUrl());
275 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
276 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
279 public function test_page_can_be_copied_without_edit_permission()
281 $page = Page::first();
282 $currentBook = $page->book;
283 $newBook = Book::where('id', '!=', $currentBook->id)->first();
284 $viewer = $this->getViewer();
286 $resp = $this->actingAs($viewer)->get($page->getUrl());
287 $resp->assertDontSee($page->getUrl('/copy'));
289 $newBook->created_by = $viewer->id;
291 $this->giveUserPermissions($viewer, ['page-create-own']);
292 $this->regenEntityPermissions($newBook);
294 $resp = $this->actingAs($viewer)->get($page->getUrl());
295 $resp->assertSee($page->getUrl('/copy'));
297 $movePageResp = $this->post($page->getUrl('/copy'), [
298 'entity_selection' => 'book:' . $newBook->id,
299 'name' => 'My copied test page'
301 $movePageResp->assertRedirect();
303 $this->assertDatabaseHas('pages', [
304 'name' => 'My copied test page',
305 'created_by' => $viewer->id,
306 'book_id' => $newBook->id,