|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Repository\Tests\Service\Mock; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
use eZ\Publish\API\Repository\PermissionResolver; |
|
13
|
|
|
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest; |
|
14
|
|
|
use eZ\Publish\Core\Repository\UserPreferenceService; |
|
15
|
|
|
use eZ\Publish\Core\Repository\Values\User\UserReference; |
|
16
|
|
|
use eZ\Publish\SPI\Persistence\UserPreference\UserPreference; |
|
17
|
|
|
use eZ\Publish\SPI\Persistence\UserPreference\UserPreferenceSetStruct; |
|
18
|
|
|
use eZ\Publish\API\Repository\Values\UserPreference\UserPreferenceSetStruct as APIUserPreferenceSetStruct; |
|
19
|
|
|
use eZ\Publish\API\Repository\Values\UserPreference\UserPreference as APIUserPreference; |
|
20
|
|
|
|
|
21
|
|
|
class UserPreferenceTest extends BaseServiceMockTest |
|
22
|
|
|
{ |
|
23
|
|
|
const CURRENT_USER_ID = 14; |
|
24
|
|
|
const USER_PREFERENCE_NAME = 'setting'; |
|
25
|
|
|
const USER_PREFERENCE_VALUE = 'value'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \eZ\Publish\SPI\Persistence\UserPreference\Handler|\PHPUnit\Framework\MockObject\MockObject |
|
29
|
|
|
*/ |
|
30
|
|
|
private $userSPIPreferenceHandler; |
|
31
|
|
|
|
|
32
|
|
View Code Duplication |
protected function setUp() |
|
33
|
|
|
{ |
|
34
|
|
|
parent::setUp(); |
|
35
|
|
|
$this->userSPIPreferenceHandler = $this->getPersistenceMockHandler('UserPreference\\Handler'); |
|
36
|
|
|
$permissionResolverMock = $this->createMock(PermissionResolver::class); |
|
37
|
|
|
$permissionResolverMock |
|
38
|
|
|
->expects($this->atLeastOnce()) |
|
39
|
|
|
->method('getCurrentUserReference') |
|
40
|
|
|
->willReturn(new UserReference(self::CURRENT_USER_ID)); |
|
41
|
|
|
$repository = $this->getRepositoryMock(); |
|
42
|
|
|
$repository |
|
43
|
|
|
->expects($this->atLeastOnce()) |
|
44
|
|
|
->method('getPermissionResolver') |
|
45
|
|
|
->willReturn($permissionResolverMock); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @covers \eZ\Publish\Core\Repository\UserPreferenceService::setUserPreference() |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testSetUserPreference() |
|
52
|
|
|
{ |
|
53
|
|
|
$apiUserPreferenceSetStruct = new APIUserPreferenceSetStruct([ |
|
54
|
|
|
'name' => 'setting', |
|
55
|
|
|
'value' => 'value', |
|
56
|
|
|
]); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertTransactionIsCommitted(function () { |
|
59
|
|
|
$this->userSPIPreferenceHandler |
|
60
|
|
|
->expects($this->once()) |
|
61
|
|
|
->method('setUserPreference') |
|
62
|
|
|
->willReturnCallback(function (UserPreferenceSetStruct $setStruct) { |
|
63
|
|
|
$this->assertEquals(self::USER_PREFERENCE_NAME, $setStruct->name); |
|
64
|
|
|
$this->assertEquals(self::USER_PREFERENCE_VALUE, $setStruct->value); |
|
65
|
|
|
$this->assertEquals(self::CURRENT_USER_ID, $setStruct->userId); |
|
66
|
|
|
|
|
67
|
|
|
return new UserPreference(); |
|
68
|
|
|
}); |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
$this->createAPIUserPreferenceService()->setUserPreference([$apiUserPreferenceSetStruct]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @covers \eZ\Publish\Core\Repository\UserPreferenceService::setUserPreference |
|
76
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testSetUserPreferenceThrowsInvalidArgumentException() |
|
79
|
|
|
{ |
|
80
|
|
|
$apiUserPreferenceSetStruct = new APIUserPreferenceSetStruct([ |
|
81
|
|
|
'value' => 'value', |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertTransactionIsNotStarted(function () { |
|
85
|
|
|
$this->userSPIPreferenceHandler->expects($this->never())->method('setUserPreference'); |
|
86
|
|
|
}); |
|
87
|
|
|
|
|
88
|
|
|
$this->createAPIUserPreferenceService()->setUserPreference([$apiUserPreferenceSetStruct]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @covers \eZ\Publish\Core\Repository\UserPreferenceService::setUserPreference |
|
93
|
|
|
* @expectedException \Exception |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testSetUserPreferenceWithRollback() |
|
96
|
|
|
{ |
|
97
|
|
|
$apiUserPreferenceSetStruct = new APIUserPreferenceSetStruct([ |
|
98
|
|
|
'name' => 'setting', |
|
99
|
|
|
'value' => 'value', |
|
100
|
|
|
]); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertTransactionIsRollback(function () { |
|
103
|
|
|
$this->userSPIPreferenceHandler |
|
104
|
|
|
->expects($this->once()) |
|
105
|
|
|
->method('setUserPreference') |
|
106
|
|
|
->willThrowException($this->createMock(Exception::class)); |
|
107
|
|
|
}); |
|
108
|
|
|
|
|
109
|
|
|
$this->createAPIUserPreferenceService()->setUserPreference([$apiUserPreferenceSetStruct]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @covers \eZ\Publish\Core\Repository\UserPreferenceService::getUserPreference() |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testGetUserPreference() |
|
116
|
|
|
{ |
|
117
|
|
|
$userPreferenceName = 'setting'; |
|
118
|
|
|
$userPreferenceValue = 'value'; |
|
119
|
|
|
|
|
120
|
|
|
$this->userSPIPreferenceHandler |
|
121
|
|
|
->expects($this->once()) |
|
122
|
|
|
->method('getUserPreferenceByUserIdAndName') |
|
123
|
|
|
->with(self::CURRENT_USER_ID, $userPreferenceName) |
|
124
|
|
|
->willReturn(new UserPreference([ |
|
125
|
|
|
'name' => $userPreferenceName, |
|
126
|
|
|
'value' => $userPreferenceValue, |
|
127
|
|
|
'userId' => self::CURRENT_USER_ID, |
|
128
|
|
|
])); |
|
129
|
|
|
|
|
130
|
|
|
$APIUserPreference = $this->createAPIUserPreferenceService()->getUserPreference($userPreferenceName); |
|
131
|
|
|
$expected = new APIUserPreference([ |
|
132
|
|
|
'name' => $userPreferenceName, |
|
133
|
|
|
'value' => $userPreferenceValue, |
|
134
|
|
|
]); |
|
135
|
|
|
$this->assertEquals($expected, $APIUserPreference); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @covers \eZ\Publish\Core\Repository\UserPreferenceService::loadUserPreferences |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testLoadUserPreferences() |
|
142
|
|
|
{ |
|
143
|
|
|
$offset = 0; |
|
144
|
|
|
$limit = 25; |
|
145
|
|
|
$expectedTotalCount = 10; |
|
146
|
|
|
|
|
147
|
|
|
$expectedItems = array_map(function () { |
|
148
|
|
|
return $this->createAPIUserPreference(); |
|
149
|
|
|
}, range(1, $expectedTotalCount)); |
|
150
|
|
|
|
|
151
|
|
|
$this->userSPIPreferenceHandler |
|
152
|
|
|
->expects($this->once()) |
|
153
|
|
|
->method('countUserPreferences') |
|
154
|
|
|
->with(self::CURRENT_USER_ID) |
|
155
|
|
|
->willReturn($expectedTotalCount); |
|
156
|
|
|
|
|
157
|
|
|
$this->userSPIPreferenceHandler |
|
158
|
|
|
->expects($this->once()) |
|
159
|
|
|
->method('loadUserPreferences') |
|
160
|
|
|
->with(self::CURRENT_USER_ID, $offset, $limit) |
|
161
|
|
|
->willReturn(array_map(function ($locationId) { |
|
|
|
|
|
|
162
|
|
|
return new UserPreference([ |
|
163
|
|
|
'name' => 'setting', |
|
164
|
|
|
'value' => 'value', |
|
165
|
|
|
]); |
|
166
|
|
|
}, range(1, $expectedTotalCount))); |
|
167
|
|
|
|
|
168
|
|
|
$userPreferences = $this->createAPIUserPreferenceService()->loadUserPreferences($offset, $limit); |
|
169
|
|
|
|
|
170
|
|
|
$this->assertEquals($expectedTotalCount, $userPreferences->totalCount); |
|
171
|
|
|
$this->assertEquals($expectedItems, $userPreferences->items); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @covers \eZ\Publish\Core\Repository\UserPreferenceService::getUserPreferenceCount() |
|
176
|
|
|
*/ |
|
177
|
|
|
public function testGetUserPreferenceCount() |
|
178
|
|
|
{ |
|
179
|
|
|
$expectedTotalCount = 10; |
|
180
|
|
|
|
|
181
|
|
|
$this->userSPIPreferenceHandler |
|
182
|
|
|
->expects($this->once()) |
|
183
|
|
|
->method('countUserPreferences') |
|
184
|
|
|
->with(self::CURRENT_USER_ID) |
|
185
|
|
|
->willReturn($expectedTotalCount); |
|
186
|
|
|
|
|
187
|
|
|
$APIUserPreference = $this->createAPIUserPreferenceService()->getUserPreferenceCount(); |
|
188
|
|
|
|
|
189
|
|
|
$this->assertEquals($expectedTotalCount, $APIUserPreference); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return \eZ\Publish\API\Repository\UserPreferenceService|\PHPUnit\Framework\MockObject\MockObject |
|
194
|
|
|
*/ |
|
195
|
|
|
private function createAPIUserPreferenceService(array $methods = null) |
|
196
|
|
|
{ |
|
197
|
|
|
return $this |
|
198
|
|
|
->getMockBuilder(UserPreferenceService::class) |
|
199
|
|
|
->setConstructorArgs([$this->getRepositoryMock(), $this->userSPIPreferenceHandler]) |
|
200
|
|
|
->setMethods($methods) |
|
201
|
|
|
->getMock(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
View Code Duplication |
private function assertTransactionIsCommitted(callable $operation): void |
|
205
|
|
|
{ |
|
206
|
|
|
$repository = $this->getRepositoryMock(); |
|
207
|
|
|
$repository->expects($this->once())->method('beginTransaction'); |
|
208
|
|
|
$operation(); |
|
209
|
|
|
$repository->expects($this->once())->method('commit'); |
|
210
|
|
|
$repository->expects($this->never())->method('rollback'); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
View Code Duplication |
private function assertTransactionIsNotStarted(callable $operation): void |
|
214
|
|
|
{ |
|
215
|
|
|
$repository = $this->getRepositoryMock(); |
|
216
|
|
|
$repository->expects($this->never())->method('beginTransaction'); |
|
217
|
|
|
$operation(); |
|
218
|
|
|
$repository->expects($this->never())->method('commit'); |
|
219
|
|
|
$repository->expects($this->never())->method('rollback'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
View Code Duplication |
private function assertTransactionIsRollback(callable $operation): void |
|
223
|
|
|
{ |
|
224
|
|
|
$repository = $this->getRepositoryMock(); |
|
225
|
|
|
$repository->expects($this->once())->method('beginTransaction'); |
|
226
|
|
|
$operation(); |
|
227
|
|
|
$repository->expects($this->never())->method('commit'); |
|
228
|
|
|
$repository->expects($this->once())->method('rollback'); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
private function createAPIUserPreference(): APIUserPreference |
|
232
|
|
|
{ |
|
233
|
|
|
return new APIUserPreference([ |
|
234
|
|
|
'name' => 'setting', |
|
235
|
|
|
'value' => 'value', |
|
236
|
|
|
]); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.