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_user_roles_manage_permission()
114 $this->actingAs($this->user)->visit('/settings/roles')
115 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
116 $this->giveUserPermissions($this->user, ['user-roles-manage']);
117 $this->actingAs($this->user)->visit('/settings/roles')
118 ->seePageIs('/settings/roles')->click('Admin')
122 public function test_settings_manage_permission()
124 $this->actingAs($this->user)->visit('/settings')
126 $this->giveUserPermissions($this->user, ['settings-manage']);
127 $this->actingAs($this->user)->visit('/settings')
128 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
131 public function test_restrictions_manage_all_permission()
133 $page = \BookStack\Entities\Page::take(1)->get()->first();
134 $this->actingAs($this->user)->visit($page->getUrl())
135 ->dontSee('Permissions')
136 ->visit($page->getUrl() . '/permissions')
138 $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
139 $this->actingAs($this->user)->visit($page->getUrl())
141 ->click('Permissions')
142 ->see('Page Permissions')->seePageIs($page->getUrl() . '/permissions');
145 public function test_restrictions_manage_own_permission()
147 $otherUsersPage = \BookStack\Entities\Page::first();
148 $content = $this->createEntityChainBelongingToUser($this->user);
149 // Check can't restrict other's content
150 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
151 ->dontSee('Permissions')
152 ->visit($otherUsersPage->getUrl() . '/permissions')
154 // Check can't restrict own content
155 $this->actingAs($this->user)->visit($content['page']->getUrl())
156 ->dontSee('Permissions')
157 ->visit($content['page']->getUrl() . '/permissions')
160 $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
162 // Check can't restrict other's content
163 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
164 ->dontSee('Permissions')
165 ->visit($otherUsersPage->getUrl() . '/permissions')
167 // Check can restrict own content
168 $this->actingAs($this->user)->visit($content['page']->getUrl())
170 ->click('Permissions')
171 ->seePageIs($content['page']->getUrl() . '/permissions');
175 * Check a standard entity access permission
176 * @param string $permission
177 * @param array $accessUrls Urls that are only accessible after having the permission
178 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
180 private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
182 foreach ($accessUrls as $url) {
183 $this->actingAs($this->user)->visit($url)
186 foreach ($visibles as $url => $text) {
187 $this->actingAs($this->user)->visit($url)
188 ->dontSeeInElement('.action-buttons',$text);
191 $this->giveUserPermissions($this->user, [$permission]);
193 foreach ($accessUrls as $url) {
194 $this->actingAs($this->user)->visit($url)
197 foreach ($visibles as $url => $text) {
198 $this->actingAs($this->user)->visit($url)
203 public function test_bookshelves_create_all_permissions()
205 $this->checkAccessPermission('bookshelf-create-all', [
208 '/shelves' => 'Create New Shelf'
211 $this->visit('/create-shelf')
212 ->type('test shelf', 'name')
213 ->type('shelf desc', 'description')
214 ->press('Save Shelf')
215 ->seePageIs('/shelves/test-shelf');
218 public function test_bookshelves_edit_own_permission()
220 $otherShelf = Bookshelf::first();
221 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
222 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
223 $this->regenEntityPermissions($ownShelf);
225 $this->checkAccessPermission('bookshelf-update-own', [
226 $ownShelf->getUrl('/edit')
228 $ownShelf->getUrl() => 'Edit'
231 $this->visit($otherShelf->getUrl())
232 ->dontSeeInElement('.action-buttons', 'Edit')
233 ->visit($otherShelf->getUrl('/edit'))
237 public function test_bookshelves_edit_all_permission()
239 $otherShelf = \BookStack\Entities\Bookshelf::first();
240 $this->checkAccessPermission('bookshelf-update-all', [
241 $otherShelf->getUrl('/edit')
243 $otherShelf->getUrl() => 'Edit'
247 public function test_bookshelves_delete_own_permission()
249 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
250 $otherShelf = \BookStack\Entities\Bookshelf::first();
251 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
252 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
253 $this->regenEntityPermissions($ownShelf);
255 $this->checkAccessPermission('bookshelf-delete-own', [
256 $ownShelf->getUrl('/delete')
258 $ownShelf->getUrl() => 'Delete'
261 $this->visit($otherShelf->getUrl())
262 ->dontSeeInElement('.action-buttons', 'Delete')
263 ->visit($otherShelf->getUrl('/delete'))
265 $this->visit($ownShelf->getUrl())->visit($ownShelf->getUrl('/delete'))
267 ->seePageIs('/shelves')
268 ->dontSee($ownShelf->name);
271 public function test_bookshelves_delete_all_permission()
273 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
274 $otherShelf = \BookStack\Entities\Bookshelf::first();
275 $this->checkAccessPermission('bookshelf-delete-all', [
276 $otherShelf->getUrl('/delete')
278 $otherShelf->getUrl() => 'Delete'
281 $this->visit($otherShelf->getUrl())->visit($otherShelf->getUrl('/delete'))
283 ->seePageIs('/shelves')
284 ->dontSee($otherShelf->name);
287 public function test_books_create_all_permissions()
289 $this->checkAccessPermission('book-create-all', [
292 '/books' => 'Create New Book'
295 $this->visit('/create-book')
296 ->type('test book', 'name')
297 ->type('book desc', 'description')
299 ->seePageIs('/books/test-book');
302 public function test_books_edit_own_permission()
304 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
305 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
306 $this->checkAccessPermission('book-update-own', [
307 $ownBook->getUrl() . '/edit'
309 $ownBook->getUrl() => 'Edit'
312 $this->visit($otherBook->getUrl())
313 ->dontSeeInElement('.action-buttons', 'Edit')
314 ->visit($otherBook->getUrl() . '/edit')
318 public function test_books_edit_all_permission()
320 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
321 $this->checkAccessPermission('book-update-all', [
322 $otherBook->getUrl() . '/edit'
324 $otherBook->getUrl() => 'Edit'
328 public function test_books_delete_own_permission()
330 $this->giveUserPermissions($this->user, ['book-update-all']);
331 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
332 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
333 $this->checkAccessPermission('book-delete-own', [
334 $ownBook->getUrl() . '/delete'
336 $ownBook->getUrl() => 'Delete'
339 $this->visit($otherBook->getUrl())
340 ->dontSeeInElement('.action-buttons', 'Delete')
341 ->visit($otherBook->getUrl() . '/delete')
343 $this->visit($ownBook->getUrl())->visit($ownBook->getUrl() . '/delete')
345 ->seePageIs('/books')
346 ->dontSee($ownBook->name);
349 public function test_books_delete_all_permission()
351 $this->giveUserPermissions($this->user, ['book-update-all']);
352 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
353 $this->checkAccessPermission('book-delete-all', [
354 $otherBook->getUrl() . '/delete'
356 $otherBook->getUrl() => 'Delete'
359 $this->visit($otherBook->getUrl())->visit($otherBook->getUrl() . '/delete')
361 ->seePageIs('/books')
362 ->dontSee($otherBook->name);
365 public function test_chapter_create_own_permissions()
367 $book = \BookStack\Entities\Book::take(1)->get()->first();
368 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
369 $this->checkAccessPermission('chapter-create-own', [
370 $ownBook->getUrl('/create-chapter')
372 $ownBook->getUrl() => 'New Chapter'
375 $this->visit($ownBook->getUrl('/create-chapter'))
376 ->type('test chapter', 'name')
377 ->type('chapter desc', 'description')
378 ->press('Save Chapter')
379 ->seePageIs($ownBook->getUrl('/chapter/test-chapter'));
381 $this->visit($book->getUrl())
382 ->dontSeeInElement('.action-buttons', 'New Chapter')
383 ->visit($book->getUrl('/create-chapter'))
387 public function test_chapter_create_all_permissions()
389 $book = \BookStack\Entities\Book::take(1)->get()->first();
390 $this->checkAccessPermission('chapter-create-all', [
391 $book->getUrl('/create-chapter')
393 $book->getUrl() => 'New Chapter'
396 $this->visit($book->getUrl('/create-chapter'))
397 ->type('test chapter', 'name')
398 ->type('chapter desc', 'description')
399 ->press('Save Chapter')
400 ->seePageIs($book->getUrl('/chapter/test-chapter'));
403 public function test_chapter_edit_own_permission()
405 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
406 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
407 $this->checkAccessPermission('chapter-update-own', [
408 $ownChapter->getUrl() . '/edit'
410 $ownChapter->getUrl() => 'Edit'
413 $this->visit($otherChapter->getUrl())
414 ->dontSeeInElement('.action-buttons', 'Edit')
415 ->visit($otherChapter->getUrl() . '/edit')
419 public function test_chapter_edit_all_permission()
421 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
422 $this->checkAccessPermission('chapter-update-all', [
423 $otherChapter->getUrl() . '/edit'
425 $otherChapter->getUrl() => 'Edit'
429 public function test_chapter_delete_own_permission()
431 $this->giveUserPermissions($this->user, ['chapter-update-all']);
432 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
433 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
434 $this->checkAccessPermission('chapter-delete-own', [
435 $ownChapter->getUrl() . '/delete'
437 $ownChapter->getUrl() => 'Delete'
440 $bookUrl = $ownChapter->book->getUrl();
441 $this->visit($otherChapter->getUrl())
442 ->dontSeeInElement('.action-buttons', 'Delete')
443 ->visit($otherChapter->getUrl() . '/delete')
445 $this->visit($ownChapter->getUrl())->visit($ownChapter->getUrl() . '/delete')
447 ->seePageIs($bookUrl)
448 ->dontSeeInElement('.book-content', $ownChapter->name);
451 public function test_chapter_delete_all_permission()
453 $this->giveUserPermissions($this->user, ['chapter-update-all']);
454 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
455 $this->checkAccessPermission('chapter-delete-all', [
456 $otherChapter->getUrl() . '/delete'
458 $otherChapter->getUrl() => 'Delete'
461 $bookUrl = $otherChapter->book->getUrl();
462 $this->visit($otherChapter->getUrl())->visit($otherChapter->getUrl() . '/delete')
464 ->seePageIs($bookUrl)
465 ->dontSeeInElement('.book-content', $otherChapter->name);
468 public function test_page_create_own_permissions()
470 $book = \BookStack\Entities\Book::first();
471 $chapter = \BookStack\Entities\Chapter::first();
473 $entities = $this->createEntityChainBelongingToUser($this->user);
474 $ownBook = $entities['book'];
475 $ownChapter = $entities['chapter'];
477 $createUrl = $ownBook->getUrl('/create-page');
478 $createUrlChapter = $ownChapter->getUrl('/create-page');
479 $accessUrls = [$createUrl, $createUrlChapter];
481 foreach ($accessUrls as $url) {
482 $this->actingAs($this->user)->visit($url)
486 $this->checkAccessPermission('page-create-own', [], [
487 $ownBook->getUrl() => 'New Page',
488 $ownChapter->getUrl() => 'New Page'
491 $this->giveUserPermissions($this->user, ['page-create-own']);
493 foreach ($accessUrls as $index => $url) {
494 $this->actingAs($this->user)->visit($url);
495 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
496 $this->seePageIs($expectedUrl);
499 $this->visit($createUrl)
500 ->type('test page', 'name')
501 ->type('page desc', 'html')
503 ->seePageIs($ownBook->getUrl('/page/test-page'));
505 $this->visit($book->getUrl())
506 ->dontSeeInElement('.action-buttons', 'New Page')
507 ->visit($book->getUrl() . '/create-page')
509 $this->visit($chapter->getUrl())
510 ->dontSeeInElement('.action-buttons', 'New Page')
511 ->visit($chapter->getUrl() . '/create-page')
515 public function test_page_create_all_permissions()
517 $book = \BookStack\Entities\Book::take(1)->get()->first();
518 $chapter = \BookStack\Entities\Chapter::take(1)->get()->first();
519 $baseUrl = $book->getUrl() . '/page';
520 $createUrl = $book->getUrl('/create-page');
522 $createUrlChapter = $chapter->getUrl('/create-page');
523 $accessUrls = [$createUrl, $createUrlChapter];
525 foreach ($accessUrls as $url) {
526 $this->actingAs($this->user)->visit($url)
530 $this->checkAccessPermission('page-create-all', [], [
531 $book->getUrl() => 'New Page',
532 $chapter->getUrl() => 'New Page'
535 $this->giveUserPermissions($this->user, ['page-create-all']);
537 foreach ($accessUrls as $index => $url) {
538 $this->actingAs($this->user)->visit($url);
539 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
540 $this->seePageIs($expectedUrl);
543 $this->visit($createUrl)
544 ->type('test page', 'name')
545 ->type('page desc', 'html')
547 ->seePageIs($book->getUrl('/page/test-page'));
549 $this->visit($chapter->getUrl('/create-page'))
550 ->type('new test page', 'name')
551 ->type('page desc', 'html')
553 ->seePageIs($book->getUrl('/page/new-test-page'));
556 public function test_page_edit_own_permission()
558 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
559 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
560 $this->checkAccessPermission('page-update-own', [
561 $ownPage->getUrl() . '/edit'
563 $ownPage->getUrl() => 'Edit'
566 $this->visit($otherPage->getUrl())
567 ->dontSeeInElement('.action-buttons', 'Edit')
568 ->visit($otherPage->getUrl() . '/edit')
572 public function test_page_edit_all_permission()
574 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
575 $this->checkAccessPermission('page-update-all', [
576 $otherPage->getUrl() . '/edit'
578 $otherPage->getUrl() => 'Edit'
582 public function test_page_delete_own_permission()
584 $this->giveUserPermissions($this->user, ['page-update-all']);
585 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
586 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
587 $this->checkAccessPermission('page-delete-own', [
588 $ownPage->getUrl() . '/delete'
590 $ownPage->getUrl() => 'Delete'
593 $bookUrl = $ownPage->book->getUrl();
594 $this->visit($otherPage->getUrl())
595 ->dontSeeInElement('.action-buttons', 'Delete')
596 ->visit($otherPage->getUrl() . '/delete')
598 $this->visit($ownPage->getUrl())->visit($ownPage->getUrl() . '/delete')
600 ->seePageIs($bookUrl)
601 ->dontSeeInElement('.book-content', $ownPage->name);
604 public function test_page_delete_all_permission()
606 $this->giveUserPermissions($this->user, ['page-update-all']);
607 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
608 $this->checkAccessPermission('page-delete-all', [
609 $otherPage->getUrl() . '/delete'
611 $otherPage->getUrl() => 'Delete'
614 $bookUrl = $otherPage->book->getUrl();
615 $this->visit($otherPage->getUrl())->visit($otherPage->getUrl() . '/delete')
617 ->seePageIs($bookUrl)
618 ->dontSeeInElement('.book-content', $otherPage->name);
621 public function test_public_role_visible_in_user_edit_screen()
623 $user = \BookStack\Auth\User::first();
624 $this->asAdmin()->visit('/settings/users/' . $user->id)
625 ->seeElement('#roles-admin')
626 ->seeElement('#roles-public');
629 public function test_public_role_visible_in_role_listing()
631 $this->asAdmin()->visit('/settings/roles')
636 public function test_public_role_visible_in_default_role_setting()
638 $this->asAdmin()->visit('/settings')
639 ->seeElement('[data-role-name="admin"]')
640 ->seeElement('[data-role-name="public"]');
644 public function test_public_role_not_deleteable()
646 $this->asAdmin()->visit('/settings/roles')
649 ->click('Delete Role')
652 ->see('Cannot be deleted');
655 public function test_image_delete_own_permission()
657 $this->giveUserPermissions($this->user, ['image-update-all']);
658 $page = \BookStack\Entities\Page::first();
659 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]);
661 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
662 ->seeStatusCode(403);
664 $this->giveUserPermissions($this->user, ['image-delete-own']);
666 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
668 ->dontSeeInDatabase('images', ['id' => $image->id]);
671 public function test_image_delete_all_permission()
673 $this->giveUserPermissions($this->user, ['image-update-all']);
674 $admin = $this->getAdmin();
675 $page = \BookStack\Entities\Page::first();
676 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
678 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
679 ->seeStatusCode(403);
681 $this->giveUserPermissions($this->user, ['image-delete-own']);
683 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
684 ->seeStatusCode(403);
686 $this->giveUserPermissions($this->user, ['image-delete-all']);
688 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
690 ->dontSeeInDatabase('images', ['id' => $image->id]);
693 public function test_role_permission_removal()
695 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
696 $page = Page::first();
697 $viewerRole = \BookStack\Auth\Role::getRole('viewer');
698 $viewer = $this->getViewer();
699 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(200);
701 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
702 'display_name' => $viewerRole->display_name,
703 'description' => $viewerRole->description,
705 ])->assertResponseStatus(302);
707 $this->expectException(HttpException::class);
708 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(404);
711 public function test_empty_state_actions_not_visible_without_permission()
713 $admin = $this->getAdmin();
715 $book = factory(\BookStack\Entities\Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
716 $this->updateEntityPermissions($book);
717 $this->actingAs($this->getViewer())->visit($book->getUrl())
718 ->dontSee('Create a new page')
719 ->dontSee('Add a chapter');
722 $chapter = factory(\BookStack\Entities\Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
723 $this->updateEntityPermissions($chapter);
724 $this->actingAs($this->getViewer())->visit($chapter->getUrl())
725 ->dontSee('Create a new page')
726 ->dontSee('Sort the current book');
729 public function test_comment_create_permission () {
730 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
732 $this->actingAs($this->user)->addComment($ownPage);
734 $this->assertResponseStatus(403);
736 $this->giveUserPermissions($this->user, ['comment-create-all']);
738 $this->actingAs($this->user)->addComment($ownPage);
739 $this->assertResponseStatus(200);
743 public function test_comment_update_own_permission () {
744 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
745 $this->giveUserPermissions($this->user, ['comment-create-all']);
746 $commentId = $this->actingAs($this->user)->addComment($ownPage);
748 // no comment-update-own
749 $this->actingAs($this->user)->updateComment($commentId);
750 $this->assertResponseStatus(403);
752 $this->giveUserPermissions($this->user, ['comment-update-own']);
754 // now has comment-update-own
755 $this->actingAs($this->user)->updateComment($commentId);
756 $this->assertResponseStatus(200);
759 public function test_comment_update_all_permission () {
760 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
761 $commentId = $this->asAdmin()->addComment($ownPage);
763 // no comment-update-all
764 $this->actingAs($this->user)->updateComment($commentId);
765 $this->assertResponseStatus(403);
767 $this->giveUserPermissions($this->user, ['comment-update-all']);
769 // now has comment-update-all
770 $this->actingAs($this->user)->updateComment($commentId);
771 $this->assertResponseStatus(200);
774 public function test_comment_delete_own_permission () {
775 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
776 $this->giveUserPermissions($this->user, ['comment-create-all']);
777 $commentId = $this->actingAs($this->user)->addComment($ownPage);
779 // no comment-delete-own
780 $this->actingAs($this->user)->deleteComment($commentId);
781 $this->assertResponseStatus(403);
783 $this->giveUserPermissions($this->user, ['comment-delete-own']);
785 // now has comment-update-own
786 $this->actingAs($this->user)->deleteComment($commentId);
787 $this->assertResponseStatus(200);
790 public function test_comment_delete_all_permission () {
791 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
792 $commentId = $this->asAdmin()->addComment($ownPage);
794 // no comment-delete-all
795 $this->actingAs($this->user)->deleteComment($commentId);
796 $this->assertResponseStatus(403);
798 $this->giveUserPermissions($this->user, ['comment-delete-all']);
800 // now has comment-delete-all
801 $this->actingAs($this->user)->deleteComment($commentId);
802 $this->assertResponseStatus(200);
805 private function addComment($page) {
806 $comment = factory(\BookStack\Actions\Comment::class)->make();
807 $url = "/ajax/page/$page->id/comment";
809 'text' => $comment->text,
810 'html' => $comment->html
813 $this->postJson($url, $request);
814 $comment = $page->comments()->first();
815 return $comment === null ? null : $comment->id;
818 private function updateComment($commentId) {
819 $comment = factory(\BookStack\Actions\Comment::class)->make();
820 $url = "/ajax/comment/$commentId";
822 'text' => $comment->text,
823 'html' => $comment->html
826 return $this->putJson($url, $request);
829 private function deleteComment($commentId) {
830 $url = '/ajax/comment/' . $commentId;
831 return $this->json('DELETE', $url);