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('moved page');
70 $newChapterResp->assertSee($page->name);
73 public function test_page_move_requires_create_permissions_on_parent()
75 $page = Page::first();
76 $currentBook = $page->book;
77 $newBook = Book::where('id', '!=', $currentBook->id)->first();
78 $editor = $this->getEditor();
80 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
82 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
83 'entity_selection' => 'book:' . $newBook->id
85 $this->assertPermissionError($movePageResp);
87 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles);
88 $movePageResp = $this->put($page->getUrl('/move'), [
89 'entity_selection' => 'book:' . $newBook->id
92 $page = Page::find($page->id);
93 $movePageResp->assertRedirect($page->getUrl());
95 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
98 public function test_page_move_requires_delete_permissions()
100 $page = Page::first();
101 $currentBook = $page->book;
102 $newBook = Book::where('id', '!=', $currentBook->id)->first();
103 $editor = $this->getEditor();
105 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
106 $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
108 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
109 'entity_selection' => 'book:' . $newBook->id
111 $this->assertPermissionError($movePageResp);
112 $pageView = $this->get($page->getUrl());
113 $pageView->assertDontSee($page->getUrl('/move'));
115 $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles);
116 $movePageResp = $this->put($page->getUrl('/move'), [
117 'entity_selection' => 'book:' . $newBook->id
120 $page = Page::find($page->id);
121 $movePageResp->assertRedirect($page->getUrl());
122 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
125 public function test_chapter_move()
127 $chapter = Chapter::first();
128 $currentBook = $chapter->book;
129 $pageToCheck = $chapter->pages->first();
130 $newBook = Book::where('id', '!=', $currentBook->id)->first();
132 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
133 $chapterMoveResp->assertSee('Move Chapter');
135 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
136 'entity_selection' => 'book:' . $newBook->id
139 $chapter = Chapter::find($chapter->id);
140 $moveChapterResp->assertRedirect($chapter->getUrl());
141 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
143 $newBookResp = $this->get($newBook->getUrl());
144 $newBookResp->assertSee('moved chapter');
145 $newBookResp->assertSee($chapter->name);
147 $pageToCheck = Page::find($pageToCheck->id);
148 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
149 $pageCheckResp = $this->get($pageToCheck->getUrl());
150 $pageCheckResp->assertSee($newBook->name);
153 public function test_chapter_move_requires_delete_permissions()
155 $chapter = Chapter::first();
156 $currentBook = $chapter->book;
157 $newBook = Book::where('id', '!=', $currentBook->id)->first();
158 $editor = $this->getEditor();
160 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
161 $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
163 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
164 'entity_selection' => 'book:' . $newBook->id
166 $this->assertPermissionError($moveChapterResp);
167 $pageView = $this->get($chapter->getUrl());
168 $pageView->assertDontSee($chapter->getUrl('/move'));
170 $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles);
171 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
172 'entity_selection' => 'book:' . $newBook->id
175 $chapter = Chapter::find($chapter->id);
176 $moveChapterResp->assertRedirect($chapter->getUrl());
177 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
180 public function test_book_sort()
182 $oldBook = Book::query()->first();
183 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
184 $newBook = $this->newBook(['name' => 'New sort book']);
185 $pagesToMove = Page::query()->take(5)->get();
187 // Create request data
190 'id' => $chapterToMove->id,
192 'parentChapter' => false,
194 'book' => $newBook->id
197 foreach ($pagesToMove as $index => $page) {
201 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
203 'book' => $newBook->id
207 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
208 $sortResp->assertRedirect($newBook->getUrl());
209 $sortResp->assertStatus(302);
210 $this->assertDatabaseHas('chapters', [
211 'id' => $chapterToMove->id,
212 'book_id' => $newBook->id,
215 $this->assertTrue($newBook->chapters()->count() === 1);
216 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
218 $checkPage = $pagesToMove[1];
219 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
220 $checkResp->assertSee($newBook->name);
223 public function test_page_copy()
225 $page = Page::first();
226 $currentBook = $page->book;
227 $newBook = Book::where('id', '!=', $currentBook->id)->first();
229 $resp = $this->asEditor()->get($page->getUrl('/copy'));
230 $resp->assertSee('Copy Page');
232 $movePageResp = $this->post($page->getUrl('/copy'), [
233 'entity_selection' => 'book:' . $newBook->id,
234 'name' => 'My copied test page'
236 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
238 $movePageResp->assertRedirect($pageCopy->getUrl());
239 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
242 public function test_page_copy_with_no_destination()
244 $page = Page::first();
245 $currentBook = $page->book;
247 $resp = $this->asEditor()->get($page->getUrl('/copy'));
248 $resp->assertSee('Copy Page');
250 $movePageResp = $this->post($page->getUrl('/copy'), [
251 '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 == $currentBook->id, 'Page was copied to correct book');
258 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
261 public function test_page_can_be_copied_without_edit_permission()
263 $page = Page::first();
264 $currentBook = $page->book;
265 $newBook = Book::where('id', '!=', $currentBook->id)->first();
266 $viewer = $this->getViewer();
268 $resp = $this->actingAs($viewer)->get($page->getUrl());
269 $resp->assertDontSee($page->getUrl('/copy'));
271 $newBook->created_by = $viewer->id;
273 $this->giveUserPermissions($viewer, ['page-create-own']);
274 $this->regenEntityPermissions($newBook);
276 $resp = $this->actingAs($viewer)->get($page->getUrl());
277 $resp->assertSee($page->getUrl('/copy'));
279 $movePageResp = $this->post($page->getUrl('/copy'), [
280 'entity_selection' => 'book:' . $newBook->id,
281 'name' => 'My copied test page'
283 $movePageResp->assertRedirect();
285 $this->assertDatabaseHas('pages', [
286 'name' => 'My copied test page',
287 'created_by' => $viewer->id,
288 'book_id' => $newBook->id,