Completed
Push — master ( 44e556...6dea28 )
by
unknown
30:10 queued 11:42
created

UserHandlerTest::testAssignRole()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains Test 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
namespace eZ\Publish\Core\Persistence\Cache\Tests;
10
11
use eZ\Publish\SPI\Persistence\Content\Location;
12
use eZ\Publish\SPI\Persistence\User;
13
use eZ\Publish\SPI\Persistence\User\Role;
14
use eZ\Publish\SPI\Persistence\User\RoleAssignment;
15
use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct;
16
use eZ\Publish\SPI\Persistence\User\RoleCreateStruct;
17
use eZ\Publish\SPI\Persistence\User\Policy;
18
use eZ\Publish\SPI\Persistence\User\Handler as SPIUserHandler;
19
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler as SPILocationHandler;
20
21
/**
22
 * Test case for Persistence\Cache\UserHandler.
23
 */
24
class UserHandlerTest extends AbstractCacheHandlerTest
25
{
26
    public function getHandlerMethodName(): string
27
    {
28
        return 'userHandler';
29
    }
30
31
    public function getHandlerClassName(): string
32
    {
33
        return SPIUserHandler::class;
34
    }
35
36
    public function providerForUnCachedMethods(): array
37
    {
38
        $user = new User(['id' => 14]);
39
        $policy = new Policy(['id' => 13, 'roleId' => 9]);
40
        $userToken = new User\UserTokenUpdateStruct(['userId' => 14]);
41
42
        // string $method, array $arguments, array? $tags, string? $key
43
        return [
44
            ['create', [$user], ['content-fields-14']],
45
            ['update', [$user], ['content-fields-14', 'user-14']],
46
            ['updateUserToken', [$userToken], ['content-fields-14', 'user-14']],
47
            ['expireUserToken', [14]],
48
            ['delete', [14], ['content-fields-14', 'user-14']],
49
            ['createRole', [new RoleCreateStruct()]],
50
            ['createRoleDraft', [new RoleCreateStruct()]],
51
            ['loadRole', [9, 1]],
52
            ['loadRoleByIdentifier', ['member', 1]],
53
            ['loadRoleDraftByRoleId', [9]],
54
            ['loadRoles', []],
55
            ['updateRole', [new RoleUpdateStruct(['id' => 9])], ['role-9']],
56
            ['deleteRole', [9], ['role-9', 'role-assignment-role-list-9']],
57
            ['deleteRole', [9, 1]],
58
            ['addPolicyByRoleDraft', [9, $policy]],
59
            ['addPolicy', [9, $policy], ['role-9']],
60
            ['updatePolicy', [$policy], ['policy-13', 'role-9']],
61
            ['deletePolicy', [13, 9], ['policy-13', 'role-9']],
62
            ['loadPoliciesByUserId', [14]],
63
            ['unassignRole', [14, 9], ['role-assignment-group-list-14', 'role-assignment-role-list-9']],
64
            ['removeRoleAssignment', [11], ['role-assignment-11']],
65
        ];
66
    }
67
68
    public function providerForCachedLoadMethods(): array
69
    {
70
        $user = new User(['id' => 14]);
71
        $role = new Role(['id' => 9]);
72
        $roleAssignment = new RoleAssignment(['id' => 11, 'roleId' => 9, 'contentId' => 14]);
73
        $calls = [['locationHandler', Location\Handler::class, 'loadLocationsByContent', [new Location(['pathString' => '/1/2/43/'])]]];
74
75
        // string $method, array $arguments, string $key, mixed? $data
76
        return [
77
            ['load', [14], 'ez-user-14', $user],
78
            ['loadByLogin', ['admin'], 'ez-user-admin-by-login', $user],
79
            ['loadByEmail', ['[email protected]'], 'ez-user-nospam§ez.no-by-email', [$user]],
80
            ['loadUserByToken', ['hash'], 'ez-user-hash-by-account-key', $user],
81
            ['loadRole', [9], 'ez-role-9', $role],
82
            ['loadRoleByIdentifier', ['member'], 'ez-role-member-by-identifier', $role],
83
            ['loadRoleAssignment', [11], 'ez-role-assignment-11', $roleAssignment],
84
            ['loadRoleAssignmentsByRoleId', [9], 'ez-role-assignment-9-by-role', [$roleAssignment]],
85
            ['loadRoleAssignmentsByGroupId', [14], 'ez-role-assignment-14-by-group', [$roleAssignment], false, $calls],
86
            ['loadRoleAssignmentsByGroupId', [14, true], 'ez-role-assignment-14-by-group-inherited', [$roleAssignment], false, $calls],
87
        ];
88
    }
89
90
    public function testPublishRoleDraftFromExistingRole()
91
    {
92
        $this->loggerMock->expects($this->once())->method('logCall');
93
        $innerHandlerMock = $this->createMock(SPIUserHandler::class);
94
        $this->persistenceHandlerMock
95
            ->expects($this->once())
96
            ->method('userHandler')
97
            ->willReturn($innerHandlerMock);
98
        $roleDraftId = 33;
99
        $originalRoleId = 30;
100
        $innerHandlerMock
101
            ->expects($this->once())
102
            ->method('loadRole')
103
            ->with($roleDraftId, Role::STATUS_DRAFT)
104
            ->willReturn(new Role(['originalId' => $originalRoleId]));
105
        $innerHandlerMock
106
            ->expects($this->once())
107
            ->method('publishRoleDraft')
108
            ->with($roleDraftId);
109
        $this->cacheMock
110
            ->expects($this->once())
111
            ->method('invalidateTags')
112
            ->with(['role-' . $originalRoleId]);
113
        $this->cacheMock
114
            ->expects($this->never())
115
            ->method('deleteItem');
116
        $handler = $this->persistenceCacheHandler->userHandler();
117
        $handler->publishRoleDraft($roleDraftId);
118
    }
119
120
    public function testPublishNewRoleDraft()
121
    {
122
        $this->loggerMock->expects($this->once())->method('logCall');
123
        $innerHandlerMock = $this->createMock(SPIUserHandler::class);
124
        $this->persistenceHandlerMock
125
            ->expects($this->once())
126
            ->method('userHandler')
127
            ->willReturn($innerHandlerMock);
128
        $roleDraftId = 33;
129
        $innerHandlerMock
130
            ->expects($this->at(0))
131
            ->method('loadRole')
132
            ->with($roleDraftId, Role::STATUS_DRAFT)
133
            ->willReturn(new Role(['originalId' => -1]));
134
        $innerHandlerMock
135
            ->expects($this->at(1))
136
            ->method('publishRoleDraft')
137
            ->with($roleDraftId);
138
        $this->cacheMock
139
            ->expects($this->never())
140
            ->method($this->anything());
141
        $handler = $this->persistenceCacheHandler->userHandler();
142
        $handler->publishRoleDraft($roleDraftId);
143
    }
144
145
    public function testAssignRole()
146
    {
147
        $innerUserHandlerMock = $this->createMock(SPIUserHandler::class);
148
        $innerLocationHandlerMock = $this->createMock(SPILocationHandler::class);
149
150
        $contentId = 14;
151
        $roleId = 9;
152
153
        $this->loggerMock->expects($this->once())->method('logCall');
154
155
        $this->persistenceHandlerMock
156
            ->expects($this->once())
157
            ->method('userHandler')
158
            ->willReturn($innerUserHandlerMock);
159
160
        $innerUserHandlerMock
161
            ->expects($this->once())
162
            ->method('assignRole')
163
            ->with($contentId, $roleId)
164
            ->will($this->returnValue(null));
165
166
        $this->persistenceHandlerMock
167
            ->expects($this->once())
168
            ->method('locationHandler')
169
            ->willReturn($innerLocationHandlerMock);
170
171
        $innerLocationHandlerMock
172
            ->expects($this->once())
173
            ->method('loadLocationsByContent')
174
            ->with($contentId)
175
            ->willReturn([new Location(['id' => '43'])]);
176
177
        $this->cacheMock
178
            ->expects($this->once())
179
            ->method('invalidateTags')
180
            ->with(['role-assignment-group-list-14', 'role-assignment-role-list-9', 'location-path-43']);
181
182
        $this->cacheMock
183
            ->expects($this->never())
184
            ->method('deleteItem');
185
186
        $handler = $this->persistenceCacheHandler->userHandler();
187
        $handler->assignRole($contentId, $roleId);
188
    }
189
}
190