3 use BookStack\Entities\Bookshelf;
4 use BookStack\Entities\Page;
5 use BookStack\Auth\Permissions\PermissionsRepo;
6 use BookStack\Auth\Role;
7 use Laravel\BrowserKitTesting\HttpException;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10 class RolesTest extends BrowserKitTest
14 public function setUp()
17 $this->user = $this->getViewer();
20 public function test_admin_can_see_settings()
22 $this->asAdmin()->visit('/settings')->see('Settings');
25 public function test_cannot_delete_admin_role()
27 $adminRole = \BookStack\Auth\Role::getRole('admin');
28 $deletePageUrl = '/settings/roles/delete/' . $adminRole->id;
29 $this->asAdmin()->visit($deletePageUrl)
31 ->seePageIs($deletePageUrl)
32 ->see('cannot be deleted');
35 public function test_role_cannot_be_deleted_if_default()
37 $newRole = $this->createNewRole();
38 $this->setSettings(['registration-role' => $newRole->id]);
40 $deletePageUrl = '/settings/roles/delete/' . $newRole->id;
41 $this->asAdmin()->visit($deletePageUrl)
43 ->seePageIs($deletePageUrl)
44 ->see('cannot be deleted');
47 public function test_role_create_update_delete_flow()
49 $testRoleName = 'Test Role';
50 $testRoleDesc = 'a little test description';
51 $testRoleUpdateName = 'An Super Updated role';
54 $this->asAdmin()->visit('/settings')
56 ->seePageIs('/settings/roles')
57 ->click('Create New Role')
58 ->type('Test Role', 'display_name')
59 ->type('A little test description', 'description')
61 ->seeInDatabase('roles', ['display_name' => $testRoleName, 'name' => 'test-role', 'description' => $testRoleDesc])
62 ->seePageIs('/settings/roles');
64 $this->asAdmin()->visit('/settings/roles')
66 ->click($testRoleName)
67 ->type($testRoleUpdateName, '#display_name')
69 ->seeInDatabase('roles', ['display_name' => $testRoleUpdateName, 'name' => 'test-role', 'description' => $testRoleDesc])
70 ->seePageIs('/settings/roles');
72 $this->asAdmin()->visit('/settings/roles')
73 ->click($testRoleUpdateName)
74 ->click('Delete Role')
75 ->see($testRoleUpdateName)
77 ->seePageIs('/settings/roles')
78 ->dontSee($testRoleUpdateName);
81 public function test_admin_role_cannot_be_removed_if_last_admin()
83 $adminRole = Role::where('system_name', '=', 'admin')->first();
84 $adminUser = $this->getAdmin();
85 $adminRole->users()->where('id', '!=', $adminUser->id)->delete();
86 $this->assertEquals($adminRole->users()->count(), 1);
88 $viewerRole = $this->getViewer()->roles()->first();
90 $editUrl = '/settings/users/' . $adminUser->id;
91 $this->actingAs($adminUser)->put($editUrl, [
92 'name' => $adminUser->name,
93 'email' => $adminUser->email,
95 'viewer' => strval($viewerRole->id),
97 ])->followRedirects();
99 $this->seePageIs($editUrl);
100 $this->see('This user is the only user assigned to the administrator role');
103 public function test_manage_user_permission()
105 $this->actingAs($this->user)->visit('/settings/users')
107 $this->giveUserPermissions($this->user, ['users-manage']);
108 $this->actingAs($this->user)->visit('/settings/users')
109 ->seePageIs('/settings/users');
112 public function test_manage_users_permission_shows_link_in_header_if_does_not_have_settings_manage_permision()
114 $usersLink = 'href="'.url('/settings/users') . '"';
115 $this->actingAs($this->user)->visit('/')->dontSee($usersLink);
116 $this->giveUserPermissions($this->user, ['users-manage']);
117 $this->actingAs($this->user)->visit('/')->see($usersLink);
118 $this->giveUserPermissions($this->user, ['settings-manage', 'users-manage']);
119 $this->actingAs($this->user)->visit('/')->dontSee($usersLink);
122 public function test_user_cannot_change_email_unless_they_have_manage_users_permission()
124 $userProfileUrl = '/settings/users/' . $this->user->id;
125 $originalEmail = $this->user->email;
126 $this->actingAs($this->user);
128 $this->visit($userProfileUrl)
130 ->seeElement('input[name=email][disabled]');
131 $this->put($userProfileUrl, [
132 'name' => 'my_new_name',
135 $this->seeInDatabase('users', [
136 'id' => $this->user->id,
137 'email' => $originalEmail,
138 'name' => 'my_new_name',
141 $this->giveUserPermissions($this->user, ['users-manage']);
143 $this->visit($userProfileUrl)
145 ->dontSeeElement('input[name=email][disabled]')
146 ->seeElement('input[name=email]');
147 $this->put($userProfileUrl, [
148 'name' => 'my_new_name_2',
152 $this->seeInDatabase('users', [
153 'id' => $this->user->id,
155 'name' => 'my_new_name_2',
159 public function test_user_roles_manage_permission()
161 $this->actingAs($this->user)->visit('/settings/roles')
162 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
163 $this->giveUserPermissions($this->user, ['user-roles-manage']);
164 $this->actingAs($this->user)->visit('/settings/roles')
165 ->seePageIs('/settings/roles')->click('Admin')
169 public function test_settings_manage_permission()
171 $this->actingAs($this->user)->visit('/settings')
173 $this->giveUserPermissions($this->user, ['settings-manage']);
174 $this->actingAs($this->user)->visit('/settings')
175 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
178 public function test_restrictions_manage_all_permission()
180 $page = \BookStack\Entities\Page::take(1)->get()->first();
181 $this->actingAs($this->user)->visit($page->getUrl())
182 ->dontSee('Permissions')
183 ->visit($page->getUrl() . '/permissions')
185 $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
186 $this->actingAs($this->user)->visit($page->getUrl())
188 ->click('Permissions')
189 ->see('Page Permissions')->seePageIs($page->getUrl() . '/permissions');
192 public function test_restrictions_manage_own_permission()
194 $otherUsersPage = \BookStack\Entities\Page::first();
195 $content = $this->createEntityChainBelongingToUser($this->user);
196 // Check can't restrict other's content
197 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
198 ->dontSee('Permissions')
199 ->visit($otherUsersPage->getUrl() . '/permissions')
201 // Check can't restrict own content
202 $this->actingAs($this->user)->visit($content['page']->getUrl())
203 ->dontSee('Permissions')
204 ->visit($content['page']->getUrl() . '/permissions')
207 $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
209 // Check can't restrict other's content
210 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
211 ->dontSee('Permissions')
212 ->visit($otherUsersPage->getUrl() . '/permissions')
214 // Check can restrict own content
215 $this->actingAs($this->user)->visit($content['page']->getUrl())
217 ->click('Permissions')
218 ->seePageIs($content['page']->getUrl() . '/permissions');
222 * Check a standard entity access permission
223 * @param string $permission
224 * @param array $accessUrls Urls that are only accessible after having the permission
225 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
227 private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
229 foreach ($accessUrls as $url) {
230 $this->actingAs($this->user)->visit($url)
233 foreach ($visibles as $url => $text) {
234 $this->actingAs($this->user)->visit($url)
235 ->dontSeeInElement('.action-buttons',$text);
238 $this->giveUserPermissions($this->user, [$permission]);
240 foreach ($accessUrls as $url) {
241 $this->actingAs($this->user)->visit($url)
244 foreach ($visibles as $url => $text) {
245 $this->actingAs($this->user)->visit($url)
250 public function test_bookshelves_create_all_permissions()
252 $this->checkAccessPermission('bookshelf-create-all', [
255 '/shelves' => 'New Shelf'
258 $this->visit('/create-shelf')
259 ->type('test shelf', 'name')
260 ->type('shelf desc', 'description')
261 ->press('Save Shelf')
262 ->seePageIs('/shelves/test-shelf');
265 public function test_bookshelves_edit_own_permission()
267 $otherShelf = Bookshelf::first();
268 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
269 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
270 $this->regenEntityPermissions($ownShelf);
272 $this->checkAccessPermission('bookshelf-update-own', [
273 $ownShelf->getUrl('/edit')
275 $ownShelf->getUrl() => 'Edit'
278 $this->visit($otherShelf->getUrl())
279 ->dontSeeInElement('.action-buttons', 'Edit')
280 ->visit($otherShelf->getUrl('/edit'))
284 public function test_bookshelves_edit_all_permission()
286 $otherShelf = \BookStack\Entities\Bookshelf::first();
287 $this->checkAccessPermission('bookshelf-update-all', [
288 $otherShelf->getUrl('/edit')
290 $otherShelf->getUrl() => 'Edit'
294 public function test_bookshelves_delete_own_permission()
296 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
297 $otherShelf = \BookStack\Entities\Bookshelf::first();
298 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
299 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
300 $this->regenEntityPermissions($ownShelf);
302 $this->checkAccessPermission('bookshelf-delete-own', [
303 $ownShelf->getUrl('/delete')
305 $ownShelf->getUrl() => 'Delete'
308 $this->visit($otherShelf->getUrl())
309 ->dontSeeInElement('.action-buttons', 'Delete')
310 ->visit($otherShelf->getUrl('/delete'))
312 $this->visit($ownShelf->getUrl())->visit($ownShelf->getUrl('/delete'))
314 ->seePageIs('/shelves')
315 ->dontSee($ownShelf->name);
318 public function test_bookshelves_delete_all_permission()
320 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
321 $otherShelf = \BookStack\Entities\Bookshelf::first();
322 $this->checkAccessPermission('bookshelf-delete-all', [
323 $otherShelf->getUrl('/delete')
325 $otherShelf->getUrl() => 'Delete'
328 $this->visit($otherShelf->getUrl())->visit($otherShelf->getUrl('/delete'))
330 ->seePageIs('/shelves')
331 ->dontSee($otherShelf->name);
334 public function test_books_create_all_permissions()
336 $this->checkAccessPermission('book-create-all', [
339 '/books' => 'Create New Book'
342 $this->visit('/create-book')
343 ->type('test book', 'name')
344 ->type('book desc', 'description')
346 ->seePageIs('/books/test-book');
349 public function test_books_edit_own_permission()
351 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
352 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
353 $this->checkAccessPermission('book-update-own', [
354 $ownBook->getUrl() . '/edit'
356 $ownBook->getUrl() => 'Edit'
359 $this->visit($otherBook->getUrl())
360 ->dontSeeInElement('.action-buttons', 'Edit')
361 ->visit($otherBook->getUrl() . '/edit')
365 public function test_books_edit_all_permission()
367 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
368 $this->checkAccessPermission('book-update-all', [
369 $otherBook->getUrl() . '/edit'
371 $otherBook->getUrl() => 'Edit'
375 public function test_books_delete_own_permission()
377 $this->giveUserPermissions($this->user, ['book-update-all']);
378 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
379 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
380 $this->checkAccessPermission('book-delete-own', [
381 $ownBook->getUrl() . '/delete'
383 $ownBook->getUrl() => 'Delete'
386 $this->visit($otherBook->getUrl())
387 ->dontSeeInElement('.action-buttons', 'Delete')
388 ->visit($otherBook->getUrl() . '/delete')
390 $this->visit($ownBook->getUrl())->visit($ownBook->getUrl() . '/delete')
392 ->seePageIs('/books')
393 ->dontSee($ownBook->name);
396 public function test_books_delete_all_permission()
398 $this->giveUserPermissions($this->user, ['book-update-all']);
399 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
400 $this->checkAccessPermission('book-delete-all', [
401 $otherBook->getUrl() . '/delete'
403 $otherBook->getUrl() => 'Delete'
406 $this->visit($otherBook->getUrl())->visit($otherBook->getUrl() . '/delete')
408 ->seePageIs('/books')
409 ->dontSee($otherBook->name);
412 public function test_chapter_create_own_permissions()
414 $book = \BookStack\Entities\Book::take(1)->get()->first();
415 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
416 $this->checkAccessPermission('chapter-create-own', [
417 $ownBook->getUrl('/create-chapter')
419 $ownBook->getUrl() => 'New Chapter'
422 $this->visit($ownBook->getUrl('/create-chapter'))
423 ->type('test chapter', 'name')
424 ->type('chapter desc', 'description')
425 ->press('Save Chapter')
426 ->seePageIs($ownBook->getUrl('/chapter/test-chapter'));
428 $this->visit($book->getUrl())
429 ->dontSeeInElement('.action-buttons', 'New Chapter')
430 ->visit($book->getUrl('/create-chapter'))
434 public function test_chapter_create_all_permissions()
436 $book = \BookStack\Entities\Book::take(1)->get()->first();
437 $this->checkAccessPermission('chapter-create-all', [
438 $book->getUrl('/create-chapter')
440 $book->getUrl() => 'New Chapter'
443 $this->visit($book->getUrl('/create-chapter'))
444 ->type('test chapter', 'name')
445 ->type('chapter desc', 'description')
446 ->press('Save Chapter')
447 ->seePageIs($book->getUrl('/chapter/test-chapter'));
450 public function test_chapter_edit_own_permission()
452 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
453 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
454 $this->checkAccessPermission('chapter-update-own', [
455 $ownChapter->getUrl() . '/edit'
457 $ownChapter->getUrl() => 'Edit'
460 $this->visit($otherChapter->getUrl())
461 ->dontSeeInElement('.action-buttons', 'Edit')
462 ->visit($otherChapter->getUrl() . '/edit')
466 public function test_chapter_edit_all_permission()
468 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
469 $this->checkAccessPermission('chapter-update-all', [
470 $otherChapter->getUrl() . '/edit'
472 $otherChapter->getUrl() => 'Edit'
476 public function test_chapter_delete_own_permission()
478 $this->giveUserPermissions($this->user, ['chapter-update-all']);
479 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
480 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
481 $this->checkAccessPermission('chapter-delete-own', [
482 $ownChapter->getUrl() . '/delete'
484 $ownChapter->getUrl() => 'Delete'
487 $bookUrl = $ownChapter->book->getUrl();
488 $this->visit($otherChapter->getUrl())
489 ->dontSeeInElement('.action-buttons', 'Delete')
490 ->visit($otherChapter->getUrl() . '/delete')
492 $this->visit($ownChapter->getUrl())->visit($ownChapter->getUrl() . '/delete')
494 ->seePageIs($bookUrl)
495 ->dontSeeInElement('.book-content', $ownChapter->name);
498 public function test_chapter_delete_all_permission()
500 $this->giveUserPermissions($this->user, ['chapter-update-all']);
501 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
502 $this->checkAccessPermission('chapter-delete-all', [
503 $otherChapter->getUrl() . '/delete'
505 $otherChapter->getUrl() => 'Delete'
508 $bookUrl = $otherChapter->book->getUrl();
509 $this->visit($otherChapter->getUrl())->visit($otherChapter->getUrl() . '/delete')
511 ->seePageIs($bookUrl)
512 ->dontSeeInElement('.book-content', $otherChapter->name);
515 public function test_page_create_own_permissions()
517 $book = \BookStack\Entities\Book::first();
518 $chapter = \BookStack\Entities\Chapter::first();
520 $entities = $this->createEntityChainBelongingToUser($this->user);
521 $ownBook = $entities['book'];
522 $ownChapter = $entities['chapter'];
524 $createUrl = $ownBook->getUrl('/create-page');
525 $createUrlChapter = $ownChapter->getUrl('/create-page');
526 $accessUrls = [$createUrl, $createUrlChapter];
528 foreach ($accessUrls as $url) {
529 $this->actingAs($this->user)->visit($url)
533 $this->checkAccessPermission('page-create-own', [], [
534 $ownBook->getUrl() => 'New Page',
535 $ownChapter->getUrl() => 'New Page'
538 $this->giveUserPermissions($this->user, ['page-create-own']);
540 foreach ($accessUrls as $index => $url) {
541 $this->actingAs($this->user)->visit($url);
542 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
543 $this->seePageIs($expectedUrl);
546 $this->visit($createUrl)
547 ->type('test page', 'name')
548 ->type('page desc', 'html')
550 ->seePageIs($ownBook->getUrl('/page/test-page'));
552 $this->visit($book->getUrl())
553 ->dontSeeInElement('.action-buttons', 'New Page')
554 ->visit($book->getUrl() . '/create-page')
556 $this->visit($chapter->getUrl())
557 ->dontSeeInElement('.action-buttons', 'New Page')
558 ->visit($chapter->getUrl() . '/create-page')
562 public function test_page_create_all_permissions()
564 $book = \BookStack\Entities\Book::take(1)->get()->first();
565 $chapter = \BookStack\Entities\Chapter::take(1)->get()->first();
566 $baseUrl = $book->getUrl() . '/page';
567 $createUrl = $book->getUrl('/create-page');
569 $createUrlChapter = $chapter->getUrl('/create-page');
570 $accessUrls = [$createUrl, $createUrlChapter];
572 foreach ($accessUrls as $url) {
573 $this->actingAs($this->user)->visit($url)
577 $this->checkAccessPermission('page-create-all', [], [
578 $book->getUrl() => 'New Page',
579 $chapter->getUrl() => 'New Page'
582 $this->giveUserPermissions($this->user, ['page-create-all']);
584 foreach ($accessUrls as $index => $url) {
585 $this->actingAs($this->user)->visit($url);
586 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
587 $this->seePageIs($expectedUrl);
590 $this->visit($createUrl)
591 ->type('test page', 'name')
592 ->type('page desc', 'html')
594 ->seePageIs($book->getUrl('/page/test-page'));
596 $this->visit($chapter->getUrl('/create-page'))
597 ->type('new test page', 'name')
598 ->type('page desc', 'html')
600 ->seePageIs($book->getUrl('/page/new-test-page'));
603 public function test_page_edit_own_permission()
605 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
606 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
607 $this->checkAccessPermission('page-update-own', [
608 $ownPage->getUrl() . '/edit'
610 $ownPage->getUrl() => 'Edit'
613 $this->visit($otherPage->getUrl())
614 ->dontSeeInElement('.action-buttons', 'Edit')
615 ->visit($otherPage->getUrl() . '/edit')
619 public function test_page_edit_all_permission()
621 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
622 $this->checkAccessPermission('page-update-all', [
623 $otherPage->getUrl() . '/edit'
625 $otherPage->getUrl() => 'Edit'
629 public function test_page_delete_own_permission()
631 $this->giveUserPermissions($this->user, ['page-update-all']);
632 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
633 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
634 $this->checkAccessPermission('page-delete-own', [
635 $ownPage->getUrl() . '/delete'
637 $ownPage->getUrl() => 'Delete'
640 $bookUrl = $ownPage->book->getUrl();
641 $this->visit($otherPage->getUrl())
642 ->dontSeeInElement('.action-buttons', 'Delete')
643 ->visit($otherPage->getUrl() . '/delete')
645 $this->visit($ownPage->getUrl())->visit($ownPage->getUrl() . '/delete')
647 ->seePageIs($bookUrl)
648 ->dontSeeInElement('.book-content', $ownPage->name);
651 public function test_page_delete_all_permission()
653 $this->giveUserPermissions($this->user, ['page-update-all']);
654 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
655 $this->checkAccessPermission('page-delete-all', [
656 $otherPage->getUrl() . '/delete'
658 $otherPage->getUrl() => 'Delete'
661 $bookUrl = $otherPage->book->getUrl();
662 $this->visit($otherPage->getUrl())->visit($otherPage->getUrl() . '/delete')
664 ->seePageIs($bookUrl)
665 ->dontSeeInElement('.book-content', $otherPage->name);
668 public function test_public_role_visible_in_user_edit_screen()
670 $user = \BookStack\Auth\User::first();
671 $this->asAdmin()->visit('/settings/users/' . $user->id)
672 ->seeElement('[name="roles[admin]"]')
673 ->seeElement('[name="roles[public]"]');
676 public function test_public_role_visible_in_role_listing()
678 $this->asAdmin()->visit('/settings/roles')
683 public function test_public_role_visible_in_default_role_setting()
685 $this->asAdmin()->visit('/settings')
686 ->seeElement('[data-role-name="admin"]')
687 ->seeElement('[data-role-name="public"]');
691 public function test_public_role_not_deleteable()
693 $this->asAdmin()->visit('/settings/roles')
696 ->click('Delete Role')
699 ->see('Cannot be deleted');
702 public function test_image_delete_own_permission()
704 $this->giveUserPermissions($this->user, ['image-update-all']);
705 $page = \BookStack\Entities\Page::first();
706 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]);
708 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
709 ->seeStatusCode(403);
711 $this->giveUserPermissions($this->user, ['image-delete-own']);
713 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
715 ->dontSeeInDatabase('images', ['id' => $image->id]);
718 public function test_image_delete_all_permission()
720 $this->giveUserPermissions($this->user, ['image-update-all']);
721 $admin = $this->getAdmin();
722 $page = \BookStack\Entities\Page::first();
723 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
725 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
726 ->seeStatusCode(403);
728 $this->giveUserPermissions($this->user, ['image-delete-own']);
730 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
731 ->seeStatusCode(403);
733 $this->giveUserPermissions($this->user, ['image-delete-all']);
735 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
737 ->dontSeeInDatabase('images', ['id' => $image->id]);
740 public function test_role_permission_removal()
742 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
743 $page = Page::first();
744 $viewerRole = \BookStack\Auth\Role::getRole('viewer');
745 $viewer = $this->getViewer();
746 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(200);
748 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
749 'display_name' => $viewerRole->display_name,
750 'description' => $viewerRole->description,
752 ])->assertResponseStatus(302);
754 $this->expectException(HttpException::class);
755 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(404);
758 public function test_empty_state_actions_not_visible_without_permission()
760 $admin = $this->getAdmin();
762 $book = factory(\BookStack\Entities\Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
763 $this->updateEntityPermissions($book);
764 $this->actingAs($this->getViewer())->visit($book->getUrl())
765 ->dontSee('Create a new page')
766 ->dontSee('Add a chapter');
769 $chapter = factory(\BookStack\Entities\Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
770 $this->updateEntityPermissions($chapter);
771 $this->actingAs($this->getViewer())->visit($chapter->getUrl())
772 ->dontSee('Create a new page')
773 ->dontSee('Sort the current book');
776 public function test_comment_create_permission () {
777 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
779 $this->actingAs($this->user)->addComment($ownPage);
781 $this->assertResponseStatus(403);
783 $this->giveUserPermissions($this->user, ['comment-create-all']);
785 $this->actingAs($this->user)->addComment($ownPage);
786 $this->assertResponseStatus(200);
790 public function test_comment_update_own_permission () {
791 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
792 $this->giveUserPermissions($this->user, ['comment-create-all']);
793 $commentId = $this->actingAs($this->user)->addComment($ownPage);
795 // no comment-update-own
796 $this->actingAs($this->user)->updateComment($commentId);
797 $this->assertResponseStatus(403);
799 $this->giveUserPermissions($this->user, ['comment-update-own']);
801 // now has comment-update-own
802 $this->actingAs($this->user)->updateComment($commentId);
803 $this->assertResponseStatus(200);
806 public function test_comment_update_all_permission () {
807 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
808 $commentId = $this->asAdmin()->addComment($ownPage);
810 // no comment-update-all
811 $this->actingAs($this->user)->updateComment($commentId);
812 $this->assertResponseStatus(403);
814 $this->giveUserPermissions($this->user, ['comment-update-all']);
816 // now has comment-update-all
817 $this->actingAs($this->user)->updateComment($commentId);
818 $this->assertResponseStatus(200);
821 public function test_comment_delete_own_permission () {
822 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
823 $this->giveUserPermissions($this->user, ['comment-create-all']);
824 $commentId = $this->actingAs($this->user)->addComment($ownPage);
826 // no comment-delete-own
827 $this->actingAs($this->user)->deleteComment($commentId);
828 $this->assertResponseStatus(403);
830 $this->giveUserPermissions($this->user, ['comment-delete-own']);
832 // now has comment-update-own
833 $this->actingAs($this->user)->deleteComment($commentId);
834 $this->assertResponseStatus(200);
837 public function test_comment_delete_all_permission () {
838 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
839 $commentId = $this->asAdmin()->addComment($ownPage);
841 // no comment-delete-all
842 $this->actingAs($this->user)->deleteComment($commentId);
843 $this->assertResponseStatus(403);
845 $this->giveUserPermissions($this->user, ['comment-delete-all']);
847 // now has comment-delete-all
848 $this->actingAs($this->user)->deleteComment($commentId);
849 $this->assertResponseStatus(200);
852 private function addComment($page) {
853 $comment = factory(\BookStack\Actions\Comment::class)->make();
854 $url = "/ajax/page/$page->id/comment";
856 'text' => $comment->text,
857 'html' => $comment->html
860 $this->postJson($url, $request);
861 $comment = $page->comments()->first();
862 return $comment === null ? null : $comment->id;
865 private function updateComment($commentId) {
866 $comment = factory(\BookStack\Actions\Comment::class)->make();
867 $url = "/ajax/comment/$commentId";
869 'text' => $comment->text,
870 'html' => $comment->html
873 return $this->putJson($url, $request);
876 private function deleteComment($commentId) {
877 $url = '/ajax/comment/' . $commentId;
878 return $this->json('DELETE', $url);