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