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->users->viewer();
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->users->createRole();
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->users->admin();
125 $adminRole->users()->where('id', '!=', $adminUser->id)->delete();
126 $this->assertEquals(1, $adminRole->users()->count());
128 $viewerRole = $this->users->viewer()->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_delete_with_empty_migrate_option_works()
168 $role = $this->users->attachNewRole($this->user);
170 $this->assertCount(1, $role->users()->get());
172 $deletePage = $this->asAdmin()->get("/settings/roles/delete/$role->id");
173 $this->withHtml($deletePage)->assertElementExists('select[name=migrate_role_id]');
174 $resp = $this->asAdmin()->delete("/settings/roles/delete/$role->id", [
175 'migrate_role_id' => '',
178 $resp->assertRedirect('/settings/roles');
179 $this->assertDatabaseMissing('roles', ['id' => $role->id]);
182 public function test_entity_permissions_are_removed_on_delete()
184 /** @var Role $roleA */
185 $roleA = Role::query()->create(['display_name' => 'Entity Permissions Delete Test']);
186 $page = $this->entities->page();
188 $this->permissions->setEntityPermissions($page, ['view'], [$roleA]);
190 $this->assertDatabaseHas('entity_permissions', [
191 'role_id' => $roleA->id,
192 'entity_id' => $page->id,
193 'entity_type' => $page->getMorphClass(),
196 $this->asAdmin()->delete("/settings/roles/delete/$roleA->id");
198 $this->assertDatabaseMissing('entity_permissions', [
199 'role_id' => $roleA->id,
200 'entity_id' => $page->id,
201 'entity_type' => $page->getMorphClass(),
205 public function test_image_view_notice_shown_on_role_form()
207 /** @var Role $role */
208 $role = Role::query()->first();
209 $this->asAdmin()->get("/settings/roles/{$role->id}")
210 ->assertSee('Actual access of uploaded image files will be dependant upon system image storage option');
213 public function test_copy_role_button_shown()
215 /** @var Role $role */
216 $role = Role::query()->first();
217 $resp = $this->asAdmin()->get("/settings/roles/{$role->id}");
218 $this->withHtml($resp)->assertElementContains('a[href$="/roles/new?copy_from=' . $role->id . '"]', 'Copy');
221 public function test_copy_from_param_on_create_prefills_with_other_role_data()
223 /** @var Role $role */
224 $role = Role::query()->first();
225 $resp = $this->asAdmin()->get("/settings/roles/new?copy_from={$role->id}");
227 $this->withHtml($resp)->assertElementExists('input[name="display_name"][value="' . ($role->display_name . ' (Copy)') . '"]');
230 public function test_manage_user_permission()
232 $this->actingAs($this->user)->get('/settings/users')->assertRedirect('/');
233 $this->permissions->grantUserRolePermissions($this->user, ['users-manage']);
234 $this->actingAs($this->user)->get('/settings/users')->assertOk();
237 public function test_manage_users_permission_shows_link_in_header_if_does_not_have_settings_manage_permision()
239 $usersLink = 'href="' . url('/settings/users') . '"';
240 $this->actingAs($this->user)->get('/')->assertDontSee($usersLink, false);
241 $this->permissions->grantUserRolePermissions($this->user, ['users-manage']);
242 $this->actingAs($this->user)->get('/')->assertSee($usersLink, false);
243 $this->permissions->grantUserRolePermissions($this->user, ['settings-manage', 'users-manage']);
244 $this->actingAs($this->user)->get('/')->assertDontSee($usersLink, false);
247 public function test_user_cannot_change_email_unless_they_have_manage_users_permission()
249 $userProfileUrl = '/settings/users/' . $this->user->id;
250 $originalEmail = $this->user->email;
251 $this->actingAs($this->user);
253 $resp = $this->get($userProfileUrl)
255 $this->withHtml($resp)->assertElementExists('input[name=email][disabled]');
256 $this->put($userProfileUrl, [
257 'name' => 'my_new_name',
260 $this->assertDatabaseHas('users', [
261 'id' => $this->user->id,
262 'email' => $originalEmail,
263 'name' => 'my_new_name',
266 $this->permissions->grantUserRolePermissions($this->user, ['users-manage']);
268 $resp = $this->get($userProfileUrl)
270 $this->withHtml($resp)->assertElementNotExists('input[name=email][disabled]')
271 ->assertElementExists('input[name=email]');
272 $this->put($userProfileUrl, [
273 'name' => 'my_new_name_2',
277 $this->assertDatabaseHas('users', [
278 'id' => $this->user->id,
280 'name' => 'my_new_name_2',
284 public function test_user_roles_manage_permission()
286 $this->actingAs($this->user)->get('/settings/roles')->assertRedirect('/');
287 $this->get('/settings/roles/1')->assertRedirect('/');
288 $this->permissions->grantUserRolePermissions($this->user, ['user-roles-manage']);
289 $this->actingAs($this->user)->get('/settings/roles')->assertOk();
290 $this->get('/settings/roles/1')
292 ->assertSee('Admin');
295 public function test_settings_manage_permission()
297 $this->actingAs($this->user)->get('/settings/features')->assertRedirect('/');
298 $this->permissions->grantUserRolePermissions($this->user, ['settings-manage']);
299 $this->get('/settings/features')->assertOk();
301 $resp = $this->post('/settings/features', []);
302 $resp->assertRedirect('/settings/features');
303 $resp = $this->get('/settings/features');
304 $resp->assertSee('Settings saved');
307 public function test_restrictions_manage_all_permission()
309 $page = Page::query()->get()->first();
311 $this->actingAs($this->user)->get($page->getUrl())->assertDontSee('Permissions');
312 $this->get($page->getUrl('/permissions'))->assertRedirect('/');
314 $this->permissions->grantUserRolePermissions($this->user, ['restrictions-manage-all']);
316 $this->actingAs($this->user)->get($page->getUrl())->assertSee('Permissions');
318 $this->get($page->getUrl('/permissions'))
320 ->assertSee('Page Permissions');
323 public function test_restrictions_manage_own_permission()
325 /** @var Page $otherUsersPage */
326 $otherUsersPage = Page::query()->first();
327 $content = $this->entities->createChainBelongingToUser($this->user);
329 // Set a different creator on the page we're checking to ensure
330 // that the owner fields are checked
331 $page = $content['page']; /** @var Page $page */
332 $page->created_by = $otherUsersPage->id;
333 $page->owned_by = $this->user->id;
336 // Check can't restrict other's content
337 $this->actingAs($this->user)->get($otherUsersPage->getUrl())->assertDontSee('Permissions');
338 $this->get($otherUsersPage->getUrl('/permissions'))->assertRedirect('/');
340 // Check can't restrict own content
341 $this->actingAs($this->user)->get($page->getUrl())->assertDontSee('Permissions');
342 $this->get($page->getUrl('/permissions'))->assertRedirect('/');
344 $this->permissions->grantUserRolePermissions($this->user, ['restrictions-manage-own']);
346 // Check can't restrict other's content
347 $this->actingAs($this->user)->get($otherUsersPage->getUrl())->assertDontSee('Permissions');
348 $this->get($otherUsersPage->getUrl('/permissions'))->assertRedirect();
350 // Check can restrict own content
351 $this->actingAs($this->user)->get($page->getUrl())->assertSee('Permissions');
352 $this->get($page->getUrl('/permissions'))->assertOk();
356 * Check a standard entity access permission.
358 private function checkAccessPermission(string $permission, array $accessUrls = [], array $visibles = [])
360 foreach ($accessUrls as $url) {
361 $this->actingAs($this->user)->get($url)->assertRedirect('/');
364 foreach ($visibles as $url => $text) {
365 $resp = $this->actingAs($this->user)->get($url);
366 $this->withHtml($resp)->assertElementNotContains('.action-buttons', $text);
369 $this->permissions->grantUserRolePermissions($this->user, [$permission]);
371 foreach ($accessUrls as $url) {
372 $this->actingAs($this->user)->get($url)->assertOk();
374 foreach ($visibles as $url => $text) {
375 $this->actingAs($this->user)->get($url)->assertSee($text);
379 public function test_bookshelves_create_all_permissions()
381 $this->checkAccessPermission('bookshelf-create-all', [
384 '/shelves' => 'New Shelf',
387 $this->post('/shelves', [
388 'name' => 'test shelf',
389 'description' => 'shelf desc',
390 ])->assertRedirect('/shelves/test-shelf');
393 public function test_bookshelves_edit_own_permission()
395 /** @var Bookshelf $otherShelf */
396 $otherShelf = Bookshelf::query()->first();
397 $ownShelf = $this->entities->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
398 $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
399 $this->permissions->regenerateForEntity($ownShelf);
401 $this->checkAccessPermission('bookshelf-update-own', [
402 $ownShelf->getUrl('/edit'),
404 $ownShelf->getUrl() => 'Edit',
407 $resp = $this->get($otherShelf->getUrl());
408 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
409 $this->get($otherShelf->getUrl('/edit'))->assertRedirect('/');
412 public function test_bookshelves_edit_all_permission()
414 /** @var Bookshelf $otherShelf */
415 $otherShelf = Bookshelf::query()->first();
416 $this->checkAccessPermission('bookshelf-update-all', [
417 $otherShelf->getUrl('/edit'),
419 $otherShelf->getUrl() => 'Edit',
423 public function test_bookshelves_delete_own_permission()
425 $this->permissions->grantUserRolePermissions($this->user, ['bookshelf-update-all']);
426 /** @var Bookshelf $otherShelf */
427 $otherShelf = Bookshelf::query()->first();
428 $ownShelf = $this->entities->newShelf(['name' => 'test-shelf', 'slug' => 'test-shelf']);
429 $ownShelf->forceFill(['owned_by' => $this->user->id, 'updated_by' => $this->user->id])->save();
430 $this->permissions->regenerateForEntity($ownShelf);
432 $this->checkAccessPermission('bookshelf-delete-own', [
433 $ownShelf->getUrl('/delete'),
435 $ownShelf->getUrl() => 'Delete',
438 $resp = $this->get($otherShelf->getUrl());
439 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
440 $this->get($otherShelf->getUrl('/delete'))->assertRedirect('/');
442 $this->get($ownShelf->getUrl());
443 $this->delete($ownShelf->getUrl())->assertRedirect('/shelves');
444 $this->get('/shelves')->assertDontSee($ownShelf->name);
447 public function test_bookshelves_delete_all_permission()
449 $this->permissions->grantUserRolePermissions($this->user, ['bookshelf-update-all']);
450 /** @var Bookshelf $otherShelf */
451 $otherShelf = Bookshelf::query()->first();
452 $this->checkAccessPermission('bookshelf-delete-all', [
453 $otherShelf->getUrl('/delete'),
455 $otherShelf->getUrl() => 'Delete',
458 $this->delete($otherShelf->getUrl())->assertRedirect('/shelves');
459 $this->get('/shelves')->assertDontSee($otherShelf->name);
462 public function test_books_create_all_permissions()
464 $this->checkAccessPermission('book-create-all', [
467 '/books' => 'Create New Book',
470 $this->post('/books', [
471 'name' => 'test book',
472 'description' => 'book desc',
473 ])->assertRedirect('/books/test-book');
476 public function test_books_edit_own_permission()
478 /** @var Book $otherBook */
479 $otherBook = Book::query()->take(1)->get()->first();
480 $ownBook = $this->entities->createChainBelongingToUser($this->user)['book'];
481 $this->checkAccessPermission('book-update-own', [
482 $ownBook->getUrl() . '/edit',
484 $ownBook->getUrl() => 'Edit',
487 $resp = $this->get($otherBook->getUrl());
488 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
489 $this->get($otherBook->getUrl('/edit'))->assertRedirect('/');
492 public function test_books_edit_all_permission()
494 /** @var Book $otherBook */
495 $otherBook = Book::query()->take(1)->get()->first();
496 $this->checkAccessPermission('book-update-all', [
497 $otherBook->getUrl() . '/edit',
499 $otherBook->getUrl() => 'Edit',
503 public function test_books_delete_own_permission()
505 $this->permissions->grantUserRolePermissions($this->user, ['book-update-all']);
506 /** @var Book $otherBook */
507 $otherBook = Book::query()->take(1)->get()->first();
508 $ownBook = $this->entities->createChainBelongingToUser($this->user)['book'];
509 $this->checkAccessPermission('book-delete-own', [
510 $ownBook->getUrl() . '/delete',
512 $ownBook->getUrl() => 'Delete',
515 $resp = $this->get($otherBook->getUrl());
516 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
517 $this->get($otherBook->getUrl('/delete'))->assertRedirect('/');
518 $this->get($ownBook->getUrl());
519 $this->delete($ownBook->getUrl())->assertRedirect('/books');
520 $this->get('/books')->assertDontSee($ownBook->name);
523 public function test_books_delete_all_permission()
525 $this->permissions->grantUserRolePermissions($this->user, ['book-update-all']);
526 /** @var Book $otherBook */
527 $otherBook = Book::query()->take(1)->get()->first();
528 $this->checkAccessPermission('book-delete-all', [
529 $otherBook->getUrl() . '/delete',
531 $otherBook->getUrl() => 'Delete',
534 $this->get($otherBook->getUrl());
535 $this->delete($otherBook->getUrl())->assertRedirect('/books');
536 $this->get('/books')->assertDontSee($otherBook->name);
539 public function test_chapter_create_own_permissions()
541 /** @var Book $book */
542 $book = Book::query()->take(1)->get()->first();
543 $ownBook = $this->entities->createChainBelongingToUser($this->user)['book'];
544 $this->checkAccessPermission('chapter-create-own', [
545 $ownBook->getUrl('/create-chapter'),
547 $ownBook->getUrl() => 'New Chapter',
550 $this->post($ownBook->getUrl('/create-chapter'), [
551 'name' => 'test chapter',
552 'description' => 'chapter desc',
553 ])->assertRedirect($ownBook->getUrl('/chapter/test-chapter'));
555 $resp = $this->get($book->getUrl());
556 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'New Chapter');
557 $this->get($book->getUrl('/create-chapter'))->assertRedirect('/');
560 public function test_chapter_create_all_permissions()
562 $book = $this->entities->book();
563 $this->checkAccessPermission('chapter-create-all', [
564 $book->getUrl('/create-chapter'),
566 $book->getUrl() => 'New Chapter',
569 $this->post($book->getUrl('/create-chapter'), [
570 'name' => 'test chapter',
571 'description' => 'chapter desc',
572 ])->assertRedirect($book->getUrl('/chapter/test-chapter'));
575 public function test_chapter_edit_own_permission()
577 /** @var Chapter $otherChapter */
578 $otherChapter = Chapter::query()->first();
579 $ownChapter = $this->entities->createChainBelongingToUser($this->user)['chapter'];
580 $this->checkAccessPermission('chapter-update-own', [
581 $ownChapter->getUrl() . '/edit',
583 $ownChapter->getUrl() => 'Edit',
586 $resp = $this->get($otherChapter->getUrl());
587 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
588 $this->get($otherChapter->getUrl('/edit'))->assertRedirect('/');
591 public function test_chapter_edit_all_permission()
593 /** @var Chapter $otherChapter */
594 $otherChapter = Chapter::query()->take(1)->get()->first();
595 $this->checkAccessPermission('chapter-update-all', [
596 $otherChapter->getUrl() . '/edit',
598 $otherChapter->getUrl() => 'Edit',
602 public function test_chapter_delete_own_permission()
604 $this->permissions->grantUserRolePermissions($this->user, ['chapter-update-all']);
605 /** @var Chapter $otherChapter */
606 $otherChapter = Chapter::query()->first();
607 $ownChapter = $this->entities->createChainBelongingToUser($this->user)['chapter'];
608 $this->checkAccessPermission('chapter-delete-own', [
609 $ownChapter->getUrl() . '/delete',
611 $ownChapter->getUrl() => 'Delete',
614 $bookUrl = $ownChapter->book->getUrl();
615 $resp = $this->get($otherChapter->getUrl());
616 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
617 $this->get($otherChapter->getUrl('/delete'))->assertRedirect('/');
618 $this->get($ownChapter->getUrl());
619 $this->delete($ownChapter->getUrl())->assertRedirect($bookUrl);
620 $resp = $this->get($bookUrl);
621 $this->withHtml($resp)->assertElementNotContains('.book-content', $ownChapter->name);
624 public function test_chapter_delete_all_permission()
626 $this->permissions->grantUserRolePermissions($this->user, ['chapter-update-all']);
627 /** @var Chapter $otherChapter */
628 $otherChapter = Chapter::query()->first();
629 $this->checkAccessPermission('chapter-delete-all', [
630 $otherChapter->getUrl() . '/delete',
632 $otherChapter->getUrl() => 'Delete',
635 $bookUrl = $otherChapter->book->getUrl();
636 $this->get($otherChapter->getUrl());
637 $this->delete($otherChapter->getUrl())->assertRedirect($bookUrl);
638 $resp = $this->get($bookUrl);
639 $this->withHtml($resp)->assertElementNotContains('.book-content', $otherChapter->name);
642 public function test_page_create_own_permissions()
644 $book = $this->entities->book();
645 $chapter = $this->entities->chapter();
647 $entities = $this->entities->createChainBelongingToUser($this->user);
648 $ownBook = $entities['book'];
649 $ownChapter = $entities['chapter'];
651 $createUrl = $ownBook->getUrl('/create-page');
652 $createUrlChapter = $ownChapter->getUrl('/create-page');
653 $accessUrls = [$createUrl, $createUrlChapter];
655 foreach ($accessUrls as $url) {
656 $this->actingAs($this->user)->get($url)->assertRedirect('/');
659 $this->checkAccessPermission('page-create-own', [], [
660 $ownBook->getUrl() => 'New Page',
661 $ownChapter->getUrl() => 'New Page',
664 $this->permissions->grantUserRolePermissions($this->user, ['page-create-own']);
666 foreach ($accessUrls as $index => $url) {
667 $resp = $this->actingAs($this->user)->get($url);
668 $expectedUrl = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
669 $resp->assertRedirect($expectedUrl);
672 $this->get($createUrl);
673 /** @var Page $draft */
674 $draft = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first();
675 $this->post($draft->getUrl(), [
676 'name' => 'test page',
677 'html' => 'page desc',
678 ])->assertRedirect($ownBook->getUrl('/page/test-page'));
680 $resp = $this->get($book->getUrl());
681 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'New Page');
682 $this->get($book->getUrl('/create-page'))->assertRedirect('/');
684 $resp = $this->get($chapter->getUrl());
685 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'New Page');
686 $this->get($chapter->getUrl('/create-page'))->assertRedirect('/');
689 public function test_page_create_all_permissions()
691 $book = $this->entities->book();
692 $chapter = $this->entities->chapter();
693 $createUrl = $book->getUrl('/create-page');
695 $createUrlChapter = $chapter->getUrl('/create-page');
696 $accessUrls = [$createUrl, $createUrlChapter];
698 foreach ($accessUrls as $url) {
699 $this->actingAs($this->user)->get($url)->assertRedirect('/');
702 $this->checkAccessPermission('page-create-all', [], [
703 $book->getUrl() => 'New Page',
704 $chapter->getUrl() => 'New Page',
707 $this->permissions->grantUserRolePermissions($this->user, ['page-create-all']);
709 foreach ($accessUrls as $index => $url) {
710 $resp = $this->actingAs($this->user)->get($url);
711 $expectedUrl = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first()->getUrl();
712 $resp->assertRedirect($expectedUrl);
715 $this->get($createUrl);
716 /** @var Page $draft */
717 $draft = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
718 $this->post($draft->getUrl(), [
719 'name' => 'test page',
720 'html' => 'page desc',
721 ])->assertRedirect($book->getUrl('/page/test-page'));
723 $this->get($chapter->getUrl('/create-page'));
724 /** @var Page $draft */
725 $draft = Page::query()->where('draft', '=', true)->orderByDesc('id')->first();
726 $this->post($draft->getUrl(), [
727 'name' => 'new test page',
728 'html' => 'page desc',
729 ])->assertRedirect($book->getUrl('/page/new-test-page'));
732 public function test_page_edit_own_permission()
734 /** @var Page $otherPage */
735 $otherPage = Page::query()->first();
736 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
737 $this->checkAccessPermission('page-update-own', [
738 $ownPage->getUrl() . '/edit',
740 $ownPage->getUrl() => 'Edit',
743 $resp = $this->get($otherPage->getUrl());
744 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Edit');
745 $this->get($otherPage->getUrl() . '/edit')->assertRedirect('/');
748 public function test_page_edit_all_permission()
750 /** @var Page $otherPage */
751 $otherPage = Page::query()->first();
752 $this->checkAccessPermission('page-update-all', [
753 $otherPage->getUrl('/edit'),
755 $otherPage->getUrl() => 'Edit',
759 public function test_page_delete_own_permission()
761 $this->permissions->grantUserRolePermissions($this->user, ['page-update-all']);
762 /** @var Page $otherPage */
763 $otherPage = Page::query()->first();
764 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
765 $this->checkAccessPermission('page-delete-own', [
766 $ownPage->getUrl() . '/delete',
768 $ownPage->getUrl() => 'Delete',
771 $parent = $ownPage->chapter ?? $ownPage->book;
772 $resp = $this->get($otherPage->getUrl());
773 $this->withHtml($resp)->assertElementNotContains('.action-buttons', 'Delete');
774 $this->get($otherPage->getUrl('/delete'))->assertRedirect('/');
775 $this->get($ownPage->getUrl());
776 $this->delete($ownPage->getUrl())->assertRedirect($parent->getUrl());
777 $resp = $this->get($parent->getUrl());
778 $this->withHtml($resp)->assertElementNotContains('.book-content', $ownPage->name);
781 public function test_page_delete_all_permission()
783 $this->permissions->grantUserRolePermissions($this->user, ['page-update-all']);
784 /** @var Page $otherPage */
785 $otherPage = Page::query()->first();
787 $this->checkAccessPermission('page-delete-all', [
788 $otherPage->getUrl() . '/delete',
790 $otherPage->getUrl() => 'Delete',
793 /** @var Entity $parent */
794 $parent = $otherPage->chapter ?? $otherPage->book;
795 $this->get($otherPage->getUrl());
797 $this->delete($otherPage->getUrl())->assertRedirect($parent->getUrl());
798 $this->get($parent->getUrl())->assertDontSee($otherPage->name);
801 public function test_public_role_visible_in_user_edit_screen()
803 /** @var User $user */
804 $user = User::query()->first();
805 $adminRole = Role::getSystemRole('admin');
806 $publicRole = Role::getSystemRole('public');
807 $resp = $this->asAdmin()->get('/settings/users/' . $user->id);
808 $this->withHtml($resp)->assertElementExists('[name="roles[' . $adminRole->id . ']"]')
809 ->assertElementExists('[name="roles[' . $publicRole->id . ']"]');
812 public function test_public_role_visible_in_role_listing()
814 $this->asAdmin()->get('/settings/roles')
816 ->assertSee('Public');
819 public function test_public_role_visible_in_default_role_setting()
821 $resp = $this->asAdmin()->get('/settings/registration');
822 $this->withHtml($resp)->assertElementExists('[data-system-role-name="admin"]')
823 ->assertElementExists('[data-system-role-name="public"]');
826 public function test_public_role_not_deletable()
828 /** @var Role $publicRole */
829 $publicRole = Role::getSystemRole('public');
830 $resp = $this->asAdmin()->delete('/settings/roles/delete/' . $publicRole->id);
831 $resp->assertRedirect('/');
833 $this->get('/settings/roles/delete/' . $publicRole->id);
834 $resp = $this->delete('/settings/roles/delete/' . $publicRole->id);
835 $resp->assertRedirect('/settings/roles/delete/' . $publicRole->id);
836 $resp = $this->get('/settings/roles/delete/' . $publicRole->id);
837 $resp->assertSee('This role is a system role and cannot be deleted');
840 public function test_image_delete_own_permission()
842 $this->permissions->grantUserRolePermissions($this->user, ['image-update-all']);
843 $page = $this->entities->page();
844 $image = Image::factory()->create([
845 'uploaded_to' => $page->id,
846 'created_by' => $this->user->id,
847 'updated_by' => $this->user->id,
850 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertStatus(403);
852 $this->permissions->grantUserRolePermissions($this->user, ['image-delete-own']);
854 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertOk();
855 $this->assertDatabaseMissing('images', ['id' => $image->id]);
858 public function test_image_delete_all_permission()
860 $this->permissions->grantUserRolePermissions($this->user, ['image-update-all']);
861 $admin = $this->users->admin();
862 $page = $this->entities->page();
863 $image = Image::factory()->create(['uploaded_to' => $page->id, 'created_by' => $admin->id, 'updated_by' => $admin->id]);
865 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertStatus(403);
867 $this->permissions->grantUserRolePermissions($this->user, ['image-delete-own']);
869 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertStatus(403);
871 $this->permissions->grantUserRolePermissions($this->user, ['image-delete-all']);
873 $this->actingAs($this->user)->json('delete', '/images/' . $image->id)->assertOk();
874 $this->assertDatabaseMissing('images', ['id' => $image->id]);
877 public function test_role_permission_removal()
879 // To cover issue fixed in f99c8ff99aee9beb8c692f36d4b84dc6e651e50a.
880 $page = $this->entities->page();
881 $viewerRole = Role::getRole('viewer');
882 $viewer = $this->users->viewer();
883 $this->actingAs($viewer)->get($page->getUrl())->assertOk();
885 $this->asAdmin()->put('/settings/roles/' . $viewerRole->id, [
886 'display_name' => $viewerRole->display_name,
887 'description' => $viewerRole->description,
889 ])->assertStatus(302);
891 $this->actingAs($viewer)->get($page->getUrl())->assertStatus(404);
894 public function test_empty_state_actions_not_visible_without_permission()
896 $admin = $this->users->admin();
898 $book = Book::factory()->create(['created_by' => $admin->id, 'updated_by' => $admin->id]);
899 $this->permissions->regenerateForEntity($book);
900 $this->actingAs($this->users->viewer())->get($book->getUrl())
901 ->assertDontSee('Create a new page')
902 ->assertDontSee('Add a chapter');
905 $chapter = Chapter::factory()->create(['created_by' => $admin->id, 'updated_by' => $admin->id, 'book_id' => $book->id]);
906 $this->permissions->regenerateForEntity($chapter);
907 $this->actingAs($this->users->viewer())->get($chapter->getUrl())
908 ->assertDontSee('Create a new page')
909 ->assertDontSee('Sort the current book');
912 public function test_comment_create_permission()
914 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
916 $this->actingAs($this->user)
917 ->addComment($ownPage)
920 $this->permissions->grantUserRolePermissions($this->user, ['comment-create-all']);
922 $this->actingAs($this->user)
923 ->addComment($ownPage)
927 public function test_comment_update_own_permission()
929 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
930 $this->permissions->grantUserRolePermissions($this->user, ['comment-create-all']);
931 $this->actingAs($this->user)->addComment($ownPage);
932 /** @var Comment $comment */
933 $comment = $ownPage->comments()->latest()->first();
935 // no comment-update-own
936 $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
938 $this->permissions->grantUserRolePermissions($this->user, ['comment-update-own']);
940 // now has comment-update-own
941 $this->actingAs($this->user)->updateComment($comment)->assertOk();
944 public function test_comment_update_all_permission()
946 /** @var Page $ownPage */
947 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
948 $this->asAdmin()->addComment($ownPage);
949 /** @var Comment $comment */
950 $comment = $ownPage->comments()->latest()->first();
952 // no comment-update-all
953 $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
955 $this->permissions->grantUserRolePermissions($this->user, ['comment-update-all']);
957 // now has comment-update-all
958 $this->actingAs($this->user)->updateComment($comment)->assertOk();
961 public function test_comment_delete_own_permission()
963 /** @var Page $ownPage */
964 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
965 $this->permissions->grantUserRolePermissions($this->user, ['comment-create-all']);
966 $this->actingAs($this->user)->addComment($ownPage);
968 /** @var Comment $comment */
969 $comment = $ownPage->comments()->latest()->first();
971 // no comment-delete-own
972 $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
974 $this->permissions->grantUserRolePermissions($this->user, ['comment-delete-own']);
976 // now has comment-update-own
977 $this->actingAs($this->user)->deleteComment($comment)->assertOk();
980 public function test_comment_delete_all_permission()
982 /** @var Page $ownPage */
983 $ownPage = $this->entities->createChainBelongingToUser($this->user)['page'];
984 $this->asAdmin()->addComment($ownPage);
985 /** @var Comment $comment */
986 $comment = $ownPage->comments()->latest()->first();
988 // no comment-delete-all
989 $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
991 $this->permissions->grantUserRolePermissions($this->user, ['comment-delete-all']);
993 // now has comment-delete-all
994 $this->actingAs($this->user)->deleteComment($comment)->assertOk();
997 private function addComment(Page $page): TestResponse
999 $comment = Comment::factory()->make();
1001 return $this->postJson("/comment/$page->id", $comment->only('text', 'html'));
1004 private function updateComment(Comment $comment): TestResponse
1006 $commentData = Comment::factory()->make();
1008 return $this->putJson("/comment/{$comment->id}", $commentData->only('text', 'html'));
1011 private function deleteComment(Comment $comment): TestResponse
1013 return $this->json('DELETE', '/comment/' . $comment->id);