Failed Conditions
Push — master ( d88873...27ba7d )
by Adrien
11:39 queued 08:54
created

AclTest.php$1 ➔ assert()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
cc 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Acl;
6
7
use Ecodev\Felix\Acl\Acl;
8
use Ecodev\Felix\Acl\Assertion\IsMyself;
9
use Ecodev\Felix\Model\CurrentUser;
10
use EcodevTests\Felix\Blog\Model\User;
11
use PHPUnit\Framework\TestCase;
12
13
final class AclTest extends TestCase
14
{
15
    protected function tearDown(): void
16
    {
17
        CurrentUser::set(null);
18
    }
19
20
    public function testIsCurrentUserAllowed(): void
21
    {
22
        $acl = new class() extends Acl {
23
            public function __construct()
24
            {
25
                $user = $this->createModelResource(User::class);
26
                $this->addRole('anonymous');
27
                $this->addRole('member');
28
                $this->allow('member', [$user], ['update'], new IsMyself());
29
            }
30
        };
31
32
        $user = new User();
33
34
        $owner = new User();
35
        $owner->setName('sarah');
36
        CurrentUser::set($owner);
37
        $user->setOwner($owner);
38
39
        CurrentUser::set(null);
40
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'anonymous cannot update');
41
        self::assertSame('Non-logged user with role anonymous is not allowed on resource "User#null" with privilege "update"', $acl->getLastDenialMessage());
42
43
        CurrentUser::set($owner);
44
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'student cannot update even if owner');
45
        self::assertSame('User "sarah" with role member is not allowed on resource "User#null" with privilege "update" because it is not himself', $acl->getLastDenialMessage());
46
47
        $other = new User();
48
        $other->setName('john');
49
        CurrentUser::set($other);
50
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'other user cannot update');
51
        self::assertSame('User "john" with role member is not allowed on resource "User#null" with privilege "update" because it is not himself', $acl->getLastDenialMessage());
52
53
        // Test again the first case to assert that reject reason does not leak from one assertion to the next
54
        CurrentUser::set(null);
55
        self::assertFalse($acl->isCurrentUserAllowed($user, 'update'), 'anonymous cannot update');
56
        self::assertSame('Non-logged user with role anonymous is not allowed on resource "User#null" with privilege "update"', $acl->getLastDenialMessage());
57
    }
58
}
59