[TASK] Introduce runtime cache for getIndpEnv()
[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::flushInternalRuntimeCaches();
399 GeneralUtility::_GETset(array('M' => 'moduleKey'));
400 $_SERVER['HTTP_HOST'] = 'baseuri';
401 $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
402 $this->mockRequest->expects($this->any())->method('getBaseUri')->will($this->returnValue('http://baseuri'));
403 $this->uriBuilder->setCreateAbsoluteUri(TRUE);
404 $expectedResult = 'http://baseuri/' . TYPO3_mainDir . 'index.php?M=moduleKey&moduleToken=dummyToken';
405 $actualResult = $this->uriBuilder->buildBackendUri();
406 $this->assertSame($expectedResult, $actualResult);
407 }
408
409 /**
410 * @test
411 */
412 public function buildBackendUriWithQueryStringMethodPostGetMergesParameters() {
413 $_POST = array(
414 'key1' => 'POST1',
415 'key2' => 'POST2',
416 'key3' => array(
417 'key31' => 'POST31',
418 'key32' => 'POST32',
419 'key33' => array(
420 'key331' => 'POST331',
421 'key332' => 'POST332',
422 )
423 ),
424 );
425 $_GET = array(
426 'key2' => 'GET2',
427 'key3' => array(
428 'key32' => 'GET32',
429 'key33' => array(
430 'key331' => 'GET331',
431 )
432 )
433 );
434 $this->uriBuilder->setAddQueryString(TRUE);
435 $this->uriBuilder->setAddQueryStringMethod('POST,GET');
436 $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');
437 $actualResult = $this->uriBuilder->buildBackendUri();
438 $this->assertEquals($expectedResult, $actualResult);
439 }
440
441 /**
442 * @test
443 */
444 public function buildBackendUriWithQueryStringMethodGetPostMergesParameters() {
445 $_GET = array(
446 'key1' => 'GET1',
447 'key2' => 'GET2',
448 'key3' => array(
449 'key31' => 'GET31',
450 'key32' => 'GET32',
451 'key33' => array(
452 'key331' => 'GET331',
453 'key332' => 'GET332',
454 )
455 ),
456 );
457 $_POST = array(
458 'key2' => 'POST2',
459 'key3' => array(
460 'key32' => 'POST32',
461 'key33' => array(
462 'key331' => 'POST331',
463 )
464 )
465 );
466 $this->uriBuilder->setAddQueryString(TRUE);
467 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
468 $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');
469 $actualResult = $this->uriBuilder->buildBackendUri();
470 $this->assertEquals($expectedResult, $actualResult);
471 }
472
473 /**
474 * Encodes square brackets in URL.
475 *
476 * @param string $string
477 * @return string
478 */
479 private function rawUrlEncodeSquareBracketsInUrl($string) {
480 return str_replace(array('[', ']'), array('%5B', '%5D'), $string);
481 }
482
483 /**
484 * @test
485 */
486 public function buildFrontendUriCreatesTypoLink() {
487 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
488 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
489 $uriBuilder->_set('contentObject', $this->mockContentObject);
490 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
491 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
492 $uriBuilder->buildFrontendUri();
493 }
494
495 /**
496 * @test
497 */
498 public function buildFrontendUriCreatesRelativeUrisByDefault() {
499 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
500 $expectedResult = 'relative/uri';
501 $actualResult = $this->uriBuilder->buildFrontendUri();
502 $this->assertSame($expectedResult, $actualResult);
503 }
504
505 /**
506 * @test
507 */
508 public function buildFrontendUriDoesNotStripLeadingSlashesFromRelativeUris() {
509 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('/relative/uri'));
510 $expectedResult = '/relative/uri';
511 $actualResult = $this->uriBuilder->buildFrontendUri();
512 $this->assertSame($expectedResult, $actualResult);
513 }
514
515 /**
516 * @test
517 */
518 public function buildFrontendUriCreatesAbsoluteUrisIfSpecified() {
519 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
520 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
521 $uriBuilder->_set('contentObject', $this->mockContentObject);
522 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
523 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => TRUE))->will($this->returnValue('http://baseuri/relative/uri'));
524 $uriBuilder->setCreateAbsoluteUri(TRUE);
525 $expectedResult = 'http://baseuri/relative/uri';
526 $actualResult = $uriBuilder->buildFrontendUri();
527 $this->assertSame($expectedResult, $actualResult);
528 }
529
530 /**
531 * @test
532 */
533 public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified() {
534 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
535 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
536 $uriBuilder->_set('contentObject', $this->mockContentObject);
537 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
538 $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'));
539 $uriBuilder->setCreateAbsoluteUri(TRUE);
540 $uriBuilder->setAbsoluteUriScheme('someScheme');
541 $expectedResult = 'http://baseuri/relative/uri';
542 $actualResult = $uriBuilder->buildFrontendUri();
543 $this->assertSame($expectedResult, $actualResult);
544 }
545
546 /**
547 * @test
548 */
549 public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse() {
550 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
551 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
552 $uriBuilder->_set('contentObject', $this->mockContentObject);
553 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
554 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
555 $uriBuilder->setCreateAbsoluteUri(FALSE);
556 $uriBuilder->setAbsoluteUriScheme('someScheme');
557 $expectedResult = 'http://baseuri/relative/uri';
558 $actualResult = $uriBuilder->buildFrontendUri();
559 $this->assertSame($expectedResult, $actualResult);
560 }
561
562 /**
563 * @test
564 */
565 public function resetSetsAllOptionsToTheirDefaultValue() {
566 $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);
567 $this->uriBuilder->reset();
568 $this->assertEquals(array(), $this->uriBuilder->getArguments());
569 $this->assertEquals('', $this->uriBuilder->getSection());
570 $this->assertEquals('', $this->uriBuilder->getFormat());
571 $this->assertEquals(FALSE, $this->uriBuilder->getCreateAbsoluteUri());
572 $this->assertEquals(FALSE, $this->uriBuilder->getAddQueryString());
573 $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
574 $this->assertEquals(NULL, $this->uriBuilder->getAddQueryStringMethod());
575 $this->assertEquals(NULL, $this->uriBuilder->getArgumentPrefix());
576 $this->assertEquals(FALSE, $this->uriBuilder->getLinkAccessRestrictedPages());
577 $this->assertEquals(NULL, $this->uriBuilder->getTargetPageUid());
578 $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
579 $this->assertEquals(FALSE, $this->uriBuilder->getNoCache());
580 $this->assertEquals(TRUE, $this->uriBuilder->getUseCacheHash());
581 }
582
583 /**
584 * @test
585 */
586 public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid() {
587 $GLOBALS['TSFE']->id = 123;
588 $this->uriBuilder->setTargetPageUid(321);
589 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
590 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
591 $this->assertEquals($expectedConfiguration, $actualConfiguration);
592 }
593
594 /**
595 * @test
596 */
597 public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet() {
598 $GLOBALS['TSFE']->id = 123;
599 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
600 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
601 $this->assertEquals($expectedConfiguration, $actualConfiguration);
602 }
603
604 /**
605 * @test
606 */
607 public function buildTypolinkConfigurationProperlySetsAdditionalArguments() {
608 $this->uriBuilder->setTargetPageUid(123);
609 $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
610 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz[extbase]=fluid');
611 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
612 $this->assertEquals($expectedConfiguration, $actualConfiguration);
613 }
614
615 /**
616 * @test
617 */
618 public function buildTypolinkConfigurationProperlySetsAddQueryString() {
619 $this->uriBuilder->setTargetPageUid(123);
620 $this->uriBuilder->setAddQueryString(TRUE);
621 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'useCacheHash' => 1);
622 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
623 $this->assertEquals($expectedConfiguration, $actualConfiguration);
624 }
625
626 /**
627 * @test
628 */
629 public function buildTypolinkConfigurationProperlySetsAddQueryStringMethod() {
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 $mockDomainObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
643 $mockDomainObject1->_set('uid', '123');
644 $mockDomainObject2 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
645 $mockDomainObject2->_set('uid', '321');
646 $this->uriBuilder->setTargetPageUid(123);
647 $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
648 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject=123&baz[someOtherDomainObject]=321');
649 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
650 $this->assertEquals($expectedConfiguration, $actualConfiguration);
651 }
652
653 /**
654 * @test
655 */
656 public function buildTypolinkConfigurationResolvesPageTypeFromFormat() {
657 $this->uriBuilder->setTargetPageUid(123);
658 $this->uriBuilder->setFormat('txt');
659 $this->mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
660
661 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
662 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
663 ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
664 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
665
666 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
667 ->with('SomeExtensionNameFromRequest', 'txt')
668 ->will($this->returnValue(2));
669
670 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
671 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
672 $this->assertEquals($expectedConfiguration, $actualConfiguration);
673 }
674
675 /**
676 * @test
677 */
678 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured() {
679 $this->uriBuilder->setTargetPageUid(123);
680 $this->uriBuilder->setFormat('txt');
681
682 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
683 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
684 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
685
686 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
687 ->with(NULL, 'txt')
688 ->will($this->returnValue(0));
689
690 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
691 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
692
693 $this->assertEquals($expectedConfiguration, $actualConfiguration);
694 }
695
696 /**
697 * @test
698 */
699 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped() {
700 $this->uriBuilder->setTargetPageUid(123);
701 $this->uriBuilder->setFormat('txt');
702
703 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
704 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
705 ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
706 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
707
708 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
709 ->with(NULL, 'txt')
710 ->will($this->returnValue(0));
711
712 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
713 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
714
715 $this->assertEquals($expectedConfiguration, $actualConfiguration);
716 }
717
718
719 /**
720 * @test
721 */
722 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
723 $this->uriBuilder->setTargetPageUid(123);
724 $this->uriBuilder->setNoCache(TRUE);
725 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
726 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
727 $this->assertEquals($expectedConfiguration, $actualConfiguration);
728 }
729
730 /**
731 * @test
732 */
733 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
734 $this->uriBuilder->setTargetPageUid(123);
735 $this->uriBuilder->setUseCacheHash(FALSE);
736 $expectedConfiguration = array('parameter' => 123);
737 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
738 $this->assertEquals($expectedConfiguration, $actualConfiguration);
739 }
740
741 /**
742 * @test
743 */
744 public function buildTypolinkConfigurationConsidersSection() {
745 $this->uriBuilder->setTargetPageUid(123);
746 $this->uriBuilder->setSection('SomeSection');
747 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
748 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
749 $this->assertEquals($expectedConfiguration, $actualConfiguration);
750 }
751
752 /**
753 * @test
754 */
755 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
756 $this->uriBuilder->setTargetPageUid(123);
757 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
758 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
759 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
760 $this->assertEquals($expectedConfiguration, $actualConfiguration);
761 }
762
763 /**
764 * @test
765 */
766 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
767 $mockDomainObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
768 $mockDomainObject1->_set('uid', '123');
769 $mockDomainObject2 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
770 $mockDomainObject2->_set('uid', '321');
771 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
772 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
773 $this->assertEquals($expectedResult, $actualResult);
774 }
775
776 /**
777 * @test
778 */
779 public function convertDomainObjectsToIdentityArraysConvertsObjectStoragesWithDomainObjects() {
780 $objectStorage = new ObjectStorage();
781 $mockChildObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
782 $mockChildObject1->_set('uid', '123');
783 $objectStorage->attach($mockChildObject1);
784 $expectedResult = array('foo' => array('bar' => 'baz'), 'objectStorage' => array('123'));
785 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'objectStorage' => $objectStorage));
786 $this->assertEquals($expectedResult, $actualResult);
787 }
788
789 /**
790 * @test
791 */
792 public function conversionOfTansientObjectsIsInvoked() {
793 $mockValueObject = new ValueObjectFixture();
794 $mockValueObject->name = 'foo';
795 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|Object $mockUriBuilder */
796 $mockUriBuilder = $this->getAccessibleMock(UriBuilder::class, array('convertTransientObjectToArray'));
797 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
798 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
799 $expectedResult = array('object' => array('foo' => 'bar'));
800 $this->assertEquals($expectedResult, $actualResult);
801 }
802
803 /**
804 * @test
805 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
806 */
807 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
808 $mockEntity = new EntityFixture();
809 $mockEntity->name = 'foo';
810 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|Object $mockUriBuilder */
811 $mockUriBuilder = $this->getAccessibleMock(UriBuilder::class, array('dummy'));
812 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
813 }
814
815 /**
816 * @test
817 */
818 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
819 $mockValueObject = new ValueObjectFixture();
820 $mockValueObject->name = 'foo';
821 $uriBuilder = new UriBuilder();
822 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
823 $expectedResult = array('name' => 'foo', 'object' => NULL, 'uid' => NULL, 'pid' => NULL);
824 $this->assertEquals($expectedResult, $actualResult);
825 }
826
827 /**
828 * @test
829 */
830 public function tansientObjectsWithObjectStorageAreConvertedToAnArrayOfProperties() {
831 $mockValueObject = new ValueObjectFixture();
832 $objectStorage = new ObjectStorage();
833 $mockValueObject->name = 'foo';
834 $mockValueObject2 = new ValueObjectFixture();
835 $mockValueObject2->name = 'bar';
836 $objectStorage->attach($mockValueObject2);
837 $mockValueObject->object = $objectStorage;
838 $uriBuilder = new UriBuilder();
839 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
840 $expectedResult = array(
841 'name' => 'foo',
842 'object' => array(
843 array(
844 'name' => 'bar',
845 'uid' => NULL,
846 'pid' => NULL,
847 'object' => NULL
848 )
849 ),
850 'uid' => NULL,
851 'pid' => NULL
852 );
853 $this->assertEquals($expectedResult, $actualResult);
854 }
855
856 /**
857 * @test
858 */
859 public function tansientObjectsAreRecursivelyConverted() {
860 $mockInnerValueObject2 = new ValueObjectFixture();
861 $mockInnerValueObject2->name = 'foo';
862 $mockInnerValueObject2->uid = 99;
863 $mockInnerValueObject1 = new ValueObjectFixture();
864 $mockInnerValueObject1->object = $mockInnerValueObject2;
865 $mockValueObject = new ValueObjectFixture();
866 $mockValueObject->object = $mockInnerValueObject1;
867 $uriBuilder = new UriBuilder();
868 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
869 $expectedResult = array(
870 'name' => NULL,
871 'object' => array(
872 'name' => NULL,
873 'object' => 99,
874 'uid' => NULL,
875 'pid' => NULL
876 ),
877 'uid' => NULL,
878 'pid' => NULL
879 );
880 $this->assertEquals($expectedResult, $actualResult);
881 }
882
883 /**
884 * @test
885 */
886 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
887 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
888 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
889 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
890 $extensionName = 'ExtensionName';
891 $pluginName = 'PluginName';
892 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
893 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
894 $this->assertEquals($expectedResult, $actualResult);
895 }
896
897 /**
898 * @test
899 */
900 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
901 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
902 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
903 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
904 $extensionName = 'ExtensionName';
905 $pluginName = 'PluginName';
906 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
907 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
908 $this->assertEquals($expectedResult, $actualResult);
909 }
910
911 /**
912 * @test
913 */
914 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
915 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
916 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
917 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
918 $extensionName = 'ExtensionName';
919 $pluginName = 'PluginName';
920 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
921 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
922 $this->assertEquals($expectedResult, $actualResult);
923 }
924
925 /**
926 * @test
927 */
928 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
929 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
930 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
931 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
932 $extensionName = 'ExtensionName';
933 $pluginName = 'PluginName';
934 $expectedResult = array('foo' => 'bar');
935 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
936 $this->assertEquals($expectedResult, $actualResult);
937 }
938
939 /**
940 * @return array
941 */
942 public function convertIteratorToArrayConvertsIteratorsToArrayProvider() {
943 return array(
944 'Extbase ObjectStorage' => array(new ObjectStorage()),
945 'SplObjectStorage' => array(new \SplObjectStorage()),
946 'ArrayIterator' => array(new \ArrayIterator())
947 );
948 }
949
950 /**
951 * @dataProvider convertIteratorToArrayConvertsIteratorsToArrayProvider
952 * @test
953 */
954 public function convertIteratorToArrayConvertsIteratorsToArray($iterator) {
955 $result = $this->uriBuilder->_call('convertIteratorToArray', $iterator);
956 $this->assertTrue(is_array($result));
957 }
958
959 }