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