]> BookStack Code Mirror - bookstack/blob - tests/Permissions/EntityPermissionsTest.php
Quick run through of applying new test entity helper class
[bookstack] / tests / Permissions / EntityPermissionsTest.php
1 <?php
2
3 namespace Tests\Permissions;
4
5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\Page;
11 use Illuminate\Support\Str;
12 use Tests\TestCase;
13
14 class EntityPermissionsTest extends TestCase
15 {
16     protected User $user;
17     protected User $viewer;
18
19     protected function setUp(): void
20     {
21         parent::setUp();
22         $this->user = $this->getEditor();
23         $this->viewer = $this->getViewer();
24     }
25
26     protected function setRestrictionsForTestRoles(Entity $entity, array $actions = [])
27     {
28         $roles = [
29             $this->user->roles->first(),
30             $this->viewer->roles->first(),
31         ];
32         $this->entities->setPermissions($entity, $actions, $roles);
33     }
34
35     public function test_bookshelf_view_restriction()
36     {
37         $shelf = $this->entities->shelf();
38
39         $this->actingAs($this->user)
40             ->get($shelf->getUrl())
41             ->assertStatus(200);
42
43         $this->setRestrictionsForTestRoles($shelf, []);
44
45         $this->followingRedirects()->get($shelf->getUrl())
46             ->assertSee('Shelf not found');
47
48         $this->setRestrictionsForTestRoles($shelf, ['view']);
49
50         $this->get($shelf->getUrl())
51             ->assertSee($shelf->name);
52     }
53
54     public function test_bookshelf_update_restriction()
55     {
56         $shelf = $this->entities->shelf();
57
58         $this->actingAs($this->user)
59             ->get($shelf->getUrl('/edit'))
60             ->assertSee('Edit Shelf');
61
62         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
63
64         $resp = $this->get($shelf->getUrl('/edit'))
65             ->assertRedirect('/');
66         $this->followRedirects($resp)->assertSee('You do not have permission');
67
68         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
69
70         $this->get($shelf->getUrl('/edit'))
71             ->assertOk();
72     }
73
74     public function test_bookshelf_delete_restriction()
75     {
76         $shelf = $this->entities->shelf();
77
78         $this->actingAs($this->user)
79             ->get($shelf->getUrl('/delete'))
80             ->assertSee('Delete Shelf');
81
82         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
83
84         $this->get($shelf->getUrl('/delete'))->assertRedirect('/');
85         $this->get('/')->assertSee('You do not have permission');
86
87         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
88
89         $this->get($shelf->getUrl('/delete'))
90             ->assertOk()
91             ->assertSee('Delete Shelf');
92     }
93
94     public function test_book_view_restriction()
95     {
96         $book = $this->entities->book();
97         $bookPage = $book->pages->first();
98         $bookChapter = $book->chapters->first();
99
100         $bookUrl = $book->getUrl();
101         $this->actingAs($this->user)
102             ->get($bookUrl)
103             ->assertOk();
104
105         $this->setRestrictionsForTestRoles($book, []);
106
107         $this->followingRedirects()->get($bookUrl)
108             ->assertSee('Book not found');
109         $this->followingRedirects()->get($bookPage->getUrl())
110             ->assertSee('Page not found');
111         $this->followingRedirects()->get($bookChapter->getUrl())
112             ->assertSee('Chapter not found');
113
114         $this->setRestrictionsForTestRoles($book, ['view']);
115
116         $this->get($bookUrl)
117             ->assertSee($book->name);
118         $this->get($bookPage->getUrl())
119             ->assertSee($bookPage->name);
120         $this->get($bookChapter->getUrl())
121             ->assertSee($bookChapter->name);
122     }
123
124     public function test_book_create_restriction()
125     {
126         $book = $this->entities->book();
127
128         $bookUrl = $book->getUrl();
129         $resp = $this->actingAs($this->viewer)->get($bookUrl);
130         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
131             ->assertElementNotContains('.actions', 'New Chapter');
132         $resp = $this->actingAs($this->user)->get($bookUrl);
133         $this->withHtml($resp)->assertElementContains('.actions', 'New Page')
134             ->assertElementContains('.actions', 'New Chapter');
135
136         $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']);
137
138         $this->get($bookUrl . '/create-chapter')->assertRedirect('/');
139         $this->get('/')->assertSee('You do not have permission');
140
141         $this->get($bookUrl . '/create-page')->assertRedirect('/');
142         $this->get('/')->assertSee('You do not have permission');
143
144         $resp = $this->get($bookUrl);
145         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
146             ->assertElementNotContains('.actions', 'New Chapter');
147
148         $this->setRestrictionsForTestRoles($book, ['view', 'create']);
149
150         $resp = $this->post($book->getUrl('/create-chapter'), [
151             'name'        => 'test chapter',
152             'description' => 'desc',
153         ]);
154         $resp->assertRedirect($book->getUrl('/chapter/test-chapter'));
155
156         $this->get($book->getUrl('/create-page'));
157         /** @var Page $page */
158         $page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
159         $resp = $this->post($page->getUrl(), [
160             'name' => 'test page',
161             'html' => 'test content',
162         ]);
163         $resp->assertRedirect($book->getUrl('/page/test-page'));
164
165         $resp = $this->get($bookUrl);
166         $this->withHtml($resp)->assertElementContains('.actions', 'New Page')
167             ->assertElementContains('.actions', 'New Chapter');
168     }
169
170     public function test_book_update_restriction()
171     {
172         $book = $this->entities->book();
173         $bookPage = $book->pages->first();
174         $bookChapter = $book->chapters->first();
175
176         $bookUrl = $book->getUrl();
177         $this->actingAs($this->user)
178             ->get($bookUrl . '/edit')
179             ->assertSee('Edit Book');
180
181         $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
182
183         $this->get($bookUrl . '/edit')->assertRedirect('/');
184         $this->get('/')->assertSee('You do not have permission');
185         $this->get($bookPage->getUrl() . '/edit')->assertRedirect('/');
186         $this->get('/')->assertSee('You do not have permission');
187         $this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/');
188         $this->get('/')->assertSee('You do not have permission');
189
190         $this->setRestrictionsForTestRoles($book, ['view', 'update']);
191
192         $this->get($bookUrl . '/edit')->assertOk();
193         $this->get($bookPage->getUrl() . '/edit')->assertOk();
194         $this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter');
195     }
196
197     public function test_book_delete_restriction()
198     {
199         $book = $this->entities->book();
200         $bookPage = $book->pages->first();
201         $bookChapter = $book->chapters->first();
202
203         $bookUrl = $book->getUrl();
204         $this->actingAs($this->user)->get($bookUrl . '/delete')
205             ->assertSee('Delete Book');
206
207         $this->setRestrictionsForTestRoles($book, ['view', 'update']);
208
209         $this->get($bookUrl . '/delete')->assertRedirect('/');
210         $this->get('/')->assertSee('You do not have permission');
211         $this->get($bookPage->getUrl() . '/delete')->assertRedirect('/');
212         $this->get('/')->assertSee('You do not have permission');
213         $this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/');
214         $this->get('/')->assertSee('You do not have permission');
215
216         $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
217
218         $this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book');
219         $this->get($bookPage->getUrl('/delete'))->assertOk()->assertSee('Delete Page');
220         $this->get($bookChapter->getUrl('/delete'))->assertSee('Delete Chapter');
221     }
222
223     public function test_chapter_view_restriction()
224     {
225         $chapter = $this->entities->chapter();
226         $chapterPage = $chapter->pages->first();
227
228         $chapterUrl = $chapter->getUrl();
229         $this->actingAs($this->user)->get($chapterUrl)->assertOk();
230
231         $this->setRestrictionsForTestRoles($chapter, []);
232
233         $this->followingRedirects()->get($chapterUrl)->assertSee('Chapter not found');
234         $this->followingRedirects()->get($chapterPage->getUrl())->assertSee('Page not found');
235
236         $this->setRestrictionsForTestRoles($chapter, ['view']);
237
238         $this->get($chapterUrl)->assertSee($chapter->name);
239         $this->get($chapterPage->getUrl())->assertSee($chapterPage->name);
240     }
241
242     public function test_chapter_create_restriction()
243     {
244         $chapter = $this->entities->chapter();
245
246         $chapterUrl = $chapter->getUrl();
247         $resp = $this->actingAs($this->user)->get($chapterUrl);
248         $this->withHtml($resp)->assertElementContains('.actions', 'New Page');
249
250         $this->setRestrictionsForTestRoles($chapter, ['view', 'delete', 'update']);
251
252         $this->get($chapterUrl . '/create-page')->assertRedirect('/');
253         $this->get('/')->assertSee('You do not have permission');
254         $this->withHtml($this->get($chapterUrl))->assertElementNotContains('.actions', 'New Page');
255
256         $this->setRestrictionsForTestRoles($chapter, ['view', 'create']);
257
258         $this->get($chapter->getUrl('/create-page'));
259         /** @var Page $page */
260         $page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
261         $resp = $this->post($page->getUrl(), [
262             'name' => 'test page',
263             'html' => 'test content',
264         ]);
265         $resp->assertRedirect($chapter->book->getUrl('/page/test-page'));
266
267         $this->withHtml($this->get($chapterUrl))->assertElementContains('.actions', 'New Page');
268     }
269
270     public function test_chapter_update_restriction()
271     {
272         $chapter = $this->entities->chapter();
273         $chapterPage = $chapter->pages->first();
274
275         $chapterUrl = $chapter->getUrl();
276         $this->actingAs($this->user)->get($chapterUrl . '/edit')
277             ->assertSee('Edit Chapter');
278
279         $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']);
280
281         $this->get($chapterUrl . '/edit')->assertRedirect('/');
282         $this->get('/')->assertSee('You do not have permission');
283         $this->get($chapterPage->getUrl() . '/edit')->assertRedirect('/');
284         $this->get('/')->assertSee('You do not have permission');
285
286         $this->setRestrictionsForTestRoles($chapter, ['view', 'update']);
287
288         $this->get($chapterUrl . '/edit')->assertOk()->assertSee('Edit Chapter');
289         $this->get($chapterPage->getUrl() . '/edit')->assertOk();
290     }
291
292     public function test_chapter_delete_restriction()
293     {
294         $chapter = $this->entities->chapter();
295         $chapterPage = $chapter->pages->first();
296
297         $chapterUrl = $chapter->getUrl();
298         $this->actingAs($this->user)
299             ->get($chapterUrl . '/delete')
300             ->assertSee('Delete Chapter');
301
302         $this->setRestrictionsForTestRoles($chapter, ['view', 'update']);
303
304         $this->get($chapterUrl . '/delete')->assertRedirect('/');
305         $this->get('/')->assertSee('You do not have permission');
306         $this->get($chapterPage->getUrl() . '/delete')->assertRedirect('/');
307         $this->get('/')->assertSee('You do not have permission');
308
309         $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']);
310
311         $this->get($chapterUrl . '/delete')->assertOk()->assertSee('Delete Chapter');
312         $this->get($chapterPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page');
313     }
314
315     public function test_page_view_restriction()
316     {
317         $page = $this->entities->page();
318
319         $pageUrl = $page->getUrl();
320         $this->actingAs($this->user)->get($pageUrl)->assertOk();
321
322         $this->setRestrictionsForTestRoles($page, ['update', 'delete']);
323
324         $this->get($pageUrl)->assertSee('Page not found');
325
326         $this->setRestrictionsForTestRoles($page, ['view']);
327
328         $this->get($pageUrl)->assertSee($page->name);
329     }
330
331     public function test_page_update_restriction()
332     {
333         $page = $this->entities->page();
334
335         $pageUrl = $page->getUrl();
336         $resp = $this->actingAs($this->user)
337             ->get($pageUrl . '/edit');
338         $this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]');
339
340         $this->setRestrictionsForTestRoles($page, ['view', 'delete']);
341
342         $this->get($pageUrl . '/edit')->assertRedirect('/');
343         $this->get('/')->assertSee('You do not have permission');
344
345         $this->setRestrictionsForTestRoles($page, ['view', 'update']);
346
347         $resp = $this->get($pageUrl . '/edit')
348             ->assertOk();
349         $this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]');
350     }
351
352     public function test_page_delete_restriction()
353     {
354         $page = $this->entities->page();
355
356         $pageUrl = $page->getUrl();
357         $this->actingAs($this->user)
358             ->get($pageUrl . '/delete')
359             ->assertSee('Delete Page');
360
361         $this->setRestrictionsForTestRoles($page, ['view', 'update']);
362
363         $this->get($pageUrl . '/delete')->assertRedirect('/');
364         $this->get('/')->assertSee('You do not have permission');
365
366         $this->setRestrictionsForTestRoles($page, ['view', 'delete']);
367
368         $this->get($pageUrl . '/delete')->assertOk()->assertSee('Delete Page');
369     }
370
371     protected function entityRestrictionFormTest(string $model, string $title, string $permission, string $roleId)
372     {
373         /** @var Entity $modelInstance */
374         $modelInstance = $model::query()->first();
375         $this->asAdmin()->get($modelInstance->getUrl('/permissions'))
376             ->assertSee($title);
377
378         $this->put($modelInstance->getUrl('/permissions'), [
379             'restricted'   => 'true',
380             'restrictions' => [
381                 $roleId => [
382                     $permission => 'true',
383                 ],
384             ],
385         ]);
386
387         $this->assertDatabaseHas($modelInstance->getTable(), ['id' => $modelInstance->id, 'restricted' => true]);
388         $this->assertDatabaseHas('entity_permissions', [
389             'restrictable_id'   => $modelInstance->id,
390             'restrictable_type' => $modelInstance->getMorphClass(),
391             'role_id'           => $roleId,
392             'action'            => $permission,
393         ]);
394     }
395
396     public function test_bookshelf_restriction_form()
397     {
398         $this->entityRestrictionFormTest(Bookshelf::class, 'Shelf Permissions', 'view', '2');
399     }
400
401     public function test_book_restriction_form()
402     {
403         $this->entityRestrictionFormTest(Book::class, 'Book Permissions', 'view', '2');
404     }
405
406     public function test_chapter_restriction_form()
407     {
408         $this->entityRestrictionFormTest(Chapter::class, 'Chapter Permissions', 'update', '2');
409     }
410
411     public function test_page_restriction_form()
412     {
413         $this->entityRestrictionFormTest(Page::class, 'Page Permissions', 'delete', '2');
414     }
415
416     public function test_restricted_pages_not_visible_in_book_navigation_on_pages()
417     {
418         $chapter = $this->entities->chapter();
419         $page = $chapter->pages->first();
420         $page2 = $chapter->pages[2];
421
422         $this->setRestrictionsForTestRoles($page, []);
423
424         $resp = $this->actingAs($this->user)->get($page2->getUrl());
425         $this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name);
426     }
427
428     public function test_restricted_pages_not_visible_in_book_navigation_on_chapters()
429     {
430         $chapter = $this->entities->chapter();
431         $page = $chapter->pages->first();
432
433         $this->setRestrictionsForTestRoles($page, []);
434
435         $resp = $this->actingAs($this->user)->get($chapter->getUrl());
436         $this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name);
437     }
438
439     public function test_restricted_pages_not_visible_on_chapter_pages()
440     {
441         $chapter = $this->entities->chapter();
442         $page = $chapter->pages->first();
443
444         $this->setRestrictionsForTestRoles($page, []);
445
446         $this->actingAs($this->user)
447             ->get($chapter->getUrl())
448             ->assertDontSee($page->name);
449     }
450
451     public function test_restricted_chapter_pages_not_visible_on_book_page()
452     {
453         $chapter = $this->entities->chapter();
454         $this->actingAs($this->user)
455             ->get($chapter->book->getUrl())
456             ->assertSee($chapter->pages->first()->name);
457
458         foreach ($chapter->pages as $page) {
459             $this->setRestrictionsForTestRoles($page, []);
460         }
461
462         $this->actingAs($this->user)
463             ->get($chapter->book->getUrl())
464             ->assertDontSee($chapter->pages->first()->name);
465     }
466
467     public function test_bookshelf_update_restriction_override()
468     {
469         $shelf = $this->entities->shelf();
470
471         $this->actingAs($this->viewer)
472             ->get($shelf->getUrl('/edit'))
473             ->assertDontSee('Edit Book');
474
475         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
476
477         $this->get($shelf->getUrl('/edit'))->assertRedirect('/');
478         $this->get('/')->assertSee('You do not have permission');
479
480         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
481
482         $this->get($shelf->getUrl('/edit'))->assertOk();
483     }
484
485     public function test_bookshelf_delete_restriction_override()
486     {
487         $shelf = $this->entities->shelf();
488
489         $this->actingAs($this->viewer)
490             ->get($shelf->getUrl('/delete'))
491             ->assertDontSee('Delete Book');
492
493         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']);
494
495         $this->get($shelf->getUrl('/delete'))->assertRedirect('/');
496         $this->get('/')->assertSee('You do not have permission');
497
498         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']);
499
500         $this->get($shelf->getUrl('/delete'))->assertOk()->assertSee('Delete Shelf');
501     }
502
503     public function test_book_create_restriction_override()
504     {
505         $book = $this->entities->book();
506
507         $bookUrl = $book->getUrl();
508         $resp = $this->actingAs($this->viewer)->get($bookUrl);
509         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
510             ->assertElementNotContains('.actions', 'New Chapter');
511
512         $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']);
513
514         $this->get($bookUrl . '/create-chapter')->assertRedirect('/');
515         $this->get('/')->assertSee('You do not have permission');
516         $this->get($bookUrl . '/create-page')->assertRedirect('/');
517         $this->get('/')->assertSee('You do not have permission');
518         $resp = $this->get($bookUrl);
519         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page')
520             ->assertElementNotContains('.actions', 'New Chapter');
521
522         $this->setRestrictionsForTestRoles($book, ['view', 'create']);
523
524         $resp = $this->post($book->getUrl('/create-chapter'), [
525             'name'        => 'test chapter',
526             'description' => 'test desc',
527         ]);
528         $resp->assertRedirect($book->getUrl('/chapter/test-chapter'));
529
530         $this->get($book->getUrl('/create-page'));
531         /** @var Page $page */
532         $page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
533         $resp = $this->post($page->getUrl(), [
534             'name' => 'test page',
535             'html' => 'test desc',
536         ]);
537         $resp->assertRedirect($book->getUrl('/page/test-page'));
538
539         $resp = $this->get($bookUrl);
540         $this->withHtml($resp)->assertElementContains('.actions', 'New Page')
541             ->assertElementContains('.actions', 'New Chapter');
542     }
543
544     public function test_book_update_restriction_override()
545     {
546         $book = $this->entities->book();
547         $bookPage = $book->pages->first();
548         $bookChapter = $book->chapters->first();
549
550         $bookUrl = $book->getUrl();
551         $this->actingAs($this->viewer)->get($bookUrl . '/edit')
552             ->assertDontSee('Edit Book');
553
554         $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
555
556         $this->get($bookUrl . '/edit')->assertRedirect('/');
557         $this->get('/')->assertSee('You do not have permission');
558         $this->get($bookPage->getUrl() . '/edit')->assertRedirect('/');
559         $this->get('/')->assertSee('You do not have permission');
560         $this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/');
561         $this->get('/')->assertSee('You do not have permission');
562
563         $this->setRestrictionsForTestRoles($book, ['view', 'update']);
564
565         $this->get($bookUrl . '/edit')->assertOk();
566         $this->get($bookPage->getUrl() . '/edit')->assertOk();
567         $this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter');
568     }
569
570     public function test_book_delete_restriction_override()
571     {
572         $book = $this->entities->book();
573         $bookPage = $book->pages->first();
574         $bookChapter = $book->chapters->first();
575
576         $bookUrl = $book->getUrl();
577         $this->actingAs($this->viewer)
578             ->get($bookUrl . '/delete')
579             ->assertDontSee('Delete Book');
580
581         $this->setRestrictionsForTestRoles($book, ['view', 'update']);
582
583         $this->get($bookUrl . '/delete')->assertRedirect('/');
584         $this->get('/')->assertSee('You do not have permission');
585         $this->get($bookPage->getUrl() . '/delete')->assertRedirect('/');
586         $this->get('/')->assertSee('You do not have permission');
587         $this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/');
588         $this->get('/')->assertSee('You do not have permission');
589
590         $this->setRestrictionsForTestRoles($book, ['view', 'delete']);
591
592         $this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book');
593         $this->get($bookPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page');
594         $this->get($bookChapter->getUrl() . '/delete')->assertSee('Delete Chapter');
595     }
596
597     public function test_page_visible_if_has_permissions_when_book_not_visible()
598     {
599         $book = $this->entities->book();
600         $bookChapter = $book->chapters->first();
601         $bookPage = $bookChapter->pages->first();
602
603         foreach ([$book, $bookChapter, $bookPage] as $entity) {
604             $entity->name = Str::random(24);
605             $entity->save();
606         }
607
608         $this->setRestrictionsForTestRoles($book, []);
609         $this->setRestrictionsForTestRoles($bookPage, ['view']);
610
611         $this->actingAs($this->viewer);
612         $resp = $this->get($bookPage->getUrl());
613         $resp->assertOk();
614         $resp->assertSee($bookPage->name);
615         $resp->assertDontSee(substr($book->name, 0, 15));
616         $resp->assertDontSee(substr($bookChapter->name, 0, 15));
617     }
618
619     public function test_book_sort_view_permission()
620     {
621         /** @var Book $firstBook */
622         $firstBook = Book::query()->first();
623         /** @var Book $secondBook */
624         $secondBook = Book::query()->find(2);
625
626         $this->setRestrictionsForTestRoles($firstBook, ['view', 'update']);
627         $this->setRestrictionsForTestRoles($secondBook, ['view']);
628
629         // Test sort page visibility
630         $this->actingAs($this->user)->get($secondBook->getUrl('/sort'))->assertRedirect('/');
631         $this->get('/')->assertSee('You do not have permission');
632
633         // Check sort page on first book
634         $this->actingAs($this->user)->get($firstBook->getUrl('/sort'));
635     }
636
637     public function test_can_create_page_if_chapter_has_permissions_when_book_not_visible()
638     {
639         $book = $this->entities->book();
640         $this->setRestrictionsForTestRoles($book, []);
641         $bookChapter = $book->chapters->first();
642         $this->setRestrictionsForTestRoles($bookChapter, ['view']);
643
644         $this->actingAs($this->user)->get($bookChapter->getUrl())
645             ->assertDontSee('New Page');
646
647         $this->setRestrictionsForTestRoles($bookChapter, ['view', 'create']);
648
649         $this->get($bookChapter->getUrl('/create-page'));
650         /** @var Page $page */
651         $page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
652         $resp = $this->post($page->getUrl(), [
653             'name' => 'test page',
654             'html' => 'test content',
655         ]);
656         $resp->assertRedirect($book->getUrl('/page/test-page'));
657     }
658 }