]> BookStack Code Mirror - bookstack/blob - tests/Permissions/Scenarios/EntityRolePermissions.php
Started more formal permission test case definitions
[bookstack] / tests / Permissions / Scenarios / EntityRolePermissions.php
1 <?php
2
3 namespace Tests\Permissions\Scenarios;
4
5 use BookStack\Entities\Models\Page;
6 use Tests\TestCase;
7
8 // Cases defined in dev/docs/permission-scenario-testing.md
9
10 class EntityRolePermissions extends TestCase
11 {
12     public function test_01_explicit_allow()
13     {
14         $user = $this->getViewer();
15         $role = $user->roles->first();
16         $page = $this->entities->page();
17         $this->entities->setPermissions($page, ['view'], [$role], false);
18
19         $this->actingAs($user);
20         $this->assertTrue(userCan('page-view', $page));
21         $this->assertNotNull(Page::visible()->findOrFail($page->id));
22     }
23
24     public function test_02_explicit_deny()
25     {
26         $user = $this->getViewer();
27         $role = $user->roles->first();
28         $page = $this->entities->page();
29         $this->entities->setPermissions($page, ['edit'], [$role], false);
30
31         $this->actingAs($user);
32         $this->assertFalse(userCan('page-view', $page));
33         $this->assertNull(Page::visible()->find($page->id));
34     }
35
36     public function test_03_same_level_conflicting()
37     {
38         $user = $this->getViewer();
39         $roleA = $user->roles->first();
40         $roleB = $this->createNewRole();
41         $user->attachRole($roleB);
42
43         $page = $this->entities->page();
44         // TODO - Can't do this as second call will overwrite first
45         $this->entities->setPermissions($page, ['edit'], [$roleA], false);
46         $this->entities->setPermissions($page, ['view'], [$roleB], false);
47
48         $this->actingAs($user);
49         $this->assertFalse(userCan('page-view', $page));
50         $this->assertNull(Page::visible()->find($page->id));
51     }
52 }