3 namespace Tests\Sorting;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
10 class MoveTest extends TestCase
12 public function test_page_move_into_book()
14 $page = $this->entities->page();
15 $currentBook = $page->book;
16 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
18 $resp = $this->asEditor()->get($page->getUrl('/move'));
19 $resp->assertSee('Move Page');
21 $movePageResp = $this->put($page->getUrl('/move'), [
22 'entity_selection' => 'book:' . $newBook->id,
26 $movePageResp->assertRedirect($page->getUrl());
27 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
29 $newBookResp = $this->get($newBook->getUrl());
30 $newBookResp->assertSee('moved page');
31 $newBookResp->assertSee($page->name);
34 public function test_page_move_into_chapter()
36 $page = $this->entities->page();
37 $currentBook = $page->book;
38 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
39 $newChapter = $newBook->chapters()->first();
41 $movePageResp = $this->actingAs($this->users->editor())->put($page->getUrl('/move'), [
42 'entity_selection' => 'chapter:' . $newChapter->id,
46 $movePageResp->assertRedirect($page->getUrl());
47 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
49 $newChapterResp = $this->get($newChapter->getUrl());
50 $newChapterResp->assertSee($page->name);
53 public function test_page_move_from_chapter_to_book()
55 $oldChapter = Chapter::query()->first();
56 $page = $oldChapter->pages()->first();
57 $newBook = Book::query()->where('id', '!=', $oldChapter->book_id)->first();
59 $movePageResp = $this->actingAs($this->users->editor())->put($page->getUrl('/move'), [
60 'entity_selection' => 'book:' . $newBook->id,
64 $movePageResp->assertRedirect($page->getUrl());
65 $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
66 $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
68 $newBookResp = $this->get($newBook->getUrl());
69 $newBookResp->assertSee($page->name);
72 public function test_page_move_requires_create_permissions_on_parent()
74 $page = $this->entities->page();
75 $currentBook = $page->book;
76 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
77 $editor = $this->users->editor();
79 $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete'], $editor->roles->all());
81 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
82 'entity_selection' => 'book:' . $newBook->id,
84 $this->assertPermissionError($movePageResp);
86 $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles->all());
87 $movePageResp = $this->put($page->getUrl('/move'), [
88 'entity_selection' => 'book:' . $newBook->id,
92 $movePageResp->assertRedirect($page->getUrl());
94 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
97 public function test_page_move_requires_delete_permissions()
99 $page = $this->entities->page();
100 $currentBook = $page->book;
101 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
102 $editor = $this->users->editor();
104 $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
105 $this->permissions->setEntityPermissions($page, ['view', 'update', 'create'], $editor->roles->all());
107 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
108 'entity_selection' => 'book:' . $newBook->id,
110 $this->assertPermissionError($movePageResp);
111 $pageView = $this->get($page->getUrl());
112 $pageView->assertDontSee($page->getUrl('/move'));
114 $this->permissions->setEntityPermissions($page, ['view', 'update', 'create', 'delete'], $editor->roles->all());
115 $movePageResp = $this->put($page->getUrl('/move'), [
116 'entity_selection' => 'book:' . $newBook->id,
120 $movePageResp->assertRedirect($page->getUrl());
121 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
124 public function test_chapter_move()
126 $chapter = $this->entities->chapter();
127 $currentBook = $chapter->book;
128 $pageToCheck = $chapter->pages->first();
129 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
131 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
132 $chapterMoveResp->assertSee('Move Chapter');
134 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
135 'entity_selection' => 'book:' . $newBook->id,
138 $chapter = Chapter::query()->find($chapter->id);
139 $moveChapterResp->assertRedirect($chapter->getUrl());
140 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
142 $newBookResp = $this->get($newBook->getUrl());
143 $newBookResp->assertSee('moved chapter');
144 $newBookResp->assertSee($chapter->name);
146 $pageToCheck = Page::query()->find($pageToCheck->id);
147 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
148 $pageCheckResp = $this->get($pageToCheck->getUrl());
149 $pageCheckResp->assertSee($newBook->name);
152 public function test_chapter_move_requires_delete_permissions()
154 $chapter = $this->entities->chapter();
155 $currentBook = $chapter->book;
156 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
157 $editor = $this->users->editor();
159 $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
160 $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create'], $editor->roles->all());
162 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
163 'entity_selection' => 'book:' . $newBook->id,
165 $this->assertPermissionError($moveChapterResp);
166 $pageView = $this->get($chapter->getUrl());
167 $pageView->assertDontSee($chapter->getUrl('/move'));
169 $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles->all());
170 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
171 'entity_selection' => 'book:' . $newBook->id,
174 $chapter = Chapter::query()->find($chapter->id);
175 $moveChapterResp->assertRedirect($chapter->getUrl());
176 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
179 public function test_chapter_move_requires_create_permissions_in_new_book()
181 $chapter = $this->entities->chapter();
182 $currentBook = $chapter->book;
183 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
184 $editor = $this->users->editor();
186 $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete'], [$editor->roles->first()]);
187 $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
189 $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
190 'entity_selection' => 'book:' . $newBook->id,
192 $this->assertPermissionError($moveChapterResp);
194 $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
195 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
196 'entity_selection' => 'book:' . $newBook->id,
199 $chapter = Chapter::query()->find($chapter->id);
200 $moveChapterResp->assertRedirect($chapter->getUrl());
201 $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
204 public function test_chapter_move_changes_book_for_deleted_pages_within()
206 /** @var Chapter $chapter */
207 $chapter = Chapter::query()->whereHas('pages')->first();
208 $currentBook = $chapter->book;
209 $pageToCheck = $chapter->pages->first();
210 $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
212 $pageToCheck->delete();
214 $this->asEditor()->put($chapter->getUrl('/move'), [
215 'entity_selection' => 'book:' . $newBook->id,
218 $pageToCheck->refresh();
219 $this->assertEquals($newBook->id, $pageToCheck->book_id);