Backup of db before drupal security update
[yaffs-website] / vendor / symfony-cmf / routing / Tests / Routing / ContentAwareGeneratorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Cmf\Component\Routing\Tests\Routing;
13
14 use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
15 use Symfony\Cmf\Component\Routing\ContentAwareGenerator;
16 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
17
18 class ContentAwareGeneratorTest extends CmfUnitTestCase
19 {
20     protected $contentDocument;
21     protected $routeDocument;
22     protected $routeCompiled;
23     protected $provider;
24
25     /**
26      * @var ContentAwareGenerator
27      */
28     protected $generator;
29     protected $context;
30
31     public function setUp()
32     {
33         $this->contentDocument = $this->buildMock('Symfony\Cmf\Component\Routing\RouteReferrersReadInterface');
34         $this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
35         $this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
36         $this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
37         $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
38
39         $this->generator = new TestableContentAwareGenerator($this->provider);
40     }
41
42     public function testGenerateFromContent()
43     {
44         $this->provider->expects($this->never())
45             ->method('getRouteByName')
46         ;
47         $this->contentDocument->expects($this->once())
48             ->method('getRoutes')
49             ->will($this->returnValue(array($this->routeDocument)))
50         ;
51         $this->routeDocument->expects($this->once())
52             ->method('compile')
53             ->will($this->returnValue($this->routeCompiled))
54         ;
55
56         $this->assertEquals('result_url', $this->generator->generate($this->contentDocument));
57     }
58
59     public function testGenerateFromContentId()
60     {
61         $this->provider->expects($this->never())
62             ->method('getRouteByName')
63         ;
64
65         $contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
66         $contentRepository->expects($this->once())
67             ->method('findById')
68             ->with('/content/id')
69             ->will($this->returnValue($this->contentDocument))
70         ;
71         $this->generator->setContentRepository($contentRepository);
72
73         $this->contentDocument->expects($this->once())
74             ->method('getRoutes')
75             ->will($this->returnValue(array($this->routeDocument)))
76         ;
77
78         $this->routeDocument->expects($this->once())
79             ->method('compile')
80             ->will($this->returnValue($this->routeCompiled))
81         ;
82
83         $this->assertEquals('result_url', $this->generator->generate('', array('content_id' => '/content/id')));
84     }
85
86     public function testGenerateEmptyRouteString()
87     {
88         $this->provider->expects($this->never())
89             ->method('getRouteByName')
90         ;
91
92         $this->contentDocument->expects($this->once())
93             ->method('getRoutes')
94             ->will($this->returnValue(array($this->routeDocument)))
95         ;
96
97         $this->routeDocument->expects($this->once())
98             ->method('compile')
99             ->will($this->returnValue($this->routeCompiled))
100         ;
101
102         $this->assertEquals('result_url', $this->generator->generate($this->contentDocument));
103     }
104
105     public function testGenerateRouteMultilang()
106     {
107         $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
108         $route_en->setLocale('en');
109         $route_de = $this->routeDocument;
110         $route_de->setLocale('de');
111
112         $this->contentDocument->expects($this->once())
113             ->method('getRoutes')
114             ->will($this->returnValue(array($route_en, $route_de)))
115         ;
116         $route_en->expects($this->once())
117             ->method('getContent')
118             ->will($this->returnValue($this->contentDocument))
119         ;
120         $route_en->expects($this->never())
121             ->method('compile')
122         ;
123         $route_de->expects($this->once())
124             ->method('compile')
125             ->will($this->returnValue($this->routeCompiled))
126         ;
127
128         $this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'de')));
129     }
130
131     public function testGenerateRouteMultilangDefaultLocale()
132     {
133         $route = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock');
134         $route->expects($this->any())
135             ->method('compile')
136             ->will($this->returnValue($this->routeCompiled))
137         ;
138         $route->expects($this->any())
139             ->method('getRequirement')
140             ->with('_locale')
141             ->will($this->returnValue('de|en'))
142         ;
143         $route->expects($this->any())
144             ->method('getDefault')
145             ->with('_locale')
146             ->will($this->returnValue('en'))
147         ;
148         $this->routeCompiled->expects($this->any())
149             ->method('getVariables')
150             ->will($this->returnValue(array()))
151         ;
152
153         $this->assertEquals('result_url', $this->generator->generate($route, array('_locale' => 'en')));
154     }
155
156     public function testGenerateRouteMultilangLocaleNomatch()
157     {
158         $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
159         $route_en->setLocale('en');
160         $route_de = $this->routeDocument;
161         $route_de->setLocale('de');
162
163         $this->contentDocument->expects($this->once())
164             ->method('getRoutes')
165             ->will($this->returnValue(array($route_en, $route_de)))
166         ;
167         $route_en->expects($this->once())
168             ->method('getContent')
169             ->will($this->returnValue($this->contentDocument))
170         ;
171         $route_en->expects($this->once())
172             ->method('compile')
173             ->will($this->returnValue($this->routeCompiled))
174         ;
175         $route_de->expects($this->never())
176             ->method('compile')
177         ;
178
179         $this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'fr')));
180     }
181
182     public function testGenerateNoncmfRouteMultilang()
183     {
184         $route_en = $this->buildMock('Symfony\Component\Routing\Route', array('getDefaults', 'compile', 'getContent'));
185
186         $route_en->expects($this->once())
187             ->method('compile')
188             ->will($this->returnValue($this->routeCompiled))
189         ;
190
191         $this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'de')));
192     }
193
194     public function testGenerateRoutenameMultilang()
195     {
196         $name = 'foo/bar';
197         $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
198         $route_en->setLocale('en');
199         $route_de = $this->routeDocument;
200         $route_de->setLocale('de');
201
202         $this->provider->expects($this->once())
203             ->method('getRouteByName')
204             ->with($name)
205             ->will($this->returnValue($route_en))
206         ;
207         $this->contentDocument->expects($this->once())
208             ->method('getRoutes')
209             ->will($this->returnValue(array($route_en, $route_de)))
210         ;
211         $route_en->expects($this->once())
212             ->method('getContent')
213             ->will($this->returnValue($this->contentDocument))
214         ;
215         $route_en->expects($this->never())
216             ->method('compile')
217         ;
218         $route_de->expects($this->once())
219             ->method('compile')
220             ->will($this->returnValue($this->routeCompiled))
221         ;
222
223         $this->assertEquals('result_url', $this->generator->generate($name, array('_locale' => 'de')));
224     }
225
226     /**
227      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
228      */
229     public function testGenerateRoutenameMultilangNotFound()
230     {
231         $name = 'foo/bar';
232
233         $this->provider->expects($this->once())
234             ->method('getRouteByName')
235             ->with($name)
236             ->will($this->returnValue(null))
237         ;
238
239         $this->generator->generate($name, array('_locale' => 'de'));
240     }
241
242     public function testGenerateDocumentMultilang()
243     {
244         $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
245         $route_en->setLocale('en');
246         $route_de = $this->routeDocument;
247         $route_de->setLocale('de');
248
249         $this->contentDocument->expects($this->once())
250             ->method('getRoutes')
251             ->will($this->returnValue(array($route_en, $route_de)))
252         ;
253         $route_en->expects($this->never())
254             ->method('compile')
255         ;
256         $route_de->expects($this->once())
257             ->method('compile')
258             ->will($this->returnValue($this->routeCompiled))
259         ;
260
261         $this->assertEquals('result_url', $this->generator->generate($this->contentDocument, array('_locale' => 'de')));
262     }
263
264     public function testGenerateDocumentMultilangLocaleNomatch()
265     {
266         $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
267         $route_en->setLocale('en');
268         $route_de = $this->routeDocument;
269         $route_de->setLocale('de');
270
271         $this->contentDocument->expects($this->once())
272             ->method('getRoutes')
273             ->will($this->returnValue(array($route_en, $route_de)))
274         ;
275         $route_en->expects($this->once())
276             ->method('compile')
277             ->will($this->returnValue($this->routeCompiled))
278         ;
279         $route_de->expects($this->never())
280             ->method('compile')
281         ;
282
283         $this->assertEquals('result_url', $this->generator->generate($this->contentDocument, array('_locale' => 'fr')));
284     }
285
286     /**
287      * Generate without any information.
288      *
289      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
290      */
291     public function testGenerateNoContent()
292     {
293         $this->generator->generate('', array());
294     }
295
296     /**
297      * Generate with an object that is neither a route nor route aware.
298      *
299      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
300      */
301     public function testGenerateInvalidContent()
302     {
303         $this->generator->generate($this);
304     }
305
306     /**
307      * Generate with a content_id but there is no content repository.
308      *
309      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
310      */
311     public function testGenerateNoContentRepository()
312     {
313         $this->provider->expects($this->never())
314             ->method('getRouteByName')
315         ;
316
317         $this->generator->generate('', array('content_id' => '/content/id'));
318     }
319
320     /**
321      * Generate with content_id but the content is not found.
322      *
323      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
324      */
325     public function testGenerateNoContentFoundInRepository()
326     {
327         $this->provider->expects($this->never())
328             ->method('getRouteByName')
329         ;
330
331         $contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
332         $contentRepository->expects($this->once())
333             ->method('findById')
334             ->with('/content/id')
335             ->will($this->returnValue(null))
336         ;
337         $this->generator->setContentRepository($contentRepository);
338
339         $this->generator->generate('', array('content_id' => '/content/id'));
340     }
341
342     /**
343      * Generate with content_id but the object at id is not route aware.
344      *
345      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
346      */
347     public function testGenerateWrongContentClassInRepository()
348     {
349         $this->provider->expects($this->never())
350             ->method('getRouteByName')
351         ;
352
353         $contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
354         $contentRepository->expects($this->once())
355             ->method('findById')
356             ->with('/content/id')
357             ->will($this->returnValue($this))
358         ;
359         $this->generator->setContentRepository($contentRepository);
360
361         $this->generator->generate('', array('content_id' => '/content/id'));
362     }
363
364     /**
365      * Generate from a content that has no routes associated.
366      *
367      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
368      */
369     public function testGenerateNoRoutes()
370     {
371         $this->contentDocument->expects($this->once())
372             ->method('getRoutes')
373             ->will($this->returnValue(array()));
374
375         $this->generator->generate($this->contentDocument);
376     }
377
378     /**
379      * Generate from a content that returns something that is not a route as route.
380      *
381      * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
382      */
383     public function testGenerateInvalidRoute()
384     {
385         $this->contentDocument->expects($this->once())
386             ->method('getRoutes')
387             ->will($this->returnValue(array($this)));
388
389         $this->generator->generate($this->contentDocument);
390     }
391
392     public function testGetLocaleAttribute()
393     {
394         $this->generator->setDefaultLocale('en');
395
396         $attributes = array('_locale' => 'fr');
397         $this->assertEquals('fr', $this->generator->getLocale($attributes));
398     }
399
400     public function testGetLocaleDefault()
401     {
402         $this->generator->setDefaultLocale('en');
403
404         $attributes = array();
405         $this->assertEquals('en', $this->generator->getLocale($attributes));
406     }
407
408     public function testGetLocaleContext()
409     {
410         $this->generator->setDefaultLocale('en');
411
412         $this->generator->getContext()->setParameter('_locale', 'de');
413
414         $attributes = array();
415         $this->assertEquals('de', $this->generator->getLocale($attributes));
416     }
417
418     public function testSupports()
419     {
420         $this->assertTrue($this->generator->supports(''));
421         $this->assertTrue($this->generator->supports(null));
422         $this->assertTrue($this->generator->supports($this->contentDocument));
423         $this->assertFalse($this->generator->supports($this));
424     }
425
426     public function testGetRouteDebugMessage()
427     {
428         $this->assertContains('/some/content', $this->generator->getRouteDebugMessage(null, array('content_id' => '/some/content')));
429         $this->assertContains('Route aware content Symfony\Cmf\Component\Routing\Tests\Routing\RouteAware', $this->generator->getRouteDebugMessage(new RouteAware()));
430         $this->assertContains('/some/content', $this->generator->getRouteDebugMessage('/some/content'));
431     }
432 }
433
434 /**
435  * Overwrite doGenerate to reduce amount of mocking needed.
436  */
437 class TestableContentAwareGenerator extends ContentAwareGenerator
438 {
439     protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
440     {
441         return 'result_url';
442     }
443
444     // expose as public
445
446     public function getLocale($parameters)
447     {
448         return parent::getLocale($parameters);
449     }
450 }
451
452 class RouteAware implements RouteReferrersReadInterface
453 {
454     public function getRoutes()
455     {
456         return array();
457     }
458 }