3 namespace Tests\Permissions;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\Comment;
7 use BookStack\Auth\Role;
8 use BookStack\Auth\User;
9 use BookStack\Entities\Models\Book;
10 use BookStack\Entities\Models\Bookshelf;
11 use BookStack\Entities\Models\Chapter;
12 use BookStack\Entities\Models\Entity;
13 use BookStack\Entities\Models\Page;
14 use BookStack\Uploads\Image;
15 use Illuminate\Testing\TestResponse;
18 class RolesTest extends TestCase
22 protected function setUp(): void
25 $this->user = $this->getViewer();
28 public function test_admin_can_see_settings()
30 $this->asAdmin()->get('/settings/features')->assertSee('Settings');
33 public function test_cannot_delete_admin_role()
35 $adminRole = Role::getRole('admin');
36 $deletePageUrl = '/settings/roles/delete/' . $adminRole->id;
38 $this->asAdmin()->get($deletePageUrl);
39 $this->delete($deletePageUrl)->assertRedirect($deletePageUrl);
40 $this->get($deletePageUrl)->assertSee('cannot be deleted');
43 public function test_role_cannot_be_deleted_if_default()
45 $newRole = $this->createNewRole();
46 $this->setSettings(['registration-role' => $newRole->id]);
48 $deletePageUrl = '/settings/roles/delete/' . $newRole->id;
49 $this->asAdmin()->get($deletePageUrl);
50 $this->delete($deletePageUrl)->assertRedirect($deletePageUrl);
51 $this->get($deletePageUrl)->assertSee('cannot be deleted');
54 public function test_role_create_update_delete_flow()
56 $testRoleName = 'Test Role';
57 $testRoleDesc = 'a little test description';
58 $testRoleUpdateName = 'An Super Updated role';
61 $resp = $this->asAdmin()->get('/settings/features');
62 $this->withHtml($resp)->assertElementContains('a[href="' . url('/settings/roles') . '"]', 'Roles');
64 $resp = $this->get('/settings/roles');
65 $this->withHtml($resp)->assertElementContains('a[href="' . url('/settings/roles/new') . '"]', 'Create New Role');
67 $resp = $this->get('/settings/roles/new');
68 $this->withHtml($resp)->assertElementContains('form[action="' . url('/settings/roles/new') . '"]', 'Save Role');
70 $resp = $this->post('/settings/roles/new', [
71 'display_name' => $testRoleName,
72 'description' => $testRoleDesc,
74 $resp->assertRedirect('/settings/roles');
76 $resp = $this->get('/settings/roles');
77 $resp->assertSee($testRoleName);
78 $resp->assertSee($testRoleDesc);
79 $this->assertDatabaseHas('roles', [
80 'display_name' => $testRoleName,
81 'description' => $testRoleDesc,
82 'mfa_enforced' => false,
85 /** @var Role $role */
86 $role = Role::query()->where('display_name', '=', $testRoleName)->first();
89 $resp = $this->get('/settings/roles/' . $role->id);
90 $resp->assertSee($testRoleName);
91 $resp->assertSee($testRoleDesc);
92 $this->withHtml($resp)->assertElementContains('form[action="' . url('/settings/roles/' . $role->id) . '"]', 'Save Role');
94 $resp = $this->put('/settings/roles/' . $role->id, [
95 'display_name' => $testRoleUpdateName,
96 'description' => $testRoleDesc,
97 'mfa_enforced' => 'true',
99 $resp->assertRedirect('/settings/roles');
100 $this->assertDatabaseHas('roles', [
101 'display_name' => $testRoleUpdateName,
102 'description' => $testRoleDesc,
103 'mfa_enforced' => true,
107 $resp = $this->get('/settings/roles/' . $role->id);
108 $this->withHtml($resp)->assertElementContains('a[href="' . url("/settings/roles/delete/$role->id") . '"]', 'Delete Role');
110 $resp = $this->get("/settings/roles/delete/$role->id");
111 $resp->assertSee($testRoleUpdateName);
112 $this->withHtml($resp)->assertElementContains('form[action="' . url("/settings/roles/delete/$role->id") . '"]', 'Confirm');
114 $resp = $this->delete("/settings/roles/delete/$role->id");
115 $resp->assertRedirect('/settings/roles');
116 $this->get('/settings/roles')->assertSee('Role successfully deleted');
117 $this->assertActivityExists(ActivityType::ROLE_DELETE);
120 public function test_admin_role_cannot_be_removed_if_user_last_admin()
122 /** @var Role $adminRole */
123 $adminRole = Role::query()->where('system_name', '=', 'admin')->first();
124 $adminUser = $this->getAdmin();
125 $adminRole->users()->where('id', '!=', $adminUser->id)->delete();
126 $this->assertEquals(1, $adminRole->users()->count());
128 $viewerRole = $this->getViewer()->roles()->first();
130 $editUrl = '/settings/users/' . $adminUser->id;
131 $resp = $this->actingAs($adminUser)->put($editUrl, [
132 'name' => $adminUser->name,
133 'email' => $adminUser->email,
135 'viewer' => strval($viewerRole->id),
139 $resp->assertRedirect($editUrl);
141 $resp = $this->get($editUrl);
142 $resp->assertSee('This user is the only user assigned to the administrator role');
145 public function test_migrate_users_on_delete_works()
147 /** @var Role $roleA */
148 $roleA = Role::query()->create(['display_name' => 'Delete Test A']);
149 /** @var Role $roleB */
150 $roleB = Role::query()->create(['display_name' => 'Delete Test B']);
151 $this->user->attachRole($roleB);
153 $this->assertCount(0, $roleA->users()->get());
154 $this->assertCount(1, $roleB->users()->get());
156 $deletePage = $this->asAdmin()->get("/settings/roles/delete/$roleB->id");
157 $this->withHtml($deletePage)->assertElementExists('select[name=migrate_role_id]');
158 $this->asAdmin()->delete("/settings/roles/delete/$roleB->id", [
159 'migrate_role_id' => $roleA->id,
162 $this->assertCount(1, $roleA->users()->get());
163 $this->assertEquals($this->user->id, $roleA->users()->first()->id);
166 public function test_image_view_notice_shown_on_role_form()
168 /** @var Role $role */
169 $role = Role::query()->first();
170 $this->asAdmin()->get("/settings/roles/{$role->id}")
171 ->assertSee('Actual access of uploaded image files will be dependant upon system image storage option');
174 public function test_copy_role_button_shown()
176 /** @var Role $role */
177 $role = Role::query()->first();
178 $resp = $this->asAdmin()->get("/settings/roles/{$role->id}");
179 $this->withHtml($resp)->assertElementContains('a[href$="/roles/new?copy_from=' . $role->id . '"]', 'Copy');
182 public function test_copy_from_param_on_create_prefills_with_other_role_data()
184 /** @var Role $role */
185 $role = Role::query()->first();
186 $resp = $this->asAdmin()->get("/settings/roles/new?copy_from={$role->id}");
188 $this->withHtml($resp)->assertElementExists('input[name="display_name"][value="' . ($role->display_name . ' (Copy)') . '"]');
191 public function test_manage_user_permission()
193 $this->actingAs($this->user)->get('/settings/users')->assertRedirect('/');
194 $this->giveUserPermissions($this->user, ['users-manage']);
195 $this->actingAs($this->user)->get('/settings/users')->assertOk();
198 public function test_manage_users_permission_shows_link_in_header_if_does_not_have_settings_manage_permision()
200 $usersLink = 'href="' . url('/settings/users') . '"';
201 $this->actingAs($this->user)->get('/')->assertDontSee($usersLink, false);
202 $this->giveUserPermissions($this->user, ['users-manage']);
203 $this->actingAs($this->user)->get('/')->assertSee($usersLink, false);
204 $this->giveUserPermissions($this->user, ['settings-manage', 'users-manage']);
205 $this->actingAs($this->user)->get('/')->assertDontSee($usersLink, false);
208 public function test_user_cannot_change_email_unless_they_have_manage_users_permission()
210 $userProfileUrl = '/settings/users/' . $this->user->id;
211 $originalEmail = $this->user->email;
212 $this->actingAs($this->user);
214 $resp = $this->get($userProfileUrl)
216 $this->withHtml($resp)->assertElementExists('input[name=email][disabled]');
217 $this->put($userProfileUrl, [
218 'name' => 'my_new_name',
221 $this->assertDatabaseHas('users', [
222 'id' => $this->user->id,
223 'email' => $originalEmail,
224 'name' => 'my_new_name',
227 $this->giveUserPermissions($this->user, ['users-manage']);
229 $resp = $this->get($userProfileUrl)
231 $this->withHtml($resp)->assertElementNotExists('input[name=email][disabled]')
232 ->assertElementExists('input[name=email]');
233 $this->put($userProfileUrl, [
234 'name' => 'my_new_name_2',
238 $this->assertDatabaseHas('users', [
239 'id' => $this->user->id,
241 'name' => 'my_new_name_2',
245 public function test_user_roles_manage_permission()
247 $this->actingAs($this->user)->get('/settings/roles')->assertRedirect('/');
248 $this->get('/settings/roles/1')->assertRedirect('/');
249 $this->giveUserPermissions($this->user, ['user-roles-manage']);
250 $this->actingAs($this->user)->get('/settings/roles')->assertOk();
251 $this->get('/settings/roles/1')
253 ->assertSee('Admin');
256 public function test_settings_manage_permission()
258 $this->actingAs($this->user)->get('/settings/features')->assertRedirect('/');
259 $this->giveUserPermissions($this->user, ['settings-manage']);
260 $this->get('/settings/features')->assertOk();
262 $resp = $this->post('/settings/features', []);
263 $resp->assertRedirect('/settings/features');
264 $resp = $this->get('/settings/features');
265 $resp->assertSee('Settings saved');
268 public function test_restrictions_manage_all_permission()
270 $page = Page::query()->get()->first();
272 $this->actingAs($this->user)->get($page->getUrl())->assertDontSee('Permissions');
273 $this->get($page->getUrl('/permissions'))->assertRedirect('/');
275 $this->giveUserPermissions($this->user, ['restrictions-manage-all']);
277 $this->actingAs($this->user)->get($page->getUrl())->assertSee('Permissions');
279 $this->get($page->getUrl('/permissions'))
281 ->assertSee('Page Permissions');
284 public function test_restrictions_manage_own_permission()
286 /** @var Page $otherUsersPage */
287 $otherUsersPage = Page::query()->first();
288 $content = $this->entities->createChainBelongingToUser($this->user);
290 // Set a different creator on the page we're checking to ensure
291 // that the owner fields are checked
292 $page = $content['page']; /** @var Page $page */
293 $page->created_by = $otherUsersPage->id;
294 $page->owned_by = $this->user->id;
297 // Check can't restrict other's content
298 $this->actingAs($this->user)->get($otherUsersPage->getUrl())->assertDontSee('Permissions');
299 $this->get($otherUsersPage->getUrl('/permissions'))->assertRedirect('/');
301 // Check can't restrict own content
302 $this->actingAs($this->user)->get($page->getUrl())->assertDontSee('Permissions');
303 $this->get($page->getUrl('/permissions'))->assertRedirect('/');
305 $this->giveUserPermissions($this->user, ['restrictions-manage-own']);
307 // Check can't restrict other's content
308 $this->actingAs($this->user)->get($otherUsersPage->getUrl())->assertDontSee('Permissions');
309 $this->get($otherUsersPage->getUrl('/permissions'))->assertRedirect();
311 // Check can restrict own content
312 $this->actingAs($this->user)->get($page->getUrl())->assertSee('Permissions');
313 $this->get($page->getUrl('/permissions'))->assertOk();
317 * Check a standard entity access permission.
319 private function checkAccessPermission(string $permission, array $accessUrls = [], array $visibles = [])
321 foreach ($accessUrls as $url) {
322 $this->actingAs($this->user)->get($url)->assertRedirect('/');
325 foreach ($visibles as $url => $text) {
326 $resp = $this->actingAs($this->user)->get($url);
327 $this->withHtml($resp)->assertElementNotContains('.action-buttons', $text);
330 $this->giveUserPermissions($this->user, [$permission]);
332 foreach ($accessUrls as $url) {
333 $this->actingAs($this->user)->get($url)->assertOk();
335 foreach ($visibles as $url => $text) {
336 $this->actingAs($this->user)->get($url)->assertSee($text);
340 public function test_bookshelves_create_all_permissions()
342 $this->checkAccessPermission('bookshelf-create-all', [
345 '/shelves' => 'New Shelf',
348 $this->post('/shelves', [
349 'name' => 'test shelf',
350 'description' => 'shelf desc',
351 ])->assertRedirect('/shelves/test-shelf');
354 public function test_bookshelves_edit_own_permission()
356 /** @var Bookshelf $otherShelf */
357 $otherShelf = Bookshelf::query()->first();
358 $ownShelf = $this->entities->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
359 $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
360 $this->entities->regenPermissions($ownShelf);
362 $this->checkAccessPermission('bookshelf-update-own', [
363 $ownShelf->getUrl('/edit'),
365 $ownShelf->getUrl() => 'Edit',
368 $resp = $this->get($otherShelf->getUrl());
369 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
370 $this->get($otherShelf->getUrl('/edit'))->assertRedirect('/');
373 public function test_bookshelves_edit_all_permission()
375 /** @var Bookshelf $otherShelf */
376 $otherShelf = Bookshelf::query()->first();
377 $this->checkAccessPermission('bookshelf-update-all', [
378 $otherShelf->getUrl('/edit'),
380 $otherShelf->getUrl() => 'Edit',
384 public function test_bookshelves_delete_own_permission()
386 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
387 /** @var Bookshelf $otherShelf */
388 $otherShelf = Bookshelf::query()->first();
389 $ownShelf = $this->entities->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
390 $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
391 $this->entities->regenPermissions($ownShelf);
393 $this->checkAccessPermission('bookshelf-delete-own', [
394 $ownShelf->getUrl('/delete'),
396 $ownShelf->getUrl() => 'Delete',
399 $resp = $this->get($otherShelf->getUrl());
400 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
401 $this->get($otherShelf->getUrl('/delete'))->assertRedirect('/');
403 $this->get($ownShelf->getUrl());
404 $this->delete($ownShelf->getUrl())->assertRedirect('/shelves');
405 $this->get('/shelves')->assertDontSee($ownShelf->name);
408 public function test_bookshelves_delete_all_permission()
410 $this->giveUserPermissions($this->user, ['bookshelf-update-all']);
411 /** @var Bookshelf $otherShelf */
412 $otherShelf = Bookshelf::query()->first();
413 $this->checkAccessPermission('bookshelf-delete-all', [
414 $otherShelf->getUrl('/delete'),
416 $otherShelf->getUrl() => 'Delete',
419 $this->delete($otherShelf->getUrl())->assertRedirect('/shelves');
420 $this->get('/shelves')->assertDontSee($otherShelf->name);
423 public function test_books_create_all_permissions()
425 $this->checkAccessPermission('book-create-all', [
428 '/books' => 'Create New Book',
431 $this->post('/books', [
432 'name' => 'test book',
433 'description' => 'book desc',
434 ])->assertRedirect('/books/test-book');
437 public function test_books_edit_own_permission()
439 /** @var Book $otherBook */
440 $otherBook = Book::query()->take(1)->get()->first();
441 $ownBook = $this->entities->createChainBelongingToUser($this->user)['book'];
442 $this->checkAccessPermission('book-update-own', [
443 $ownBook->getUrl() . '/edit',
445 $ownBook->getUrl() => 'Edit',
448 $resp = $this->get($otherBook->getUrl());
449 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
450 $this->get($otherBook->getUrl('/edit'))->assertRedirect('/');
453 public function test_books_edit_all_permission()
455 /** @var Book $otherBook */
456 $otherBook = Book::query()->take(1)->get()->first();
457 $this->checkAccessPermission('book-update-all', [
458 $otherBook->getUrl() . '/edit',
460 $otherBook->getUrl() => 'Edit',
464 public function test_books_delete_own_permission()
466 $this->giveUserPermissions($this->user, ['book-update-all']);
467 /** @var Book $otherBook */
468 $otherBook = Book::query()->take(1)->get()->first();
469 $ownBook = $this->entities->createChainBelongingToUser($this->user)['book'];
470 $this->checkAccessPermission('book-delete-own', [
471 $ownBook->getUrl() . '/delete',
473 $ownBook->getUrl() => 'Delete',
476 $resp = $this->get($otherBook->getUrl());
477 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
478 $this->get($otherBook->getUrl('/delete'))->assertRedirect('/');
479 $this->get($ownBook->getUrl());
480 $this->delete($ownBook->getUrl())->assertRedirect('/books');
481 $this->get('/books')->assertDontSee($ownBook->name);
484 public function test_books_delete_all_permission()
486 $this->giveUserPermissions($this->user, ['book-update-all']);
487 /** @var Book $otherBook */
488 $otherBook = Book::query()->take(1)->get()->first();
489 $this->checkAccessPermission('book-delete-all', [
490 $otherBook->getUrl() . '/delete',
492 $otherBook->getUrl() => 'Delete',
495 $this->get($otherBook->getUrl());
496 $this->delete($otherBook->getUrl())->assertRedirect('/books');
497 $this->get('/books')->assertDontSee($otherBook->name);
500 public function test_chapter_create_own_permissions()
502 /** @var Book $book */
503 $book = Book::query()->take(1)->get()->first();
504 $ownBook = $this->entities->createChainBelongingToUser($this->user)['book'];
505 $this->checkAccessPermission('chapter-create-own', [
506 $ownBook->getUrl('/create-chapter'),
508 $ownBook->getUrl() => 'New Chapter',
511 $this->post($ownBook->getUrl('/create-chapter'), [
512 'name' => 'test chapter',
513 'description' => 'chapter desc',
514 ])->assertRedirect($ownBook->getUrl('/chapter/test-chapter'));
516 $resp = $this->get($book->getUrl());
517 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'New Chapter');
518 $this->get($book->getUrl('/create-chapter'))->assertRedirect('/');
521 public function test_chapter_create_all_permissions()
523 $book = $this->entities->book();
524 $this->checkAccessPermission('chapter-create-all', [
525 $book->getUrl('/create-chapter'),
527 $book->getUrl() => 'New Chapter',
530 $this->post($book->getUrl('/create-chapter'), [
531 'name' => 'test chapter',
532 'description' => 'chapter desc',
533 ])->assertRedirect($book->getUrl('/chapter/test-chapter'));
536 public function test_chapter_edit_own_permission()
538 /** @var Chapter $otherChapter */
539 $otherChapter = Chapter::query()->first();
540 $ownChapter = $this->entities->createChainBelongingToUser($this->user)['chapter'];
541 $this->checkAccessPermission('chapter-update-own', [
542 $ownChapter->getUrl() . '/edit',
544 $ownChapter->getUrl() => 'Edit',
547 $resp = $this->get($otherChapter->getUrl());
548 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
549 $this->get($otherChapter->getUrl('/edit'))->assertRedirect('/');
552 public function test_chapter_edit_all_permission()
554 /** @var Chapter $otherChapter */
555 $otherChapter = Chapter::query()->take(1)->get()->first();
556 $this->checkAccessPermission('chapter-update-all', [
557 $otherChapter->getUrl() . '/edit',
559 $otherChapter->getUrl() => 'Edit',
563 public function test_chapter_delete_own_permission()
565 $this->giveUserPermissions($this->user, ['chapter-update-all']);
566 /** @var Chapter $otherChapter */
567 $otherChapter = Chapter::query()->first();
568 $ownChapter = $this->entities->createChainBelongingToUser($this->user)['chapter'];
569 $this->checkAccessPermission('chapter-delete-own', [
570 $ownChapter->getUrl() . '/delete',
572 $ownChapter->getUrl() => 'Delete',
575 $bookUrl = $ownChapter->book->getUrl();
576 $resp = $this->get($otherChapter->getUrl());
577 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
578 $this->get($otherChapter->getUrl('/delete'))->assertRedirect('/');
579 $this->get($ownChapter->getUrl());
580 $this->delete($ownChapter->getUrl())->assertRedirect($bookUrl);
581 $resp = $this->get($bookUrl);
582 $this->withHtml($resp)->assertElementNotContains('.book-content', $ownChapter->name);
585 public function test_chapter_delete_all_permission()
587 $this->giveUserPermissions($this->user, ['chapter-update-all']);
588 /** @var Chapter $otherChapter */
589 $otherChapter = Chapter::query()->first();
590 $this->checkAccessPermission('chapter-delete-all', [
591 $otherChapter->getUrl() . '/delete',
593 $otherChapter->getUrl() => 'Delete',
596 $bookUrl = $otherChapter->book->getUrl();
597 $this->get($otherChapter->getUrl());
598 $this->delete($otherChapter->getUrl())->assertRedirect($bookUrl);
599 $resp = $this->get($bookUrl);
600 $this->withHtml($resp)->assertElementNotContains('.book-content', $otherChapter->name);
603 public function test_page_create_own_permissions()
605 $book = $this->entities->book();
606 $chapter = $this->entities->chapter();
608 $entities = $this->entities->createChainBelongingToUser($this->user);
609 $ownBook = $entities['book'];
610 $ownChapter = $entities['chapter'];
612 $createUrl = $ownBook->getUrl('/create-page');
613 $createUrlChapter = $ownChapter->getUrl('/create-page');
614 $accessUrls = [$createUrl, $createUrlChapter];
616 foreach ($accessUrls as $url) {
617 $this->actingAs($this->user)->get($url)->assertRedirect('/');
620 $this->checkAccessPermission('page-create-own', [], [
621 $ownBook->getUrl() => 'New Page',
622 $ownChapter->getUrl() => 'New Page',
625 $this->giveUserPermissions($this->user, ['page-create-own']);
627 foreach ($accessUrls as $index => $url) {
628 $resp = $this->actingAs($this->user)->get($url);
629 $expectedUrl = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
630 $resp->assertRedirect($expectedUrl);
633 $this->get($createUrl);
634 /** @var Page $draft */
635 $draft = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
636 $this->post($draft->getUrl(), [
637 'name' => 'test page',
638 'html' => 'page desc',
639 ])->assertRedirect($ownBook->getUrl('/page/test-page'));
641 $resp = $this->get($book->getUrl());
642 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'New Page');
643 $this->get($book->getUrl('/create-page'))->assertRedirect('/');
645 $resp = $this->get($chapter->getUrl());
646 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'New Page');
647 $this->get($chapter->getUrl('/create-page'))->assertRedirect('/');
650 public function test_page_create_all_permissions()
652 $book = $this->entities->book();
653 $chapter = $this->entities->chapter();
654 $createUrl = $book->getUrl('/create-page');
656 $createUrlChapter = $chapter->getUrl('/create-page');
657 $accessUrls = [$createUrl, $createUrlChapter];
659 foreach ($accessUrls as $url) {
660 $this->actingAs($this->user)->get($url)->assertRedirect('/');
663 $this->checkAccessPermission('page-create-all', [], [
664 $book->getUrl() => 'New Page',
665 $chapter->getUrl() => 'New Page',
668 $this->giveUserPermissions($this->user, ['page-create-all']);
670 foreach ($accessUrls as $index => $url) {
671 $resp = $this->actingAs($this->user)->get($url);
672 $expectedUrl = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
673 $resp->assertRedirect($expectedUrl);
676 $this->get($createUrl);
677 /** @var Page $draft */
678 $draft = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
679 $this->post($draft->getUrl(), [
680 'name' => 'test page',
681 'html' => 'page desc',
682 ])->assertRedirect($book->getUrl('/page/test-page'));
684 $this->get($chapter->getUrl('/create-page'));
685 /** @var Page $draft */
686 $draft = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
687 $this->post($draft->getUrl(), [
688 'name' => 'new test page',
689 'html' => 'page desc',
690 ])->assertRedirect($book->getUrl('/page/new-test-page'));
693 public function test_page_edit_own_permission()
695 /** @var Page $otherPage */
696 $otherPage = Page::query()->first();
697 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
698 $this->checkAccessPermission('page-update-own', [
699 $ownPage->getUrl() . '/edit',
701 $ownPage->getUrl() => 'Edit',
704 $resp = $this->get($otherPage->getUrl());
705 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
706 $this->get($otherPage->getUrl() . '/edit')->assertRedirect('/');
709 public function test_page_edit_all_permission()
711 /** @var Page $otherPage */
712 $otherPage = Page::query()->first();
713 $this->checkAccessPermission('page-update-all', [
714 $otherPage->getUrl('/edit'),
716 $otherPage->getUrl() => 'Edit',
720 public function test_page_delete_own_permission()
722 $this->giveUserPermissions($this->user, ['page-update-all']);
723 /** @var Page $otherPage */
724 $otherPage = Page::query()->first();
725 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
726 $this->checkAccessPermission('page-delete-own', [
727 $ownPage->getUrl() . '/delete',
729 $ownPage->getUrl() => 'Delete',
732 $parent = $ownPage->chapter ?? $ownPage->book;
733 $resp = $this->get($otherPage->getUrl());
734 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
735 $this->get($otherPage->getUrl('/delete'))->assertRedirect('/');
736 $this->get($ownPage->getUrl());
737 $this->delete($ownPage->getUrl())->assertRedirect($parent->getUrl());
738 $resp = $this->get($parent->getUrl());
739 $this->withHtml($resp)->assertElementNotContains('.book-content', $ownPage->name);
742 public function test_page_delete_all_permission()
744 $this->giveUserPermissions($this->user, ['page-update-all']);
745 /** @var Page $otherPage */
746 $otherPage = Page::query()->first();
748 $this->checkAccessPermission('page-delete-all', [
749 $otherPage->getUrl() . '/delete',
751 $otherPage->getUrl() => 'Delete',
754 /** @var Entity $parent */
755 $parent = $otherPage->chapter ?? $otherPage->book;
756 $this->get($otherPage->getUrl());
758 $this->delete($otherPage->getUrl())->assertRedirect($parent->getUrl());
759 $this->get($parent->getUrl())->assertDontSee($otherPage->name);
762 public function test_public_role_visible_in_user_edit_screen()
764 /** @var User $user */
765 $user = User::query()->first();
766 $adminRole = Role::getSystemRole('admin');
767 $publicRole = Role::getSystemRole('public');
768 $resp = $this->asAdmin()->get('/settings/users/' . $user->id);
769 $this->withHtml($resp)->assertElementExists('[name="roles[' . $adminRole->id . ']"]')
770 ->assertElementExists('[name="roles[' . $publicRole->id . ']"]');
773 public function test_public_role_visible_in_role_listing()
775 $this->asAdmin()->get('/settings/roles')
777 ->assertSee('Public');
780 public function test_public_role_visible_in_default_role_setting()
782 $resp = $this->asAdmin()->get('/settings/registration');
783 $this->withHtml($resp)->assertElementExists('[data-system-role-name="admin"]')
784 ->assertElementExists('[data-system-role-name="public"]');
787 public function test_public_role_not_deletable()
789 /** @var Role $publicRole */
790 $publicRole = Role::getSystemRole('public');
791 $resp = $this->asAdmin()->delete('/settings/roles/delete/' . $publicRole->id);
792 $resp->assertRedirect('/');
794 $this->get('/settings/roles/delete/' . $publicRole->id);
795 $resp = $this->delete('/settings/roles/delete/' . $publicRole->id);
796 $resp->assertRedirect('/settings/roles/delete/' . $publicRole->id);
797 $resp = $this->get('/settings/roles/delete/' . $publicRole->id);
798 $resp->assertSee('This role is a system role and cannot be deleted');
801 public function test_image_delete_own_permission()
803 $this->giveUserPermissions($this->user, ['image-update-all']);
804 $page = $this->entities->page();
805 $image = Image::factory()->create([
806 'uploaded_to' => $page->id,
807 'created_by' => $this->user->id,
808 'updated_by' => $this->user->id,
811 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertStatus(403);
813 $this->giveUserPermissions($this->user, ['image-delete-own']);
815 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertOk();
816 $this->assertDatabaseMissing('images', ['id' => $image->id]);
819 public function test_image_delete_all_permission()
821 $this->giveUserPermissions($this->user, ['image-update-all']);
822 $admin = $this->getAdmin();
823 $page = $this->entities->page();
824 $image = Image::factory()->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
826 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertStatus(403);
828 $this->giveUserPermissions($this->user, ['image-delete-own']);
830 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertStatus(403);
832 $this->giveUserPermissions($this->user, ['image-delete-all']);
834 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertOk();
835 $this->assertDatabaseMissing('images', ['id' => $image->id]);
838 public function test_role_permission_removal()
840 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
841 $page = $this->entities->page();
842 $viewerRole = Role::getRole('viewer');
843 $viewer = $this->getViewer();
844 $this->actingAs($viewer)->get($page->getUrl())->assertOk();
846 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
847 'display_name' => $viewerRole->display_name,
848 'description' => $viewerRole->description,
850 ])->assertStatus(302);
852 $this->actingAs($viewer)->get($page->getUrl())->assertStatus(404);
855 public function test_empty_state_actions_not_visible_without_permission()
857 $admin = $this->getAdmin();
859 $book = Book::factory()->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
860 $this->entities->regenPermissions($book);
861 $this->actingAs($this->getViewer())->get($book->getUrl())
862 ->assertDontSee('Create a new page')
863 ->assertDontSee('Add a chapter');
866 $chapter = Chapter::factory()->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
867 $this->entities->regenPermissions($chapter);
868 $this->actingAs($this->getViewer())->get($chapter->getUrl())
869 ->assertDontSee('Create a new page')
870 ->assertDontSee('Sort the current book');
873 public function test_comment_create_permission()
875 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
877 $this->actingAs($this->user)
878 ->addComment($ownPage)
881 $this->giveUserPermissions($this->user, ['comment-create-all']);
883 $this->actingAs($this->user)
884 ->addComment($ownPage)
888 public function test_comment_update_own_permission()
890 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
891 $this->giveUserPermissions($this->user, ['comment-create-all']);
892 $this->actingAs($this->user)->addComment($ownPage);
893 /** @var Comment $comment */
894 $comment = $ownPage->comments()->latest()->first();
896 // no comment-update-own
897 $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
899 $this->giveUserPermissions($this->user, ['comment-update-own']);
901 // now has comment-update-own
902 $this->actingAs($this->user)->updateComment($comment)->assertOk();
905 public function test_comment_update_all_permission()
907 /** @var Page $ownPage */
908 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
909 $this->asAdmin()->addComment($ownPage);
910 /** @var Comment $comment */
911 $comment = $ownPage->comments()->latest()->first();
913 // no comment-update-all
914 $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
916 $this->giveUserPermissions($this->user, ['comment-update-all']);
918 // now has comment-update-all
919 $this->actingAs($this->user)->updateComment($comment)->assertOk();
922 public function test_comment_delete_own_permission()
924 /** @var Page $ownPage */
925 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
926 $this->giveUserPermissions($this->user, ['comment-create-all']);
927 $this->actingAs($this->user)->addComment($ownPage);
929 /** @var Comment $comment */
930 $comment = $ownPage->comments()->latest()->first();
932 // no comment-delete-own
933 $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
935 $this->giveUserPermissions($this->user, ['comment-delete-own']);
937 // now has comment-update-own
938 $this->actingAs($this->user)->deleteComment($comment)->assertOk();
941 public function test_comment_delete_all_permission()
943 /** @var Page $ownPage */
944 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
945 $this->asAdmin()->addComment($ownPage);
946 /** @var Comment $comment */
947 $comment = $ownPage->comments()->latest()->first();
949 // no comment-delete-all
950 $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
952 $this->giveUserPermissions($this->user, ['comment-delete-all']);
954 // now has comment-delete-all
955 $this->actingAs($this->user)->deleteComment($comment)->assertOk();
958 private function addComment(Page $page): TestResponse
960 $comment = Comment::factory()->make();
962 return $this->postJson("/comment/$page->id", $comment->only('text', 'html'));
965 private function updateComment(Comment $comment): TestResponse
967 $commentData = Comment::factory()->make();
969 return $this->putJson("/comment/{$comment->id}", $commentData->only('text', 'html'));
972 private function deleteComment(Comment $comment): TestResponse
974 return $this->json('DELETE', '/comment/' . $comment->id);