[BUGFIX] Properly resolve link format to page type
[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 = PATH_typo3 . '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 = PATH_typo3 . '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 PATH_typo3 . '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 PATH_typo3 . '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 PATH_typo3 . '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 PATH_typo3 . '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 = PATH_typo3 . '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 = PATH_typo3 . '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 = PATH_typo3 . '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 = PATH_typo3 . '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 $_SERVER['HTTP_HOST'] = 'baseuri';
362 $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
363 $this->mockRequest->expects($this->any())->method('getBaseUri')->will($this->returnValue('http://baseuri'));
364 $this->uriBuilder->setCreateAbsoluteUri(TRUE);
365 $expectedResult = 'http://baseuri/' . TYPO3_mainDir . 'mod.php?M=moduleKey&moduleToken=dummyToken';
366 $actualResult = $this->uriBuilder->buildBackendUri();
367 $this->assertSame($expectedResult, $actualResult);
368 }
369
370 /**
371 * @test
372 */
373 public function buildBackendUriWithQueryStringMethodPostGetMergesParameters() {
374 $_POST = array(
375 'key1' => 'POST1',
376 'key2' => 'POST2',
377 'key3' => array(
378 'key31' => 'POST31',
379 'key32' => 'POST32',
380 'key33' => array(
381 'key331' => 'POST331',
382 'key332' => 'POST332',
383 )
384 ),
385 );
386 $_GET = array(
387 'key2' => 'GET2',
388 'key3' => array(
389 'key32' => 'GET32',
390 'key33' => array(
391 'key331' => 'GET331',
392 )
393 )
394 );
395 $this->uriBuilder->setAddQueryString(TRUE);
396 $this->uriBuilder->setAddQueryStringMethod('POST,GET');
397 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl(PATH_typo3 . 'mod.php?moduleToken=dummyToken&key1=POST1&key2=GET2&key3[key31]=POST31&key3[key32]=GET32&key3[key33][key331]=GET331&key3[key33][key332]=POST332');
398 $actualResult = $this->uriBuilder->buildBackendUri();
399 $this->assertEquals($expectedResult, $actualResult);
400 }
401
402 /**
403 * @test
404 */
405 public function buildBackendUriWithQueryStringMethodGetPostMergesParameters() {
406 $_GET = array(
407 'key1' => 'GET1',
408 'key2' => 'GET2',
409 'key3' => array(
410 'key31' => 'GET31',
411 'key32' => 'GET32',
412 'key33' => array(
413 'key331' => 'GET331',
414 'key332' => 'GET332',
415 )
416 ),
417 );
418 $_POST = array(
419 'key2' => 'POST2',
420 'key3' => array(
421 'key32' => 'POST32',
422 'key33' => array(
423 'key331' => 'POST331',
424 )
425 )
426 );
427 $this->uriBuilder->setAddQueryString(TRUE);
428 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
429 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl(PATH_typo3 . 'mod.php?moduleToken=dummyToken&key1=GET1&key2=POST2&key3[key31]=GET31&key3[key32]=POST32&key3[key33][key331]=POST331&key3[key33][key332]=GET332');
430 $actualResult = $this->uriBuilder->buildBackendUri();
431 $this->assertEquals($expectedResult, $actualResult);
432 }
433
434 /**
435 * Encodes square brackets in URL.
436 *
437 * @param string $string
438 * @return string
439 */
440 private function rawUrlEncodeSquareBracketsInUrl($string) {
441 return str_replace(array('[', ']'), array('%5B', '%5D'), $string);
442 }
443
444 /**
445 * @test
446 */
447 public function buildFrontendUriCreatesTypoLink() {
448 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
449 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
450 $uriBuilder->_set('contentObject', $this->mockContentObject);
451 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
452 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
453 $uriBuilder->buildFrontendUri();
454 }
455
456 /**
457 * @test
458 */
459 public function buildFrontendUriCreatesRelativeUrisByDefault() {
460 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
461 $expectedResult = 'relative/uri';
462 $actualResult = $this->uriBuilder->buildFrontendUri();
463 $this->assertSame($expectedResult, $actualResult);
464 }
465
466 /**
467 * @test
468 */
469 public function buildFrontendUriDoesNotStripLeadingSlashesFromRelativeUris() {
470 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('/relative/uri'));
471 $expectedResult = '/relative/uri';
472 $actualResult = $this->uriBuilder->buildFrontendUri();
473 $this->assertSame($expectedResult, $actualResult);
474 }
475
476 /**
477 * @test
478 */
479 public function buildFrontendUriCreatesAbsoluteUrisIfSpecified() {
480 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
481 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
482 $uriBuilder->_set('contentObject', $this->mockContentObject);
483 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
484 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => TRUE))->will($this->returnValue('http://baseuri/relative/uri'));
485 $uriBuilder->setCreateAbsoluteUri(TRUE);
486 $expectedResult = 'http://baseuri/relative/uri';
487 $actualResult = $uriBuilder->buildFrontendUri();
488 $this->assertSame($expectedResult, $actualResult);
489 }
490
491 /**
492 * @test
493 */
494 public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified() {
495 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
496 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
497 $uriBuilder->_set('contentObject', $this->mockContentObject);
498 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
499 $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'));
500 $uriBuilder->setCreateAbsoluteUri(TRUE);
501 $uriBuilder->setAbsoluteUriScheme('someScheme');
502 $expectedResult = 'http://baseuri/relative/uri';
503 $actualResult = $uriBuilder->buildFrontendUri();
504 $this->assertSame($expectedResult, $actualResult);
505 }
506
507 /**
508 * @test
509 */
510 public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse() {
511 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
512 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
513 $uriBuilder->_set('contentObject', $this->mockContentObject);
514 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
515 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
516 $uriBuilder->setCreateAbsoluteUri(FALSE);
517 $uriBuilder->setAbsoluteUriScheme('someScheme');
518 $expectedResult = 'http://baseuri/relative/uri';
519 $actualResult = $uriBuilder->buildFrontendUri();
520 $this->assertSame($expectedResult, $actualResult);
521 }
522
523 /**
524 * @test
525 */
526 public function resetSetsAllOptionsToTheirDefaultValue() {
527 $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);
528 $this->uriBuilder->reset();
529 $this->assertEquals(array(), $this->uriBuilder->getArguments());
530 $this->assertEquals('', $this->uriBuilder->getSection());
531 $this->assertEquals('', $this->uriBuilder->getFormat());
532 $this->assertEquals(FALSE, $this->uriBuilder->getCreateAbsoluteUri());
533 $this->assertEquals(FALSE, $this->uriBuilder->getAddQueryString());
534 $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
535 $this->assertEquals(NULL, $this->uriBuilder->getAddQueryStringMethod());
536 $this->assertEquals(NULL, $this->uriBuilder->getArgumentPrefix());
537 $this->assertEquals(FALSE, $this->uriBuilder->getLinkAccessRestrictedPages());
538 $this->assertEquals(NULL, $this->uriBuilder->getTargetPageUid());
539 $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
540 $this->assertEquals(FALSE, $this->uriBuilder->getNoCache());
541 $this->assertEquals(TRUE, $this->uriBuilder->getUseCacheHash());
542 }
543
544 /**
545 * @test
546 */
547 public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid() {
548 $GLOBALS['TSFE']->id = 123;
549 $this->uriBuilder->setTargetPageUid(321);
550 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
551 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
552 $this->assertEquals($expectedConfiguration, $actualConfiguration);
553 }
554
555 /**
556 * @test
557 */
558 public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet() {
559 $GLOBALS['TSFE']->id = 123;
560 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
561 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
562 $this->assertEquals($expectedConfiguration, $actualConfiguration);
563 }
564
565 /**
566 * @test
567 */
568 public function buildTypolinkConfigurationProperlySetsAdditionalArguments() {
569 $this->uriBuilder->setTargetPageUid(123);
570 $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
571 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz[extbase]=fluid');
572 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
573 $this->assertEquals($expectedConfiguration, $actualConfiguration);
574 }
575
576 /**
577 * @test
578 */
579 public function buildTypolinkConfigurationProperlySetsAddQueryString() {
580 $this->uriBuilder->setTargetPageUid(123);
581 $this->uriBuilder->setAddQueryString(TRUE);
582 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'useCacheHash' => 1);
583 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
584 $this->assertEquals($expectedConfiguration, $actualConfiguration);
585 }
586
587 /**
588 * @test
589 */
590 public function buildTypolinkConfigurationProperlySetsAddQueryStringMethod() {
591 $this->uriBuilder->setTargetPageUid(123);
592 $this->uriBuilder->setAddQueryString(TRUE);
593 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
594 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'addQueryString.' => array('method' => 'GET,POST'), 'useCacheHash' => 1);
595 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
596 $this->assertEquals($expectedConfiguration, $actualConfiguration);
597 }
598
599 /**
600 * @test
601 */
602 public function buildTypolinkConfigurationConvertsDomainObjects() {
603 $mockDomainObject1 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
604 $mockDomainObject1->_set('uid', '123');
605 $mockDomainObject2 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
606 $mockDomainObject2->_set('uid', '321');
607 $this->uriBuilder->setTargetPageUid(123);
608 $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
609 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject=123&baz[someOtherDomainObject]=321');
610 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
611 $this->assertEquals($expectedConfiguration, $actualConfiguration);
612 }
613
614 /**
615 * @test
616 */
617 public function buildTypolinkConfigurationResolvesPageTypeFromFormat() {
618 $this->uriBuilder->setTargetPageUid(123);
619 $this->uriBuilder->setFormat('txt');
620 $this->mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
621
622 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
623 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
624 ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
625 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
626
627 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
628 ->with('SomeExtensionNameFromRequest', 'txt')
629 ->will($this->returnValue(2));
630
631 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
632 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
633 $this->assertEquals($expectedConfiguration, $actualConfiguration);
634 }
635
636 /**
637 * @test
638 */
639 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured() {
640 $this->uriBuilder->setTargetPageUid(123);
641 $this->uriBuilder->setFormat('txt');
642
643 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
644 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
645 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
646
647 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
648 ->with(NULL, 'txt')
649 ->will($this->returnValue(0));
650
651 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
652 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
653
654 $this->assertEquals($expectedConfiguration, $actualConfiguration);
655 }
656
657 /**
658 * @test
659 */
660 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped() {
661 $this->uriBuilder->setTargetPageUid(123);
662 $this->uriBuilder->setFormat('txt');
663
664 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
665 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
666 ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
667 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
668
669 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
670 ->with(NULL, 'txt')
671 ->will($this->returnValue(0));
672
673 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
674 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
675
676 $this->assertEquals($expectedConfiguration, $actualConfiguration);
677 }
678
679
680 /**
681 * @test
682 */
683 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
684 $this->uriBuilder->setTargetPageUid(123);
685 $this->uriBuilder->setNoCache(TRUE);
686 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
687 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
688 $this->assertEquals($expectedConfiguration, $actualConfiguration);
689 }
690
691 /**
692 * @test
693 */
694 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
695 $this->uriBuilder->setTargetPageUid(123);
696 $this->uriBuilder->setUseCacheHash(FALSE);
697 $expectedConfiguration = array('parameter' => 123);
698 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
699 $this->assertEquals($expectedConfiguration, $actualConfiguration);
700 }
701
702 /**
703 * @test
704 */
705 public function buildTypolinkConfigurationConsidersSection() {
706 $this->uriBuilder->setTargetPageUid(123);
707 $this->uriBuilder->setSection('SomeSection');
708 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
709 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
710 $this->assertEquals($expectedConfiguration, $actualConfiguration);
711 }
712
713 /**
714 * @test
715 */
716 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
717 $this->uriBuilder->setTargetPageUid(123);
718 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
719 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
720 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
721 $this->assertEquals($expectedConfiguration, $actualConfiguration);
722 }
723
724 /**
725 * @test
726 */
727 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
728 $mockDomainObject1 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
729 $mockDomainObject1->_set('uid', '123');
730 $mockDomainObject2 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
731 $mockDomainObject2->_set('uid', '321');
732 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
733 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
734 $this->assertEquals($expectedResult, $actualResult);
735 }
736
737 /**
738 * @test
739 */
740 public function conversionOfTansientObjectsIsInvoked() {
741 $className = $this->getUniqueId('FixturesObject_');
742 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
743 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
744 $mockValueObject = new $classNameWithNS();
745 $mockValueObject->name = 'foo';
746 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('convertTransientObjectToArray'));
747 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
748 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
749 $expectedResult = array('object' => array('foo' => 'bar'));
750 $this->assertEquals($expectedResult, $actualResult);
751 }
752
753 /**
754 * @test
755 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
756 */
757 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
758 $className = $this->getUniqueId('FixturesObject_');
759 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
760 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' { public $name; public $uid; }');
761 $mockEntity = new $classNameWithNS();
762 $mockEntity->name = 'foo';
763 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
764 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
765 }
766
767 /**
768 * @test
769 */
770 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
771 $className = $this->getUniqueId('FixturesObject_');
772 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
773 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $name; public $uid; }');
774 $mockValueObject = new $classNameWithNS();
775 $mockValueObject->name = 'foo';
776 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
777 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
778 $expectedResult = array('name' => 'foo', 'uid' => NULL, 'pid' => NULL);
779 $this->assertEquals($expectedResult, $actualResult);
780 }
781
782 /**
783 * @test
784 */
785 public function tansientObjectsAreRecursivelyConverted() {
786 $className = $this->getUniqueId('FixturesObject_');
787 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
788 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $name; public $uid; }');
789 $mockInnerValueObject2 = new $classNameWithNS();
790 $mockInnerValueObject2->name = 'foo';
791 $mockInnerValueObject2->uid = 99;
792 $className = $this->getUniqueId('FixturesObject_');
793 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
794 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $object; public $uid; }');
795 $mockInnerValueObject1 = new $classNameWithNS();
796 $mockInnerValueObject1->object = $mockInnerValueObject2;
797 $className = $this->getUniqueId('FixturesObject_');
798 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
799 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $object; public $uid; }');
800 $mockValueObject = new $classNameWithNS();
801 $mockValueObject->object = $mockInnerValueObject1;
802 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
803 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
804 $expectedResult = array(
805 'object' => array(
806 'object' => 99,
807 'uid' => NULL,
808 'pid' => NULL
809 ),
810 'uid' => NULL,
811 'pid' => NULL
812 );
813 $this->assertEquals($expectedResult, $actualResult);
814 }
815
816 /**
817 * @test
818 */
819 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
820 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
821 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
822 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
823 $extensionName = 'ExtensionName';
824 $pluginName = 'PluginName';
825 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
826 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
827 $this->assertEquals($expectedResult, $actualResult);
828 }
829
830 /**
831 * @test
832 */
833 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
834 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
835 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
836 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
837 $extensionName = 'ExtensionName';
838 $pluginName = 'PluginName';
839 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
840 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
841 $this->assertEquals($expectedResult, $actualResult);
842 }
843
844 /**
845 * @test
846 */
847 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
848 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
849 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
850 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
851 $extensionName = 'ExtensionName';
852 $pluginName = 'PluginName';
853 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
854 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
855 $this->assertEquals($expectedResult, $actualResult);
856 }
857
858 /**
859 * @test
860 */
861 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
862 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
863 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
864 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
865 $extensionName = 'ExtensionName';
866 $pluginName = 'PluginName';
867 $expectedResult = array('foo' => 'bar');
868 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
869 $this->assertEquals($expectedResult, $actualResult);
870 }
871
872 }