]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
Merge pull request #1734 from ististudio/master
[bookstack] / tests / Entity / SortTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\Repos\PageRepo;
7
8 class SortTest extends TestCase
9 {
10     protected $book;
11
12     public function setUp(): void
13     {
14         parent::setUp();
15         $this->book = Book::first();
16     }
17
18     public function test_drafts_do_not_show_up()
19     {
20         $this->asAdmin();
21         $pageRepo = app(PageRepo::class);
22         $draft = $pageRepo->getNewDraftPage($this->book);
23
24         $resp = $this->get($this->book->getUrl());
25         $resp->assertSee($draft->name);
26
27         $resp = $this->get($this->book->getUrl() . '/sort');
28         $resp->assertDontSee($draft->name);
29     }
30
31     public function test_page_move_into_book()
32     {
33         $page = Page::first();
34         $currentBook = $page->book;
35         $newBook = Book::where('id', '!=', $currentBook->id)->first();
36
37         $resp = $this->asEditor()->get($page->getUrl('/move'));
38         $resp->assertSee('Move Page');
39
40         $movePageResp = $this->put($page->getUrl('/move'), [
41             'entity_selection' => 'book:' . $newBook->id
42         ]);
43         $page = Page::find($page->id);
44
45         $movePageResp->assertRedirect($page->getUrl());
46         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
47
48         $newBookResp = $this->get($newBook->getUrl());
49         $newBookResp->assertSee('moved page');
50         $newBookResp->assertSee($page->name);
51     }
52
53     public function test_page_move_into_chapter()
54     {
55         $page = Page::first();
56         $currentBook = $page->book;
57         $newBook = Book::where('id', '!=', $currentBook->id)->first();
58         $newChapter = $newBook->chapters()->first();
59
60         $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
61             'entity_selection' => 'chapter:' . $newChapter->id
62         ]);
63         $page = Page::find($page->id);
64
65         $movePageResp->assertRedirect($page->getUrl());
66         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
67
68         $newChapterResp = $this->get($newChapter->getUrl());
69         $newChapterResp->assertSee($page->name);
70     }
71
72     public function test_page_move_from_chapter_to_book()
73     {
74         $oldChapter = Chapter::first();
75         $page = $oldChapter->pages()->first();
76         $newBook = Book::where('id', '!=', $oldChapter->book_id)->first();
77
78         $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
79             'entity_selection' => 'book:' . $newBook->id
80         ]);
81         $page = Page::find($page->id);
82
83         $movePageResp->assertRedirect($page->getUrl());
84         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
85         $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
86
87         $newBookResp = $this->get($newBook->getUrl());
88         $newBookResp->assertSee($page->name);
89     }
90
91     public function test_page_move_requires_create_permissions_on_parent()
92     {
93         $page = Page::first();
94         $currentBook = $page->book;
95         $newBook = Book::where('id', '!=', $currentBook->id)->first();
96         $editor = $this->getEditor();
97
98         $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
99
100         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
101             'entity_selection' => 'book:' . $newBook->id
102         ]);
103         $this->assertPermissionError($movePageResp);
104
105         $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles);
106         $movePageResp = $this->put($page->getUrl('/move'), [
107             'entity_selection' => 'book:' . $newBook->id
108         ]);
109
110         $page = Page::find($page->id);
111         $movePageResp->assertRedirect($page->getUrl());
112
113         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
114     }
115
116     public function test_page_move_requires_delete_permissions()
117     {
118         $page = Page::first();
119         $currentBook = $page->book;
120         $newBook = Book::where('id', '!=', $currentBook->id)->first();
121         $editor = $this->getEditor();
122
123         $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
124         $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
125
126         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
127             'entity_selection' => 'book:' . $newBook->id
128         ]);
129         $this->assertPermissionError($movePageResp);
130         $pageView = $this->get($page->getUrl());
131         $pageView->assertDontSee($page->getUrl('/move'));
132
133         $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles);
134         $movePageResp = $this->put($page->getUrl('/move'), [
135             'entity_selection' => 'book:' . $newBook->id
136         ]);
137
138         $page = Page::find($page->id);
139         $movePageResp->assertRedirect($page->getUrl());
140         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
141     }
142
143     public function test_chapter_move()
144     {
145         $chapter = Chapter::first();
146         $currentBook = $chapter->book;
147         $pageToCheck = $chapter->pages->first();
148         $newBook = Book::where('id', '!=', $currentBook->id)->first();
149
150         $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
151         $chapterMoveResp->assertSee('Move Chapter');
152
153         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
154             'entity_selection' => 'book:' . $newBook->id
155         ]);
156
157         $chapter = Chapter::find($chapter->id);
158         $moveChapterResp->assertRedirect($chapter->getUrl());
159         $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
160
161         $newBookResp = $this->get($newBook->getUrl());
162         $newBookResp->assertSee('moved chapter');
163         $newBookResp->assertSee($chapter->name);
164
165         $pageToCheck = Page::find($pageToCheck->id);
166         $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
167         $pageCheckResp = $this->get($pageToCheck->getUrl());
168         $pageCheckResp->assertSee($newBook->name);
169     }
170
171     public function test_chapter_move_requires_delete_permissions()
172     {
173         $chapter = Chapter::first();
174         $currentBook = $chapter->book;
175         $newBook = Book::where('id', '!=', $currentBook->id)->first();
176         $editor = $this->getEditor();
177
178         $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
179         $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
180
181         $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
182             'entity_selection' => 'book:' . $newBook->id
183         ]);
184         $this->assertPermissionError($moveChapterResp);
185         $pageView = $this->get($chapter->getUrl());
186         $pageView->assertDontSee($chapter->getUrl('/move'));
187
188         $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles);
189         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
190             'entity_selection' => 'book:' . $newBook->id
191         ]);
192
193         $chapter = Chapter::find($chapter->id);
194         $moveChapterResp->assertRedirect($chapter->getUrl());
195         $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
196     }
197
198     public function test_book_sort()
199     {
200         $oldBook = Book::query()->first();
201         $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
202         $newBook = $this->newBook(['name' => 'New sort book']);
203         $pagesToMove = Page::query()->take(5)->get();
204
205         // Create request data
206         $reqData = [
207             [
208                 'id' => $chapterToMove->id,
209                 'sort' => 0,
210                 'parentChapter' => false,
211                 'type' => 'chapter',
212                 'book' => $newBook->id
213             ]
214         ];
215         foreach ($pagesToMove as $index => $page) {
216             $reqData[] = [
217                 'id' => $page->id,
218                 'sort' => $index,
219                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
220                 'type' => 'page',
221                 'book' => $newBook->id
222             ];
223         }
224
225         $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
226         $sortResp->assertRedirect($newBook->getUrl());
227         $sortResp->assertStatus(302);
228         $this->assertDatabaseHas('chapters', [
229             'id' => $chapterToMove->id,
230             'book_id' => $newBook->id,
231             'priority' => 0
232         ]);
233         $this->assertTrue($newBook->chapters()->count() === 1);
234         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
235
236         $checkPage = $pagesToMove[1];
237         $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
238         $checkResp->assertSee($newBook->name);
239     }
240
241     public function test_page_copy()
242     {
243         $page = Page::first();
244         $currentBook = $page->book;
245         $newBook = Book::where('id', '!=', $currentBook->id)->first();
246
247         $resp = $this->asEditor()->get($page->getUrl('/copy'));
248         $resp->assertSee('Copy Page');
249
250         $movePageResp = $this->post($page->getUrl('/copy'), [
251             'entity_selection' => 'book:' . $newBook->id,
252             'name' => 'My copied test page'
253         ]);
254         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
255
256         $movePageResp->assertRedirect($pageCopy->getUrl());
257         $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
258     }
259
260     public function test_page_copy_with_no_destination()
261     {
262         $page = Page::first();
263         $currentBook = $page->book;
264
265         $resp = $this->asEditor()->get($page->getUrl('/copy'));
266         $resp->assertSee('Copy Page');
267
268         $movePageResp = $this->post($page->getUrl('/copy'), [
269             'name' => 'My copied test page'
270         ]);
271
272         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
273
274         $movePageResp->assertRedirect($pageCopy->getUrl());
275         $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
276         $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
277     }
278
279     public function test_page_can_be_copied_without_edit_permission()
280     {
281         $page = Page::first();
282         $currentBook = $page->book;
283         $newBook = Book::where('id', '!=', $currentBook->id)->first();
284         $viewer = $this->getViewer();
285
286         $resp = $this->actingAs($viewer)->get($page->getUrl());
287         $resp->assertDontSee($page->getUrl('/copy'));
288
289         $newBook->created_by = $viewer->id;
290         $newBook->save();
291         $this->giveUserPermissions($viewer, ['page-create-own']);
292         $this->regenEntityPermissions($newBook);
293
294         $resp = $this->actingAs($viewer)->get($page->getUrl());
295         $resp->assertSee($page->getUrl('/copy'));
296
297         $movePageResp = $this->post($page->getUrl('/copy'), [
298             'entity_selection' => 'book:' . $newBook->id,
299             'name' => 'My copied test page'
300         ]);
301         $movePageResp->assertRedirect();
302
303         $this->assertDatabaseHas('pages', [
304             'name' => 'My copied test page',
305             'created_by' => $viewer->id,
306             'book_id' => $newBook->id,
307         ]);
308     }
309
310 }