Completed
Push — sf_cache ( 18214b...3b3329 )
by André
19:05
created

UserHandlerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerMethodName() 0 4 1
A getHandlerClassName() 0 4 1
B providerForUnCachedMethods() 0 29 1
A providerForCachedLoadMethods() 0 20 1
B testPublishRoleDraftFromExistingRole() 0 35 1
B testPublishNewRoleDraft() 0 29 1
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\Handler;
14
use eZ\Publish\SPI\Persistence\User\Role;
15
use eZ\Publish\SPI\Persistence\User\RoleAssignment;
16
use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct;
17
use eZ\Publish\SPI\Persistence\User\RoleCreateStruct;
18
use eZ\Publish\SPI\Persistence\User\Policy;
19
20
/**
21
 * Test case for Persistence\Cache\UserHandler.
22
 */
23
class UserHandlerTest extends AbstractCacheHandlerTest
24
{
25
    public function getHandlerMethodName(): string
26
    {
27
        return 'userHandler';
28
    }
29
30
    public function getHandlerClassName(): string
31
    {
32
        return Handler::class;
33
    }
34
35
    public function providerForUnCachedMethods(): array
36
    {
37
        $user = new User(['id' => 14]);
38
        $policy = new Policy(['id' => 13, 'roleId' => 9]);
39
40
        // string $method, array $arguments, array? $tags, string? $key
41
        return [
42
            ['create', [$user], ['content-fields-14']],
43
            ['update', [$user], ['content-fields-14', 'user-14']],
44
            ['delete', [14], ['content-fields-14', 'user-14']],
45
            ['createRole', [new RoleCreateStruct()]],
46
            ['createRoleDraft', [new RoleCreateStruct()]],
47
            ['loadRole', [9, 1]],
48
            ['loadRoleByIdentifier', ['member', 1]],
49
            ['loadRoleDraftByRoleId', [9]],
50
            ['loadRoles', []],
51
            ['updateRole', [new RoleUpdateStruct(['id' => 9])], ['role-9']],
52
            ['deleteRole', [9], ['role-9', 'role-assignment-role-list-9']],
53
            ['deleteRole', [9, 1]],
54
            ['addPolicyByRoleDraft', [9, $policy]],
55
            ['addPolicy', [9, $policy], ['role-9']],
56
            ['updatePolicy', [$policy], ['policy-13', 'role-9']],
57
            ['deletePolicy', [13, 9], ['policy-13', 'role-9']],
58
            ['loadPoliciesByUserId', [14]],
59
            ['assignRole', [14, 9], ['role-assignment-group-list-14', 'role-assignment-role-list-9']],
60
            ['unassignRole', [14, 9], ['role-assignment-group-list-14', 'role-assignment-role-list-9']],
61
            ['removeRoleAssignment', [11], ['role-assignment-11']],
62
        ];
63
    }
64
65
    public function providerForCachedLoadMethods(): array
66
    {
67
        $user = new User(['id' => 14]);
68
        $role = new Role(['id' => 9]);
69
        $roleAssignment = new RoleAssignment(['id' => 11, 'roleId' => 9, 'contentId' => 14]);
70
        $calls = [['locationHandler', Location\Handler::class, 'loadLocationsByContent', [new Location(['pathString' => '/1/2/43/'])]]];
71
72
        // string $method, array $arguments, string $key, mixed? $data
73
        return [
74
            ['load', [14], 'ez-user-14', $user],
75
            ['loadByLogin', ['admin'], 'ez-user-admin-by-login', $user],
76
            ['loadByEmail', ['[email protected]'], 'ez-user-nospam§ez.no-by-email', [$user]],
77
            ['loadRole', [9], 'ez-role-9', $role],
78
            ['loadRoleByIdentifier', ['member'], 'ez-role-member-by-identifier', $role],
79
            ['loadRoleAssignment', [11], 'ez-role-assignment-11', $roleAssignment],
80
            ['loadRoleAssignmentsByRoleId', [9], 'ez-role-assignment-9-by-role', [$roleAssignment]],
81
            ['loadRoleAssignmentsByGroupId', [14], 'ez-role-assignment-14-by-group', [$roleAssignment], false, $calls],
82
            ['loadRoleAssignmentsByGroupId', [14, true], 'ez-role-assignment-14-by-group-inherited', [$roleAssignment], false, $calls],
83
        ];
84
    }
85
86
    public function testPublishRoleDraftFromExistingRole()
87
    {
88
        $this->loggerMock->expects($this->once())->method('logCall');
89
90
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\User\\Handler');
91
        $this->persistenceHandlerMock
92
            ->expects($this->once())
93
            ->method('userHandler')
94
            ->willReturn($innerHandlerMock);
95
96
        $roleDraftId = 33;
97
        $originalRoleId = 30;
98
        $innerHandlerMock
99
            ->expects($this->once())
100
            ->method('loadRole')
101
            ->with($roleDraftId, Role::STATUS_DRAFT)
102
            ->willReturn(new Role(['originalId' => $originalRoleId]));
103
104
        $innerHandlerMock
105
            ->expects($this->once())
106
            ->method('publishRoleDraft')
107
            ->with($roleDraftId);
108
109
        $this->cacheMock
110
            ->expects($this->once())
111
            ->method('invalidateTags')
112
            ->with(['role-' . $originalRoleId]);
113
114
        $this->cacheMock
115
            ->expects($this->never())
116
            ->method('deleteItem');
117
118
        $handler = $this->persistenceCacheHandler->userHandler();
119
        $handler->publishRoleDraft($roleDraftId);
120
    }
121
122
    public function testPublishNewRoleDraft()
123
    {
124
        $this->loggerMock->expects($this->once())->method('logCall');
125
126
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\User\\Handler');
127
        $this->persistenceHandlerMock
128
            ->expects($this->once())
129
            ->method('userHandler')
130
            ->willReturn($innerHandlerMock);
131
132
        $roleDraftId = 33;
133
        $innerHandlerMock
134
            ->expects($this->at(0))
135
            ->method('loadRole')
136
            ->with($roleDraftId, Role::STATUS_DRAFT)
137
            ->willReturn(new Role(['originalId' => -1]));
138
139
        $innerHandlerMock
140
            ->expects($this->at(1))
141
            ->method('publishRoleDraft')
142
            ->with($roleDraftId);
143
144
        $this->cacheMock
145
            ->expects($this->never())
146
            ->method($this->anything());
147
148
        $handler = $this->persistenceCacheHandler->userHandler();
149
        $handler->publishRoleDraft($roleDraftId);
150
    }
151
}
152