Completed
Push — EZP-26057-permission-api ( b3fc4f )
by
unknown
25:48
created

getPermissionServiceMock()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 23

Duplication

Lines 28
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
nc 1
nop 1
dl 28
loc 28
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RoleIdTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\MVC\Symfony\Security\Tests\User\Identifier;
12
13
use eZ\Publish\Core\MVC\Symfony\Security\User\ContextProvider\RoleContextProvider;
14
use FOS\HttpCache\UserContext\UserContext;
15
use PHPUnit_Framework_TestCase;
16
17
class RoleContextProviderTest extends PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var \PHPUnit_Framework_MockObject_MockObject
21
     */
22
    private $repositoryMock;
23
24
    /**
25
     * @var \PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private $roleServiceMock;
28
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
        $this->repositoryMock = $this
33
            ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Repository')
34
            ->disableOriginalConstructor()
35
            ->setMethods(array('getRoleService', 'getCurrentUser', 'getPermissionService'))
36
            ->getMock();
37
38
        $this->roleServiceMock = $this->getMock('eZ\\Publish\\API\\Repository\\RoleService');
39
40
        $this->repositoryMock
41
            ->expects($this->any())
42
            ->method('getRoleService')
43
            ->will($this->returnValue($this->roleServiceMock));
44
        $this->repositoryMock
45
            ->expects($this->any())
46
            ->method('getPermissionService')
47
            ->will($this->returnValue($this->getPermissionServiceMock($this->repositoryMock)));
48
    }
49
50
    public function testSetIdentity()
51
    {
52
        $user = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\User');
53
        $userContext = new UserContext();
54
55
        $this->repositoryMock
56
            ->expects($this->once())
57
            ->method('getCurrentUser')
58
            ->will($this->returnValue($user));
59
60
        $roleId1 = 123;
61
        $roleId2 = 456;
62
        $roleId3 = 789;
63
        $limitationForRole2 = $this->generateLimitationMock(
64
            array(
65
                'limitationValues' => array('/1/2', '/1/2/43'),
66
            )
67
        );
68
        $limitationForRole3 = $this->generateLimitationMock(
69
            array(
70
                'limitationValues' => array('foo', 'bar'),
71
            )
72
        );
73
        $returnedRoleAssignments = array(
74
            $this->generateRoleAssignmentMock(
75
                array(
76
                    'role' => $this->generateRoleMock(
77
                        array(
78
                            'id' => $roleId1,
79
                        )
80
                    ),
81
                )
82
            ),
83
            $this->generateRoleAssignmentMock(
84
                array(
85
                    'role' => $this->generateRoleMock(
86
                        array(
87
                            'id' => $roleId2,
88
                        )
89
                    ),
90
                    'limitation' => $limitationForRole2,
91
                )
92
            ),
93
            $this->generateRoleAssignmentMock(
94
                array(
95
                    'role' => $this->generateRoleMock(
96
                        array(
97
                            'id' => $roleId3,
98
                        )
99
                    ),
100
                    'limitation' => $limitationForRole3,
101
                )
102
            ),
103
        );
104
105
        $this->roleServiceMock
106
            ->expects($this->once())
107
            ->method('getRoleAssignmentsForUser')
108
            ->with($user, true)
109
            ->will($this->returnValue($returnedRoleAssignments));
110
111
        $this->assertSame(array(), $userContext->getParameters());
112
        $contextProvider = new RoleContextProvider($this->repositoryMock);
113
        $contextProvider->updateUserContext($userContext);
114
        $userContextParams = $userContext->getParameters();
115
        $this->assertArrayHasKey('roleIdList', $userContextParams);
116
        $this->assertSame(array($roleId1, $roleId2, $roleId3), $userContextParams['roleIdList']);
117
        $this->assertArrayHasKey('roleLimitationList', $userContextParams);
118
        $limitationIdentifierForRole2 = get_class($limitationForRole2);
119
        $limitationIdentifierForRole3 = get_class($limitationForRole3);
120
        $this->assertSame(
121
            array(
122
                "$roleId2-$limitationIdentifierForRole2" => array('/1/2', '/1/2/43'),
123
                "$roleId3-$limitationIdentifierForRole3" => array('foo', 'bar'),
124
            ),
125
            $userContextParams['roleLimitationList']
126
        );
127
    }
128
129
    private function generateRoleAssignmentMock(array $properties = array())
130
    {
131
        return $this
132
            ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Values\\User\\UserRoleAssignment')
133
            ->setConstructorArgs(array($properties))
134
            ->getMockForAbstractClass();
135
    }
136
137
    private function generateRoleMock(array $properties = array())
138
    {
139
        return $this
140
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\User\\Role')
141
            ->setConstructorArgs(array($properties))
142
            ->getMockForAbstractClass();
143
    }
144
145
    private function generateLimitationMock(array $properties = array())
146
    {
147
        $limitationMock = $this
148
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\User\\Limitation\\RoleLimitation')
149
            ->setConstructorArgs(array($properties))
150
            ->getMockForAbstractClass();
151
        $limitationMock
152
            ->expects($this->any())
153
            ->method('getIdentifier')
154
            ->will($this->returnValue(get_class($limitationMock)));
155
156
        return $limitationMock;
157
    }
158
159 View Code Duplication
    protected function getPermissionServiceMock($repository)
160
    {
161
        return $this
162
            ->getMockBuilder('\eZ\Publish\Core\Repository\PermissionService')
163
            ->setMethods(null)
164
            ->setConstructorArgs(
165
                [
166
                    $repository,
167
                    $this
168
                        ->getMockBuilder('eZ\Publish\API\Repository\UserService')
169
                        ->getMock(),
170
                    $this
171
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\RoleDomainMapper')
172
                        ->disableOriginalConstructor()
173
                        ->getMock(),
174
                    $this
175
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\LimitationService')
176
                        ->getMock(),
177
                    $this
178
                        ->getMockBuilder('eZ\Publish\SPI\Persistence\User\Handler')
179
                        ->getMock(),
180
                    $this
181
                        ->getMockBuilder('eZ\Publish\API\Repository\Values\User\UserReference')
182
                        ->getMock(),
183
                ]
184
            )
185
            ->getMock();
186
    }
187
}
188