Test Failed
Branch master (c90c22)
by Elijah
02:50
created

PermissionVoterTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 13
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eloyekunle\PermissionsBundle\Tests\Security;
13
14
use Eloyekunle\PermissionsBundle\Model\RoleInterface;
15
use Eloyekunle\PermissionsBundle\Security\PermissionsVoter;
16
use Eloyekunle\PermissionsBundle\Tests\TestRole;
17
use Eloyekunle\PermissionsBundle\Tests\TestUser;
18
use Eloyekunle\PermissionsBundle\Util\PermissionsHandler;
19
use Eloyekunle\PermissionsBundle\Util\PermissionsHandlerInterface;
20
use PHPUnit\Framework\TestCase;
21
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
22
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
23
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
24
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
25
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
26
27
class PermissionVoterTest extends TestCase
28
{
29
    /** @var TokenInterface */
30
    protected $token;
31
32
    /** @var PermissionsVoter */
33
    protected $voter;
34
35
    /** @var AccessDecisionManagerInterface */
36
    protected $accessDecisionManager;
37
38
    /** @var PermissionsHandlerInterface */
39
    protected $permissionsHandler;
40
41
    protected function setUp()
42
    {
43
        $this->accessDecisionManager = new AccessDecisionManager([new RoleVoter()]);
44
        $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
45
        $this->permissionsHandler = new PermissionsHandler(__DIR__.'/../Fixtures');
46
47
        $this->voter = $this->createPermissionsVoter();
48
    }
49
50
    public function getTests()
51
    {
52
        return [
53
            [['delete users'], VoterInterface::ACCESS_DENIED, new \StdClass(), ''],
54
            [['review articles'], VoterInterface::ACCESS_ABSTAIN, new \StdClass(), 'Access Abstain if permission is not declared in modules.'],
55
        ];
56
    }
57
58
    public function getTestsNew()
59
    {
60
        return [
61
            [['delete users'], new \StdClass()],
62
            [['review articles'], new \StdClass(), VoterInterface::ACCESS_ABSTAIN],
63
        ];
64
    }
65
66
    /**
67
     * @dataProvider getTests
68
     *
69
     * @param array $attributes
70
     * @param $expectedVote
71
     * @param $subject
72
     * @param $message
73
     */
74
    public function testVote(array $attributes, $expectedVote, $subject, $message)
75
    {
76
        $voter = new PermissionsVoter(new AccessDecisionManager(), $this->permissionsHandler);
77
        $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
78
79
        $this->assertSame($expectedVote, $voter->vote($token, $subject, $attributes), $message);
80
81
        if (VoterInterface::ACCESS_ABSTAIN !== $expectedVote) {
82
            $role = $this->createMockRole();
83
            foreach ($attributes as $attribute) {
84
                $role->grantPermission($attribute);
85
            }
86
            $user = $this->createMockUser();
87
            $user->addRole($role);
88
            $token->expects($this->atLeastOnce())
89
                ->method('getUser')
90
                ->willReturn($user);
91
92
            $this->assertSame(VoterInterface::ACCESS_GRANTED, $voter->vote($token, $subject, $attributes));
93
94
            foreach ($attributes as $attribute) {
95
                $role->revokePermission($attribute);
96
            }
97
98
            $this->assertSame(VoterInterface::ACCESS_DENIED, $voter->vote($token, $subject, $attributes));
99
        }
100
    }
101
102
    /**
103
     * @dataProvider getTestsNew
104
     *
105
     * @param array $attributes
106
     * @param $subject
107
     * @param $expectedVote
108
     */
109
    public function testSuperAdminPass(array $attributes, $subject, $expectedVote = VoterInterface::ACCESS_GRANTED)
110
    {
111
        $this->assertSame($expectedVote, $this->voter->vote($this->getToken([RoleInterface::ROLE_SUPER_ADMIN]), $subject, $attributes));
112
    }
113
114
    protected function createPermissionsVoter()
115
    {
116
        return new PermissionsVoter($this->accessDecisionManager, $this->permissionsHandler);
117
    }
118
119
    protected function getToken(array $roles)
120
    {
121
        foreach ($roles as $i => $role) {
122
            $roles[$i] = $this->createMockRole()->setRole($role);
123
        }
124
        $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
125
        $token->expects($this->any())
126
            ->method('getRoles')
127
            ->will($this->returnValue($roles));
128
129
        return $token;
130
    }
131
132
    protected function createMockRole()
133
    {
134
        return new TestRole();
135
    }
136
137
    protected function createMockUser()
138
    {
139
        return new TestUser();
140
    }
141
}
142