AclTest::testIsCurrentUserAllowed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 34
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Acl;
6
7
use Application\Acl\Acl;
8
use Application\Model\User;
9
use PHPUnit\Framework\TestCase;
10
11
class AclTest extends TestCase
12
{
13
    public function testIsCurrentUserAllowed(): void
14
    {
15
        $acl = new Acl();
16
        $user = new User();
17
18
        $owner = new User();
19
        $owner->setEmail('sarah');
20
        User::setCurrent($owner);
21
        $user->timestampCreation();
22
23
        User::setCurrent(null);
24
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'anonymous cannot update');
25
        self::assertSame('Non-logged user with role anonymous is not allowed on resource "User#null" with privilege "update"', $acl->getLastDenialMessage());
26
27
        User::setCurrent($owner);
28
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'student cannot update even if owner');
29
        self::assertSame('User "sarah" with role member is not allowed on resource "User#null" with privilege "update" because it is not himself', $acl->getLastDenialMessage());
30
31
        $other = new User();
32
        $other->setEmail('john');
33
        User::setCurrent($other);
34
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'other user cannot update');
35
        self::assertSame('User "john" with role member is not allowed on resource "User#null" with privilege "update" because it is not himself', $acl->getLastDenialMessage());
36
37
        // Test again the first case to assert that reject reason does not leak from one assertion to the next
38
        User::setCurrent(null);
39
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'anonymous cannot update');
40
        self::assertSame('Non-logged user with role anonymous is not allowed on resource "User#null" with privilege "update"', $acl->getLastDenialMessage());
41
42
        $administrator = new User(User::ROLE_ADMINISTRATOR);
43
        $administrator->setEmail('jane');
44
        User::setCurrent($administrator);
45
        self::assertTrue($acl->isCurrentUserAllowed($user, 'update'), 'admin can do anything');
46
        self::assertNull($acl->getLastDenialMessage());
47
    }
48
}
49