]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
Merge branch 'fix/oidc-logout' into development
[bookstack] / tests / Entity / SortTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use Tests\TestCase;
10
11 class SortTest extends TestCase
12 {
13     public function test_drafts_do_not_show_up()
14     {
15         $this->asAdmin();
16         $pageRepo = app(PageRepo::class);
17         $book = $this->entities->book();
18         $draft = $pageRepo->getNewDraftPage($book);
19
20         $resp = $this->get($book->getUrl());
21         $resp->assertSee($draft->name);
22
23         $resp = $this->get($book->getUrl() . '/sort');
24         $resp->assertDontSee($draft->name);
25     }
26
27     public function test_page_move_into_book()
28     {
29         $page = $this->entities->page();
30         $currentBook = $page->book;
31         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
32
33         $resp = $this->asEditor()->get($page->getUrl('/move'));
34         $resp->assertSee('Move Page');
35
36         $movePageResp = $this->put($page->getUrl('/move'), [
37             'entity_selection' => 'book:' . $newBook->id,
38         ]);
39         $page->refresh();
40
41         $movePageResp->assertRedirect($page->getUrl());
42         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
43
44         $newBookResp = $this->get($newBook->getUrl());
45         $newBookResp->assertSee('moved page');
46         $newBookResp->assertSee($page->name);
47     }
48
49     public function test_page_move_into_chapter()
50     {
51         $page = $this->entities->page();
52         $currentBook = $page->book;
53         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
54         $newChapter = $newBook->chapters()->first();
55
56         $movePageResp = $this->actingAs($this->users->editor())->put($page->getUrl('/move'), [
57             'entity_selection' => 'chapter:' . $newChapter->id,
58         ]);
59         $page->refresh();
60
61         $movePageResp->assertRedirect($page->getUrl());
62         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
63
64         $newChapterResp = $this->get($newChapter->getUrl());
65         $newChapterResp->assertSee($page->name);
66     }
67
68     public function test_page_move_from_chapter_to_book()
69     {
70         $oldChapter = Chapter::query()->first();
71         $page = $oldChapter->pages()->first();
72         $newBook = Book::query()->where('id', '!=', $oldChapter->book_id)->first();
73
74         $movePageResp = $this->actingAs($this->users->editor())->put($page->getUrl('/move'), [
75             'entity_selection' => 'book:' . $newBook->id,
76         ]);
77         $page->refresh();
78
79         $movePageResp->assertRedirect($page->getUrl());
80         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
81         $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
82
83         $newBookResp = $this->get($newBook->getUrl());
84         $newBookResp->assertSee($page->name);
85     }
86
87     public function test_page_move_requires_create_permissions_on_parent()
88     {
89         $page = $this->entities->page();
90         $currentBook = $page->book;
91         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
92         $editor = $this->users->editor();
93
94         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete'], $editor->roles->all());
95
96         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
97             'entity_selection' => 'book:' . $newBook->id,
98         ]);
99         $this->assertPermissionError($movePageResp);
100
101         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles->all());
102         $movePageResp = $this->put($page->getUrl('/move'), [
103             'entity_selection' => 'book:' . $newBook->id,
104         ]);
105
106         $page->refresh();
107         $movePageResp->assertRedirect($page->getUrl());
108
109         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
110     }
111
112     public function test_page_move_requires_delete_permissions()
113     {
114         $page = $this->entities->page();
115         $currentBook = $page->book;
116         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
117         $editor = $this->users->editor();
118
119         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
120         $this->permissions->setEntityPermissions($page, ['view', 'update', 'create'], $editor->roles->all());
121
122         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
123             'entity_selection' => 'book:' . $newBook->id,
124         ]);
125         $this->assertPermissionError($movePageResp);
126         $pageView = $this->get($page->getUrl());
127         $pageView->assertDontSee($page->getUrl('/move'));
128
129         $this->permissions->setEntityPermissions($page, ['view', 'update', 'create', 'delete'], $editor->roles->all());
130         $movePageResp = $this->put($page->getUrl('/move'), [
131             'entity_selection' => 'book:' . $newBook->id,
132         ]);
133
134         $page->refresh();
135         $movePageResp->assertRedirect($page->getUrl());
136         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
137     }
138
139     public function test_chapter_move()
140     {
141         $chapter = $this->entities->chapter();
142         $currentBook = $chapter->book;
143         $pageToCheck = $chapter->pages->first();
144         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
145
146         $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
147         $chapterMoveResp->assertSee('Move Chapter');
148
149         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
150             'entity_selection' => 'book:' . $newBook->id,
151         ]);
152
153         $chapter = Chapter::query()->find($chapter->id);
154         $moveChapterResp->assertRedirect($chapter->getUrl());
155         $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
156
157         $newBookResp = $this->get($newBook->getUrl());
158         $newBookResp->assertSee('moved chapter');
159         $newBookResp->assertSee($chapter->name);
160
161         $pageToCheck = Page::query()->find($pageToCheck->id);
162         $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
163         $pageCheckResp = $this->get($pageToCheck->getUrl());
164         $pageCheckResp->assertSee($newBook->name);
165     }
166
167     public function test_chapter_move_requires_delete_permissions()
168     {
169         $chapter = $this->entities->chapter();
170         $currentBook = $chapter->book;
171         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
172         $editor = $this->users->editor();
173
174         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
175         $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create'], $editor->roles->all());
176
177         $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
178             'entity_selection' => 'book:' . $newBook->id,
179         ]);
180         $this->assertPermissionError($moveChapterResp);
181         $pageView = $this->get($chapter->getUrl());
182         $pageView->assertDontSee($chapter->getUrl('/move'));
183
184         $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles->all());
185         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
186             'entity_selection' => 'book:' . $newBook->id,
187         ]);
188
189         $chapter = Chapter::query()->find($chapter->id);
190         $moveChapterResp->assertRedirect($chapter->getUrl());
191         $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
192     }
193
194     public function test_chapter_move_requires_create_permissions_in_new_book()
195     {
196         $chapter = $this->entities->chapter();
197         $currentBook = $chapter->book;
198         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
199         $editor = $this->users->editor();
200
201         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete'], [$editor->roles->first()]);
202         $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
203
204         $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
205             'entity_selection' => 'book:' . $newBook->id,
206         ]);
207         $this->assertPermissionError($moveChapterResp);
208
209         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
210         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
211             'entity_selection' => 'book:' . $newBook->id,
212         ]);
213
214         $chapter = Chapter::query()->find($chapter->id);
215         $moveChapterResp->assertRedirect($chapter->getUrl());
216         $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
217     }
218
219     public function test_chapter_move_changes_book_for_deleted_pages_within()
220     {
221         /** @var Chapter $chapter */
222         $chapter = Chapter::query()->whereHas('pages')->first();
223         $currentBook = $chapter->book;
224         $pageToCheck = $chapter->pages->first();
225         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
226
227         $pageToCheck->delete();
228
229         $this->asEditor()->put($chapter->getUrl('/move'), [
230             'entity_selection' => 'book:' . $newBook->id,
231         ]);
232
233         $pageToCheck->refresh();
234         $this->assertEquals($newBook->id, $pageToCheck->book_id);
235     }
236
237     public function test_book_sort_page_shows()
238     {
239         $bookToSort = $this->entities->book();
240
241         $resp = $this->asAdmin()->get($bookToSort->getUrl());
242         $this->withHtml($resp)->assertElementExists('a[href="' . $bookToSort->getUrl('/sort') . '"]');
243
244         $resp = $this->get($bookToSort->getUrl('/sort'));
245         $resp->assertStatus(200);
246         $resp->assertSee($bookToSort->name);
247     }
248
249     public function test_book_sort()
250     {
251         $oldBook = $this->entities->book();
252         $chapterToMove = $this->entities->newChapter(['name' => 'chapter to move'], $oldBook);
253         $newBook = $this->entities->newBook(['name' => 'New sort book']);
254         $pagesToMove = Page::query()->take(5)->get();
255
256         // Create request data
257         $reqData = [
258             [
259                 'id'            => $chapterToMove->id,
260                 'sort'          => 0,
261                 'parentChapter' => false,
262                 'type'          => 'chapter',
263                 'book'          => $newBook->id,
264             ],
265         ];
266         foreach ($pagesToMove as $index => $page) {
267             $reqData[] = [
268                 'id'            => $page->id,
269                 'sort'          => $index,
270                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
271                 'type'          => 'page',
272                 'book'          => $newBook->id,
273             ];
274         }
275
276         $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
277         $sortResp->assertRedirect($newBook->getUrl());
278         $sortResp->assertStatus(302);
279         $this->assertDatabaseHas('chapters', [
280             'id'       => $chapterToMove->id,
281             'book_id'  => $newBook->id,
282             'priority' => 0,
283         ]);
284         $this->assertTrue($newBook->chapters()->count() === 1);
285         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
286
287         $checkPage = $pagesToMove[1];
288         $checkResp = $this->get($checkPage->refresh()->getUrl());
289         $checkResp->assertSee($newBook->name);
290     }
291
292     public function test_book_sort_makes_no_changes_if_new_chapter_does_not_align_with_new_book()
293     {
294         $page = $this->entities->pageWithinChapter();
295         $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
296
297         $sortData = [
298             'id'            => $page->id,
299             'sort'          => 0,
300             'parentChapter' => $otherChapter->id,
301             'type'          => 'page',
302             'book'          => $page->book_id,
303         ];
304         $this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
305
306         $this->assertDatabaseHas('pages', [
307             'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
308         ]);
309     }
310
311     public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_chapter()
312     {
313         $page = $this->entities->pageWithinChapter();
314         /** @var Chapter $otherChapter */
315         $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
316         $this->permissions->setEntityPermissions($otherChapter);
317
318         $sortData = [
319             'id'            => $page->id,
320             'sort'          => 0,
321             'parentChapter' => $otherChapter->id,
322             'type'          => 'page',
323             'book'          => $otherChapter->book_id,
324         ];
325         $this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
326
327         $this->assertDatabaseHas('pages', [
328             'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
329         ]);
330     }
331
332     public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_book()
333     {
334         $page = $this->entities->pageWithinChapter();
335         /** @var Chapter $otherChapter */
336         $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
337         $editor = $this->users->editor();
338         $this->permissions->setEntityPermissions($otherChapter->book, ['update', 'delete'], [$editor->roles()->first()]);
339
340         $sortData = [
341             'id'            => $page->id,
342             'sort'          => 0,
343             'parentChapter' => $otherChapter->id,
344             'type'          => 'page',
345             'book'          => $otherChapter->book_id,
346         ];
347         $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
348
349         $this->assertDatabaseHas('pages', [
350             'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
351         ]);
352     }
353
354     public function test_book_sort_makes_no_changes_if_no_update_or_create_permissions_on_new_chapter()
355     {
356         $page = $this->entities->pageWithinChapter();
357         /** @var Chapter $otherChapter */
358         $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
359         $editor = $this->users->editor();
360         $this->permissions->setEntityPermissions($otherChapter, ['view', 'delete'], [$editor->roles()->first()]);
361
362         $sortData = [
363             'id'            => $page->id,
364             'sort'          => 0,
365             'parentChapter' => $otherChapter->id,
366             'type'          => 'page',
367             'book'          => $otherChapter->book_id,
368         ];
369         $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
370
371         $this->assertDatabaseHas('pages', [
372             'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
373         ]);
374     }
375
376     public function test_book_sort_makes_no_changes_if_no_update_permissions_on_moved_item()
377     {
378         $page = $this->entities->pageWithinChapter();
379         /** @var Chapter $otherChapter */
380         $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
381         $editor = $this->users->editor();
382         $this->permissions->setEntityPermissions($page, ['view', 'delete'], [$editor->roles()->first()]);
383
384         $sortData = [
385             'id'            => $page->id,
386             'sort'          => 0,
387             'parentChapter' => $otherChapter->id,
388             'type'          => 'page',
389             'book'          => $otherChapter->book_id,
390         ];
391         $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
392
393         $this->assertDatabaseHas('pages', [
394             'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
395         ]);
396     }
397
398     public function test_book_sort_makes_no_changes_if_no_delete_permissions_on_moved_item()
399     {
400         $page = $this->entities->pageWithinChapter();
401         /** @var Chapter $otherChapter */
402         $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
403         $editor = $this->users->editor();
404         $this->permissions->setEntityPermissions($page, ['view', 'update'], [$editor->roles()->first()]);
405
406         $sortData = [
407             'id'            => $page->id,
408             'sort'          => 0,
409             'parentChapter' => $otherChapter->id,
410             'type'          => 'page',
411             'book'          => $otherChapter->book_id,
412         ];
413         $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
414
415         $this->assertDatabaseHas('pages', [
416             'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
417         ]);
418     }
419
420     public function test_book_sort_item_returns_book_content()
421     {
422         $bookToSort = $this->entities->book();
423         $firstPage = $bookToSort->pages[0];
424         $firstChapter = $bookToSort->chapters[0];
425
426         $resp = $this->asAdmin()->get($bookToSort->getUrl() . '/sort-item');
427
428         // Ensure book details are returned
429         $resp->assertSee($bookToSort->name);
430         $resp->assertSee($firstPage->name);
431         $resp->assertSee($firstChapter->name);
432     }
433
434     public function test_pages_in_book_show_sorted_by_priority()
435     {
436         $book = $this->entities->bookHasChaptersAndPages();
437         $book->chapters()->forceDelete();
438         /** @var Page[] $pages */
439         $pages = $book->pages()->where('chapter_id', '=', 0)->take(2)->get();
440         $book->pages()->whereNotIn('id', $pages->pluck('id'))->delete();
441
442         $resp = $this->asEditor()->get($book->getUrl());
443         $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[0]->name);
444         $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[1]->name);
445
446         $pages[0]->forceFill(['priority' => 10])->save();
447         $pages[1]->forceFill(['priority' => 5])->save();
448
449         $resp = $this->asEditor()->get($book->getUrl());
450         $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[1]->name);
451         $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[0]->name);
452     }
453 }