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
|
|
|
namespace eZ\Publish\Core\Repository\Tests\Service\Mock; |
8
|
|
|
|
9
|
|
|
use DateTime; |
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit; |
11
|
|
|
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult; |
12
|
|
|
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest; |
13
|
|
|
use eZ\Publish\API\Repository\SearchService; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion as ContentCriterion; |
16
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query as ContentQuery; |
17
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult as ContentSearchResults; |
18
|
|
|
use eZ\Publish\API\Repository\Values\URL\SearchResult; |
19
|
|
|
use eZ\Publish\API\Repository\Values\URL\URL; |
20
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLQuery; |
21
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct; |
22
|
|
|
use eZ\Publish\Core\Repository\URLService; |
23
|
|
|
use eZ\Publish\SPI\Persistence\URL\URL as SpiUrl; |
24
|
|
|
|
25
|
|
|
class UrlTest extends BaseServiceMockTest |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \eZ\Publish\API\Repository\URLService|\PHPUnit_Framework_MockObject_MockObject |
29
|
|
|
*/ |
30
|
|
|
private $urlHandler; |
31
|
|
|
|
32
|
|
|
protected function setUp() |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
$this->urlHandler = $this->getPersistenceMockHandler('URL\\Handler'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
40
|
|
|
*/ |
41
|
|
|
public function testFindUrlsUnauthorized() |
42
|
|
|
{ |
43
|
|
|
$this->configureUrlViewPermission(false); |
44
|
|
|
|
45
|
|
|
$this->createUrlService()->findUrls(new URLQuery()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testFindUrlsNonNumericOffset() |
52
|
|
|
{ |
53
|
|
|
$this->configureUrlViewPermission(true); |
54
|
|
|
|
55
|
|
|
$query = new URLQuery(); |
56
|
|
|
$query->offset = 'foo'; |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->createUrlService()->findUrls($query); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function testFindUrlsNonNumericLimit() |
65
|
|
|
{ |
66
|
|
|
$this->configureUrlViewPermission(true); |
67
|
|
|
|
68
|
|
|
$query = new URLQuery(); |
69
|
|
|
$query->limit = 'foo'; |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->createUrlService()->findUrls($query); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testFindUrls() |
75
|
|
|
{ |
76
|
|
|
$this->configureUrlViewPermission(true); |
77
|
|
|
|
78
|
|
|
$query = new URLQuery(); |
79
|
|
|
|
80
|
|
|
$url = $this->getApiUrl(); |
81
|
|
|
|
82
|
|
|
$results = [ |
83
|
|
|
'count' => 1, |
84
|
|
|
'items' => [ |
85
|
|
|
new SpiUrl(), |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$expected = new SearchResult([ |
90
|
|
|
'totalCount' => 1, |
91
|
|
|
'items' => [$url], |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->urlHandler |
95
|
|
|
->expects($this->once()) |
96
|
|
|
->method('find') |
97
|
|
|
->with($query) |
98
|
|
|
->willReturn($results); |
99
|
|
|
|
100
|
|
|
$this->assertEquals($expected, $this->createUrlService()->findUrls($query)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
105
|
|
|
*/ |
106
|
|
|
public function testUpdateUrlUnauthorized() |
107
|
|
|
{ |
108
|
|
|
$url = $this->getApiUrl(); |
109
|
|
|
|
110
|
|
|
$this->getRepositoryMock() |
111
|
|
|
->expects($this->once()) |
112
|
|
|
->method('hasAccess') |
113
|
|
|
->with('url', 'update') |
114
|
|
|
->willReturn(false); |
115
|
|
|
|
116
|
|
|
$this->createUrlService()->updateUrl($url, new URLUpdateStruct()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
121
|
|
|
*/ |
122
|
|
|
public function testUpdateUrlNonUnique() |
123
|
|
|
{ |
124
|
|
|
$this->configureUrlUpdatePermission(true); |
125
|
|
|
|
126
|
|
|
$url = $this->getApiUrl(1, 'http://ez.no'); |
127
|
|
|
$struct = new URLUpdateStruct([ |
128
|
|
|
'url' => 'http://ez.com', |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$urlService = $this->createUrlService(['isUnique']); |
132
|
|
|
$urlService |
133
|
|
|
->expects($this->once()) |
134
|
|
|
->method('isUnique') |
135
|
|
|
->with($url->id, $struct->url) |
|
|
|
|
136
|
|
|
->willReturn(false); |
137
|
|
|
|
138
|
|
|
$urlService->updateUrl($url, $struct); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testUpdateUrl() |
142
|
|
|
{ |
143
|
|
|
$apiUrl = $this->getApiUrl(1, 'http://ez.no'); |
144
|
|
|
$apiStruct = new URLUpdateStruct([ |
145
|
|
|
'url' => 'http://ez.com', |
146
|
|
|
'isValid' => false, |
147
|
|
|
'lastChecked' => new DateTime(), |
148
|
|
|
]); |
149
|
|
|
|
150
|
|
|
$this->configurePermissions([ |
151
|
|
|
['url', 'update', null], |
152
|
|
|
['url', 'view', null], |
153
|
|
|
['url', 'view', null], |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
$urlService = $this->createUrlService(['isUnique']); |
157
|
|
|
$urlService |
158
|
|
|
->expects($this->once()) |
159
|
|
|
->method('isUnique') |
160
|
|
|
->with($apiUrl->id, $apiStruct->url) |
|
|
|
|
161
|
|
|
->willReturn(true); |
162
|
|
|
|
163
|
|
|
$this->urlHandler |
164
|
|
|
->expects($this->once()) |
165
|
|
|
->method('updateUrl') |
166
|
|
|
->willReturnCallback(function ($id, $struct) use ($apiUrl, $apiStruct) { |
167
|
|
|
$this->assertEquals($apiUrl->id, $id); |
168
|
|
|
|
169
|
|
|
$this->assertEquals($apiStruct->url, $struct->url); |
170
|
|
|
$this->assertEquals(0, $struct->lastChecked); |
171
|
|
|
$this->assertTrue($struct->isValid); |
172
|
|
|
}); |
173
|
|
|
|
174
|
|
|
$this->urlHandler |
175
|
|
|
->method('loadById') |
176
|
|
|
->with($apiUrl->id) |
|
|
|
|
177
|
|
|
->willReturnOnConsecutiveCalls( |
178
|
|
|
new SpiUrl([ |
179
|
|
|
'id' => $apiUrl->id, |
|
|
|
|
180
|
|
|
'url' => $apiUrl->url, |
|
|
|
|
181
|
|
|
'isValid' => $apiUrl->isValid, |
|
|
|
|
182
|
|
|
'lastChecked' => $apiUrl->lastChecked, |
|
|
|
|
183
|
|
|
]), |
184
|
|
|
new SpiUrl([ |
185
|
|
|
'id' => $apiUrl->id, |
|
|
|
|
186
|
|
|
'url' => $apiStruct->url, |
187
|
|
|
'isValid' => true, |
188
|
|
|
'lastChecked' => 0, |
189
|
|
|
]) |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
$this->assertEquals(new URL([ |
193
|
|
|
'id' => $apiUrl->id, |
|
|
|
|
194
|
|
|
'url' => $apiStruct->url, |
195
|
|
|
'isValid' => true, |
196
|
|
|
'lastChecked' => null, |
197
|
|
|
]), $urlService->updateUrl($apiUrl, $apiStruct)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testUpdateUrlStatus() |
201
|
|
|
{ |
202
|
|
|
$apiUrl = $this->getApiUrl(1, 'http://ez.no'); |
203
|
|
|
$apiStruct = new URLUpdateStruct([ |
204
|
|
|
'isValid' => true, |
205
|
|
|
'lastChecked' => new DateTime('@' . time()), |
206
|
|
|
]); |
207
|
|
|
|
208
|
|
|
$this->configurePermissions([ |
209
|
|
|
['url', 'update', null], |
210
|
|
|
['url', 'view', null], |
211
|
|
|
['url', 'view', null], |
212
|
|
|
]); |
213
|
|
|
|
214
|
|
|
$urlService = $this->createUrlService(['isUnique']); |
215
|
|
|
$urlService |
216
|
|
|
->expects($this->once()) |
217
|
|
|
->method('isUnique') |
218
|
|
|
->with($apiUrl->id, $apiStruct->url) |
|
|
|
|
219
|
|
|
->willReturn(true); |
220
|
|
|
|
221
|
|
|
$this->urlHandler |
222
|
|
|
->expects($this->once()) |
223
|
|
|
->method('updateUrl') |
224
|
|
|
->willReturnCallback(function ($id, $struct) use ($apiUrl, $apiStruct) { |
225
|
|
|
$this->assertEquals($apiUrl->id, $id); |
226
|
|
|
|
227
|
|
|
$this->assertEquals($apiUrl->url, $struct->url); |
228
|
|
|
$this->assertEquals($apiStruct->lastChecked->getTimestamp(), $struct->lastChecked); |
229
|
|
|
$this->assertTrue($apiStruct->isValid, $struct->isValid); |
230
|
|
|
}); |
231
|
|
|
|
232
|
|
|
$this->urlHandler |
233
|
|
|
->method('loadById') |
234
|
|
|
->with($apiUrl->id) |
|
|
|
|
235
|
|
|
->willReturnOnConsecutiveCalls( |
236
|
|
|
new SpiUrl([ |
237
|
|
|
'id' => $apiUrl->id, |
|
|
|
|
238
|
|
|
'url' => $apiUrl->url, |
|
|
|
|
239
|
|
|
'isValid' => $apiUrl->isValid, |
|
|
|
|
240
|
|
|
'lastChecked' => $apiUrl->lastChecked, |
|
|
|
|
241
|
|
|
]), |
242
|
|
|
new SpiUrl([ |
243
|
|
|
'id' => $apiUrl->id, |
|
|
|
|
244
|
|
|
'url' => $apiUrl->url, |
|
|
|
|
245
|
|
|
'isValid' => $apiStruct->isValid, |
246
|
|
|
'lastChecked' => $apiStruct->lastChecked->getTimestamp(), |
247
|
|
|
]) |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
$this->assertEquals(new URL([ |
251
|
|
|
'id' => $apiUrl->id, |
|
|
|
|
252
|
|
|
'url' => $apiUrl->url, |
|
|
|
|
253
|
|
|
'isValid' => $apiStruct->isValid, |
254
|
|
|
'lastChecked' => $apiStruct->lastChecked, |
255
|
|
|
]), $urlService->updateUrl($apiUrl, $apiStruct)); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
260
|
|
|
*/ |
261
|
|
|
public function testLoadByIdUnauthorized() |
262
|
|
|
{ |
263
|
|
|
$this->configureUrlViewPermission(false); |
264
|
|
|
|
265
|
|
|
$this->createUrlService()->loadById(1); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
View Code Duplication |
public function testLoadById() |
269
|
|
|
{ |
270
|
|
|
$this->configureUrlViewPermission(true); |
271
|
|
|
|
272
|
|
|
$urlId = 12; |
273
|
|
|
|
274
|
|
|
$this->urlHandler |
275
|
|
|
->expects($this->once()) |
276
|
|
|
->method('loadById') |
277
|
|
|
->with($urlId) |
278
|
|
|
->willReturn(new SpiUrl([ |
279
|
|
|
'id' => $urlId, |
280
|
|
|
])); |
281
|
|
|
|
282
|
|
|
$this->assertEquals(new URL([ |
283
|
|
|
'id' => $urlId, |
284
|
|
|
]), $this->createUrlService()->loadById($urlId)); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
289
|
|
|
*/ |
290
|
|
|
public function testLoadByUrlUnauthorized() |
291
|
|
|
{ |
292
|
|
|
$this->configureUrlViewPermission(false); |
293
|
|
|
|
294
|
|
|
$this->createUrlService()->loadByUrl('http://ez.no'); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
View Code Duplication |
public function testLoadByUrl() |
298
|
|
|
{ |
299
|
|
|
$this->configureUrlViewPermission(true); |
300
|
|
|
|
301
|
|
|
$url = 'http://ez.no'; |
302
|
|
|
|
303
|
|
|
$this->urlHandler |
304
|
|
|
->expects($this->once()) |
305
|
|
|
->method('loadByUrl') |
306
|
|
|
->with($url) |
307
|
|
|
->willReturn(new SpiUrl([ |
308
|
|
|
'url' => $url, |
309
|
|
|
])); |
310
|
|
|
|
311
|
|
|
$this->assertEquals(new URL([ |
312
|
|
|
'url' => $url, |
313
|
|
|
]), $this->createUrlService()->loadByUrl($url)); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @dataProvider dateProviderForFindUsages |
318
|
|
|
*/ |
319
|
|
|
public function testFindUsages($offset, $limit, ContentQuery $expectedQuery, array $usages) |
320
|
|
|
{ |
321
|
|
|
$url = $this->getApiUrl(1, 'http://ez.no'); |
322
|
|
|
|
323
|
|
|
if (!empty($usages)) { |
324
|
|
|
$searchService = $this->createMock(SearchService::class); |
325
|
|
|
$searchService |
326
|
|
|
->expects($this->once()) |
327
|
|
|
->method('findContentInfo') |
328
|
|
|
->willReturnCallback(function ($query) use ($expectedQuery, $usages) { |
329
|
|
|
$this->assertEquals($expectedQuery, $query); |
330
|
|
|
|
331
|
|
|
return new ContentSearchResults([ |
332
|
|
|
'searchHits' => array_map(function ($id) { |
333
|
|
|
return new SearchHit([ |
334
|
|
|
'valueObject' => new ContentInfo([ |
335
|
|
|
'id' => $id, |
336
|
|
|
]), |
337
|
|
|
]); |
338
|
|
|
}, $usages), |
339
|
|
|
'totalCount' => count($usages), |
340
|
|
|
]); |
341
|
|
|
}); |
342
|
|
|
|
343
|
|
|
$this->getRepositoryMock() |
344
|
|
|
->expects($this->once()) |
345
|
|
|
->method('getSearchService') |
346
|
|
|
->willReturn($searchService); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
$this->urlHandler |
350
|
|
|
->expects($this->once()) |
351
|
|
|
->method('findUsages') |
352
|
|
|
->with($url->id) |
|
|
|
|
353
|
|
|
->willReturn($usages); |
354
|
|
|
|
355
|
|
|
$usageSearchResult = $this->createUrlService()->findUsages($url, $offset, $limit); |
356
|
|
|
|
357
|
|
|
$this->assertInstanceOf(UsageSearchResult::class, $usageSearchResult); |
358
|
|
|
$this->assertEquals(count($usages), $usageSearchResult->totalCount); |
359
|
|
|
foreach ($usageSearchResult as $contentInfo) { |
360
|
|
|
$this->assertContains($contentInfo->id, $usages); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function dateProviderForFindUsages() |
365
|
|
|
{ |
366
|
|
|
return [ |
367
|
|
|
[ |
368
|
|
|
10, -1, new ContentQuery([ |
369
|
|
|
'filter' => new ContentCriterion\MatchNone(), |
370
|
|
|
'offset' => 10, |
371
|
|
|
]), [], |
372
|
|
|
], |
373
|
|
|
[ |
374
|
|
|
10, -1, new ContentQuery([ |
375
|
|
|
'filter' => new ContentCriterion\LogicalAnd([ |
376
|
|
|
new ContentCriterion\ContentId([1, 2, 3]), |
377
|
|
|
new ContentCriterion\Visibility(ContentCriterion\Visibility::VISIBLE), |
378
|
|
|
]), |
379
|
|
|
'offset' => 10, |
380
|
|
|
]), [1, 2, 3], |
381
|
|
|
], |
382
|
|
|
[ |
383
|
|
|
10, 10, new ContentQuery([ |
384
|
|
|
'filter' => new ContentCriterion\LogicalAnd([ |
385
|
|
|
new ContentCriterion\ContentId([1, 2, 3]), |
386
|
|
|
new ContentCriterion\Visibility(ContentCriterion\Visibility::VISIBLE), |
387
|
|
|
]), |
388
|
|
|
'offset' => 10, |
389
|
|
|
'limit' => 10, |
390
|
|
|
]), [1, 2, 3], |
391
|
|
|
], |
392
|
|
|
]; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
public function testCreateUpdateStruct() |
396
|
|
|
{ |
397
|
|
|
$this->assertEquals(new URLUpdateStruct(), $this->createUrlService()->createUpdateStruct()); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
protected function configureUrlViewPermission($hasAccess = false) |
401
|
|
|
{ |
402
|
|
|
$this->getRepositoryMock() |
403
|
|
|
->expects($this->once()) |
404
|
|
|
->method('hasAccess') |
405
|
|
|
->with('url', 'view') |
406
|
|
|
->willReturn($hasAccess); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
protected function configureUrlUpdatePermission($hasAccess = false) |
410
|
|
|
{ |
411
|
|
|
$this->getRepositoryMock() |
412
|
|
|
->expects($this->once()) |
413
|
|
|
->method('hasAccess') |
414
|
|
|
->with('url', 'update') |
415
|
|
|
->willReturn($hasAccess); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
protected function configurePermissions(array $permissions) |
419
|
|
|
{ |
420
|
|
|
$this->getRepositoryMock() |
421
|
|
|
->expects($this->exactly(count($permissions))) |
422
|
|
|
->method('hasAccess') |
423
|
|
|
->withConsecutive(...$permissions) |
424
|
|
|
->willReturn(true); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @return \eZ\Publish\API\Repository\URLService|\PHPUnit_Framework_MockObject_MockObject |
429
|
|
|
*/ |
430
|
|
|
private function createUrlService(array $methods = null) |
431
|
|
|
{ |
432
|
|
|
return $this |
433
|
|
|
->getMockBuilder(URLService::class) |
434
|
|
|
->setConstructorArgs([$this->getRepositoryMock(), $this->urlHandler]) |
435
|
|
|
->setMethods($methods) |
436
|
|
|
->getMock(); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
private function getApiUrl($id = null, $url = null) |
440
|
|
|
{ |
441
|
|
|
return new URL(['id' => $id, 'url' => $url]); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.