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_roles_manage_permission()
124 $this->actingAs($this->user)->visit('/settings/roles')
125 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
126 $this->giveUserPermissions($this->user, ['user-roles-manage']);
127 $this->actingAs($this->user)->visit('/settings/roles')
128 ->seePageIs('/settings/roles')->click('Admin')
132 public function test_settings_manage_permission()
134 $this->actingAs($this->user)->visit('/settings')
136 $this->giveUserPermissions($this->user, ['settings-manage']);
137 $this->actingAs($this->user)->visit('/settings')
138 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
141 public function test_restrictions_manage_all_permission()
143 $page = \BookStack\Entities\Page::take(1)->get()->first();
144 $this->actingAs($this->user)->visit($page->getUrl())
145 ->dontSee('Permissions')
146 ->visit($page->getUrl() . '/permissions')
148 $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
149 $this->actingAs($this->user)->visit($page->getUrl())
151 ->click('Permissions')
152 ->see('Page Permissions')->seePageIs($page->getUrl() . '/permissions');
155 public function test_restrictions_manage_own_permission()
157 $otherUsersPage = \BookStack\Entities\Page::first();
158 $content = $this->createEntityChainBelongingToUser($this->user);
159 // Check can't restrict other's content
160 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
161 ->dontSee('Permissions')
162 ->visit($otherUsersPage->getUrl() . '/permissions')
164 // Check can't restrict own content
165 $this->actingAs($this->user)->visit($content['page']->getUrl())
166 ->dontSee('Permissions')
167 ->visit($content['page']->getUrl() . '/permissions')
170 $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
172 // Check can't restrict other's content
173 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
174 ->dontSee('Permissions')
175 ->visit($otherUsersPage->getUrl() . '/permissions')
177 // Check can restrict own content
178 $this->actingAs($this->user)->visit($content['page']->getUrl())
180 ->click('Permissions')
181 ->seePageIs($content['page']->getUrl() . '/permissions');
185 * Check a standard entity access permission
186 * @param string $permission
187 * @param array $accessUrls Urls that are only accessible after having the permission
188 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
190 private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
192 foreach ($accessUrls as $url) {
193 $this->actingAs($this->user)->visit($url)
196 foreach ($visibles as $url => $text) {
197 $this->actingAs($this->user)->visit($url)
198 ->dontSeeInElement('.action-buttons',$text);
201 $this->giveUserPermissions($this->user, [$permission]);
203 foreach ($accessUrls as $url) {
204 $this->actingAs($this->user)->visit($url)
207 foreach ($visibles as $url => $text) {
208 $this->actingAs($this->user)->visit($url)
213 public function test_bookshelves_create_all_permissions()
215 $this->checkAccessPermission('bookshelf-create-all', [
218 '/shelves' => 'New Shelf'
221 $this->visit('/create-shelf')
222 ->type('test shelf', 'name')
223 ->type('shelf desc', 'description')
224 ->press('Save Shelf')
225 ->seePageIs('/shelves/test-shelf');
228 public function test_bookshelves_edit_own_permission()
230 $otherShelf = Bookshelf::first();
231 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
232 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
233 $this->regenEntityPermissions($ownShelf);
235 $this->checkAccessPermission('bookshelf-update-own', [
236 $ownShelf->getUrl('/edit')
238 $ownShelf->getUrl() => 'Edit'
241 $this->visit($otherShelf->getUrl())
242 ->dontSeeInElement('.action-buttons', 'Edit')
243 ->visit($otherShelf->getUrl('/edit'))
247 public function test_bookshelves_edit_all_permission()
249 $otherShelf = \BookStack\Entities\Bookshelf::first();
250 $this->checkAccessPermission('bookshelf-update-all', [
251 $otherShelf->getUrl('/edit')
253 $otherShelf->getUrl() => 'Edit'
257 public function test_bookshelves_delete_own_permission()
259 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
260 $otherShelf = \BookStack\Entities\Bookshelf::first();
261 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
262 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
263 $this->regenEntityPermissions($ownShelf);
265 $this->checkAccessPermission('bookshelf-delete-own', [
266 $ownShelf->getUrl('/delete')
268 $ownShelf->getUrl() => 'Delete'
271 $this->visit($otherShelf->getUrl())
272 ->dontSeeInElement('.action-buttons', 'Delete')
273 ->visit($otherShelf->getUrl('/delete'))
275 $this->visit($ownShelf->getUrl())->visit($ownShelf->getUrl('/delete'))
277 ->seePageIs('/shelves')
278 ->dontSee($ownShelf->name);
281 public function test_bookshelves_delete_all_permission()
283 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
284 $otherShelf = \BookStack\Entities\Bookshelf::first();
285 $this->checkAccessPermission('bookshelf-delete-all', [
286 $otherShelf->getUrl('/delete')
288 $otherShelf->getUrl() => 'Delete'
291 $this->visit($otherShelf->getUrl())->visit($otherShelf->getUrl('/delete'))
293 ->seePageIs('/shelves')
294 ->dontSee($otherShelf->name);
297 public function test_books_create_all_permissions()
299 $this->checkAccessPermission('book-create-all', [
302 '/books' => 'Create New Book'
305 $this->visit('/create-book')
306 ->type('test book', 'name')
307 ->type('book desc', 'description')
309 ->seePageIs('/books/test-book');
312 public function test_books_edit_own_permission()
314 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
315 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
316 $this->checkAccessPermission('book-update-own', [
317 $ownBook->getUrl() . '/edit'
319 $ownBook->getUrl() => 'Edit'
322 $this->visit($otherBook->getUrl())
323 ->dontSeeInElement('.action-buttons', 'Edit')
324 ->visit($otherBook->getUrl() . '/edit')
328 public function test_books_edit_all_permission()
330 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
331 $this->checkAccessPermission('book-update-all', [
332 $otherBook->getUrl() . '/edit'
334 $otherBook->getUrl() => 'Edit'
338 public function test_books_delete_own_permission()
340 $this->giveUserPermissions($this->user, ['book-update-all']);
341 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
342 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
343 $this->checkAccessPermission('book-delete-own', [
344 $ownBook->getUrl() . '/delete'
346 $ownBook->getUrl() => 'Delete'
349 $this->visit($otherBook->getUrl())
350 ->dontSeeInElement('.action-buttons', 'Delete')
351 ->visit($otherBook->getUrl() . '/delete')
353 $this->visit($ownBook->getUrl())->visit($ownBook->getUrl() . '/delete')
355 ->seePageIs('/books')
356 ->dontSee($ownBook->name);
359 public function test_books_delete_all_permission()
361 $this->giveUserPermissions($this->user, ['book-update-all']);
362 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
363 $this->checkAccessPermission('book-delete-all', [
364 $otherBook->getUrl() . '/delete'
366 $otherBook->getUrl() => 'Delete'
369 $this->visit($otherBook->getUrl())->visit($otherBook->getUrl() . '/delete')
371 ->seePageIs('/books')
372 ->dontSee($otherBook->name);
375 public function test_chapter_create_own_permissions()
377 $book = \BookStack\Entities\Book::take(1)->get()->first();
378 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
379 $this->checkAccessPermission('chapter-create-own', [
380 $ownBook->getUrl('/create-chapter')
382 $ownBook->getUrl() => 'New Chapter'
385 $this->visit($ownBook->getUrl('/create-chapter'))
386 ->type('test chapter', 'name')
387 ->type('chapter desc', 'description')
388 ->press('Save Chapter')
389 ->seePageIs($ownBook->getUrl('/chapter/test-chapter'));
391 $this->visit($book->getUrl())
392 ->dontSeeInElement('.action-buttons', 'New Chapter')
393 ->visit($book->getUrl('/create-chapter'))
397 public function test_chapter_create_all_permissions()
399 $book = \BookStack\Entities\Book::take(1)->get()->first();
400 $this->checkAccessPermission('chapter-create-all', [
401 $book->getUrl('/create-chapter')
403 $book->getUrl() => 'New Chapter'
406 $this->visit($book->getUrl('/create-chapter'))
407 ->type('test chapter', 'name')
408 ->type('chapter desc', 'description')
409 ->press('Save Chapter')
410 ->seePageIs($book->getUrl('/chapter/test-chapter'));
413 public function test_chapter_edit_own_permission()
415 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
416 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
417 $this->checkAccessPermission('chapter-update-own', [
418 $ownChapter->getUrl() . '/edit'
420 $ownChapter->getUrl() => 'Edit'
423 $this->visit($otherChapter->getUrl())
424 ->dontSeeInElement('.action-buttons', 'Edit')
425 ->visit($otherChapter->getUrl() . '/edit')
429 public function test_chapter_edit_all_permission()
431 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
432 $this->checkAccessPermission('chapter-update-all', [
433 $otherChapter->getUrl() . '/edit'
435 $otherChapter->getUrl() => 'Edit'
439 public function test_chapter_delete_own_permission()
441 $this->giveUserPermissions($this->user, ['chapter-update-all']);
442 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
443 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
444 $this->checkAccessPermission('chapter-delete-own', [
445 $ownChapter->getUrl() . '/delete'
447 $ownChapter->getUrl() => 'Delete'
450 $bookUrl = $ownChapter->book->getUrl();
451 $this->visit($otherChapter->getUrl())
452 ->dontSeeInElement('.action-buttons', 'Delete')
453 ->visit($otherChapter->getUrl() . '/delete')
455 $this->visit($ownChapter->getUrl())->visit($ownChapter->getUrl() . '/delete')
457 ->seePageIs($bookUrl)
458 ->dontSeeInElement('.book-content', $ownChapter->name);
461 public function test_chapter_delete_all_permission()
463 $this->giveUserPermissions($this->user, ['chapter-update-all']);
464 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
465 $this->checkAccessPermission('chapter-delete-all', [
466 $otherChapter->getUrl() . '/delete'
468 $otherChapter->getUrl() => 'Delete'
471 $bookUrl = $otherChapter->book->getUrl();
472 $this->visit($otherChapter->getUrl())->visit($otherChapter->getUrl() . '/delete')
474 ->seePageIs($bookUrl)
475 ->dontSeeInElement('.book-content', $otherChapter->name);
478 public function test_page_create_own_permissions()
480 $book = \BookStack\Entities\Book::first();
481 $chapter = \BookStack\Entities\Chapter::first();
483 $entities = $this->createEntityChainBelongingToUser($this->user);
484 $ownBook = $entities['book'];
485 $ownChapter = $entities['chapter'];
487 $createUrl = $ownBook->getUrl('/create-page');
488 $createUrlChapter = $ownChapter->getUrl('/create-page');
489 $accessUrls = [$createUrl, $createUrlChapter];
491 foreach ($accessUrls as $url) {
492 $this->actingAs($this->user)->visit($url)
496 $this->checkAccessPermission('page-create-own', [], [
497 $ownBook->getUrl() => 'New Page',
498 $ownChapter->getUrl() => 'New Page'
501 $this->giveUserPermissions($this->user, ['page-create-own']);
503 foreach ($accessUrls as $index => $url) {
504 $this->actingAs($this->user)->visit($url);
505 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
506 $this->seePageIs($expectedUrl);
509 $this->visit($createUrl)
510 ->type('test page', 'name')
511 ->type('page desc', 'html')
513 ->seePageIs($ownBook->getUrl('/page/test-page'));
515 $this->visit($book->getUrl())
516 ->dontSeeInElement('.action-buttons', 'New Page')
517 ->visit($book->getUrl() . '/create-page')
519 $this->visit($chapter->getUrl())
520 ->dontSeeInElement('.action-buttons', 'New Page')
521 ->visit($chapter->getUrl() . '/create-page')
525 public function test_page_create_all_permissions()
527 $book = \BookStack\Entities\Book::take(1)->get()->first();
528 $chapter = \BookStack\Entities\Chapter::take(1)->get()->first();
529 $baseUrl = $book->getUrl() . '/page';
530 $createUrl = $book->getUrl('/create-page');
532 $createUrlChapter = $chapter->getUrl('/create-page');
533 $accessUrls = [$createUrl, $createUrlChapter];
535 foreach ($accessUrls as $url) {
536 $this->actingAs($this->user)->visit($url)
540 $this->checkAccessPermission('page-create-all', [], [
541 $book->getUrl() => 'New Page',
542 $chapter->getUrl() => 'New Page'
545 $this->giveUserPermissions($this->user, ['page-create-all']);
547 foreach ($accessUrls as $index => $url) {
548 $this->actingAs($this->user)->visit($url);
549 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
550 $this->seePageIs($expectedUrl);
553 $this->visit($createUrl)
554 ->type('test page', 'name')
555 ->type('page desc', 'html')
557 ->seePageIs($book->getUrl('/page/test-page'));
559 $this->visit($chapter->getUrl('/create-page'))
560 ->type('new test page', 'name')
561 ->type('page desc', 'html')
563 ->seePageIs($book->getUrl('/page/new-test-page'));
566 public function test_page_edit_own_permission()
568 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
569 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
570 $this->checkAccessPermission('page-update-own', [
571 $ownPage->getUrl() . '/edit'
573 $ownPage->getUrl() => 'Edit'
576 $this->visit($otherPage->getUrl())
577 ->dontSeeInElement('.action-buttons', 'Edit')
578 ->visit($otherPage->getUrl() . '/edit')
582 public function test_page_edit_all_permission()
584 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
585 $this->checkAccessPermission('page-update-all', [
586 $otherPage->getUrl() . '/edit'
588 $otherPage->getUrl() => 'Edit'
592 public function test_page_delete_own_permission()
594 $this->giveUserPermissions($this->user, ['page-update-all']);
595 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
596 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
597 $this->checkAccessPermission('page-delete-own', [
598 $ownPage->getUrl() . '/delete'
600 $ownPage->getUrl() => 'Delete'
603 $bookUrl = $ownPage->book->getUrl();
604 $this->visit($otherPage->getUrl())
605 ->dontSeeInElement('.action-buttons', 'Delete')
606 ->visit($otherPage->getUrl() . '/delete')
608 $this->visit($ownPage->getUrl())->visit($ownPage->getUrl() . '/delete')
610 ->seePageIs($bookUrl)
611 ->dontSeeInElement('.book-content', $ownPage->name);
614 public function test_page_delete_all_permission()
616 $this->giveUserPermissions($this->user, ['page-update-all']);
617 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
618 $this->checkAccessPermission('page-delete-all', [
619 $otherPage->getUrl() . '/delete'
621 $otherPage->getUrl() => 'Delete'
624 $bookUrl = $otherPage->book->getUrl();
625 $this->visit($otherPage->getUrl())->visit($otherPage->getUrl() . '/delete')
627 ->seePageIs($bookUrl)
628 ->dontSeeInElement('.book-content', $otherPage->name);
631 public function test_public_role_visible_in_user_edit_screen()
633 $user = \BookStack\Auth\User::first();
634 $this->asAdmin()->visit('/settings/users/' . $user->id)
635 ->seeElement('[name="roles[admin]"]')
636 ->seeElement('[name="roles[public]"]');
639 public function test_public_role_visible_in_role_listing()
641 $this->asAdmin()->visit('/settings/roles')
646 public function test_public_role_visible_in_default_role_setting()
648 $this->asAdmin()->visit('/settings')
649 ->seeElement('[data-role-name="admin"]')
650 ->seeElement('[data-role-name="public"]');
654 public function test_public_role_not_deleteable()
656 $this->asAdmin()->visit('/settings/roles')
659 ->click('Delete Role')
662 ->see('Cannot be deleted');
665 public function test_image_delete_own_permission()
667 $this->giveUserPermissions($this->user, ['image-update-all']);
668 $page = \BookStack\Entities\Page::first();
669 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]);
671 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
672 ->seeStatusCode(403);
674 $this->giveUserPermissions($this->user, ['image-delete-own']);
676 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
678 ->dontSeeInDatabase('images', ['id' => $image->id]);
681 public function test_image_delete_all_permission()
683 $this->giveUserPermissions($this->user, ['image-update-all']);
684 $admin = $this->getAdmin();
685 $page = \BookStack\Entities\Page::first();
686 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
688 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
689 ->seeStatusCode(403);
691 $this->giveUserPermissions($this->user, ['image-delete-own']);
693 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
694 ->seeStatusCode(403);
696 $this->giveUserPermissions($this->user, ['image-delete-all']);
698 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
700 ->dontSeeInDatabase('images', ['id' => $image->id]);
703 public function test_role_permission_removal()
705 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
706 $page = Page::first();
707 $viewerRole = \BookStack\Auth\Role::getRole('viewer');
708 $viewer = $this->getViewer();
709 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(200);
711 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
712 'display_name' => $viewerRole->display_name,
713 'description' => $viewerRole->description,
715 ])->assertResponseStatus(302);
717 $this->expectException(HttpException::class);
718 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(404);
721 public function test_empty_state_actions_not_visible_without_permission()
723 $admin = $this->getAdmin();
725 $book = factory(\BookStack\Entities\Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
726 $this->updateEntityPermissions($book);
727 $this->actingAs($this->getViewer())->visit($book->getUrl())
728 ->dontSee('Create a new page')
729 ->dontSee('Add a chapter');
732 $chapter = factory(\BookStack\Entities\Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
733 $this->updateEntityPermissions($chapter);
734 $this->actingAs($this->getViewer())->visit($chapter->getUrl())
735 ->dontSee('Create a new page')
736 ->dontSee('Sort the current book');
739 public function test_comment_create_permission () {
740 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
742 $this->actingAs($this->user)->addComment($ownPage);
744 $this->assertResponseStatus(403);
746 $this->giveUserPermissions($this->user, ['comment-create-all']);
748 $this->actingAs($this->user)->addComment($ownPage);
749 $this->assertResponseStatus(200);
753 public function test_comment_update_own_permission () {
754 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
755 $this->giveUserPermissions($this->user, ['comment-create-all']);
756 $commentId = $this->actingAs($this->user)->addComment($ownPage);
758 // no comment-update-own
759 $this->actingAs($this->user)->updateComment($commentId);
760 $this->assertResponseStatus(403);
762 $this->giveUserPermissions($this->user, ['comment-update-own']);
764 // now has comment-update-own
765 $this->actingAs($this->user)->updateComment($commentId);
766 $this->assertResponseStatus(200);
769 public function test_comment_update_all_permission () {
770 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
771 $commentId = $this->asAdmin()->addComment($ownPage);
773 // no comment-update-all
774 $this->actingAs($this->user)->updateComment($commentId);
775 $this->assertResponseStatus(403);
777 $this->giveUserPermissions($this->user, ['comment-update-all']);
779 // now has comment-update-all
780 $this->actingAs($this->user)->updateComment($commentId);
781 $this->assertResponseStatus(200);
784 public function test_comment_delete_own_permission () {
785 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
786 $this->giveUserPermissions($this->user, ['comment-create-all']);
787 $commentId = $this->actingAs($this->user)->addComment($ownPage);
789 // no comment-delete-own
790 $this->actingAs($this->user)->deleteComment($commentId);
791 $this->assertResponseStatus(403);
793 $this->giveUserPermissions($this->user, ['comment-delete-own']);
795 // now has comment-update-own
796 $this->actingAs($this->user)->deleteComment($commentId);
797 $this->assertResponseStatus(200);
800 public function test_comment_delete_all_permission () {
801 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
802 $commentId = $this->asAdmin()->addComment($ownPage);
804 // no comment-delete-all
805 $this->actingAs($this->user)->deleteComment($commentId);
806 $this->assertResponseStatus(403);
808 $this->giveUserPermissions($this->user, ['comment-delete-all']);
810 // now has comment-delete-all
811 $this->actingAs($this->user)->deleteComment($commentId);
812 $this->assertResponseStatus(200);
815 private function addComment($page) {
816 $comment = factory(\BookStack\Actions\Comment::class)->make();
817 $url = "/ajax/page/$page->id/comment";
819 'text' => $comment->text,
820 'html' => $comment->html
823 $this->postJson($url, $request);
824 $comment = $page->comments()->first();
825 return $comment === null ? null : $comment->id;
828 private function updateComment($commentId) {
829 $comment = factory(\BookStack\Actions\Comment::class)->make();
830 $url = "/ajax/comment/$commentId";
832 'text' => $comment->text,
833 'html' => $comment->html
836 return $this->putJson($url, $request);
839 private function deleteComment($commentId) {
840 $url = '/ajax/comment/' . $commentId;
841 return $this->json('DELETE', $url);