]> BookStack Code Mirror - bookstack/blob - tests/Permissions/Scenarios/PermissionScenarioTestCase.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / tests / Permissions / Scenarios / PermissionScenarioTestCase.php
1 <?php
2
3 namespace Tests\Permissions\Scenarios;
4
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Users\Models\User;
7 use Tests\TestCase;
8
9 // Cases defined in dev/docs/permission-scenario-testing.md
10
11 class PermissionScenarioTestCase extends TestCase
12 {
13     protected function assertVisibleToUser(Entity $entity, User $user)
14     {
15         $this->actingAs($user);
16         $funcView = userCan($entity->getMorphClass() . '-view', $entity);
17         $queryView = $entity->newQuery()->scopes(['visible'])->find($entity->id) !== null;
18
19         $id = $entity->getMorphClass() . ':' . $entity->id;
20         $msg = "Item [{$id}] should be visible but was not found via ";
21         $msg .= implode(' and ', array_filter([!$funcView ? 'userCan' : '', !$queryView ? 'query' : '']));
22
23         static::assertTrue($funcView && $queryView, $msg);
24     }
25
26     protected function assertNotVisibleToUser(Entity $entity, User $user)
27     {
28         $this->actingAs($user);
29         $funcView = userCan($entity->getMorphClass() . '-view', $entity);
30         $queryView = $entity->newQuery()->scopes(['visible'])->find($entity->id) !== null;
31
32         $id = $entity->getMorphClass() . ':' . $entity->id;
33         $msg = "Item [{$id}] should not be visible but was found via ";
34         $msg .= implode(' and ', array_filter([$funcView ? 'userCan' : '', $queryView ? 'query' : '']));
35
36         static::assertTrue(!$funcView && !$queryView, $msg);
37     }
38 }