[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Web / Routing / UriBuilderTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Web\Routing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
17 use TYPO3\CMS\Core\Tests\UnitTestCase;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
20 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
21 use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
22 use TYPO3\CMS\Extbase\Mvc\Web\Request;
23 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
24 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
25 use TYPO3\CMS\Extbase\Service\EnvironmentService;
26 use TYPO3\CMS\Extbase\Service\ExtensionService;
27 use TYPO3\CMS\Extbase\Tests\Unit\Mvc\Web\Routing\Fixtures\EntityFixture;
28 use TYPO3\CMS\Extbase\Tests\Unit\Mvc\Web\Routing\Fixtures\ValueObjectFixture;
29 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
30 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
31
32 /**
33 * Test case
34 */
35 class UriBuilderTest extends UnitTestCase
36 {
37 /**
38 * @var ConfigurationManagerInterface
39 */
40 protected $mockConfigurationManager;
41
42 /**
43 * @var ContentObjectRenderer|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $mockContentObject;
46
47 /**
48 * @var Request|\PHPUnit_Framework_MockObject_MockObject
49 */
50 protected $mockRequest;
51
52 /**
53 * @var ExtensionService|\PHPUnit_Framework_MockObject_MockObject
54 */
55 protected $mockExtensionService;
56
57 /**
58 * @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
59 */
60 protected $uriBuilder;
61
62 /**
63 * @throws \InvalidArgumentException
64 * @throws \PHPUnit_Framework_Exception
65 */
66 protected function setUp()
67 {
68 $GLOBALS['TSFE'] = $this->getMock(TypoScriptFrontendController::class, array(), array(), '', false);
69 $this->mockContentObject = $this->getMock(ContentObjectRenderer::class);
70 $this->mockRequest = $this->getMock(Request::class);
71 $this->mockExtensionService = $this->getMock(ExtensionService::class);
72 $this->mockConfigurationManager = $this->getMock(ConfigurationManagerInterface::class);
73 $this->uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('build'));
74 $this->uriBuilder->setRequest($this->mockRequest);
75 $this->uriBuilder->_set('contentObject', $this->mockContentObject);
76 $this->uriBuilder->_set('configurationManager', $this->mockConfigurationManager);
77 $this->uriBuilder->_set('extensionService', $this->mockExtensionService);
78 $this->uriBuilder->_set('environmentService', $this->getMock(EnvironmentService::class));
79 // Mocking backend user is required for backend URI generation as BackendUtility::getModuleUrl() is called
80 $backendUserMock = $this->getMock(BackendUserAuthentication::class);
81 $backendUserMock->expects($this->any())->method('check')->will($this->returnValue(true));
82 $GLOBALS['BE_USER'] = $backendUserMock;
83 }
84
85 /**
86 * @test
87 */
88 public function settersAndGettersWorkAsExpected()
89 {
90 $this->uriBuilder
91 ->reset()
92 ->setArguments(array('test' => 'arguments'))
93 ->setSection('testSection')
94 ->setFormat('testFormat')
95 ->setCreateAbsoluteUri(true)
96 ->setAbsoluteUriScheme('https')
97 ->setAddQueryString(true)
98 ->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))
99 ->setAddQueryStringMethod('GET,POST')
100 ->setArgumentPrefix('testArgumentPrefix')
101 ->setLinkAccessRestrictedPages(true)
102 ->setTargetPageUid(123)
103 ->setTargetPageType(321)
104 ->setNoCache(true)
105 ->setUseCacheHash(false);
106 $this->assertEquals(array('test' => 'arguments'), $this->uriBuilder->getArguments());
107 $this->assertEquals('testSection', $this->uriBuilder->getSection());
108 $this->assertEquals('testFormat', $this->uriBuilder->getFormat());
109 $this->assertEquals(true, $this->uriBuilder->getCreateAbsoluteUri());
110 $this->assertEquals('https', $this->uriBuilder->getAbsoluteUriScheme());
111 $this->assertEquals(true, $this->uriBuilder->getAddQueryString());
112 $this->assertEquals(array('test' => 'addQueryStringExcludeArguments'), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
113 $this->assertEquals('GET,POST', $this->uriBuilder->getAddQueryStringMethod());
114 $this->assertEquals('testArgumentPrefix', $this->uriBuilder->getArgumentPrefix());
115 $this->assertEquals(true, $this->uriBuilder->getLinkAccessRestrictedPages());
116 $this->assertEquals(123, $this->uriBuilder->getTargetPageUid());
117 $this->assertEquals(321, $this->uriBuilder->getTargetPageType());
118 $this->assertEquals(true, $this->uriBuilder->getNoCache());
119 $this->assertEquals(false, $this->uriBuilder->getUseCacheHash());
120 }
121
122 /**
123 * @test
124 */
125 public function uriForPrefixesArgumentsWithExtensionAndPluginNameAndSetsControllerArgument()
126 {
127 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
128 $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'));
129 $GLOBALS['TSFE'] = null;
130 $this->uriBuilder->uriFor(null, array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')), 'SomeController', 'SomeExtension', 'SomePlugin');
131 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
132 }
133
134 /**
135 * @test
136 */
137 public function uriForRecursivelyMergesAndOverrulesControllerArgumentsWithArguments()
138 {
139 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
140 $arguments = array('tx_someextension_someplugin' => array('foo' => 'bar'), 'additionalParam' => 'additionalValue');
141 $controllerArguments = array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'));
142 $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'), 'additionalParam' => 'additionalValue');
143 $this->uriBuilder->setArguments($arguments);
144 $this->uriBuilder->uriFor(null, $controllerArguments, 'SomeController', 'SomeExtension', 'SomePlugin');
145 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
146 }
147
148 /**
149 * @test
150 */
151 public function uriForOnlySetsActionArgumentIfSpecified()
152 {
153 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
154 $expectedArguments = array('tx_someextension_someplugin' => array('controller' => 'SomeController'));
155 $this->uriBuilder->uriFor(null, array(), 'SomeController', 'SomeExtension', 'SomePlugin');
156 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
157 }
158
159 /**
160 * @test
161 */
162 public function uriForSetsControllerFromRequestIfControllerIsNotSet()
163 {
164 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
165 $this->mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('SomeControllerFromRequest'));
166 $expectedArguments = array('tx_someextension_someplugin' => array('controller' => 'SomeControllerFromRequest'));
167 $this->uriBuilder->uriFor(null, array(), null, 'SomeExtension', 'SomePlugin');
168 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
169 }
170
171 /**
172 * @test
173 */
174 public function uriForSetsExtensionNameFromRequestIfExtensionNameIsNotSet()
175 {
176 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_someextensionnamefromrequest_someplugin'));
177 $this->mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
178 $expectedArguments = array('tx_someextensionnamefromrequest_someplugin' => array('controller' => 'SomeController'));
179 $this->uriBuilder->uriFor(null, array(), 'SomeController', null, 'SomePlugin');
180 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
181 }
182
183 /**
184 * @test
185 */
186 public function uriForSetsPluginNameFromRequestIfPluginNameIsNotSet()
187 {
188 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_somepluginnamefromrequest'));
189 $this->mockRequest->expects($this->once())->method('getPluginName')->will($this->returnValue('SomePluginNameFromRequest'));
190 $expectedArguments = array('tx_someextension_somepluginnamefromrequest' => array('controller' => 'SomeController'));
191 $this->uriBuilder->uriFor(null, array(), 'SomeController', 'SomeExtension');
192 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
193 }
194
195 /**
196 * @test
197 */
198 public function uriForDoesNotDisableCacheHashForNonCacheableActions()
199 {
200 $this->mockExtensionService->expects($this->any())->method('isActionCacheable')->will($this->returnValue(false));
201 $this->uriBuilder->uriFor('someNonCacheableAction', array(), 'SomeController', 'SomeExtension');
202 $this->assertTrue($this->uriBuilder->getUseCacheHash());
203 }
204
205 /**
206 * @test
207 */
208 public function buildBackendUriKeepsQueryParametersIfAddQueryStringIsSet()
209 {
210 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
211 $_POST = array();
212 $_POST['foo2'] = 'bar2';
213 $this->uriBuilder->setAddQueryString(true);
214 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
215 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&id=pageId&foo=bar&foo2=bar2';
216 $actualResult = $this->uriBuilder->buildBackendUri();
217 $this->assertEquals($expectedResult, $actualResult);
218 }
219
220 /**
221 * @test
222 */
223 public function buildBackendUriKeepsQueryParametersIfAddQueryStringMethodIsNotSet()
224 {
225 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
226 $_POST = array();
227 $_POST['foo2'] = 'bar2';
228 $this->uriBuilder->setAddQueryString(true);
229 $this->uriBuilder->setAddQueryStringMethod(null);
230 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&id=pageId&foo=bar';
231 $actualResult = $this->uriBuilder->buildBackendUri();
232 $this->assertEquals($expectedResult, $actualResult);
233 }
234
235 /**
236 * return array
237 */
238 public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider()
239 {
240 return array(
241 'Arguments to be excluded in the beginning' => array(
242 array(
243 'M' => 'moduleKey',
244 'id' => 'pageId',
245 'foo' => 'bar'
246 ),
247 array(
248 'foo2' => 'bar2'
249 ),
250 array(
251 'M',
252 'id'
253 ),
254 '/typo3/index.php?moduleToken=dummyToken&foo=bar&foo2=bar2'
255 ),
256 'Arguments to be excluded in the end' => array(
257 array(
258 'foo' => 'bar',
259 'id' => 'pageId',
260 'M' => 'moduleKey'
261 ),
262 array(
263 'foo2' => 'bar2'
264 ),
265 array(
266 'M',
267 'id'
268 ),
269 '/typo3/index.php?moduleToken=dummyToken&foo=bar&foo2=bar2'
270 ),
271 'Arguments in nested array to be excluded' => array(
272 array(
273 'tx_foo' => array(
274 'bar' => 'baz'
275 ),
276 'id' => 'pageId',
277 'M' => 'moduleKey'
278 ),
279 array(
280 'foo2' => 'bar2'
281 ),
282 array(
283 'id',
284 'tx_foo[bar]'
285 ),
286 '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&foo2=bar2'
287 ),
288 'Arguments in multidimensional array to be excluded' => array(
289 array(
290 'tx_foo' => array(
291 'bar' => array(
292 'baz' => 'bay'
293 )
294 ),
295 'id' => 'pageId',
296 'M' => 'moduleKey'
297 ),
298 array(
299 'foo2' => 'bar2'
300 ),
301 array(
302 'id',
303 'tx_foo[bar][baz]'
304 ),
305 '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&foo2=bar2'
306 ),
307 );
308 }
309
310 /**
311 * @test
312 * @dataProvider buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider
313 * @param array $parameters
314 * @param array $postArguments
315 * @param array $excluded
316 * @param string $expected
317 */
318 public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSet(array $parameters, array $postArguments, array $excluded, $expected)
319 {
320 GeneralUtility::_GETset($parameters);
321 $_POST = $postArguments;
322 $this->uriBuilder->setAddQueryString(true);
323 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
324 $this->uriBuilder->setArgumentsToBeExcludedFromQueryString($excluded);
325 $actualResult = $this->uriBuilder->buildBackendUri();
326 $this->assertEquals($expected, $actualResult);
327 }
328
329 /**
330 * @test
331 */
332 public function buildBackendUriKeepsModuleQueryParametersIfAddQueryStringIsNotSet()
333 {
334 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
335 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&id=pageId';
336 $actualResult = $this->uriBuilder->buildBackendUri();
337 $this->assertEquals($expectedResult, $actualResult);
338 }
339
340 /**
341 * @test
342 */
343 public function buildBackendUriMergesAndOverrulesQueryParametersWithArguments()
344 {
345 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
346 $this->uriBuilder->setArguments(array('M' => 'overwrittenModuleKey', 'somePrefix' => array('bar' => 'baz')));
347 $expectedResult = '/typo3/index.php?M=overwrittenModuleKey&moduleToken=dummyToken&id=pageId&somePrefix%5Bbar%5D=baz';
348 $actualResult = $this->uriBuilder->buildBackendUri();
349 $this->assertEquals($expectedResult, $actualResult);
350 }
351
352 /**
353 * @test
354 */
355 public function buildBackendUriConvertsDomainObjectsAfterArgumentsHaveBeenMerged()
356 {
357 GeneralUtility::_GETset(array('M' => 'moduleKey'));
358 $mockDomainObject = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
359 $mockDomainObject->_set('uid', '123');
360 $this->uriBuilder->setArguments(array('somePrefix' => array('someDomainObject' => $mockDomainObject)));
361 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&somePrefix%5BsomeDomainObject%5D=123';
362 $actualResult = $this->uriBuilder->buildBackendUri();
363 $this->assertEquals($expectedResult, $actualResult);
364 }
365
366 /**
367 * @test
368 */
369 public function buildBackendUriRespectsSection()
370 {
371 GeneralUtility::_GETset(array('M' => 'moduleKey'));
372 $this->uriBuilder->setSection('someSection');
373 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken#someSection';
374 $actualResult = $this->uriBuilder->buildBackendUri();
375 $this->assertEquals($expectedResult, $actualResult);
376 }
377
378 /**
379 * @test
380 */
381 public function buildBackendUriCreatesAbsoluteUrisIfSpecified()
382 {
383 GeneralUtility::flushInternalRuntimeCaches();
384 GeneralUtility::_GETset(array('M' => 'moduleKey'));
385 $_SERVER['HTTP_HOST'] = 'baseuri';
386 $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
387 $this->mockRequest->expects($this->any())->method('getBaseUri')->will($this->returnValue('http://baseuri'));
388 $this->uriBuilder->setCreateAbsoluteUri(true);
389 $expectedResult = 'http://baseuri/' . TYPO3_mainDir . 'index.php?M=moduleKey&moduleToken=dummyToken';
390 $actualResult = $this->uriBuilder->buildBackendUri();
391 $this->assertSame($expectedResult, $actualResult);
392 }
393
394 /**
395 * @test
396 */
397 public function buildBackendUriWithQueryStringMethodPostGetMergesParameters()
398 {
399 $_POST = array(
400 'key1' => 'POST1',
401 'key2' => 'POST2',
402 'key3' => array(
403 'key31' => 'POST31',
404 'key32' => 'POST32',
405 'key33' => array(
406 'key331' => 'POST331',
407 'key332' => 'POST332',
408 )
409 ),
410 );
411 $_GET = array(
412 'key2' => 'GET2',
413 'key3' => array(
414 'key32' => 'GET32',
415 'key33' => array(
416 'key331' => 'GET331',
417 )
418 )
419 );
420 $this->uriBuilder->setAddQueryString(true);
421 $this->uriBuilder->setAddQueryStringMethod('POST,GET');
422 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('/typo3/index.php?moduleToken=dummyToken&key1=POST1&key2=GET2&key3[key31]=POST31&key3[key32]=GET32&key3[key33][key331]=GET331&key3[key33][key332]=POST332');
423 $actualResult = $this->uriBuilder->buildBackendUri();
424 $this->assertEquals($expectedResult, $actualResult);
425 }
426
427 /**
428 * @test
429 */
430 public function buildBackendUriWithQueryStringMethodGetPostMergesParameters()
431 {
432 $_GET = array(
433 'key1' => 'GET1',
434 'key2' => 'GET2',
435 'key3' => array(
436 'key31' => 'GET31',
437 'key32' => 'GET32',
438 'key33' => array(
439 'key331' => 'GET331',
440 'key332' => 'GET332',
441 )
442 ),
443 );
444 $_POST = array(
445 'key2' => 'POST2',
446 'key3' => array(
447 'key32' => 'POST32',
448 'key33' => array(
449 'key331' => 'POST331',
450 )
451 )
452 );
453 $this->uriBuilder->setAddQueryString(true);
454 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
455 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('/typo3/index.php?moduleToken=dummyToken&key1=GET1&key2=POST2&key3[key31]=GET31&key3[key32]=POST32&key3[key33][key331]=POST331&key3[key33][key332]=GET332');
456 $actualResult = $this->uriBuilder->buildBackendUri();
457 $this->assertEquals($expectedResult, $actualResult);
458 }
459
460 /**
461 * Encodes square brackets in URL.
462 *
463 * @param string $string
464 * @return string
465 */
466 private function rawUrlEncodeSquareBracketsInUrl($string)
467 {
468 return str_replace(array('[', ']'), array('%5B', '%5D'), $string);
469 }
470
471 /**
472 * @test
473 */
474 public function buildFrontendUriCreatesTypoLink()
475 {
476 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
477 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
478 $uriBuilder->_set('contentObject', $this->mockContentObject);
479 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
480 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
481 $uriBuilder->buildFrontendUri();
482 }
483
484 /**
485 * @test
486 */
487 public function buildFrontendUriCreatesRelativeUrisByDefault()
488 {
489 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
490 $expectedResult = 'relative/uri';
491 $actualResult = $this->uriBuilder->buildFrontendUri();
492 $this->assertSame($expectedResult, $actualResult);
493 }
494
495 /**
496 * @test
497 */
498 public function buildFrontendUriDoesNotStripLeadingSlashesFromRelativeUris()
499 {
500 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('/relative/uri'));
501 $expectedResult = '/relative/uri';
502 $actualResult = $this->uriBuilder->buildFrontendUri();
503 $this->assertSame($expectedResult, $actualResult);
504 }
505
506 /**
507 * @test
508 */
509 public function buildFrontendUriCreatesAbsoluteUrisIfSpecified()
510 {
511 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
512 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
513 $uriBuilder->_set('contentObject', $this->mockContentObject);
514 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
515 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => true))->will($this->returnValue('http://baseuri/relative/uri'));
516 $uriBuilder->setCreateAbsoluteUri(true);
517 $expectedResult = 'http://baseuri/relative/uri';
518 $actualResult = $uriBuilder->buildFrontendUri();
519 $this->assertSame($expectedResult, $actualResult);
520 }
521
522 /**
523 * @test
524 */
525 public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified()
526 {
527 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
528 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
529 $uriBuilder->_set('contentObject', $this->mockContentObject);
530 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
531 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => true, 'forceAbsoluteUrl.' => array('scheme' => 'someScheme')))->will($this->returnValue('http://baseuri/relative/uri'));
532 $uriBuilder->setCreateAbsoluteUri(true);
533 $uriBuilder->setAbsoluteUriScheme('someScheme');
534 $expectedResult = 'http://baseuri/relative/uri';
535 $actualResult = $uriBuilder->buildFrontendUri();
536 $this->assertSame($expectedResult, $actualResult);
537 }
538
539 /**
540 * @test
541 */
542 public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse()
543 {
544 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
545 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
546 $uriBuilder->_set('contentObject', $this->mockContentObject);
547 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
548 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
549 $uriBuilder->setCreateAbsoluteUri(false);
550 $uriBuilder->setAbsoluteUriScheme('someScheme');
551 $expectedResult = 'http://baseuri/relative/uri';
552 $actualResult = $uriBuilder->buildFrontendUri();
553 $this->assertSame($expectedResult, $actualResult);
554 }
555
556 /**
557 * @test
558 */
559 public function resetSetsAllOptionsToTheirDefaultValue()
560 {
561 $this->uriBuilder->setArguments(array('test' => 'arguments'))->setSection('testSection')->setFormat('someFormat')->setCreateAbsoluteUri(true)->setAddQueryString(true)->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))->setAddQueryStringMethod(null)->setArgumentPrefix('testArgumentPrefix')->setLinkAccessRestrictedPages(true)->setTargetPageUid(123)->setTargetPageType(321)->setNoCache(true)->setUseCacheHash(false);
562 $this->uriBuilder->reset();
563 $this->assertEquals(array(), $this->uriBuilder->getArguments());
564 $this->assertEquals('', $this->uriBuilder->getSection());
565 $this->assertEquals('', $this->uriBuilder->getFormat());
566 $this->assertEquals(false, $this->uriBuilder->getCreateAbsoluteUri());
567 $this->assertEquals(false, $this->uriBuilder->getAddQueryString());
568 $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
569 $this->assertEquals(null, $this->uriBuilder->getAddQueryStringMethod());
570 $this->assertEquals(null, $this->uriBuilder->getArgumentPrefix());
571 $this->assertEquals(false, $this->uriBuilder->getLinkAccessRestrictedPages());
572 $this->assertEquals(null, $this->uriBuilder->getTargetPageUid());
573 $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
574 $this->assertEquals(false, $this->uriBuilder->getNoCache());
575 $this->assertEquals(true, $this->uriBuilder->getUseCacheHash());
576 }
577
578 /**
579 * @test
580 */
581 public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid()
582 {
583 $GLOBALS['TSFE']->id = 123;
584 $this->uriBuilder->setTargetPageUid(321);
585 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
586 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
587 $this->assertEquals($expectedConfiguration, $actualConfiguration);
588 }
589
590 /**
591 * @test
592 */
593 public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet()
594 {
595 $GLOBALS['TSFE']->id = 123;
596 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
597 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
598 $this->assertEquals($expectedConfiguration, $actualConfiguration);
599 }
600
601 /**
602 * @test
603 */
604 public function buildTypolinkConfigurationProperlySetsAdditionalArguments()
605 {
606 $this->uriBuilder->setTargetPageUid(123);
607 $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
608 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz[extbase]=fluid');
609 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
610 $this->assertEquals($expectedConfiguration, $actualConfiguration);
611 }
612
613 /**
614 * @test
615 */
616 public function buildTypolinkConfigurationProperlySetsAddQueryString()
617 {
618 $this->uriBuilder->setTargetPageUid(123);
619 $this->uriBuilder->setAddQueryString(true);
620 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'useCacheHash' => 1);
621 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
622 $this->assertEquals($expectedConfiguration, $actualConfiguration);
623 }
624
625 /**
626 * @test
627 */
628 public function buildTypolinkConfigurationProperlySetsAddQueryStringMethod()
629 {
630 $this->uriBuilder->setTargetPageUid(123);
631 $this->uriBuilder->setAddQueryString(true);
632 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
633 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'addQueryString.' => array('method' => 'GET,POST'), 'useCacheHash' => 1);
634 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
635 $this->assertEquals($expectedConfiguration, $actualConfiguration);
636 }
637
638 /**
639 * @test
640 */
641 public function buildTypolinkConfigurationConvertsDomainObjects()
642 {
643 $mockDomainObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
644 $mockDomainObject1->_set('uid', '123');
645 $mockDomainObject2 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
646 $mockDomainObject2->_set('uid', '321');
647 $this->uriBuilder->setTargetPageUid(123);
648 $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
649 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject=123&baz[someOtherDomainObject]=321');
650 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
651 $this->assertEquals($expectedConfiguration, $actualConfiguration);
652 }
653
654 /**
655 * @test
656 */
657 public function buildTypolinkConfigurationResolvesPageTypeFromFormat()
658 {
659 $this->uriBuilder->setTargetPageUid(123);
660 $this->uriBuilder->setFormat('txt');
661 $this->mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
662
663 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
664 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
665 ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
666 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
667
668 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
669 ->with('SomeExtensionNameFromRequest', 'txt')
670 ->will($this->returnValue(2));
671
672 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
673 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
674 $this->assertEquals($expectedConfiguration, $actualConfiguration);
675 }
676
677 /**
678 * @test
679 */
680 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured()
681 {
682 $this->uriBuilder->setTargetPageUid(123);
683 $this->uriBuilder->setFormat('txt');
684
685 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
686 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
687 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
688
689 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
690 ->with(null, 'txt')
691 ->will($this->returnValue(0));
692
693 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
694 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
695
696 $this->assertEquals($expectedConfiguration, $actualConfiguration);
697 }
698
699 /**
700 * @test
701 */
702 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped()
703 {
704 $this->uriBuilder->setTargetPageUid(123);
705 $this->uriBuilder->setFormat('txt');
706
707 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
708 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
709 ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
710 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
711
712 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
713 ->with(null, 'txt')
714 ->will($this->returnValue(0));
715
716 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
717 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
718
719 $this->assertEquals($expectedConfiguration, $actualConfiguration);
720 }
721
722 /**
723 * @test
724 */
725 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet()
726 {
727 $this->uriBuilder->setTargetPageUid(123);
728 $this->uriBuilder->setNoCache(true);
729 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
730 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
731 $this->assertEquals($expectedConfiguration, $actualConfiguration);
732 }
733
734 /**
735 * @test
736 */
737 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled()
738 {
739 $this->uriBuilder->setTargetPageUid(123);
740 $this->uriBuilder->setUseCacheHash(false);
741 $expectedConfiguration = array('parameter' => 123);
742 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
743 $this->assertEquals($expectedConfiguration, $actualConfiguration);
744 }
745
746 /**
747 * @test
748 */
749 public function buildTypolinkConfigurationConsidersSection()
750 {
751 $this->uriBuilder->setTargetPageUid(123);
752 $this->uriBuilder->setSection('SomeSection');
753 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
754 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
755 $this->assertEquals($expectedConfiguration, $actualConfiguration);
756 }
757
758 /**
759 * @test
760 */
761 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting()
762 {
763 $this->uriBuilder->setTargetPageUid(123);
764 $this->uriBuilder->setLinkAccessRestrictedPages(true);
765 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
766 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
767 $this->assertEquals($expectedConfiguration, $actualConfiguration);
768 }
769
770 /**
771 * @test
772 */
773 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects()
774 {
775 $mockDomainObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
776 $mockDomainObject1->_set('uid', '123');
777 $mockDomainObject2 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
778 $mockDomainObject2->_set('uid', '321');
779 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
780 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
781 $this->assertEquals($expectedResult, $actualResult);
782 }
783
784 /**
785 * @test
786 */
787 public function convertDomainObjectsToIdentityArraysConvertsObjectStoragesWithDomainObjects()
788 {
789 $objectStorage = new ObjectStorage();
790 $mockChildObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
791 $mockChildObject1->_set('uid', '123');
792 $objectStorage->attach($mockChildObject1);
793 $expectedResult = array('foo' => array('bar' => 'baz'), 'objectStorage' => array('123'));
794 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'objectStorage' => $objectStorage));
795 $this->assertEquals($expectedResult, $actualResult);
796 }
797
798 /**
799 * @test
800 */
801 public function conversionOfTansientObjectsIsInvoked()
802 {
803 $mockValueObject = new ValueObjectFixture();
804 $mockValueObject->name = 'foo';
805 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|Object $mockUriBuilder */
806 $mockUriBuilder = $this->getAccessibleMock(UriBuilder::class, array('convertTransientObjectToArray'));
807 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
808 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
809 $expectedResult = array('object' => array('foo' => 'bar'));
810 $this->assertEquals($expectedResult, $actualResult);
811 }
812
813 /**
814 * @test
815 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
816 */
817 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects()
818 {
819 $mockEntity = new EntityFixture();
820 $mockEntity->name = 'foo';
821 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|Object $mockUriBuilder */
822 $mockUriBuilder = $this->getAccessibleMock(UriBuilder::class, array('dummy'));
823 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
824 }
825
826 /**
827 * @test
828 */
829 public function tansientObjectsAreConvertedToAnArrayOfProperties()
830 {
831 $mockValueObject = new ValueObjectFixture();
832 $mockValueObject->name = 'foo';
833 $uriBuilder = new UriBuilder();
834 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
835 $expectedResult = array('name' => 'foo', 'object' => null, 'uid' => null, 'pid' => null);
836 $this->assertEquals($expectedResult, $actualResult);
837 }
838
839 /**
840 * @test
841 */
842 public function tansientObjectsWithObjectStorageAreConvertedToAnArrayOfProperties()
843 {
844 $mockValueObject = new ValueObjectFixture();
845 $objectStorage = new ObjectStorage();
846 $mockValueObject->name = 'foo';
847 $mockValueObject2 = new ValueObjectFixture();
848 $mockValueObject2->name = 'bar';
849 $objectStorage->attach($mockValueObject2);
850 $mockValueObject->object = $objectStorage;
851 $uriBuilder = new UriBuilder();
852 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
853 $expectedResult = array(
854 'name' => 'foo',
855 'object' => array(
856 array(
857 'name' => 'bar',
858 'uid' => null,
859 'pid' => null,
860 'object' => null
861 )
862 ),
863 'uid' => null,
864 'pid' => null
865 );
866 $this->assertEquals($expectedResult, $actualResult);
867 }
868
869 /**
870 * @test
871 */
872 public function tansientObjectsAreRecursivelyConverted()
873 {
874 $mockInnerValueObject2 = new ValueObjectFixture();
875 $mockInnerValueObject2->name = 'foo';
876 $mockInnerValueObject2->uid = 99;
877 $mockInnerValueObject1 = new ValueObjectFixture();
878 $mockInnerValueObject1->object = $mockInnerValueObject2;
879 $mockValueObject = new ValueObjectFixture();
880 $mockValueObject->object = $mockInnerValueObject1;
881 $uriBuilder = new UriBuilder();
882 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
883 $expectedResult = array(
884 'name' => null,
885 'object' => array(
886 'name' => null,
887 'object' => 99,
888 'uid' => null,
889 'pid' => null
890 ),
891 'uid' => null,
892 'pid' => null
893 );
894 $this->assertEquals($expectedResult, $actualResult);
895 }
896
897 /**
898 * @test
899 */
900 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults()
901 {
902 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
903 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
904 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
905 $extensionName = 'ExtensionName';
906 $pluginName = 'PluginName';
907 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
908 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
909 $this->assertEquals($expectedResult, $actualResult);
910 }
911
912 /**
913 * @test
914 */
915 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault()
916 {
917 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
918 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
919 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
920 $extensionName = 'ExtensionName';
921 $pluginName = 'PluginName';
922 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
923 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
924 $this->assertEquals($expectedResult, $actualResult);
925 }
926
927 /**
928 * @test
929 */
930 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault()
931 {
932 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
933 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
934 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
935 $extensionName = 'ExtensionName';
936 $pluginName = 'PluginName';
937 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
938 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
939 $this->assertEquals($expectedResult, $actualResult);
940 }
941
942 /**
943 * @test
944 */
945 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault()
946 {
947 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
948 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
949 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
950 $extensionName = 'ExtensionName';
951 $pluginName = 'PluginName';
952 $expectedResult = array('foo' => 'bar');
953 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
954 $this->assertEquals($expectedResult, $actualResult);
955 }
956
957 /**
958 * @return array
959 */
960 public function convertIteratorToArrayConvertsIteratorsToArrayProvider()
961 {
962 return array(
963 'Extbase ObjectStorage' => array(new ObjectStorage()),
964 'SplObjectStorage' => array(new \SplObjectStorage()),
965 'ArrayIterator' => array(new \ArrayIterator())
966 );
967 }
968
969 /**
970 * @dataProvider convertIteratorToArrayConvertsIteratorsToArrayProvider
971 * @test
972 */
973 public function convertIteratorToArrayConvertsIteratorsToArray($iterator)
974 {
975 $result = $this->uriBuilder->_call('convertIteratorToArray', $iterator);
976 $this->assertTrue(is_array($result));
977 }
978 }