3 use BookStack\Auth\Role;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\Page;
7 use BookStack\Entities\Repos\PageRepo;
9 class SortTest extends TestCase
13 public function setUp()
16 $this->book = Book::first();
19 public function test_drafts_do_not_show_up()
22 $pageRepo = app(PageRepo::class);
23 $draft = $pageRepo->getDraftPage($this->book);
25 $resp = $this->get($this->book->getUrl());
26 $resp->assertSee($draft->name);
28 $resp = $this->get($this->book->getUrl() . '/sort');
29 $resp->assertDontSee($draft->name);
32 public function test_page_move()
34 $page = Page::first();
35 $currentBook = $page->book;
36 $newBook = Book::where('id', '!=', $currentBook->id)->first();
38 $resp = $this->asEditor()->get($page->getUrl('/move'));
39 $resp->assertSee('Move Page');
41 $movePageResp = $this->put($page->getUrl('/move'), [
42 'entity_selection' => 'book:' . $newBook->id
44 $page = Page::find($page->id);
46 $movePageResp->assertRedirect($page->getUrl());
47 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
49 $newBookResp = $this->get($newBook->getUrl());
50 $newBookResp->assertSee('moved page');
51 $newBookResp->assertSee($page->name);
54 public function test_page_move_requires_create_permissions_on_parent()
56 $page = Page::first();
57 $currentBook = $page->book;
58 $newBook = Book::where('id', '!=', $currentBook->id)->first();
59 $editor = $this->getEditor();
61 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
63 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
64 'entity_selection' => 'book:' . $newBook->id
66 $this->assertPermissionError($movePageResp);
68 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles);
69 $movePageResp = $this->put($page->getUrl('/move'), [
70 'entity_selection' => 'book:' . $newBook->id
73 $page = Page::find($page->id);
74 $movePageResp->assertRedirect($page->getUrl());
76 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
79 public function test_page_move_requires_delete_permissions()
81 $page = Page::first();
82 $currentBook = $page->book;
83 $newBook = Book::where('id', '!=', $currentBook->id)->first();
84 $editor = $this->getEditor();
86 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
87 $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
89 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
90 'entity_selection' => 'book:' . $newBook->id
92 $this->assertPermissionError($movePageResp);
93 $pageView = $this->get($page->getUrl());
94 $pageView->assertDontSee($page->getUrl('/move'));
96 $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles);
97 $movePageResp = $this->put($page->getUrl('/move'), [
98 'entity_selection' => 'book:' . $newBook->id
101 $page = Page::find($page->id);
102 $movePageResp->assertRedirect($page->getUrl());
103 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
106 public function test_chapter_move()
108 $chapter = Chapter::first();
109 $currentBook = $chapter->book;
110 $pageToCheck = $chapter->pages->first();
111 $newBook = Book::where('id', '!=', $currentBook->id)->first();
113 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
114 $chapterMoveResp->assertSee('Move Chapter');
116 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
117 'entity_selection' => 'book:' . $newBook->id
120 $chapter = Chapter::find($chapter->id);
121 $moveChapterResp->assertRedirect($chapter->getUrl());
122 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
124 $newBookResp = $this->get($newBook->getUrl());
125 $newBookResp->assertSee('moved chapter');
126 $newBookResp->assertSee($chapter->name);
128 $pageToCheck = Page::find($pageToCheck->id);
129 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
130 $pageCheckResp = $this->get($pageToCheck->getUrl());
131 $pageCheckResp->assertSee($newBook->name);
134 public function test_chapter_move_requires_delete_permissions()
136 $chapter = Chapter::first();
137 $currentBook = $chapter->book;
138 $newBook = Book::where('id', '!=', $currentBook->id)->first();
139 $editor = $this->getEditor();
141 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
142 $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
144 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
145 'entity_selection' => 'book:' . $newBook->id
147 $this->assertPermissionError($moveChapterResp);
148 $pageView = $this->get($chapter->getUrl());
149 $pageView->assertDontSee($chapter->getUrl('/move'));
151 $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles);
152 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
153 'entity_selection' => 'book:' . $newBook->id
156 $chapter = Chapter::find($chapter->id);
157 $moveChapterResp->assertRedirect($chapter->getUrl());
158 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
161 public function test_book_sort()
163 $oldBook = Book::query()->first();
164 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
165 $newBook = $this->newBook(['name' => 'New sort book']);
166 $pagesToMove = Page::query()->take(5)->get();
168 // Create request data
171 'id' => $chapterToMove->id,
173 'parentChapter' => false,
175 'book' => $newBook->id
178 foreach ($pagesToMove as $index => $page) {
182 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
184 'book' => $newBook->id
188 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
189 $sortResp->assertRedirect($newBook->getUrl());
190 $sortResp->assertStatus(302);
191 $this->assertDatabaseHas('chapters', [
192 'id' => $chapterToMove->id,
193 'book_id' => $newBook->id,
196 $this->assertTrue($newBook->chapters()->count() === 1);
197 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
199 $checkPage = $pagesToMove[1];
200 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
201 $checkResp->assertSee($newBook->name);
204 public function test_page_copy()
206 $page = Page::first();
207 $currentBook = $page->book;
208 $newBook = Book::where('id', '!=', $currentBook->id)->first();
210 $resp = $this->asEditor()->get($page->getUrl('/copy'));
211 $resp->assertSee('Copy Page');
213 $movePageResp = $this->post($page->getUrl('/copy'), [
214 'entity_selection' => 'book:' . $newBook->id,
215 'name' => 'My copied test page'
218 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
220 $movePageResp->assertRedirect($pageCopy->getUrl());
221 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
224 public function test_page_copy_with_no_destination()
226 $page = Page::first();
227 $currentBook = $page->book;
229 $resp = $this->asEditor()->get($page->getUrl('/copy'));
230 $resp->assertSee('Copy Page');
232 $movePageResp = $this->post($page->getUrl('/copy'), [
233 '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 == $currentBook->id, 'Page was copied to correct book');
240 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
243 public function test_page_can_be_copied_without_edit_permission()
245 $page = Page::first();
246 $currentBook = $page->book;
247 $newBook = Book::where('id', '!=', $currentBook->id)->first();
248 $viewer = $this->getViewer();
250 $resp = $this->actingAs($viewer)->get($page->getUrl());
251 $resp->assertDontSee($page->getUrl('/copy'));
253 $newBook->created_by = $viewer->id;
255 $this->giveUserPermissions($viewer, ['page-create-own']);
256 $this->regenEntityPermissions($newBook);
258 $resp = $this->actingAs($viewer)->get($page->getUrl());
259 $resp->assertSee($page->getUrl('/copy'));
261 $movePageResp = $this->post($page->getUrl('/copy'), [
262 'entity_selection' => 'book:' . $newBook->id,
263 'name' => 'My copied test page'
265 $movePageResp->assertRedirect();
267 $this->assertDatabaseHas('pages', [
268 'name' => 'My copied test page',
269 'created_by' => $viewer->id,
270 'book_id' => $newBook->id,