1 <?php namespace Tests\Permissions;
3 use BookStack\Actions\Comment;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Auth\Role;
10 use BookStack\Uploads\Image;
11 use Laravel\BrowserKitTesting\HttpException;
12 use Tests\BrowserKitTest;
14 class RolesTest extends BrowserKitTest
18 public function setUp(): void
21 $this->user = $this->getViewer();
24 public function test_admin_can_see_settings()
26 $this->asAdmin()->visit('/settings')->see('Settings');
29 public function test_cannot_delete_admin_role()
31 $adminRole = Role::getRole('admin');
32 $deletePageUrl = '/settings/roles/delete/' . $adminRole->id;
33 $this->asAdmin()->visit($deletePageUrl)
35 ->seePageIs($deletePageUrl)
36 ->see('cannot be deleted');
39 public function test_role_cannot_be_deleted_if_default()
41 $newRole = $this->createNewRole();
42 $this->setSettings(['registration-role' => $newRole->id]);
44 $deletePageUrl = '/settings/roles/delete/' . $newRole->id;
45 $this->asAdmin()->visit($deletePageUrl)
47 ->seePageIs($deletePageUrl)
48 ->see('cannot be deleted');
51 public function test_role_create_update_delete_flow()
53 $testRoleName = 'Test Role';
54 $testRoleDesc = 'a little test description';
55 $testRoleUpdateName = 'An Super Updated role';
58 $this->asAdmin()->visit('/settings')
60 ->seePageIs('/settings/roles')
61 ->click('Create New Role')
62 ->type('Test Role', 'display_name')
63 ->type('A little test description', 'description')
65 ->seeInDatabase('roles', ['display_name' => $testRoleName, 'description' => $testRoleDesc])
66 ->seePageIs('/settings/roles');
68 $this->asAdmin()->visit('/settings/roles')
70 ->click($testRoleName)
71 ->type($testRoleUpdateName, '#display_name')
73 ->seeInDatabase('roles', ['display_name' => $testRoleUpdateName, 'description' => $testRoleDesc])
74 ->seePageIs('/settings/roles');
76 $this->asAdmin()->visit('/settings/roles')
77 ->click($testRoleUpdateName)
78 ->click('Delete Role')
79 ->see($testRoleUpdateName)
81 ->seePageIs('/settings/roles')
82 ->dontSee($testRoleUpdateName);
85 public function test_admin_role_cannot_be_removed_if_last_admin()
87 $adminRole = Role::where('system_name', '=', 'admin')->first();
88 $adminUser = $this->getAdmin();
89 $adminRole->users()->where('id', '!=', $adminUser->id)->delete();
90 $this->assertEquals($adminRole->users()->count(), 1);
92 $viewerRole = $this->getViewer()->roles()->first();
94 $editUrl = '/settings/users/' . $adminUser->id;
95 $this->actingAs($adminUser)->put($editUrl, [
96 'name' => $adminUser->name,
97 'email' => $adminUser->email,
99 'viewer' => strval($viewerRole->id),
101 ])->followRedirects();
103 $this->seePageIs($editUrl);
104 $this->see('This user is the only user assigned to the administrator role');
107 public function test_migrate_users_on_delete_works()
109 $roleA = Role::query()->create(['display_name' => 'Delete Test A']);
110 $roleB = Role::query()->create(['display_name' => 'Delete Test B']);
111 $this->user->attachRole($roleB);
113 $this->assertCount(0, $roleA->users()->get());
114 $this->assertCount(1, $roleB->users()->get());
116 $deletePage = $this->asAdmin()->get("/settings/roles/delete/{$roleB->id}");
117 $deletePage->seeElement('select[name=migrate_role_id]');
118 $this->asAdmin()->delete("/settings/roles/delete/{$roleB->id}", [
119 'migrate_role_id' => $roleA->id,
122 $this->assertCount(1, $roleA->users()->get());
123 $this->assertEquals($this->user->id, $roleA->users()->first()->id);
126 public function test_manage_user_permission()
128 $this->actingAs($this->user)->visit('/settings/users')
130 $this->giveUserPermissions($this->user, ['users-manage']);
131 $this->actingAs($this->user)->visit('/settings/users')
132 ->seePageIs('/settings/users');
135 public function test_manage_users_permission_shows_link_in_header_if_does_not_have_settings_manage_permision()
137 $usersLink = 'href="'.url('/settings/users') . '"';
138 $this->actingAs($this->user)->visit('/')->dontSee($usersLink);
139 $this->giveUserPermissions($this->user, ['users-manage']);
140 $this->actingAs($this->user)->visit('/')->see($usersLink);
141 $this->giveUserPermissions($this->user, ['settings-manage', 'users-manage']);
142 $this->actingAs($this->user)->visit('/')->dontSee($usersLink);
145 public function test_user_cannot_change_email_unless_they_have_manage_users_permission()
147 $userProfileUrl = '/settings/users/' . $this->user->id;
148 $originalEmail = $this->user->email;
149 $this->actingAs($this->user);
151 $this->visit($userProfileUrl)
153 ->seeElement('input[name=email][disabled]');
154 $this->put($userProfileUrl, [
155 'name' => 'my_new_name',
158 $this->seeInDatabase('users', [
159 'id' => $this->user->id,
160 'email' => $originalEmail,
161 'name' => 'my_new_name',
164 $this->giveUserPermissions($this->user, ['users-manage']);
166 $this->visit($userProfileUrl)
168 ->dontSeeElement('input[name=email][disabled]')
169 ->seeElement('input[name=email]');
170 $this->put($userProfileUrl, [
171 'name' => 'my_new_name_2',
175 $this->seeInDatabase('users', [
176 'id' => $this->user->id,
178 'name' => 'my_new_name_2',
182 public function test_user_roles_manage_permission()
184 $this->actingAs($this->user)->visit('/settings/roles')
185 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
186 $this->giveUserPermissions($this->user, ['user-roles-manage']);
187 $this->actingAs($this->user)->visit('/settings/roles')
188 ->seePageIs('/settings/roles')->click('Admin')
192 public function test_settings_manage_permission()
194 $this->actingAs($this->user)->visit('/settings')
196 $this->giveUserPermissions($this->user, ['settings-manage']);
197 $this->actingAs($this->user)->visit('/settings')
198 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
201 public function test_restrictions_manage_all_permission()
203 $page = Page::take(1)->get()->first();
204 $this->actingAs($this->user)->visit($page->getUrl())
205 ->dontSee('Permissions')
206 ->visit($page->getUrl() . '/permissions')
208 $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
209 $this->actingAs($this->user)->visit($page->getUrl())
211 ->click('Permissions')
212 ->see('Page Permissions')->seePageIs($page->getUrl() . '/permissions');
215 public function test_restrictions_manage_own_permission()
217 $otherUsersPage = Page::first();
218 $content = $this->createEntityChainBelongingToUser($this->user);
220 // Set a different creator on the page we're checking to ensure
221 // that the owner fields are checked
222 $page = $content['page']; /** @var Page $page */
223 $page->created_by = $otherUsersPage->id;
224 $page->owned_by = $this->user->id;
227 // Check can't restrict other's content
228 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
229 ->dontSee('Permissions')
230 ->visit($otherUsersPage->getUrl() . '/permissions')
232 // Check can't restrict own content
233 $this->actingAs($this->user)->visit($page->getUrl())
234 ->dontSee('Permissions')
235 ->visit($page->getUrl() . '/permissions')
238 $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
240 // Check can't restrict other's content
241 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
242 ->dontSee('Permissions')
243 ->visit($otherUsersPage->getUrl() . '/permissions')
245 // Check can restrict own content
246 $this->actingAs($this->user)->visit($page->getUrl())
248 ->click('Permissions')
249 ->seePageIs($page->getUrl() . '/permissions');
253 * Check a standard entity access permission
254 * @param string $permission
255 * @param array $accessUrls Urls that are only accessible after having the permission
256 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
258 private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
260 foreach ($accessUrls as $url) {
261 $this->actingAs($this->user)->visit($url)
264 foreach ($visibles as $url => $text) {
265 $this->actingAs($this->user)->visit($url)
266 ->dontSeeInElement('.action-buttons',$text);
269 $this->giveUserPermissions($this->user, [$permission]);
271 foreach ($accessUrls as $url) {
272 $this->actingAs($this->user)->visit($url)
275 foreach ($visibles as $url => $text) {
276 $this->actingAs($this->user)->visit($url)
281 public function test_bookshelves_create_all_permissions()
283 $this->checkAccessPermission('bookshelf-create-all', [
286 '/shelves' => 'New Shelf'
289 $this->visit('/create-shelf')
290 ->type('test shelf', 'name')
291 ->type('shelf desc', 'description')
292 ->press('Save Shelf')
293 ->seePageIs('/shelves/test-shelf');
296 public function test_bookshelves_edit_own_permission()
298 $otherShelf = Bookshelf::first();
299 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
300 $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
301 $this->regenEntityPermissions($ownShelf);
303 $this->checkAccessPermission('bookshelf-update-own', [
304 $ownShelf->getUrl('/edit')
306 $ownShelf->getUrl() => 'Edit'
309 $this->visit($otherShelf->getUrl())
310 ->dontSeeInElement('.action-buttons', 'Edit')
311 ->visit($otherShelf->getUrl('/edit'))
315 public function test_bookshelves_edit_all_permission()
317 $otherShelf = Bookshelf::first();
318 $this->checkAccessPermission('bookshelf-update-all', [
319 $otherShelf->getUrl('/edit')
321 $otherShelf->getUrl() => 'Edit'
325 public function test_bookshelves_delete_own_permission()
327 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
328 $otherShelf = Bookshelf::first();
329 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
330 $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
331 $this->regenEntityPermissions($ownShelf);
333 $this->checkAccessPermission('bookshelf-delete-own', [
334 $ownShelf->getUrl('/delete')
336 $ownShelf->getUrl() => 'Delete'
339 $this->visit($otherShelf->getUrl())
340 ->dontSeeInElement('.action-buttons', 'Delete')
341 ->visit($otherShelf->getUrl('/delete'))
343 $this->visit($ownShelf->getUrl())->visit($ownShelf->getUrl('/delete'))
345 ->seePageIs('/shelves')
346 ->dontSee($ownShelf->name);
349 public function test_bookshelves_delete_all_permission()
351 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
352 $otherShelf = Bookshelf::first();
353 $this->checkAccessPermission('bookshelf-delete-all', [
354 $otherShelf->getUrl('/delete')
356 $otherShelf->getUrl() => 'Delete'
359 $this->visit($otherShelf->getUrl())->visit($otherShelf->getUrl('/delete'))
361 ->seePageIs('/shelves')
362 ->dontSee($otherShelf->name);
365 public function test_books_create_all_permissions()
367 $this->checkAccessPermission('book-create-all', [
370 '/books' => 'Create New Book'
373 $this->visit('/create-book')
374 ->type('test book', 'name')
375 ->type('book desc', 'description')
377 ->seePageIs('/books/test-book');
380 public function test_books_edit_own_permission()
382 $otherBook = Book::take(1)->get()->first();
383 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
384 $this->checkAccessPermission('book-update-own', [
385 $ownBook->getUrl() . '/edit'
387 $ownBook->getUrl() => 'Edit'
390 $this->visit($otherBook->getUrl())
391 ->dontSeeInElement('.action-buttons', 'Edit')
392 ->visit($otherBook->getUrl() . '/edit')
396 public function test_books_edit_all_permission()
398 $otherBook = Book::take(1)->get()->first();
399 $this->checkAccessPermission('book-update-all', [
400 $otherBook->getUrl() . '/edit'
402 $otherBook->getUrl() => 'Edit'
406 public function test_books_delete_own_permission()
408 $this->giveUserPermissions($this->user, ['book-update-all']);
409 $otherBook = Book::take(1)->get()->first();
410 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
411 $this->checkAccessPermission('book-delete-own', [
412 $ownBook->getUrl() . '/delete'
414 $ownBook->getUrl() => 'Delete'
417 $this->visit($otherBook->getUrl())
418 ->dontSeeInElement('.action-buttons', 'Delete')
419 ->visit($otherBook->getUrl() . '/delete')
421 $this->visit($ownBook->getUrl())->visit($ownBook->getUrl() . '/delete')
423 ->seePageIs('/books')
424 ->dontSee($ownBook->name);
427 public function test_books_delete_all_permission()
429 $this->giveUserPermissions($this->user, ['book-update-all']);
430 $otherBook = Book::take(1)->get()->first();
431 $this->checkAccessPermission('book-delete-all', [
432 $otherBook->getUrl() . '/delete'
434 $otherBook->getUrl() => 'Delete'
437 $this->visit($otherBook->getUrl())->visit($otherBook->getUrl() . '/delete')
439 ->seePageIs('/books')
440 ->dontSee($otherBook->name);
443 public function test_chapter_create_own_permissions()
445 $book = Book::take(1)->get()->first();
446 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
447 $this->checkAccessPermission('chapter-create-own', [
448 $ownBook->getUrl('/create-chapter')
450 $ownBook->getUrl() => 'New Chapter'
453 $this->visit($ownBook->getUrl('/create-chapter'))
454 ->type('test chapter', 'name')
455 ->type('chapter desc', 'description')
456 ->press('Save Chapter')
457 ->seePageIs($ownBook->getUrl('/chapter/test-chapter'));
459 $this->visit($book->getUrl())
460 ->dontSeeInElement('.action-buttons', 'New Chapter')
461 ->visit($book->getUrl('/create-chapter'))
465 public function test_chapter_create_all_permissions()
467 $book = Book::take(1)->get()->first();
468 $this->checkAccessPermission('chapter-create-all', [
469 $book->getUrl('/create-chapter')
471 $book->getUrl() => 'New Chapter'
474 $this->visit($book->getUrl('/create-chapter'))
475 ->type('test chapter', 'name')
476 ->type('chapter desc', 'description')
477 ->press('Save Chapter')
478 ->seePageIs($book->getUrl('/chapter/test-chapter'));
481 public function test_chapter_edit_own_permission()
483 $otherChapter = Chapter::take(1)->get()->first();
484 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
485 $this->checkAccessPermission('chapter-update-own', [
486 $ownChapter->getUrl() . '/edit'
488 $ownChapter->getUrl() => 'Edit'
491 $this->visit($otherChapter->getUrl())
492 ->dontSeeInElement('.action-buttons', 'Edit')
493 ->visit($otherChapter->getUrl() . '/edit')
497 public function test_chapter_edit_all_permission()
499 $otherChapter = Chapter::take(1)->get()->first();
500 $this->checkAccessPermission('chapter-update-all', [
501 $otherChapter->getUrl() . '/edit'
503 $otherChapter->getUrl() => 'Edit'
507 public function test_chapter_delete_own_permission()
509 $this->giveUserPermissions($this->user, ['chapter-update-all']);
510 $otherChapter = Chapter::take(1)->get()->first();
511 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
512 $this->checkAccessPermission('chapter-delete-own', [
513 $ownChapter->getUrl() . '/delete'
515 $ownChapter->getUrl() => 'Delete'
518 $bookUrl = $ownChapter->book->getUrl();
519 $this->visit($otherChapter->getUrl())
520 ->dontSeeInElement('.action-buttons', 'Delete')
521 ->visit($otherChapter->getUrl() . '/delete')
523 $this->visit($ownChapter->getUrl())->visit($ownChapter->getUrl() . '/delete')
525 ->seePageIs($bookUrl)
526 ->dontSeeInElement('.book-content', $ownChapter->name);
529 public function test_chapter_delete_all_permission()
531 $this->giveUserPermissions($this->user, ['chapter-update-all']);
532 $otherChapter = Chapter::take(1)->get()->first();
533 $this->checkAccessPermission('chapter-delete-all', [
534 $otherChapter->getUrl() . '/delete'
536 $otherChapter->getUrl() => 'Delete'
539 $bookUrl = $otherChapter->book->getUrl();
540 $this->visit($otherChapter->getUrl())->visit($otherChapter->getUrl() . '/delete')
542 ->seePageIs($bookUrl)
543 ->dontSeeInElement('.book-content', $otherChapter->name);
546 public function test_page_create_own_permissions()
548 $book = Book::first();
549 $chapter = Chapter::first();
551 $entities = $this->createEntityChainBelongingToUser($this->user);
552 $ownBook = $entities['book'];
553 $ownChapter = $entities['chapter'];
555 $createUrl = $ownBook->getUrl('/create-page');
556 $createUrlChapter = $ownChapter->getUrl('/create-page');
557 $accessUrls = [$createUrl, $createUrlChapter];
559 foreach ($accessUrls as $url) {
560 $this->actingAs($this->user)->visit($url)
564 $this->checkAccessPermission('page-create-own', [], [
565 $ownBook->getUrl() => 'New Page',
566 $ownChapter->getUrl() => 'New Page'
569 $this->giveUserPermissions($this->user, ['page-create-own']);
571 foreach ($accessUrls as $index => $url) {
572 $this->actingAs($this->user)->visit($url);
573 $expectedUrl = Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
574 $this->seePageIs($expectedUrl);
577 $this->visit($createUrl)
578 ->type('test page', 'name')
579 ->type('page desc', 'html')
581 ->seePageIs($ownBook->getUrl('/page/test-page'));
583 $this->visit($book->getUrl())
584 ->dontSeeInElement('.action-buttons', 'New Page')
585 ->visit($book->getUrl() . '/create-page')
587 $this->visit($chapter->getUrl())
588 ->dontSeeInElement('.action-buttons', 'New Page')
589 ->visit($chapter->getUrl() . '/create-page')
593 public function test_page_create_all_permissions()
595 $book = Book::take(1)->get()->first();
596 $chapter = Chapter::take(1)->get()->first();
597 $baseUrl = $book->getUrl() . '/page';
598 $createUrl = $book->getUrl('/create-page');
600 $createUrlChapter = $chapter->getUrl('/create-page');
601 $accessUrls = [$createUrl, $createUrlChapter];
603 foreach ($accessUrls as $url) {
604 $this->actingAs($this->user)->visit($url)
608 $this->checkAccessPermission('page-create-all', [], [
609 $book->getUrl() => 'New Page',
610 $chapter->getUrl() => 'New Page'
613 $this->giveUserPermissions($this->user, ['page-create-all']);
615 foreach ($accessUrls as $index => $url) {
616 $this->actingAs($this->user)->visit($url);
617 $expectedUrl = Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
618 $this->seePageIs($expectedUrl);
621 $this->visit($createUrl)
622 ->type('test page', 'name')
623 ->type('page desc', 'html')
625 ->seePageIs($book->getUrl('/page/test-page'));
627 $this->visit($chapter->getUrl('/create-page'))
628 ->type('new test page', 'name')
629 ->type('page desc', 'html')
631 ->seePageIs($book->getUrl('/page/new-test-page'));
634 public function test_page_edit_own_permission()
636 $otherPage = Page::take(1)->get()->first();
637 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
638 $this->checkAccessPermission('page-update-own', [
639 $ownPage->getUrl() . '/edit'
641 $ownPage->getUrl() => 'Edit'
644 $this->visit($otherPage->getUrl())
645 ->dontSeeInElement('.action-buttons', 'Edit')
646 ->visit($otherPage->getUrl() . '/edit')
650 public function test_page_edit_all_permission()
652 $otherPage = Page::take(1)->get()->first();
653 $this->checkAccessPermission('page-update-all', [
654 $otherPage->getUrl() . '/edit'
656 $otherPage->getUrl() => 'Edit'
660 public function test_page_delete_own_permission()
662 $this->giveUserPermissions($this->user, ['page-update-all']);
663 $otherPage = Page::take(1)->get()->first();
664 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
665 $this->checkAccessPermission('page-delete-own', [
666 $ownPage->getUrl() . '/delete'
668 $ownPage->getUrl() => 'Delete'
671 $parent = $ownPage->chapter ?? $ownPage->book;
672 $this->visit($otherPage->getUrl())
673 ->dontSeeInElement('.action-buttons', 'Delete')
674 ->visit($otherPage->getUrl() . '/delete')
676 $this->visit($ownPage->getUrl())->visit($ownPage->getUrl() . '/delete')
678 ->seePageIs($parent->getUrl())
679 ->dontSeeInElement('.book-content', $ownPage->name);
682 public function test_page_delete_all_permission()
684 $this->giveUserPermissions($this->user, ['page-update-all']);
685 $otherPage = Page::take(1)->get()->first();
686 $this->checkAccessPermission('page-delete-all', [
687 $otherPage->getUrl() . '/delete'
689 $otherPage->getUrl() => 'Delete'
692 $parent = $otherPage->chapter ?? $otherPage->book;
693 $this->visit($otherPage->getUrl())->visit($otherPage->getUrl() . '/delete')
695 ->seePageIs($parent->getUrl())
696 ->dontSeeInElement('.book-content', $otherPage->name);
699 public function test_public_role_visible_in_user_edit_screen()
701 $user = User::first();
702 $adminRole = Role::getSystemRole('admin');
703 $publicRole = Role::getSystemRole('public');
704 $this->asAdmin()->visit('/settings/users/' . $user->id)
705 ->seeElement('[name="roles['.$adminRole->id.']"]')
706 ->seeElement('[name="roles['.$publicRole->id.']"]');
709 public function test_public_role_visible_in_role_listing()
711 $this->asAdmin()->visit('/settings/roles')
716 public function test_public_role_visible_in_default_role_setting()
718 $this->asAdmin()->visit('/settings')
719 ->seeElement('[data-system-role-name="admin"]')
720 ->seeElement('[data-system-role-name="public"]');
723 public function test_public_role_not_deleteable()
725 $this->asAdmin()->visit('/settings/roles')
728 ->click('Delete Role')
731 ->see('Cannot be deleted');
734 public function test_image_delete_own_permission()
736 $this->giveUserPermissions($this->user, ['image-update-all']);
737 $page = Page::first();
738 $image = factory(Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]);
740 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
741 ->seeStatusCode(403);
743 $this->giveUserPermissions($this->user, ['image-delete-own']);
745 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
747 ->dontSeeInDatabase('images', ['id' => $image->id]);
750 public function test_image_delete_all_permission()
752 $this->giveUserPermissions($this->user, ['image-update-all']);
753 $admin = $this->getAdmin();
754 $page = Page::first();
755 $image = factory(Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
757 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
758 ->seeStatusCode(403);
760 $this->giveUserPermissions($this->user, ['image-delete-own']);
762 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
763 ->seeStatusCode(403);
765 $this->giveUserPermissions($this->user, ['image-delete-all']);
767 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
769 ->dontSeeInDatabase('images', ['id' => $image->id]);
772 public function test_role_permission_removal()
774 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
775 $page = Page::first();
776 $viewerRole = Role::getRole('viewer');
777 $viewer = $this->getViewer();
778 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(200);
780 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
781 'display_name' => $viewerRole->display_name,
782 'description' => $viewerRole->description,
784 ])->assertResponseStatus(302);
786 $this->expectException(HttpException::class);
787 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(404);
790 public function test_empty_state_actions_not_visible_without_permission()
792 $admin = $this->getAdmin();
794 $book = factory(Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
795 $this->updateEntityPermissions($book);
796 $this->actingAs($this->getViewer())->visit($book->getUrl())
797 ->dontSee('Create a new page')
798 ->dontSee('Add a chapter');
801 $chapter = factory(Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
802 $this->updateEntityPermissions($chapter);
803 $this->actingAs($this->getViewer())->visit($chapter->getUrl())
804 ->dontSee('Create a new page')
805 ->dontSee('Sort the current book');
808 public function test_comment_create_permission () {
809 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
811 $this->actingAs($this->user)->addComment($ownPage);
813 $this->assertResponseStatus(403);
815 $this->giveUserPermissions($this->user, ['comment-create-all']);
817 $this->actingAs($this->user)->addComment($ownPage);
818 $this->assertResponseStatus(200);
822 public function test_comment_update_own_permission () {
823 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
824 $this->giveUserPermissions($this->user, ['comment-create-all']);
825 $commentId = $this->actingAs($this->user)->addComment($ownPage);
827 // no comment-update-own
828 $this->actingAs($this->user)->updateComment($commentId);
829 $this->assertResponseStatus(403);
831 $this->giveUserPermissions($this->user, ['comment-update-own']);
833 // now has comment-update-own
834 $this->actingAs($this->user)->updateComment($commentId);
835 $this->assertResponseStatus(200);
838 public function test_comment_update_all_permission () {
839 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
840 $commentId = $this->asAdmin()->addComment($ownPage);
842 // no comment-update-all
843 $this->actingAs($this->user)->updateComment($commentId);
844 $this->assertResponseStatus(403);
846 $this->giveUserPermissions($this->user, ['comment-update-all']);
848 // now has comment-update-all
849 $this->actingAs($this->user)->updateComment($commentId);
850 $this->assertResponseStatus(200);
853 public function test_comment_delete_own_permission () {
854 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
855 $this->giveUserPermissions($this->user, ['comment-create-all']);
856 $commentId = $this->actingAs($this->user)->addComment($ownPage);
858 // no comment-delete-own
859 $this->actingAs($this->user)->deleteComment($commentId);
860 $this->assertResponseStatus(403);
862 $this->giveUserPermissions($this->user, ['comment-delete-own']);
864 // now has comment-update-own
865 $this->actingAs($this->user)->deleteComment($commentId);
866 $this->assertResponseStatus(200);
869 public function test_comment_delete_all_permission () {
870 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
871 $commentId = $this->asAdmin()->addComment($ownPage);
873 // no comment-delete-all
874 $this->actingAs($this->user)->deleteComment($commentId);
875 $this->assertResponseStatus(403);
877 $this->giveUserPermissions($this->user, ['comment-delete-all']);
879 // now has comment-delete-all
880 $this->actingAs($this->user)->deleteComment($commentId);
881 $this->assertResponseStatus(200);
884 private function addComment($page) {
885 $comment = factory(Comment::class)->make();
886 $url = "/comment/$page->id";
888 'text' => $comment->text,
889 'html' => $comment->html
892 $this->postJson($url, $request);
893 $comment = $page->comments()->first();
894 return $comment === null ? null : $comment->id;
897 private function updateComment($commentId) {
898 $comment = factory(Comment::class)->make();
899 $url = "/comment/$commentId";
901 'text' => $comment->text,
902 'html' => $comment->html
905 return $this->putJson($url, $request);
908 private function deleteComment($commentId) {
909 $url = '/comment/' . $commentId;
910 return $this->json('DELETE', $url);