1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Chapter;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Repos\PageRepo;
9 class SortTest extends TestCase
13 public function setUp(): void
16 $this->book = Book::first();
19 public function test_drafts_do_not_show_up()
22 $pageRepo = app(PageRepo::class);
23 $draft = $pageRepo->getNewDraftPage($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_into_book()
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_into_chapter()
56 $page = Page::first();
57 $currentBook = $page->book;
58 $newBook = Book::where('id', '!=', $currentBook->id)->first();
59 $newChapter = $newBook->chapters()->first();
61 $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
62 'entity_selection' => 'chapter:' . $newChapter->id
64 $page = Page::find($page->id);
66 $movePageResp->assertRedirect($page->getUrl());
67 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
69 $newChapterResp = $this->get($newChapter->getUrl());
70 $newChapterResp->assertSee($page->name);
73 public function test_page_move_from_chapter_to_book()
75 $oldChapter = Chapter::first();
76 $page = $oldChapter->pages()->first();
77 $newBook = Book::where('id', '!=', $oldChapter->book_id)->first();
79 $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
80 'entity_selection' => 'book:' . $newBook->id
84 $movePageResp->assertRedirect($page->getUrl());
85 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
86 $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
88 $newBookResp = $this->get($newBook->getUrl());
89 $newBookResp->assertSee($page->name);
92 public function test_page_move_requires_create_permissions_on_parent()
94 $page = Page::first();
95 $currentBook = $page->book;
96 $newBook = Book::where('id', '!=', $currentBook->id)->first();
97 $editor = $this->getEditor();
99 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
101 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
102 'entity_selection' => 'book:' . $newBook->id
104 $this->assertPermissionError($movePageResp);
106 $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles);
107 $movePageResp = $this->put($page->getUrl('/move'), [
108 'entity_selection' => 'book:' . $newBook->id
111 $page = Page::find($page->id);
112 $movePageResp->assertRedirect($page->getUrl());
114 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
117 public function test_page_move_requires_delete_permissions()
119 $page = Page::first();
120 $currentBook = $page->book;
121 $newBook = Book::where('id', '!=', $currentBook->id)->first();
122 $editor = $this->getEditor();
124 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
125 $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
127 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
128 'entity_selection' => 'book:' . $newBook->id
130 $this->assertPermissionError($movePageResp);
131 $pageView = $this->get($page->getUrl());
132 $pageView->assertDontSee($page->getUrl('/move'));
134 $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles);
135 $movePageResp = $this->put($page->getUrl('/move'), [
136 'entity_selection' => 'book:' . $newBook->id
139 $page = Page::find($page->id);
140 $movePageResp->assertRedirect($page->getUrl());
141 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
144 public function test_chapter_move()
146 $chapter = Chapter::first();
147 $currentBook = $chapter->book;
148 $pageToCheck = $chapter->pages->first();
149 $newBook = Book::where('id', '!=', $currentBook->id)->first();
151 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
152 $chapterMoveResp->assertSee('Move Chapter');
154 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
155 'entity_selection' => 'book:' . $newBook->id
158 $chapter = Chapter::find($chapter->id);
159 $moveChapterResp->assertRedirect($chapter->getUrl());
160 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
162 $newBookResp = $this->get($newBook->getUrl());
163 $newBookResp->assertSee('moved chapter');
164 $newBookResp->assertSee($chapter->name);
166 $pageToCheck = Page::find($pageToCheck->id);
167 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
168 $pageCheckResp = $this->get($pageToCheck->getUrl());
169 $pageCheckResp->assertSee($newBook->name);
172 public function test_chapter_move_requires_delete_permissions()
174 $chapter = Chapter::first();
175 $currentBook = $chapter->book;
176 $newBook = Book::where('id', '!=', $currentBook->id)->first();
177 $editor = $this->getEditor();
179 $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
180 $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
182 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
183 'entity_selection' => 'book:' . $newBook->id
185 $this->assertPermissionError($moveChapterResp);
186 $pageView = $this->get($chapter->getUrl());
187 $pageView->assertDontSee($chapter->getUrl('/move'));
189 $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles);
190 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
191 'entity_selection' => 'book:' . $newBook->id
194 $chapter = Chapter::find($chapter->id);
195 $moveChapterResp->assertRedirect($chapter->getUrl());
196 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
199 public function test_book_sort()
201 $oldBook = Book::query()->first();
202 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
203 $newBook = $this->newBook(['name' => 'New sort book']);
204 $pagesToMove = Page::query()->take(5)->get();
206 // Create request data
209 'id' => $chapterToMove->id,
211 'parentChapter' => false,
213 'book' => $newBook->id
216 foreach ($pagesToMove as $index => $page) {
220 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
222 'book' => $newBook->id
226 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
227 $sortResp->assertRedirect($newBook->getUrl());
228 $sortResp->assertStatus(302);
229 $this->assertDatabaseHas('chapters', [
230 'id' => $chapterToMove->id,
231 'book_id' => $newBook->id,
234 $this->assertTrue($newBook->chapters()->count() === 1);
235 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
237 $checkPage = $pagesToMove[1];
238 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
239 $checkResp->assertSee($newBook->name);