Completed
Push — feature-EZP-25696 ( 52d929...5f47d3 )
by André
23:51
created

testDoGenerateWithSiteAccessParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 66
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 44
nc 1
nop 3
dl 0
loc 66
rs 9.3191
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $repository
91
            ->expects($this->any())
92
            ->method('getPermissionResolver')
93
            ->will($this->returnValue($this->getPermissionResolverMock()));
94
95
        $urlAliasCharmap = array(
96
            '"' => '%22',
97
            "'" => '%27',
98
            '<' => '%3C',
99
            '>' => '%3E',
100
        );
101
        $this->urlAliasGenerator = new UrlAliasGenerator(
102
            $this->repository,
103
            $this->router,
104
            $this->configResolver,
105
            $urlAliasCharmap
106
        );
107
        $this->urlAliasGenerator->setLogger($this->logger);
108
        $this->urlAliasGenerator->setSiteAccessRouter($this->siteAccessRouter);
109
    }
110
111
    public function testGetPathPrefixByRootLocationId()
112
    {
113
        $rootLocationId = 123;
114
        $rootLocation = new Location(array('id' => $rootLocationId));
115
        $pathPrefix = '/foo/bar';
116
        $rootUrlAlias = new URLAlias(array('path' => $pathPrefix));
117
        $this->locationService
118
            ->expects($this->once())
119
            ->method('loadLocation')
120
            ->with($rootLocationId)
121
            ->will($this->returnValue($rootLocation));
122
        $this->urlAliasService
123
            ->expects($this->once())
124
            ->method('reverseLookup')
125
            ->with($rootLocation)
126
            ->will($this->returnValue($rootUrlAlias));
127
128
        $this->assertSame($pathPrefix, $this->urlAliasGenerator->getPathPrefixByRootLocationId($rootLocationId));
129
    }
130
131
    /**
132
     * @dataProvider providerTestIsPrefixExcluded
133
     */
134
    public function testIsPrefixExcluded($uri, $expectedIsExcluded)
135
    {
136
        $this->urlAliasGenerator->setExcludedUriPrefixes(
137
            array(
138
                '/products',
139
                '/shared/content',
140
                '/something/in-the-way/',
141
            )
142
        );
143
        $this->assertSame($expectedIsExcluded, $this->urlAliasGenerator->isUriPrefixExcluded($uri));
144
    }
145
146
    public function providerTestIsPrefixExcluded()
147
    {
148
        return array(
149
            array('/foo/bar', false),
150
            array('/products/bar', true),
151
            array('/ProDUctS/eZ-Publish', true),
152
            array('/ProductsFoo/eZ-Publish', true),
153
            array('/shared/foo', false),
154
            array('/SHARED/contenT/bar', true),
155
            array('/SomeThing/bidule/chose', false),
156
            array('/SomeThing/in-the-way/truc/', true),
157
            array('/CMS/eZ-Publish', false),
158
            array('/Lyon/Best/city', false),
159
        );
160
    }
161
162
    public function testLoadLocation()
163
    {
164
        $locationId = 123;
165
        $location = new Location(array('id' => $locationId));
166
        $this->locationService
167
            ->expects($this->once())
168
            ->method('loadLocation')
169
            ->with($locationId)
170
            ->will($this->returnValue($location));
171
        $this->urlAliasGenerator->loadLocation($locationId);
172
    }
173
174
    /**
175
     * @dataProvider providerTestDoGenerate
176
     */
177
    public function testDoGenerate(URLAlias $urlAlias, array $parameters, $expected)
178
    {
179
        $location = new Location(array('id' => 123));
180
        $this->urlAliasService
181
            ->expects($this->once())
182
            ->method('listLocationAliases')
183
            ->with($location, false)
184
            ->will($this->returnValue(array($urlAlias)));
185
186
        $this->urlAliasGenerator->setSiteAccess(new SiteAccess('test', 'fake', $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\URILexer')));
187
188
        $this->assertSame($expected, $this->urlAliasGenerator->doGenerate($location, $parameters));
189
    }
190
191
    public function providerTestDoGenerate()
192
    {
193
        return array(
194
            array(
195
                new URLAlias(array('path' => '/foo/bar')),
196
                array(),
197
                '/foo/bar',
198
            ),
199
            array(
200
                new URLAlias(array('path' => '/foo/bar')),
201
                array('some' => 'thing'),
202
                '/foo/bar?some=thing',
203
            ),
204
            array(
205
                new URLAlias(array('path' => '/foo/bar')),
206
                array('some' => 'thing', 'truc' => 'muche'),
207
                '/foo/bar?some=thing&truc=muche',
208
            ),
209
        );
210
    }
211
212
    /**
213
     * @dataProvider providerTestDoGenerateWithSiteaccess
214
     */
215
    public function testDoGenerateWithSiteAccessParam(URLAlias $urlAlias, array $parameters, $expected)
216
    {
217
        $siteaccessName = 'foo';
218
        $parameters += array('siteaccess' => $siteaccessName);
219
        $languages = array('esl-ES', 'fre-FR', 'eng-GB');
220
221
        $saRootLocations = array(
222
            'foo' => 2,
223
            'bar' => 100,
224
        );
225
        $treeRootUrlAlias = array(
226
            2 => new URLAlias(array('path' => '/')),
227
            100 => new URLAlias(array('path' => '/foo/bar')),
228
        );
229
230
        $this->configResolver
231
            ->expects($this->any())
232
            ->method('getParameter')
233
            ->will(
234
                $this->returnValueMap(
235
                    array(
236
                        array('languages', null, 'foo', $languages),
237
                        array('languages', null, 'bar', $languages),
238
                        array('content.tree_root.location_id', null, 'foo', $saRootLocations['foo']),
239
                        array('content.tree_root.location_id', null, 'bar', $saRootLocations['bar']),
240
                    )
241
                )
242
            );
243
244
        $location = new Location(array('id' => 123));
245
        $this->urlAliasService
246
            ->expects($this->exactly(1))
247
            ->method('listLocationAliases')
248
            ->will(
249
                $this->returnValueMap(
250
                    array(
251
                        array($location, false, null, null, $languages, array($urlAlias)),
252
                    )
253
                )
254
            );
255
256
        $this->locationService
257
            ->expects($this->once())
258
            ->method('loadLocation')
259
            ->will(
260
                $this->returnCallback(
261
                    function ($locationId) {
262
                        return new Location(array('id' => $locationId));
263
                    }
264
                )
265
            );
266
        $this->urlAliasService
267
            ->expects($this->exactly(1))
268
            ->method('reverseLookup')
269
            ->will(
270
                $this->returnCallback(
271
                    function ($location) use ($treeRootUrlAlias) {
272
                        return $treeRootUrlAlias[$location->id];
273
                    }
274
                )
275
            );
276
277
        $this->urlAliasGenerator->setSiteAccess(new SiteAccess('test', 'fake', $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\URILexer')));
278
279
        $this->assertSame($expected, $this->urlAliasGenerator->doGenerate($location, $parameters));
280
    }
281
282
    public function providerTestDoGenerateWithSiteaccess()
283
    {
284
        return array(
285
            array(
286
                new URLAlias(array('path' => '/foo/bar')),
287
                array(),
288
                '/foo/bar',
289
            ),
290
            array(
291
                new URLAlias(array('path' => '/foo/bar/baz')),
292
                array('siteaccess' => 'bar'),
293
                '/baz',
294
            ),
295
            array(
296
                new UrlAlias(array('path' => '/special-chars-"<>\'')),
297
                array(),
298
                '/special-chars-%22%3C%3E%27',
299
            ),
300
        );
301
    }
302
303
    public function testDoGenerateNoUrlAlias()
304
    {
305
        $location = new Location(array('id' => 123, 'contentInfo' => new ContentInfo(array('id' => 456))));
306
        $uri = "/content/location/$location->id";
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
307
        $this->urlAliasService
308
            ->expects($this->once())
309
            ->method('listLocationAliases')
310
            ->with($location, false)
311
            ->will($this->returnValue(array()));
312
        $this->router
313
            ->expects($this->once())
314
            ->method('generate')
315
            ->with(
316
                UrlAliasGenerator::INTERNAL_CONTENT_VIEW_ROUTE,
317
                array('contentId' => $location->contentId, 'locationId' => $location->id)
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
318
            )
319
            ->will($this->returnValue($uri));
320
321
        $this->assertSame($uri, $this->urlAliasGenerator->doGenerate($location, array()));
322
    }
323
324
    /**
325
     * @dataProvider providerTestDoGenerateRootLocation
326
     */
327
    public function testDoGenerateRootLocation(URLAlias $urlAlias, $isOutsideAndNotExcluded, $expected, $pathPrefix)
328
    {
329
        $excludedPrefixes = array('/products', '/shared');
330
        $rootLocationId = 456;
331
        $this->urlAliasGenerator->setRootLocationId($rootLocationId);
332
        $this->urlAliasGenerator->setExcludedUriPrefixes($excludedPrefixes);
333
        $location = new Location(array('id' => 123));
334
335
        $rootLocation = new Location(array('id' => $rootLocationId));
336
        $rootUrlAlias = new URLAlias(array('path' => $pathPrefix));
337
        $this->locationService
338
            ->expects($this->once())
339
            ->method('loadLocation')
340
            ->with($rootLocationId)
341
            ->will($this->returnValue($rootLocation));
342
        $this->urlAliasService
343
            ->expects($this->once())
344
            ->method('reverseLookup')
345
            ->with($rootLocation)
346
            ->will($this->returnValue($rootUrlAlias));
347
348
        $this->urlAliasService
349
            ->expects($this->once())
350
            ->method('listLocationAliases')
351
            ->with($location, false)
352
            ->will($this->returnValue(array($urlAlias)));
353
354
        if ($isOutsideAndNotExcluded) {
355
            $this->logger
356
                ->expects($this->once())
357
                ->method('warning');
358
        }
359
360
        $this->assertSame($expected, $this->urlAliasGenerator->doGenerate($location, array()));
361
    }
362
363
    public function providerTestDoGenerateRootLocation()
364
    {
365
        return array(
366
            array(
367
                new UrlAlias(array('path' => '/my/root-folder/foo/bar')),
368
                false,
369
                '/foo/bar',
370
                '/my/root-folder',
371
            ),
372
            array(
373
                new UrlAlias(array('path' => '/my/root-folder/something')),
374
                false,
375
                '/something',
376
                '/my/root-folder',
377
            ),
378
            array(
379
                new UrlAlias(array('path' => '/my/root-folder')),
380
                false,
381
                '/',
382
                '/my/root-folder',
383
            ),
384
            array(
385
                new UrlAlias(array('path' => '/foo/bar')),
386
                false,
387
                '/foo/bar',
388
                '/',
389
            ),
390
            array(
391
                new UrlAlias(array('path' => '/something')),
392
                false,
393
                '/something',
394
                '/',
395
            ),
396
            array(
397
                new UrlAlias(array('path' => '/')),
398
                false,
399
                '/',
400
                '/',
401
            ),
402
            array(
403
                new UrlAlias(array('path' => '/outside/tree/foo/bar')),
404
                true,
405
                '/outside/tree/foo/bar',
406
                '/my/root-folder',
407
            ),
408
            array(
409
                new UrlAlias(array('path' => '/products/ez-publish')),
410
                false,
411
                '/products/ez-publish',
412
                '/my/root-folder',
413
            ),
414
            array(
415
                new UrlAlias(array('path' => '/shared/some-content')),
416
                false,
417
                '/shared/some-content',
418
                '/my/root-folder',
419
            ),
420
        );
421
    }
422
423 View Code Duplication
    protected function getPermissionResolverMock()
424
    {
425
        return $this
426
            ->getMockBuilder('\eZ\Publish\Core\Repository\Permission\PermissionResolver')
427
            ->setMethods(null)
428
            ->setConstructorArgs(
429
                [
430
                    $this
431
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\RoleDomainMapper')
432
                        ->disableOriginalConstructor()
433
                        ->getMock(),
434
                    $this
435
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\LimitationService')
436
                        ->getMock(),
437
                    $this
438
                        ->getMockBuilder('eZ\Publish\SPI\Persistence\User\Handler')
439
                        ->getMock(),
440
                    $this
441
                        ->getMockBuilder('eZ\Publish\API\Repository\Values\User\UserReference')
442
                        ->getMock(),
443
                ]
444
            )
445
            ->getMock();
446
    }
447
}
448