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_manage_user_permission()
83 $this->actingAs($this->user)->visit('/settings/users')
85 $this->giveUserPermissions($this->user, ['users-manage']);
86 $this->actingAs($this->user)->visit('/settings/users')
87 ->seePageIs('/settings/users');
90 public function test_user_roles_manage_permission()
92 $this->actingAs($this->user)->visit('/settings/roles')
93 ->seePageIs('/')->visit('/settings/roles/1')->seePageIs('/');
94 $this->giveUserPermissions($this->user, ['user-roles-manage']);
95 $this->actingAs($this->user)->visit('/settings/roles')
96 ->seePageIs('/settings/roles')->click('Admin')
100 public function test_settings_manage_permission()
102 $this->actingAs($this->user)->visit('/settings')
104 $this->giveUserPermissions($this->user, ['settings-manage']);
105 $this->actingAs($this->user)->visit('/settings')
106 ->seePageIs('/settings')->press('Save Settings')->see('Settings Saved');
109 public function test_restrictions_manage_all_permission()
111 $page = \BookStack\Entities\Page::take(1)->get()->first();
112 $this->actingAs($this->user)->visit($page->getUrl())
113 ->dontSee('Permissions')
114 ->visit($page->getUrl() . '/permissions')
116 $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
117 $this->actingAs($this->user)->visit($page->getUrl())
119 ->click('Permissions')
120 ->see('Page Permissions')->seePageIs($page->getUrl() . '/permissions');
123 public function test_restrictions_manage_own_permission()
125 $otherUsersPage = \BookStack\Entities\Page::first();
126 $content = $this->createEntityChainBelongingToUser($this->user);
127 // Check can't restrict other's content
128 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
129 ->dontSee('Permissions')
130 ->visit($otherUsersPage->getUrl() . '/permissions')
132 // Check can't restrict own content
133 $this->actingAs($this->user)->visit($content['page']->getUrl())
134 ->dontSee('Permissions')
135 ->visit($content['page']->getUrl() . '/permissions')
138 $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
140 // Check can't restrict other's content
141 $this->actingAs($this->user)->visit($otherUsersPage->getUrl())
142 ->dontSee('Permissions')
143 ->visit($otherUsersPage->getUrl() . '/permissions')
145 // Check can restrict own content
146 $this->actingAs($this->user)->visit($content['page']->getUrl())
148 ->click('Permissions')
149 ->seePageIs($content['page']->getUrl() . '/permissions');
153 * Check a standard entity access permission
154 * @param string $permission
155 * @param array $accessUrls Urls that are only accessible after having the permission
156 * @param array $visibles Check this text, In the buttons toolbar, is only visible with the permission
158 private function checkAccessPermission($permission, $accessUrls = [], $visibles = [])
160 foreach ($accessUrls as $url) {
161 $this->actingAs($this->user)->visit($url)
164 foreach ($visibles as $url => $text) {
165 $this->actingAs($this->user)->visit($url)
166 ->dontSeeInElement('.action-buttons',$text);
169 $this->giveUserPermissions($this->user, [$permission]);
171 foreach ($accessUrls as $url) {
172 $this->actingAs($this->user)->visit($url)
175 foreach ($visibles as $url => $text) {
176 $this->actingAs($this->user)->visit($url)
181 public function test_bookshelves_create_all_permissions()
183 $this->checkAccessPermission('bookshelf-create-all', [
186 '/shelves' => 'Create New Shelf'
189 $this->visit('/create-shelf')
190 ->type('test shelf', 'name')
191 ->type('shelf desc', 'description')
192 ->press('Save Shelf')
193 ->seePageIs('/shelves/test-shelf');
196 public function test_bookshelves_edit_own_permission()
198 $otherShelf = Bookshelf::first();
199 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
200 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
201 $this->regenEntityPermissions($ownShelf);
203 $this->checkAccessPermission('bookshelf-update-own', [
204 $ownShelf->getUrl('/edit')
206 $ownShelf->getUrl() => 'Edit'
209 $this->visit($otherShelf->getUrl())
210 ->dontSeeInElement('.action-buttons', 'Edit')
211 ->visit($otherShelf->getUrl('/edit'))
215 public function test_bookshelves_edit_all_permission()
217 $otherShelf = \BookStack\Entities\Bookshelf::first();
218 $this->checkAccessPermission('bookshelf-update-all', [
219 $otherShelf->getUrl('/edit')
221 $otherShelf->getUrl() => 'Edit'
225 public function test_bookshelves_delete_own_permission()
227 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
228 $otherShelf = \BookStack\Entities\Bookshelf::first();
229 $ownShelf = $this->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
230 $ownShelf->forceFill(['created_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
231 $this->regenEntityPermissions($ownShelf);
233 $this->checkAccessPermission('bookshelf-delete-own', [
234 $ownShelf->getUrl('/delete')
236 $ownShelf->getUrl() => 'Delete'
239 $this->visit($otherShelf->getUrl())
240 ->dontSeeInElement('.action-buttons', 'Delete')
241 ->visit($otherShelf->getUrl('/delete'))
243 $this->visit($ownShelf->getUrl())->visit($ownShelf->getUrl('/delete'))
245 ->seePageIs('/shelves')
246 ->dontSee($ownShelf->name);
249 public function test_bookshelves_delete_all_permission()
251 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
252 $otherShelf = \BookStack\Entities\Bookshelf::first();
253 $this->checkAccessPermission('bookshelf-delete-all', [
254 $otherShelf->getUrl('/delete')
256 $otherShelf->getUrl() => 'Delete'
259 $this->visit($otherShelf->getUrl())->visit($otherShelf->getUrl('/delete'))
261 ->seePageIs('/shelves')
262 ->dontSee($otherShelf->name);
265 public function test_books_create_all_permissions()
267 $this->checkAccessPermission('book-create-all', [
270 '/books' => 'Create New Book'
273 $this->visit('/create-book')
274 ->type('test book', 'name')
275 ->type('book desc', 'description')
277 ->seePageIs('/books/test-book');
280 public function test_books_edit_own_permission()
282 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
283 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
284 $this->checkAccessPermission('book-update-own', [
285 $ownBook->getUrl() . '/edit'
287 $ownBook->getUrl() => 'Edit'
290 $this->visit($otherBook->getUrl())
291 ->dontSeeInElement('.action-buttons', 'Edit')
292 ->visit($otherBook->getUrl() . '/edit')
296 public function test_books_edit_all_permission()
298 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
299 $this->checkAccessPermission('book-update-all', [
300 $otherBook->getUrl() . '/edit'
302 $otherBook->getUrl() => 'Edit'
306 public function test_books_delete_own_permission()
308 $this->giveUserPermissions($this->user, ['book-update-all']);
309 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
310 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
311 $this->checkAccessPermission('book-delete-own', [
312 $ownBook->getUrl() . '/delete'
314 $ownBook->getUrl() => 'Delete'
317 $this->visit($otherBook->getUrl())
318 ->dontSeeInElement('.action-buttons', 'Delete')
319 ->visit($otherBook->getUrl() . '/delete')
321 $this->visit($ownBook->getUrl())->visit($ownBook->getUrl() . '/delete')
323 ->seePageIs('/books')
324 ->dontSee($ownBook->name);
327 public function test_books_delete_all_permission()
329 $this->giveUserPermissions($this->user, ['book-update-all']);
330 $otherBook = \BookStack\Entities\Book::take(1)->get()->first();
331 $this->checkAccessPermission('book-delete-all', [
332 $otherBook->getUrl() . '/delete'
334 $otherBook->getUrl() => 'Delete'
337 $this->visit($otherBook->getUrl())->visit($otherBook->getUrl() . '/delete')
339 ->seePageIs('/books')
340 ->dontSee($otherBook->name);
343 public function test_chapter_create_own_permissions()
345 $book = \BookStack\Entities\Book::take(1)->get()->first();
346 $ownBook = $this->createEntityChainBelongingToUser($this->user)['book'];
347 $this->checkAccessPermission('chapter-create-own', [
348 $ownBook->getUrl('/create-chapter')
350 $ownBook->getUrl() => 'New Chapter'
353 $this->visit($ownBook->getUrl('/create-chapter'))
354 ->type('test chapter', 'name')
355 ->type('chapter desc', 'description')
356 ->press('Save Chapter')
357 ->seePageIs($ownBook->getUrl('/chapter/test-chapter'));
359 $this->visit($book->getUrl())
360 ->dontSeeInElement('.action-buttons', 'New Chapter')
361 ->visit($book->getUrl('/create-chapter'))
365 public function test_chapter_create_all_permissions()
367 $book = \BookStack\Entities\Book::take(1)->get()->first();
368 $this->checkAccessPermission('chapter-create-all', [
369 $book->getUrl('/create-chapter')
371 $book->getUrl() => 'New Chapter'
374 $this->visit($book->getUrl('/create-chapter'))
375 ->type('test chapter', 'name')
376 ->type('chapter desc', 'description')
377 ->press('Save Chapter')
378 ->seePageIs($book->getUrl('/chapter/test-chapter'));
381 public function test_chapter_edit_own_permission()
383 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
384 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
385 $this->checkAccessPermission('chapter-update-own', [
386 $ownChapter->getUrl() . '/edit'
388 $ownChapter->getUrl() => 'Edit'
391 $this->visit($otherChapter->getUrl())
392 ->dontSeeInElement('.action-buttons', 'Edit')
393 ->visit($otherChapter->getUrl() . '/edit')
397 public function test_chapter_edit_all_permission()
399 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
400 $this->checkAccessPermission('chapter-update-all', [
401 $otherChapter->getUrl() . '/edit'
403 $otherChapter->getUrl() => 'Edit'
407 public function test_chapter_delete_own_permission()
409 $this->giveUserPermissions($this->user, ['chapter-update-all']);
410 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
411 $ownChapter = $this->createEntityChainBelongingToUser($this->user)['chapter'];
412 $this->checkAccessPermission('chapter-delete-own', [
413 $ownChapter->getUrl() . '/delete'
415 $ownChapter->getUrl() => 'Delete'
418 $bookUrl = $ownChapter->book->getUrl();
419 $this->visit($otherChapter->getUrl())
420 ->dontSeeInElement('.action-buttons', 'Delete')
421 ->visit($otherChapter->getUrl() . '/delete')
423 $this->visit($ownChapter->getUrl())->visit($ownChapter->getUrl() . '/delete')
425 ->seePageIs($bookUrl)
426 ->dontSeeInElement('.book-content', $ownChapter->name);
429 public function test_chapter_delete_all_permission()
431 $this->giveUserPermissions($this->user, ['chapter-update-all']);
432 $otherChapter = \BookStack\Entities\Chapter::take(1)->get()->first();
433 $this->checkAccessPermission('chapter-delete-all', [
434 $otherChapter->getUrl() . '/delete'
436 $otherChapter->getUrl() => 'Delete'
439 $bookUrl = $otherChapter->book->getUrl();
440 $this->visit($otherChapter->getUrl())->visit($otherChapter->getUrl() . '/delete')
442 ->seePageIs($bookUrl)
443 ->dontSeeInElement('.book-content', $otherChapter->name);
446 public function test_page_create_own_permissions()
448 $book = \BookStack\Entities\Book::first();
449 $chapter = \BookStack\Entities\Chapter::first();
451 $entities = $this->createEntityChainBelongingToUser($this->user);
452 $ownBook = $entities['book'];
453 $ownChapter = $entities['chapter'];
455 $createUrl = $ownBook->getUrl('/create-page');
456 $createUrlChapter = $ownChapter->getUrl('/create-page');
457 $accessUrls = [$createUrl, $createUrlChapter];
459 foreach ($accessUrls as $url) {
460 $this->actingAs($this->user)->visit($url)
464 $this->checkAccessPermission('page-create-own', [], [
465 $ownBook->getUrl() => 'New Page',
466 $ownChapter->getUrl() => 'New Page'
469 $this->giveUserPermissions($this->user, ['page-create-own']);
471 foreach ($accessUrls as $index => $url) {
472 $this->actingAs($this->user)->visit($url);
473 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
474 $this->seePageIs($expectedUrl);
477 $this->visit($createUrl)
478 ->type('test page', 'name')
479 ->type('page desc', 'html')
481 ->seePageIs($ownBook->getUrl('/page/test-page'));
483 $this->visit($book->getUrl())
484 ->dontSeeInElement('.action-buttons', 'New Page')
485 ->visit($book->getUrl() . '/create-page')
487 $this->visit($chapter->getUrl())
488 ->dontSeeInElement('.action-buttons', 'New Page')
489 ->visit($chapter->getUrl() . '/create-page')
493 public function test_page_create_all_permissions()
495 $book = \BookStack\Entities\Book::take(1)->get()->first();
496 $chapter = \BookStack\Entities\Chapter::take(1)->get()->first();
497 $baseUrl = $book->getUrl() . '/page';
498 $createUrl = $book->getUrl('/create-page');
500 $createUrlChapter = $chapter->getUrl('/create-page');
501 $accessUrls = [$createUrl, $createUrlChapter];
503 foreach ($accessUrls as $url) {
504 $this->actingAs($this->user)->visit($url)
508 $this->checkAccessPermission('page-create-all', [], [
509 $book->getUrl() => 'New Page',
510 $chapter->getUrl() => 'New Page'
513 $this->giveUserPermissions($this->user, ['page-create-all']);
515 foreach ($accessUrls as $index => $url) {
516 $this->actingAs($this->user)->visit($url);
517 $expectedUrl = \BookStack\Entities\Page::where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
518 $this->seePageIs($expectedUrl);
521 $this->visit($createUrl)
522 ->type('test page', 'name')
523 ->type('page desc', 'html')
525 ->seePageIs($book->getUrl('/page/test-page'));
527 $this->visit($chapter->getUrl('/create-page'))
528 ->type('new test page', 'name')
529 ->type('page desc', 'html')
531 ->seePageIs($book->getUrl('/page/new-test-page'));
534 public function test_page_edit_own_permission()
536 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
537 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
538 $this->checkAccessPermission('page-update-own', [
539 $ownPage->getUrl() . '/edit'
541 $ownPage->getUrl() => 'Edit'
544 $this->visit($otherPage->getUrl())
545 ->dontSeeInElement('.action-buttons', 'Edit')
546 ->visit($otherPage->getUrl() . '/edit')
550 public function test_page_edit_all_permission()
552 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
553 $this->checkAccessPermission('page-update-all', [
554 $otherPage->getUrl() . '/edit'
556 $otherPage->getUrl() => 'Edit'
560 public function test_page_delete_own_permission()
562 $this->giveUserPermissions($this->user, ['page-update-all']);
563 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
564 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
565 $this->checkAccessPermission('page-delete-own', [
566 $ownPage->getUrl() . '/delete'
568 $ownPage->getUrl() => 'Delete'
571 $bookUrl = $ownPage->book->getUrl();
572 $this->visit($otherPage->getUrl())
573 ->dontSeeInElement('.action-buttons', 'Delete')
574 ->visit($otherPage->getUrl() . '/delete')
576 $this->visit($ownPage->getUrl())->visit($ownPage->getUrl() . '/delete')
578 ->seePageIs($bookUrl)
579 ->dontSeeInElement('.book-content', $ownPage->name);
582 public function test_page_delete_all_permission()
584 $this->giveUserPermissions($this->user, ['page-update-all']);
585 $otherPage = \BookStack\Entities\Page::take(1)->get()->first();
586 $this->checkAccessPermission('page-delete-all', [
587 $otherPage->getUrl() . '/delete'
589 $otherPage->getUrl() => 'Delete'
592 $bookUrl = $otherPage->book->getUrl();
593 $this->visit($otherPage->getUrl())->visit($otherPage->getUrl() . '/delete')
595 ->seePageIs($bookUrl)
596 ->dontSeeInElement('.book-content', $otherPage->name);
599 public function test_public_role_visible_in_user_edit_screen()
601 $user = \BookStack\Auth\User::first();
602 $this->asAdmin()->visit('/settings/users/' . $user->id)
603 ->seeElement('#roles-admin')
604 ->seeElement('#roles-public');
607 public function test_public_role_visible_in_role_listing()
609 $this->asAdmin()->visit('/settings/roles')
614 public function test_public_role_visible_in_default_role_setting()
616 $this->asAdmin()->visit('/settings')
617 ->seeElement('[data-role-name="admin"]')
618 ->seeElement('[data-role-name="public"]');
622 public function test_public_role_not_deleteable()
624 $this->asAdmin()->visit('/settings/roles')
627 ->click('Delete Role')
630 ->see('Cannot be deleted');
633 public function test_image_delete_own_permission()
635 $this->giveUserPermissions($this->user, ['image-update-all']);
636 $page = \BookStack\Entities\Page::first();
637 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $this->user->id, 'updated_by' => $this->user->id]);
639 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
640 ->seeStatusCode(403);
642 $this->giveUserPermissions($this->user, ['image-delete-own']);
644 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
646 ->dontSeeInDatabase('images', ['id' => $image->id]);
649 public function test_image_delete_all_permission()
651 $this->giveUserPermissions($this->user, ['image-update-all']);
652 $admin = $this->getAdmin();
653 $page = \BookStack\Entities\Page::first();
654 $image = factory(\BookStack\Uploads\Image::class)->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
656 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
657 ->seeStatusCode(403);
659 $this->giveUserPermissions($this->user, ['image-delete-own']);
661 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
662 ->seeStatusCode(403);
664 $this->giveUserPermissions($this->user, ['image-delete-all']);
666 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)
668 ->dontSeeInDatabase('images', ['id' => $image->id]);
671 public function test_role_permission_removal()
673 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
674 $page = Page::first();
675 $viewerRole = \BookStack\Auth\Role::getRole('viewer');
676 $viewer = $this->getViewer();
677 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(200);
679 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
680 'display_name' => $viewerRole->display_name,
681 'description' => $viewerRole->description,
683 ])->assertResponseStatus(302);
685 $this->expectException(HttpException::class);
686 $this->actingAs($viewer)->visit($page->getUrl())->assertResponseStatus(404);
689 public function test_empty_state_actions_not_visible_without_permission()
691 $admin = $this->getAdmin();
693 $book = factory(\BookStack\Entities\Book::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
694 $this->updateEntityPermissions($book);
695 $this->actingAs($this->getViewer())->visit($book->getUrl())
696 ->dontSee('Create a new page')
697 ->dontSee('Add a chapter');
700 $chapter = factory(\BookStack\Entities\Chapter::class)->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
701 $this->updateEntityPermissions($chapter);
702 $this->actingAs($this->getViewer())->visit($chapter->getUrl())
703 ->dontSee('Create a new page')
704 ->dontSee('Sort the current book');
707 public function test_comment_create_permission () {
708 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
710 $this->actingAs($this->user)->addComment($ownPage);
712 $this->assertResponseStatus(403);
714 $this->giveUserPermissions($this->user, ['comment-create-all']);
716 $this->actingAs($this->user)->addComment($ownPage);
717 $this->assertResponseStatus(200);
721 public function test_comment_update_own_permission () {
722 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
723 $this->giveUserPermissions($this->user, ['comment-create-all']);
724 $commentId = $this->actingAs($this->user)->addComment($ownPage);
726 // no comment-update-own
727 $this->actingAs($this->user)->updateComment($commentId);
728 $this->assertResponseStatus(403);
730 $this->giveUserPermissions($this->user, ['comment-update-own']);
732 // now has comment-update-own
733 $this->actingAs($this->user)->updateComment($commentId);
734 $this->assertResponseStatus(200);
737 public function test_comment_update_all_permission () {
738 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
739 $commentId = $this->asAdmin()->addComment($ownPage);
741 // no comment-update-all
742 $this->actingAs($this->user)->updateComment($commentId);
743 $this->assertResponseStatus(403);
745 $this->giveUserPermissions($this->user, ['comment-update-all']);
747 // now has comment-update-all
748 $this->actingAs($this->user)->updateComment($commentId);
749 $this->assertResponseStatus(200);
752 public function test_comment_delete_own_permission () {
753 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
754 $this->giveUserPermissions($this->user, ['comment-create-all']);
755 $commentId = $this->actingAs($this->user)->addComment($ownPage);
757 // no comment-delete-own
758 $this->actingAs($this->user)->deleteComment($commentId);
759 $this->assertResponseStatus(403);
761 $this->giveUserPermissions($this->user, ['comment-delete-own']);
763 // now has comment-update-own
764 $this->actingAs($this->user)->deleteComment($commentId);
765 $this->assertResponseStatus(200);
768 public function test_comment_delete_all_permission () {
769 $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
770 $commentId = $this->asAdmin()->addComment($ownPage);
772 // no comment-delete-all
773 $this->actingAs($this->user)->deleteComment($commentId);
774 $this->assertResponseStatus(403);
776 $this->giveUserPermissions($this->user, ['comment-delete-all']);
778 // now has comment-delete-all
779 $this->actingAs($this->user)->deleteComment($commentId);
780 $this->assertResponseStatus(200);
783 private function addComment($page) {
784 $comment = factory(\BookStack\Actions\Comment::class)->make();
785 $url = "/ajax/page/$page->id/comment";
787 'text' => $comment->text,
788 'html' => $comment->html
791 $this->postJson($url, $request);
792 $comment = $page->comments()->first();
793 return $comment === null ? null : $comment->id;
796 private function updateComment($commentId) {
797 $comment = factory(\BookStack\Actions\Comment::class)->make();
798 $url = "/ajax/comment/$commentId";
800 'text' => $comment->text,
801 'html' => $comment->html
804 return $this->putJson($url, $request);
807 private function deleteComment($commentId) {
808 $url = '/ajax/comment/' . $commentId;
809 return $this->json('DELETE', $url);