1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\UrlWildcardTest 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\Repository\Tests\Service\Mock; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest; |
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\URLWildcard; |
13
|
|
|
use eZ\Publish\Core\Repository\URLWildcardService; |
14
|
|
|
use eZ\Publish\SPI\Persistence\Content\UrlWildcard as SPIURLWildcard; |
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Mock Test case for UrlWildcard Service. |
19
|
|
|
*/ |
20
|
|
|
class UrlWildcardTest extends BaseServiceMockTest |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Test for the __construct() method. |
24
|
|
|
* |
25
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::__construct |
26
|
|
|
*/ |
27
|
|
|
public function testConstructor() |
28
|
|
|
{ |
29
|
|
|
$service = $this->getPartlyMockedURLWildcardService(); |
30
|
|
|
|
31
|
|
|
self::assertAttributeSame($this->getRepositoryMock(), 'repository', $service); |
32
|
|
|
self::assertAttributeSame($this->getPersistenceMockHandler('Content\\UrlWildcard\\Handler'), 'urlWildcardHandler', $service); |
33
|
|
|
self::assertAttributeSame(array(), 'settings', $service); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test for the create() method. |
38
|
|
|
* |
39
|
|
|
* @depends testConstructor |
40
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::create |
41
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
42
|
|
|
*/ |
43
|
|
|
public function testCreateThrowsUnauthorizedException() |
44
|
|
|
{ |
45
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
46
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
47
|
|
|
$repositoryMock->expects( |
48
|
|
|
$this->once() |
49
|
|
|
)->method( |
50
|
|
|
'hasAccess' |
51
|
|
|
)->with( |
52
|
|
|
$this->equalTo('content'), |
53
|
|
|
$this->equalTo('urltranslator') |
54
|
|
|
)->will( |
55
|
|
|
$this->returnValue(false) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$mockedService->create('lorem/ipsum', 'opossum', true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Test for the create() method. |
63
|
|
|
* |
64
|
|
|
* @depends testConstructor |
65
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::create |
66
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
67
|
|
|
*/ |
68
|
|
|
public function testCreateThrowsInvalidArgumentException() |
69
|
|
|
{ |
70
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
71
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
72
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
73
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
74
|
|
|
|
75
|
|
|
$repositoryMock->expects( |
76
|
|
|
$this->once() |
77
|
|
|
)->method( |
78
|
|
|
'hasAccess' |
79
|
|
|
)->with( |
80
|
|
|
$this->equalTo('content'), |
81
|
|
|
$this->equalTo('urltranslator') |
82
|
|
|
)->will( |
83
|
|
|
$this->returnValue(true) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$handlerMock->expects( |
87
|
|
|
$this->once() |
88
|
|
|
)->method( |
89
|
|
|
'loadAll' |
90
|
|
|
)->will( |
91
|
|
|
$this->returnValue( |
92
|
|
|
array( |
93
|
|
|
new SPIURLWildcard(array('sourceUrl' => '/lorem/ipsum')), |
94
|
|
|
) |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$mockedService->create('/lorem/ipsum', 'opossum', true); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function providerForTestCreateThrowsContentValidationException() |
102
|
|
|
{ |
103
|
|
|
return array( |
104
|
|
|
array('fruit', 'food/{1}', true), |
105
|
|
|
array('fruit/*', 'food/{2}', false), |
106
|
|
|
array('fruit/*/*', 'food/{3}', true), |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Test for the create() method. |
112
|
|
|
* |
113
|
|
|
* @depends testConstructor |
114
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::create |
115
|
|
|
* @dataProvider providerForTestCreateThrowsContentValidationException |
116
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\ContentValidationException |
117
|
|
|
*/ |
118
|
|
|
public function testCreateThrowsContentValidationException($sourceUrl, $destinationUrl, $forward) |
119
|
|
|
{ |
120
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
121
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
122
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
123
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
124
|
|
|
|
125
|
|
|
$repositoryMock->expects( |
126
|
|
|
$this->once() |
127
|
|
|
)->method( |
128
|
|
|
'hasAccess' |
129
|
|
|
)->with( |
130
|
|
|
$this->equalTo('content'), |
131
|
|
|
$this->equalTo('urltranslator') |
132
|
|
|
)->will( |
133
|
|
|
$this->returnValue(true) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$handlerMock->expects( |
137
|
|
|
$this->once() |
138
|
|
|
)->method( |
139
|
|
|
'loadAll' |
140
|
|
|
)->will( |
141
|
|
|
$this->returnValue(array()) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$mockedService->create($sourceUrl, $destinationUrl, $forward); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function providerForTestCreate() |
148
|
|
|
{ |
149
|
|
|
return array( |
150
|
|
|
array('fruit', 'food', true), |
151
|
|
|
array(' /fruit/ ', ' /food/ ', true), |
152
|
|
|
array('/fruit/*', '/food', false), |
153
|
|
|
array('/fruit/*', '/food/{1}', true), |
154
|
|
|
array('/fruit/*/*', '/food/{1}', true), |
155
|
|
|
array('/fruit/*/*', '/food/{2}', true), |
156
|
|
|
array('/fruit/*/*', '/food/{1}/{2}', true), |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test for the create() method. |
162
|
|
|
* |
163
|
|
|
* @depends testConstructor |
164
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::create |
165
|
|
|
* @dataProvider providerForTestCreate |
166
|
|
|
*/ |
167
|
|
|
public function testCreate($sourceUrl, $destinationUrl, $forward) |
168
|
|
|
{ |
169
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
170
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
171
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
172
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
173
|
|
|
|
174
|
|
|
$sourceUrl = '/' . trim($sourceUrl, '/ '); |
175
|
|
|
$destinationUrl = '/' . trim($destinationUrl, '/ '); |
176
|
|
|
|
177
|
|
|
$repositoryMock->expects( |
178
|
|
|
$this->once() |
179
|
|
|
)->method( |
180
|
|
|
'hasAccess' |
181
|
|
|
)->with( |
182
|
|
|
$this->equalTo('content'), |
183
|
|
|
$this->equalTo('urltranslator') |
184
|
|
|
)->will( |
185
|
|
|
$this->returnValue(true) |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$repositoryMock->expects($this->once())->method('beginTransaction'); |
189
|
|
|
$repositoryMock->expects($this->once())->method('commit'); |
190
|
|
|
|
191
|
|
|
$handlerMock->expects( |
192
|
|
|
$this->once() |
193
|
|
|
)->method( |
194
|
|
|
'loadAll' |
195
|
|
|
)->will( |
196
|
|
|
$this->returnValue(array()) |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$handlerMock->expects( |
200
|
|
|
$this->once() |
201
|
|
|
)->method( |
202
|
|
|
'create' |
203
|
|
|
)->with( |
204
|
|
|
$this->equalTo($sourceUrl), |
205
|
|
|
$this->equalTo($destinationUrl), |
206
|
|
|
$this->equalTo($forward) |
207
|
|
|
)->will( |
208
|
|
|
$this->returnValue( |
209
|
|
|
new SPIURLWildcard( |
210
|
|
|
array( |
211
|
|
|
'id' => 123456, |
212
|
|
|
'sourceUrl' => $sourceUrl, |
213
|
|
|
'destinationUrl' => $destinationUrl, |
214
|
|
|
'forward' => $forward, |
215
|
|
|
) |
216
|
|
|
) |
217
|
|
|
) |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
$urlWildCard = $mockedService->create($sourceUrl, $destinationUrl, $forward); |
221
|
|
|
|
222
|
|
|
$this->assertEquals( |
223
|
|
|
new URLWildcard( |
224
|
|
|
array( |
225
|
|
|
'id' => 123456, |
226
|
|
|
'sourceUrl' => $sourceUrl, |
227
|
|
|
'destinationUrl' => $destinationUrl, |
228
|
|
|
'forward' => $forward, |
229
|
|
|
) |
230
|
|
|
), |
231
|
|
|
$urlWildCard |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Test for the create() method. |
237
|
|
|
* |
238
|
|
|
* @depends testConstructor |
239
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::create |
240
|
|
|
* @expectedException \Exception |
241
|
|
|
*/ |
242
|
|
|
public function testCreateWithRollback() |
243
|
|
|
{ |
244
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
245
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
246
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
247
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
248
|
|
|
|
249
|
|
|
$repositoryMock->expects( |
250
|
|
|
$this->once() |
251
|
|
|
)->method( |
252
|
|
|
'hasAccess' |
253
|
|
|
)->with( |
254
|
|
|
$this->equalTo('content'), |
255
|
|
|
$this->equalTo('urltranslator') |
256
|
|
|
)->will( |
257
|
|
|
$this->returnValue(true) |
258
|
|
|
); |
259
|
|
|
|
260
|
|
|
$repositoryMock->expects($this->once())->method('beginTransaction'); |
261
|
|
|
$repositoryMock->expects($this->once())->method('rollback'); |
262
|
|
|
|
263
|
|
|
$handlerMock->expects( |
264
|
|
|
$this->once() |
265
|
|
|
)->method( |
266
|
|
|
'loadAll' |
267
|
|
|
)->will( |
268
|
|
|
$this->returnValue(array()) |
269
|
|
|
); |
270
|
|
|
|
271
|
|
|
$sourceUrl = '/lorem'; |
272
|
|
|
$destinationUrl = '/ipsum'; |
273
|
|
|
$forward = true; |
274
|
|
|
|
275
|
|
|
$handlerMock->expects( |
276
|
|
|
$this->once() |
277
|
|
|
)->method( |
278
|
|
|
'create' |
279
|
|
|
)->with( |
280
|
|
|
$this->equalTo($sourceUrl), |
281
|
|
|
$this->equalTo($destinationUrl), |
282
|
|
|
$this->equalTo($forward) |
283
|
|
|
)->will( |
284
|
|
|
$this->throwException(new \Exception()) |
285
|
|
|
); |
286
|
|
|
|
287
|
|
|
$mockedService->create($sourceUrl, $destinationUrl, $forward); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Test for the remove() method. |
292
|
|
|
* |
293
|
|
|
* @depends testConstructor |
294
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::remove |
295
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
296
|
|
|
*/ |
297
|
|
|
public function testRemoveThrowsUnauthorizedException() |
298
|
|
|
{ |
299
|
|
|
$wildcard = new URLWildcard(['id' => 'McBoom']); |
300
|
|
|
|
301
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
302
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
303
|
|
|
$repositoryMock->expects( |
304
|
|
|
$this->once() |
305
|
|
|
)->method( |
306
|
|
|
'canUser' |
307
|
|
|
)->with( |
308
|
|
|
$this->equalTo('content'), |
309
|
|
|
$this->equalTo('urltranslator'), |
310
|
|
|
$this->equalTo($wildcard) |
311
|
|
|
)->will( |
312
|
|
|
$this->returnValue(false) |
313
|
|
|
); |
314
|
|
|
|
315
|
|
|
$repositoryMock->expects($this->never())->method('beginTransaction'); |
316
|
|
|
|
317
|
|
|
$mockedService->remove($wildcard); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Test for the remove() method. |
322
|
|
|
* |
323
|
|
|
* @depends testConstructor |
324
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::remove |
325
|
|
|
*/ |
326
|
|
View Code Duplication |
public function testRemove() |
327
|
|
|
{ |
328
|
|
|
$wildcard = new URLWildcard(['id' => 'McBomb']); |
329
|
|
|
|
330
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
331
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
332
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
333
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
334
|
|
|
|
335
|
|
|
$repositoryMock->expects( |
336
|
|
|
$this->once() |
337
|
|
|
)->method( |
338
|
|
|
'canUser' |
339
|
|
|
)->with( |
340
|
|
|
$this->equalTo('content'), |
341
|
|
|
$this->equalTo('urltranslator'), |
342
|
|
|
$this->equalTo($wildcard) |
343
|
|
|
)->will( |
344
|
|
|
$this->returnValue(true) |
345
|
|
|
); |
346
|
|
|
|
347
|
|
|
$repositoryMock->expects($this->once())->method('beginTransaction'); |
348
|
|
|
$repositoryMock->expects($this->once())->method('commit'); |
349
|
|
|
|
350
|
|
|
$handlerMock->expects( |
351
|
|
|
$this->once() |
352
|
|
|
)->method( |
353
|
|
|
'remove' |
354
|
|
|
)->with( |
355
|
|
|
$this->equalTo('McBomb') |
356
|
|
|
); |
357
|
|
|
|
358
|
|
|
$mockedService->remove($wildcard); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Test for the remove() method. |
363
|
|
|
* |
364
|
|
|
* @depends testConstructor |
365
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::remove |
366
|
|
|
* @expectedException \Exception |
367
|
|
|
*/ |
368
|
|
View Code Duplication |
public function testRemoveWithRollback() |
369
|
|
|
{ |
370
|
|
|
$wildcard = new URLWildcard(['id' => 'McBoo']); |
371
|
|
|
|
372
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
373
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
374
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
375
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
376
|
|
|
|
377
|
|
|
$repositoryMock->expects( |
378
|
|
|
$this->once() |
379
|
|
|
)->method( |
380
|
|
|
'canUser' |
381
|
|
|
)->with( |
382
|
|
|
$this->equalTo('content'), |
383
|
|
|
$this->equalTo('urltranslator'), |
384
|
|
|
$this->equalTo($wildcard) |
385
|
|
|
)->will( |
386
|
|
|
$this->returnValue(true) |
387
|
|
|
); |
388
|
|
|
|
389
|
|
|
$repositoryMock->expects($this->once())->method('beginTransaction'); |
390
|
|
|
$repositoryMock->expects($this->once())->method('rollback'); |
391
|
|
|
|
392
|
|
|
$handlerMock->expects( |
393
|
|
|
$this->once() |
394
|
|
|
)->method( |
395
|
|
|
'remove' |
396
|
|
|
)->with( |
397
|
|
|
$this->equalTo('McBoo') |
398
|
|
|
)->will( |
399
|
|
|
$this->throwException(new \Exception()) |
400
|
|
|
); |
401
|
|
|
|
402
|
|
|
$mockedService->remove($wildcard); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Test for the load() method. |
407
|
|
|
* |
408
|
|
|
* @depends testConstructor |
409
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::remove |
410
|
|
|
* @expectedException \Exception |
411
|
|
|
*/ |
412
|
|
View Code Duplication |
public function testLoadThrowsException() |
413
|
|
|
{ |
414
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
415
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
416
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
417
|
|
|
|
418
|
|
|
$handlerMock->expects( |
419
|
|
|
$this->once() |
420
|
|
|
)->method( |
421
|
|
|
'load' |
422
|
|
|
)->with( |
423
|
|
|
$this->equalTo('Luigi') |
424
|
|
|
)->will( |
425
|
|
|
$this->throwException(new \Exception()) |
426
|
|
|
); |
427
|
|
|
|
428
|
|
|
$mockedService->load('Luigi'); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Test for the load() method. |
433
|
|
|
* |
434
|
|
|
* @depends testConstructor |
435
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::remove |
436
|
|
|
*/ |
437
|
|
|
public function testLoad() |
438
|
|
|
{ |
439
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
440
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
441
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
442
|
|
|
|
443
|
|
|
$handlerMock->expects( |
444
|
|
|
$this->once() |
445
|
|
|
)->method( |
446
|
|
|
'load' |
447
|
|
|
)->with( |
448
|
|
|
$this->equalTo('Luigi') |
449
|
|
|
)->will( |
450
|
|
|
$this->returnValue( |
451
|
|
|
new SPIURLWildcard( |
452
|
|
|
array( |
453
|
|
|
'id' => 'Luigi', |
454
|
|
|
'sourceUrl' => 'this', |
455
|
|
|
'destinationUrl' => 'that', |
456
|
|
|
'forward' => true, |
457
|
|
|
) |
458
|
|
|
) |
459
|
|
|
) |
460
|
|
|
); |
461
|
|
|
|
462
|
|
|
$urlWildcard = $mockedService->load('Luigi'); |
463
|
|
|
|
464
|
|
|
$this->assertEquals( |
465
|
|
|
new URLWildcard( |
466
|
|
|
array( |
467
|
|
|
'id' => 'Luigi', |
468
|
|
|
'sourceUrl' => 'this', |
469
|
|
|
'destinationUrl' => 'that', |
470
|
|
|
'forward' => true, |
471
|
|
|
) |
472
|
|
|
), |
473
|
|
|
$urlWildcard |
474
|
|
|
); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Test for the loadAll() method. |
479
|
|
|
* |
480
|
|
|
* @depends testConstructor |
481
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::loadAll |
482
|
|
|
*/ |
483
|
|
View Code Duplication |
public function testLoadAll() |
484
|
|
|
{ |
485
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
486
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
487
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
488
|
|
|
|
489
|
|
|
$handlerMock->expects( |
490
|
|
|
$this->once() |
491
|
|
|
)->method( |
492
|
|
|
'loadAll' |
493
|
|
|
)->with( |
494
|
|
|
$this->equalTo(0), |
495
|
|
|
$this->equalTo(-1) |
496
|
|
|
)->will( |
497
|
|
|
$this->returnValue(array()) |
498
|
|
|
); |
499
|
|
|
|
500
|
|
|
$mockedService->loadAll(); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Test for the loadAll() method. |
505
|
|
|
* |
506
|
|
|
* @depends testConstructor |
507
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::loadAll |
508
|
|
|
*/ |
509
|
|
|
public function testLoadAllWithLimitAndOffset() |
510
|
|
|
{ |
511
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
512
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
513
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
514
|
|
|
|
515
|
|
|
$handlerMock->expects( |
516
|
|
|
$this->once() |
517
|
|
|
)->method( |
518
|
|
|
'loadAll' |
519
|
|
|
)->with( |
520
|
|
|
$this->equalTo(12), |
521
|
|
|
$this->equalTo(34) |
522
|
|
|
)->will( |
523
|
|
|
$this->returnValue( |
524
|
|
|
array( |
525
|
|
|
new SPIURLWildcard( |
526
|
|
|
array( |
527
|
|
|
'id' => 'Luigi', |
528
|
|
|
'sourceUrl' => 'this', |
529
|
|
|
'destinationUrl' => 'that', |
530
|
|
|
'forward' => true, |
531
|
|
|
) |
532
|
|
|
), |
533
|
|
|
) |
534
|
|
|
) |
535
|
|
|
); |
536
|
|
|
|
537
|
|
|
$urlWildcards = $mockedService->loadAll(12, 34); |
538
|
|
|
|
539
|
|
|
$this->assertEquals( |
540
|
|
|
array( |
541
|
|
|
new URLWildcard( |
542
|
|
|
array( |
543
|
|
|
'id' => 'Luigi', |
544
|
|
|
'sourceUrl' => 'this', |
545
|
|
|
'destinationUrl' => 'that', |
546
|
|
|
'forward' => true, |
547
|
|
|
) |
548
|
|
|
), |
549
|
|
|
), |
550
|
|
|
$urlWildcards |
551
|
|
|
); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* @return array |
556
|
|
|
*/ |
557
|
|
|
public function providerForTestTranslateThrowsNotFoundException() |
558
|
|
|
{ |
559
|
|
|
return array( |
560
|
|
|
array( |
561
|
|
|
array( |
562
|
|
|
'sourceUrl' => '/fruit', |
563
|
|
|
'destinationUrl' => '/food', |
564
|
|
|
'forward' => true, |
565
|
|
|
), |
566
|
|
|
'/vegetable', |
567
|
|
|
), |
568
|
|
|
array( |
569
|
|
|
array( |
570
|
|
|
'sourceUrl' => '/fruit/apricot', |
571
|
|
|
'destinationUrl' => '/food/apricot', |
572
|
|
|
'forward' => true, |
573
|
|
|
), |
574
|
|
|
'/fruit/lemon', |
575
|
|
|
), |
576
|
|
|
array( |
577
|
|
|
array( |
578
|
|
|
'sourceUrl' => '/fruit/*', |
579
|
|
|
'destinationUrl' => '/food/{1}', |
580
|
|
|
'forward' => true, |
581
|
|
|
), |
582
|
|
|
'/fruit', |
583
|
|
|
), |
584
|
|
|
array( |
585
|
|
|
array( |
586
|
|
|
'sourceUrl' => '/fruit/*/*', |
587
|
|
|
'destinationUrl' => '/food/{1}/{2}', |
588
|
|
|
'forward' => true, |
589
|
|
|
), |
590
|
|
|
'/fruit/citrus', |
591
|
|
|
), |
592
|
|
|
); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Test for the translate() method. |
597
|
|
|
* |
598
|
|
|
* @depends testConstructor |
599
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::translate |
600
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
601
|
|
|
* @dataProvider providerForTestTranslateThrowsNotFoundException |
602
|
|
|
*/ |
603
|
|
View Code Duplication |
public function testTranslateThrowsNotFoundException($createArray, $url) |
604
|
|
|
{ |
605
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
606
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
607
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
608
|
|
|
|
609
|
|
|
$handlerMock->expects( |
610
|
|
|
$this->once() |
611
|
|
|
)->method( |
612
|
|
|
'loadAll' |
613
|
|
|
)->with( |
614
|
|
|
$this->equalTo(0), |
615
|
|
|
$this->equalTo(-1) |
616
|
|
|
)->will( |
617
|
|
|
$this->returnValue(array(new SPIURLWildcard($createArray))) |
618
|
|
|
); |
619
|
|
|
|
620
|
|
|
$mockedService->translate($url); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* @return array |
625
|
|
|
*/ |
626
|
|
|
public function providerForTestTranslate() |
627
|
|
|
{ |
628
|
|
|
return array( |
629
|
|
|
array( |
630
|
|
|
array( |
631
|
|
|
'sourceUrl' => '/fruit/apricot', |
632
|
|
|
'destinationUrl' => '/food/apricot', |
633
|
|
|
'forward' => true, |
634
|
|
|
), |
635
|
|
|
'/fruit/apricot', |
636
|
|
|
'/food/apricot', |
637
|
|
|
), |
638
|
|
|
array( |
639
|
|
|
array( |
640
|
|
|
'sourceUrl' => '/fruit/*', |
641
|
|
|
'destinationUrl' => '/food/{1}', |
642
|
|
|
'forward' => true, |
643
|
|
|
), |
644
|
|
|
'/fruit/citrus', |
645
|
|
|
'/food/citrus', |
646
|
|
|
), |
647
|
|
|
array( |
648
|
|
|
array( |
649
|
|
|
'sourceUrl' => '/fruit/*', |
650
|
|
|
'destinationUrl' => '/food/{1}', |
651
|
|
|
'forward' => true, |
652
|
|
|
), |
653
|
|
|
'/fruit/citrus/orange', |
654
|
|
|
'/food/citrus/orange', |
655
|
|
|
), |
656
|
|
|
array( |
657
|
|
|
array( |
658
|
|
|
'sourceUrl' => '/fruit/*/*', |
659
|
|
|
'destinationUrl' => '/food/{2}', |
660
|
|
|
'forward' => true, |
661
|
|
|
), |
662
|
|
|
'/fruit/citrus/orange', |
663
|
|
|
'/food/orange', |
664
|
|
|
), |
665
|
|
|
array( |
666
|
|
|
array( |
667
|
|
|
'sourceUrl' => '/fruit/*/*', |
668
|
|
|
'destinationUrl' => '/food/{1}/{2}', |
669
|
|
|
'forward' => true, |
670
|
|
|
), |
671
|
|
|
'/fruit/citrus/orange', |
672
|
|
|
'/food/citrus/orange', |
673
|
|
|
), |
674
|
|
|
array( |
675
|
|
|
array( |
676
|
|
|
'sourceUrl' => '/fruit/*/pamplemousse', |
677
|
|
|
'destinationUrl' => '/food/weird', |
678
|
|
|
'forward' => true, |
679
|
|
|
), |
680
|
|
|
'/fruit/citrus/pamplemousse', |
681
|
|
|
'/food/weird', |
682
|
|
|
), |
683
|
|
|
array( |
684
|
|
|
array( |
685
|
|
|
'sourceUrl' => '/fruit/*/pamplemousse', |
686
|
|
|
'destinationUrl' => '/food/weird/{1}', |
687
|
|
|
'forward' => true, |
688
|
|
|
), |
689
|
|
|
'/fruit/citrus/pamplemousse', |
690
|
|
|
'/food/weird/citrus', |
691
|
|
|
), |
692
|
|
|
array( |
693
|
|
|
array( |
694
|
|
|
'sourceUrl' => '/fruit/*/pamplemousse', |
695
|
|
|
'destinationUrl' => '/food/weird/{1}', |
696
|
|
|
'forward' => true, |
697
|
|
|
), |
698
|
|
|
'/fruit/citrus/yellow/pamplemousse', |
699
|
|
|
'/food/weird/citrus/yellow', |
700
|
|
|
), |
701
|
|
|
); |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* Test for the translate() method. |
706
|
|
|
* |
707
|
|
|
* @depends testConstructor |
708
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::translate |
709
|
|
|
* @dataProvider providerForTestTranslate |
710
|
|
|
*/ |
711
|
|
|
public function testTranslate($createArray, $url, $uri) |
712
|
|
|
{ |
713
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
714
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
715
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
716
|
|
|
|
717
|
|
|
$handlerMock->expects( |
718
|
|
|
$this->once() |
719
|
|
|
)->method( |
720
|
|
|
'loadAll' |
721
|
|
|
)->with( |
722
|
|
|
$this->equalTo(0), |
723
|
|
|
$this->equalTo(-1) |
724
|
|
|
)->will( |
725
|
|
|
$this->returnValue(array(new SPIURLWildcard($createArray))) |
726
|
|
|
); |
727
|
|
|
|
728
|
|
|
$translationResult = $mockedService->translate($url); |
729
|
|
|
|
730
|
|
|
$this->assertEquals( |
731
|
|
|
new URLWildcardTranslationResult( |
732
|
|
|
array( |
733
|
|
|
'uri' => $uri, |
734
|
|
|
'forward' => $createArray['forward'], |
735
|
|
|
) |
736
|
|
|
), |
737
|
|
|
$translationResult |
738
|
|
|
); |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
/** |
742
|
|
|
* Test for the translate() method. |
743
|
|
|
* |
744
|
|
|
* @depends testConstructor |
745
|
|
|
* @covers \eZ\Publish\Core\Repository\URLWildcardService::translate |
746
|
|
|
*/ |
747
|
|
|
public function testTranslateUsesLongestMatchingWildcard() |
748
|
|
|
{ |
749
|
|
|
$mockedService = $this->getPartlyMockedURLWildcardService(); |
750
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject $handlerMock */ |
751
|
|
|
$handlerMock = $this->getPersistenceMock()->urlWildcardHandler(); |
752
|
|
|
|
753
|
|
|
$handlerMock->expects( |
754
|
|
|
$this->once() |
755
|
|
|
)->method( |
756
|
|
|
'loadAll' |
757
|
|
|
)->with( |
758
|
|
|
$this->equalTo(0), |
759
|
|
|
$this->equalTo(-1) |
760
|
|
|
)->will( |
761
|
|
|
$this->returnValue( |
762
|
|
|
array( |
763
|
|
|
new SPIURLWildcard( |
764
|
|
|
array( |
765
|
|
|
'sourceUrl' => '/something/*', |
766
|
|
|
'destinationUrl' => '/short', |
767
|
|
|
'forward' => true, |
768
|
|
|
) |
769
|
|
|
), |
770
|
|
|
new SPIURLWildcard( |
771
|
|
|
array( |
772
|
|
|
'sourceUrl' => '/something/something/*', |
773
|
|
|
'destinationUrl' => '/long', |
774
|
|
|
'forward' => false, |
775
|
|
|
) |
776
|
|
|
), |
777
|
|
|
) |
778
|
|
|
) |
779
|
|
|
); |
780
|
|
|
|
781
|
|
|
$translationResult = $mockedService->translate('/something/something/thing'); |
782
|
|
|
|
783
|
|
|
$this->assertEquals( |
784
|
|
|
new URLWildcardTranslationResult( |
785
|
|
|
array( |
786
|
|
|
'uri' => '/long', |
787
|
|
|
'forward' => false, |
788
|
|
|
) |
789
|
|
|
), |
790
|
|
|
$translationResult |
791
|
|
|
); |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* Returns the content service to test with $methods mocked. |
796
|
|
|
* |
797
|
|
|
* Injected Repository comes from {@see getRepositoryMock()} and persistence handler from {@see getPersistenceMock()} |
798
|
|
|
* |
799
|
|
|
* @param string[] $methods |
800
|
|
|
* |
801
|
|
|
* @return \eZ\Publish\Core\Repository\URLWildcardService|\PHPUnit\Framework\MockObject\MockObject |
802
|
|
|
*/ |
803
|
|
|
protected function getPartlyMockedURLWildcardService(array $methods = null) |
804
|
|
|
{ |
805
|
|
|
return $this->getMockBuilder(URLWildcardService::class) |
806
|
|
|
->setMethods($methods) |
807
|
|
|
->setConstructorArgs( |
808
|
|
|
array( |
809
|
|
|
$this->getRepositoryMock(), |
810
|
|
|
$this->getPersistenceMock()->urlWildcardHandler(), |
811
|
|
|
) |
812
|
|
|
) |
813
|
|
|
->getMock(); |
814
|
|
|
} |
815
|
|
|
} |
816
|
|
|
|