1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the UrlAliasGeneratorTest 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
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\Routing\Tests; |
12
|
|
|
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\URLAlias; |
15
|
|
|
use eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator; |
16
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
17
|
|
|
use eZ\Publish\Core\Repository\Values\Content\Location; |
18
|
|
|
use PHPUnit_Framework_TestCase; |
19
|
|
|
|
20
|
|
|
class UrlAliasGeneratorTest extends PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
24
|
|
|
*/ |
25
|
|
|
private $repository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
29
|
|
|
*/ |
30
|
|
|
private $urlAliasService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
34
|
|
|
*/ |
35
|
|
|
private $locationService; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
39
|
|
|
*/ |
40
|
|
|
private $router; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
44
|
|
|
*/ |
45
|
|
|
private $logger; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var UrlAliasGenerator |
49
|
|
|
*/ |
50
|
|
|
private $urlAliasGenerator; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
54
|
|
|
*/ |
55
|
|
|
private $siteAccessRouter; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
59
|
|
|
*/ |
60
|
|
|
private $configResolver; |
61
|
|
|
|
62
|
|
|
protected function setUp() |
63
|
|
|
{ |
64
|
|
|
parent::setUp(); |
65
|
|
|
$this->router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface'); |
66
|
|
|
$this->logger = $this->getMock('Psr\\Log\\LoggerInterface'); |
67
|
|
|
$this->siteAccessRouter = $this->getMock('eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessRouterInterface'); |
68
|
|
|
$this->configResolver = $this->getMock('eZ\Publish\Core\MVC\ConfigResolverInterface'); |
69
|
|
|
$repositoryClass = 'eZ\\Publish\\Core\\Repository\\Repository'; |
70
|
|
|
$this->repository = $repository = $this |
|
|
|
|
71
|
|
|
->getMockBuilder($repositoryClass) |
72
|
|
|
->disableOriginalConstructor() |
73
|
|
|
->setMethods( |
74
|
|
|
array_diff( |
75
|
|
|
get_class_methods($repositoryClass), |
76
|
|
|
array('sudo') |
77
|
|
|
) |
78
|
|
|
) |
79
|
|
|
->getMock(); |
80
|
|
|
$this->urlAliasService = $this->getMock('eZ\\Publish\\API\\Repository\\URLAliasService'); |
81
|
|
|
$this->locationService = $this->getMock('eZ\\Publish\\API\\Repository\\LocationService'); |
82
|
|
|
$this->repository |
83
|
|
|
->expects($this->any()) |
84
|
|
|
->method('getURLAliasService') |
85
|
|
|
->will($this->returnValue($this->urlAliasService)); |
86
|
|
|
$this->repository |
87
|
|
|
->expects($this->any()) |
88
|
|
|
->method('getLocationService') |
89
|
|
|
->will($this->returnValue($this->locationService)); |
90
|
|
|
|
91
|
|
|
$urlAliasCharmap = array( |
92
|
|
|
'"' => '%22', |
93
|
|
|
"'" => '%27', |
94
|
|
|
'<' => '%3C', |
95
|
|
|
'>' => '%3E', |
96
|
|
|
); |
97
|
|
|
$this->urlAliasGenerator = new UrlAliasGenerator( |
98
|
|
|
$this->repository, |
99
|
|
|
$this->router, |
100
|
|
|
$this->configResolver, |
101
|
|
|
$urlAliasCharmap |
102
|
|
|
); |
103
|
|
|
$this->urlAliasGenerator->setLogger($this->logger); |
104
|
|
|
$this->urlAliasGenerator->setSiteAccessRouter($this->siteAccessRouter); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetPathPrefixByRootLocationId() |
108
|
|
|
{ |
109
|
|
|
$rootLocationId = 123; |
110
|
|
|
$rootLocation = new Location(array('id' => $rootLocationId)); |
111
|
|
|
$pathPrefix = '/foo/bar'; |
112
|
|
|
$rootUrlAlias = new URLAlias(array('path' => $pathPrefix)); |
113
|
|
|
$this->locationService |
114
|
|
|
->expects($this->once()) |
115
|
|
|
->method('loadLocation') |
116
|
|
|
->with($rootLocationId) |
117
|
|
|
->will($this->returnValue($rootLocation)); |
118
|
|
|
$this->urlAliasService |
119
|
|
|
->expects($this->once()) |
120
|
|
|
->method('reverseLookup') |
121
|
|
|
->with($rootLocation) |
122
|
|
|
->will($this->returnValue($rootUrlAlias)); |
123
|
|
|
|
124
|
|
|
$this->assertSame($pathPrefix, $this->urlAliasGenerator->getPathPrefixByRootLocationId($rootLocationId)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @dataProvider providerTestIsPrefixExcluded |
129
|
|
|
*/ |
130
|
|
|
public function testIsPrefixExcluded($uri, $expectedIsExcluded) |
131
|
|
|
{ |
132
|
|
|
$this->urlAliasGenerator->setExcludedUriPrefixes( |
133
|
|
|
array( |
134
|
|
|
'/products', |
135
|
|
|
'/shared/content', |
136
|
|
|
'/something/in-the-way/', |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
$this->assertSame($expectedIsExcluded, $this->urlAliasGenerator->isUriPrefixExcluded($uri)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function providerTestIsPrefixExcluded() |
143
|
|
|
{ |
144
|
|
|
return array( |
145
|
|
|
array('/foo/bar', false), |
146
|
|
|
array('/products/bar', true), |
147
|
|
|
array('/ProDUctS/eZ-Publish', true), |
148
|
|
|
array('/ProductsFoo/eZ-Publish', true), |
149
|
|
|
array('/shared/foo', false), |
150
|
|
|
array('/SHARED/contenT/bar', true), |
151
|
|
|
array('/SomeThing/bidule/chose', false), |
152
|
|
|
array('/SomeThing/in-the-way/truc/', true), |
153
|
|
|
array('/CMS/eZ-Publish', false), |
154
|
|
|
array('/Lyon/Best/city', false), |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testLoadLocation() |
159
|
|
|
{ |
160
|
|
|
$locationId = 123; |
161
|
|
|
$location = new Location(array('id' => $locationId)); |
162
|
|
|
$this->locationService |
163
|
|
|
->expects($this->once()) |
164
|
|
|
->method('loadLocation') |
165
|
|
|
->with($locationId) |
166
|
|
|
->will($this->returnValue($location)); |
167
|
|
|
$this->urlAliasGenerator->loadLocation($locationId); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @dataProvider providerTestDoGenerate |
172
|
|
|
*/ |
173
|
|
|
public function testDoGenerate(URLAlias $urlAlias, array $parameters, $expected) |
174
|
|
|
{ |
175
|
|
|
$location = new Location(array('id' => 123)); |
176
|
|
|
$this->urlAliasService |
177
|
|
|
->expects($this->once()) |
178
|
|
|
->method('listLocationAliases') |
179
|
|
|
->with($location, false) |
180
|
|
|
->will($this->returnValue(array($urlAlias))); |
181
|
|
|
|
182
|
|
|
$this->urlAliasGenerator->setSiteAccess(new SiteAccess('test', 'fake', $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\URILexer'))); |
183
|
|
|
|
184
|
|
|
$this->assertSame($expected, $this->urlAliasGenerator->doGenerate($location, $parameters)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function providerTestDoGenerate() |
188
|
|
|
{ |
189
|
|
|
return array( |
190
|
|
|
array( |
191
|
|
|
new URLAlias(array('path' => '/foo/bar')), |
192
|
|
|
array(), |
193
|
|
|
'/foo/bar', |
194
|
|
|
), |
195
|
|
|
array( |
196
|
|
|
new URLAlias(array('path' => '/foo/bar')), |
197
|
|
|
array('some' => 'thing'), |
198
|
|
|
'/foo/bar?some=thing', |
199
|
|
|
), |
200
|
|
|
array( |
201
|
|
|
new URLAlias(array('path' => '/foo/bar')), |
202
|
|
|
array('some' => 'thing', 'truc' => 'muche'), |
203
|
|
|
'/foo/bar?some=thing&truc=muche', |
204
|
|
|
), |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @dataProvider providerTestDoGenerateWithSiteaccess |
210
|
|
|
*/ |
211
|
|
|
public function testDoGenerateWithSiteAccessParam(URLAlias $urlAlias, array $parameters, $expected) |
212
|
|
|
{ |
213
|
|
|
$siteaccessName = 'foo'; |
214
|
|
|
$parameters += array('siteaccess' => $siteaccessName); |
215
|
|
|
$languages = array('esl-ES', 'fre-FR', 'eng-GB'); |
216
|
|
|
|
217
|
|
|
$saRootLocations = array( |
218
|
|
|
'foo' => 2, |
219
|
|
|
'bar' => 100, |
220
|
|
|
); |
221
|
|
|
$treeRootUrlAlias = array( |
222
|
|
|
2 => new URLAlias(array('path' => '/')), |
223
|
|
|
100 => new URLAlias(array('path' => '/foo/bar')), |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
$this->configResolver |
227
|
|
|
->expects($this->any()) |
228
|
|
|
->method('getParameter') |
229
|
|
|
->will( |
230
|
|
|
$this->returnValueMap( |
231
|
|
|
array( |
232
|
|
|
array('languages', null, 'foo', $languages), |
233
|
|
|
array('languages', null, 'bar', $languages), |
234
|
|
|
array('content.tree_root.location_id', null, 'foo', $saRootLocations['foo']), |
235
|
|
|
array('content.tree_root.location_id', null, 'bar', $saRootLocations['bar']), |
236
|
|
|
) |
237
|
|
|
) |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$location = new Location(array('id' => 123)); |
241
|
|
|
$this->urlAliasService |
242
|
|
|
->expects($this->exactly(1)) |
243
|
|
|
->method('listLocationAliases') |
244
|
|
|
->will( |
245
|
|
|
$this->returnValueMap( |
246
|
|
|
array( |
247
|
|
|
array($location, false, null, null, $languages, array($urlAlias)), |
248
|
|
|
) |
249
|
|
|
) |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
$this->locationService |
253
|
|
|
->expects($this->once()) |
254
|
|
|
->method('loadLocation') |
255
|
|
|
->will( |
256
|
|
|
$this->returnCallback( |
257
|
|
|
function ($locationId) { |
258
|
|
|
return new Location(array('id' => $locationId)); |
259
|
|
|
} |
260
|
|
|
) |
261
|
|
|
); |
262
|
|
|
$this->urlAliasService |
263
|
|
|
->expects($this->exactly(1)) |
264
|
|
|
->method('reverseLookup') |
265
|
|
|
->will( |
266
|
|
|
$this->returnCallback( |
267
|
|
|
function ($location) use ($treeRootUrlAlias) { |
268
|
|
|
return $treeRootUrlAlias[$location->id]; |
269
|
|
|
} |
270
|
|
|
) |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
$this->urlAliasGenerator->setSiteAccess(new SiteAccess('test', 'fake', $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\URILexer'))); |
274
|
|
|
|
275
|
|
|
$this->assertSame($expected, $this->urlAliasGenerator->doGenerate($location, $parameters)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function providerTestDoGenerateWithSiteaccess() |
279
|
|
|
{ |
280
|
|
|
return array( |
281
|
|
|
array( |
282
|
|
|
new URLAlias(array('path' => '/foo/bar')), |
283
|
|
|
array(), |
284
|
|
|
'/foo/bar', |
285
|
|
|
), |
286
|
|
|
array( |
287
|
|
|
new URLAlias(array('path' => '/foo/bar/baz')), |
288
|
|
|
array('siteaccess' => 'bar'), |
289
|
|
|
'/baz', |
290
|
|
|
), |
291
|
|
|
array( |
292
|
|
|
new UrlAlias(array('path' => '/special-chars-"<>\'')), |
293
|
|
|
array(), |
294
|
|
|
'/special-chars-%22%3C%3E%27', |
295
|
|
|
), |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function testDoGenerateNoUrlAlias() |
300
|
|
|
{ |
301
|
|
|
$location = new Location(array('id' => 123, 'contentInfo' => new ContentInfo(array('id' => 456)))); |
302
|
|
|
$uri = "/content/location/$location->id"; |
|
|
|
|
303
|
|
|
$this->urlAliasService |
304
|
|
|
->expects($this->once()) |
305
|
|
|
->method('listLocationAliases') |
306
|
|
|
->with($location, false) |
307
|
|
|
->will($this->returnValue(array())); |
308
|
|
|
$this->router |
309
|
|
|
->expects($this->once()) |
310
|
|
|
->method('generate') |
311
|
|
|
->with( |
312
|
|
|
UrlAliasGenerator::INTERNAL_CONTENT_VIEW_ROUTE, |
313
|
|
|
array('contentId' => $location->contentId, 'locationId' => $location->id) |
|
|
|
|
314
|
|
|
) |
315
|
|
|
->will($this->returnValue($uri)); |
316
|
|
|
|
317
|
|
|
$this->assertSame($uri, $this->urlAliasGenerator->doGenerate($location, array())); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @dataProvider providerTestDoGenerateRootLocation |
322
|
|
|
*/ |
323
|
|
|
public function testDoGenerateRootLocation(URLAlias $urlAlias, $isOutsideAndNotExcluded, $expected, $pathPrefix) |
324
|
|
|
{ |
325
|
|
|
$excludedPrefixes = array('/products', '/shared'); |
326
|
|
|
$rootLocationId = 456; |
327
|
|
|
$this->urlAliasGenerator->setRootLocationId($rootLocationId); |
328
|
|
|
$this->urlAliasGenerator->setExcludedUriPrefixes($excludedPrefixes); |
329
|
|
|
$location = new Location(array('id' => 123)); |
330
|
|
|
|
331
|
|
|
$rootLocation = new Location(array('id' => $rootLocationId)); |
332
|
|
|
$rootUrlAlias = new URLAlias(array('path' => $pathPrefix)); |
333
|
|
|
$this->locationService |
334
|
|
|
->expects($this->once()) |
335
|
|
|
->method('loadLocation') |
336
|
|
|
->with($rootLocationId) |
337
|
|
|
->will($this->returnValue($rootLocation)); |
338
|
|
|
$this->urlAliasService |
339
|
|
|
->expects($this->once()) |
340
|
|
|
->method('reverseLookup') |
341
|
|
|
->with($rootLocation) |
342
|
|
|
->will($this->returnValue($rootUrlAlias)); |
343
|
|
|
|
344
|
|
|
$this->urlAliasService |
345
|
|
|
->expects($this->once()) |
346
|
|
|
->method('listLocationAliases') |
347
|
|
|
->with($location, false) |
348
|
|
|
->will($this->returnValue(array($urlAlias))); |
349
|
|
|
|
350
|
|
|
if ($isOutsideAndNotExcluded) { |
351
|
|
|
$this->logger |
352
|
|
|
->expects($this->once()) |
353
|
|
|
->method('warning'); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
$this->assertSame($expected, $this->urlAliasGenerator->doGenerate($location, array())); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function providerTestDoGenerateRootLocation() |
360
|
|
|
{ |
361
|
|
|
return array( |
362
|
|
|
array( |
363
|
|
|
new UrlAlias(array('path' => '/my/root-folder/foo/bar')), |
364
|
|
|
false, |
365
|
|
|
'/foo/bar', |
366
|
|
|
'/my/root-folder', |
367
|
|
|
), |
368
|
|
|
array( |
369
|
|
|
new UrlAlias(array('path' => '/my/root-folder/something')), |
370
|
|
|
false, |
371
|
|
|
'/something', |
372
|
|
|
'/my/root-folder', |
373
|
|
|
), |
374
|
|
|
array( |
375
|
|
|
new UrlAlias(array('path' => '/my/root-folder')), |
376
|
|
|
false, |
377
|
|
|
'/', |
378
|
|
|
'/my/root-folder', |
379
|
|
|
), |
380
|
|
|
array( |
381
|
|
|
new UrlAlias(array('path' => '/foo/bar')), |
382
|
|
|
false, |
383
|
|
|
'/foo/bar', |
384
|
|
|
'/', |
385
|
|
|
), |
386
|
|
|
array( |
387
|
|
|
new UrlAlias(array('path' => '/something')), |
388
|
|
|
false, |
389
|
|
|
'/something', |
390
|
|
|
'/', |
391
|
|
|
), |
392
|
|
|
array( |
393
|
|
|
new UrlAlias(array('path' => '/')), |
394
|
|
|
false, |
395
|
|
|
'/', |
396
|
|
|
'/', |
397
|
|
|
), |
398
|
|
|
array( |
399
|
|
|
new UrlAlias(array('path' => '/outside/tree/foo/bar')), |
400
|
|
|
true, |
401
|
|
|
'/outside/tree/foo/bar', |
402
|
|
|
'/my/root-folder', |
403
|
|
|
), |
404
|
|
|
array( |
405
|
|
|
new UrlAlias(array('path' => '/products/ez-publish')), |
406
|
|
|
false, |
407
|
|
|
'/products/ez-publish', |
408
|
|
|
'/my/root-folder', |
409
|
|
|
), |
410
|
|
|
array( |
411
|
|
|
new UrlAlias(array('path' => '/shared/some-content')), |
412
|
|
|
false, |
413
|
|
|
'/shared/some-content', |
414
|
|
|
'/my/root-folder', |
415
|
|
|
), |
416
|
|
|
); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.