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