Failed Conditions
Push — master ( 4369d4...a5c076 )
by Adrien
15:03
created

AclTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 27
c 0
b 0
f 0
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 3 1
A testIsCurrentUserAllowed() 0 34 1
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
    protected function tearDown(): void
14
    {
15
        User::setCurrent(null);
16
    }
17
18
    public function testIsCurrentUserAllowed(): void
19
    {
20
        $acl = new Acl();
21
        $user = new User();
22
23
        $owner = new User();
24
        $owner->setLogin('sarah');
25
        User::setCurrent($owner);
26
        $user->timestampCreation();
27
28
        User::setCurrent(null);
29
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'anonymous cannot update');
30
        self::assertSame('Non-logged user with role anonymous is not allowed on resource "User#null" with privilege "update"', $acl->getLastDenialMessage());
31
32
        User::setCurrent($owner);
33
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'student cannot update even if owner');
34
        self::assertSame('User "sarah" with role individual is not allowed on resource "User#null" with privilege "update" because it is not himself', $acl->getLastDenialMessage());
35
36
        $other = new User();
37
        $other->setLogin('john');
38
        User::setCurrent($other);
39
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'other user cannot update');
40
        self::assertSame('User "john" with role individual is not allowed on resource "User#null" with privilege "update" because it is not himself', $acl->getLastDenialMessage());
41
42
        // Test again the first case to assert that reject reason does not leak from one assertion to the next
43
        User::setCurrent(null);
44
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'anonymous cannot update');
45
        self::assertSame('Non-logged user with role anonymous is not allowed on resource "User#null" with privilege "update"', $acl->getLastDenialMessage());
46
47
        $administrator = new User(User::ROLE_ADMINISTRATOR);
48
        $administrator->setLogin('jane');
49
        User::setCurrent($administrator);
50
        self::assertTrue($acl->isCurrentUserAllowed($user, 'update'), 'admin can do anything');
51
        self::assertNull($acl->getLastDenialMessage());
52
    }
53
}
54