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()
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_requires_create_permissions_on_parent()
55 $page = Page::first();
56 $currentBook = $page->book;
57 $newBook = Book::where('id', '!=', $currentBook->id)->first();
58 $editor = $this->getEditor();
60 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
62 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
63 'entity_selection' => 'book:' . $newBook->id
65 $this->assertPermissionError($movePageResp);
67 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles);
68 $movePageResp = $this->put($page->getUrl('/move'), [
69 'entity_selection' => 'book:' . $newBook->id
72 $page = Page::find($page->id);
73 $movePageResp->assertRedirect($page->getUrl());
75 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
78 public function test_page_move_requires_delete_permissions()
80 $page = Page::first();
81 $currentBook = $page->book;
82 $newBook = Book::where('id', '!=', $currentBook->id)->first();
83 $editor = $this->getEditor();
85 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
86 $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
88 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
89 'entity_selection' => 'book:' . $newBook->id
91 $this->assertPermissionError($movePageResp);
92 $pageView = $this->get($page->getUrl());
93 $pageView->assertDontSee($page->getUrl('/move'));
95 $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles);
96 $movePageResp = $this->put($page->getUrl('/move'), [
97 'entity_selection' => 'book:' . $newBook->id
100 $page = Page::find($page->id);
101 $movePageResp->assertRedirect($page->getUrl());
102 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
105 public function test_chapter_move()
107 $chapter = Chapter::first();
108 $currentBook = $chapter->book;
109 $pageToCheck = $chapter->pages->first();
110 $newBook = Book::where('id', '!=', $currentBook->id)->first();
112 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
113 $chapterMoveResp->assertSee('Move Chapter');
115 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
116 'entity_selection' => 'book:' . $newBook->id
119 $chapter = Chapter::find($chapter->id);
120 $moveChapterResp->assertRedirect($chapter->getUrl());
121 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
123 $newBookResp = $this->get($newBook->getUrl());
124 $newBookResp->assertSee('moved chapter');
125 $newBookResp->assertSee($chapter->name);
127 $pageToCheck = Page::find($pageToCheck->id);
128 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
129 $pageCheckResp = $this->get($pageToCheck->getUrl());
130 $pageCheckResp->assertSee($newBook->name);
133 public function test_chapter_move_requires_delete_permissions()
135 $chapter = Chapter::first();
136 $currentBook = $chapter->book;
137 $newBook = Book::where('id', '!=', $currentBook->id)->first();
138 $editor = $this->getEditor();
140 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
141 $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
143 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
144 'entity_selection' => 'book:' . $newBook->id
146 $this->assertPermissionError($moveChapterResp);
147 $pageView = $this->get($chapter->getUrl());
148 $pageView->assertDontSee($chapter->getUrl('/move'));
150 $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles);
151 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
152 'entity_selection' => 'book:' . $newBook->id
155 $chapter = Chapter::find($chapter->id);
156 $moveChapterResp->assertRedirect($chapter->getUrl());
157 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
160 public function test_book_sort()
162 $oldBook = Book::query()->first();
163 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
164 $newBook = $this->newBook(['name' => 'New sort book']);
165 $pagesToMove = Page::query()->take(5)->get();
167 // Create request data
170 'id' => $chapterToMove->id,
172 'parentChapter' => false,
174 'book' => $newBook->id
177 foreach ($pagesToMove as $index => $page) {
181 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
183 'book' => $newBook->id
187 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
188 $sortResp->assertRedirect($newBook->getUrl());
189 $sortResp->assertStatus(302);
190 $this->assertDatabaseHas('chapters', [
191 'id' => $chapterToMove->id,
192 'book_id' => $newBook->id,
195 $this->assertTrue($newBook->chapters()->count() === 1);
196 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
198 $checkPage = $pagesToMove[1];
199 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
200 $checkResp->assertSee($newBook->name);
203 public function test_page_copy()
205 $page = Page::first();
206 $currentBook = $page->book;
207 $newBook = Book::where('id', '!=', $currentBook->id)->first();
209 $resp = $this->asEditor()->get($page->getUrl('/copy'));
210 $resp->assertSee('Copy Page');
212 $movePageResp = $this->post($page->getUrl('/copy'), [
213 'entity_selection' => 'book:' . $newBook->id,
214 'name' => 'My copied test page'
216 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
218 $movePageResp->assertRedirect($pageCopy->getUrl());
219 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
222 public function test_page_copy_with_no_destination()
224 $page = Page::first();
225 $currentBook = $page->book;
227 $resp = $this->asEditor()->get($page->getUrl('/copy'));
228 $resp->assertSee('Copy Page');
230 $movePageResp = $this->post($page->getUrl('/copy'), [
231 'name' => 'My copied test page'
234 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
236 $movePageResp->assertRedirect($pageCopy->getUrl());
237 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
238 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
241 public function test_page_can_be_copied_without_edit_permission()
243 $page = Page::first();
244 $currentBook = $page->book;
245 $newBook = Book::where('id', '!=', $currentBook->id)->first();
246 $viewer = $this->getViewer();
248 $resp = $this->actingAs($viewer)->get($page->getUrl());
249 $resp->assertDontSee($page->getUrl('/copy'));
251 $newBook->created_by = $viewer->id;
253 $this->giveUserPermissions($viewer, ['page-create-own']);
254 $this->regenEntityPermissions($newBook);
256 $resp = $this->actingAs($viewer)->get($page->getUrl());
257 $resp->assertSee($page->getUrl('/copy'));
259 $movePageResp = $this->post($page->getUrl('/copy'), [
260 'entity_selection' => 'book:' . $newBook->id,
261 'name' => 'My copied test page'
263 $movePageResp->assertRedirect();
265 $this->assertDatabaseHas('pages', [
266 'name' => 'My copied test page',
267 'created_by' => $viewer->id,
268 'book_id' => $newBook->id,