[TASK] Make Backend Module URLs absolute
[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
621 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
622 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
623 ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
624 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
625
626 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
627 ->with(NULL, 'txt')
628 ->will($this->returnValue(2));
629
630 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
631 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
632 $this->assertEquals($expectedConfiguration, $actualConfiguration);
633 }
634
635 /**
636 * @test
637 */
638 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured() {
639 $this->uriBuilder->setTargetPageUid(123);
640 $this->uriBuilder->setFormat('txt');
641
642 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
643 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
644 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
645
646 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
647 ->with(NULL, 'txt')
648 ->will($this->returnValue(0));
649
650 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
651 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
652
653 $this->assertEquals($expectedConfiguration, $actualConfiguration);
654 }
655
656 /**
657 * @test
658 */
659 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped() {
660 $this->uriBuilder->setTargetPageUid(123);
661 $this->uriBuilder->setFormat('txt');
662
663 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
664 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
665 ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
666 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
667
668 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
669 ->with(NULL, 'txt')
670 ->will($this->returnValue(0));
671
672 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
673 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
674
675 $this->assertEquals($expectedConfiguration, $actualConfiguration);
676 }
677
678
679 /**
680 * @test
681 */
682 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
683 $this->uriBuilder->setTargetPageUid(123);
684 $this->uriBuilder->setNoCache(TRUE);
685 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
686 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
687 $this->assertEquals($expectedConfiguration, $actualConfiguration);
688 }
689
690 /**
691 * @test
692 */
693 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
694 $this->uriBuilder->setTargetPageUid(123);
695 $this->uriBuilder->setUseCacheHash(FALSE);
696 $expectedConfiguration = array('parameter' => 123);
697 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
698 $this->assertEquals($expectedConfiguration, $actualConfiguration);
699 }
700
701 /**
702 * @test
703 */
704 public function buildTypolinkConfigurationConsidersSection() {
705 $this->uriBuilder->setTargetPageUid(123);
706 $this->uriBuilder->setSection('SomeSection');
707 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
708 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
709 $this->assertEquals($expectedConfiguration, $actualConfiguration);
710 }
711
712 /**
713 * @test
714 */
715 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
716 $this->uriBuilder->setTargetPageUid(123);
717 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
718 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
719 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
720 $this->assertEquals($expectedConfiguration, $actualConfiguration);
721 }
722
723 /**
724 * @test
725 */
726 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
727 $mockDomainObject1 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
728 $mockDomainObject1->_set('uid', '123');
729 $mockDomainObject2 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
730 $mockDomainObject2->_set('uid', '321');
731 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
732 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
733 $this->assertEquals($expectedResult, $actualResult);
734 }
735
736 /**
737 * @test
738 */
739 public function conversionOfTansientObjectsIsInvoked() {
740 $className = $this->getUniqueId('FixturesObject_');
741 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
742 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
743 $mockValueObject = new $classNameWithNS();
744 $mockValueObject->name = 'foo';
745 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('convertTransientObjectToArray'));
746 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
747 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
748 $expectedResult = array('object' => array('foo' => 'bar'));
749 $this->assertEquals($expectedResult, $actualResult);
750 }
751
752 /**
753 * @test
754 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
755 */
756 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
757 $className = $this->getUniqueId('FixturesObject_');
758 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
759 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' { public $name; public $uid; }');
760 $mockEntity = new $classNameWithNS();
761 $mockEntity->name = 'foo';
762 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
763 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
764 }
765
766 /**
767 * @test
768 */
769 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
770 $className = $this->getUniqueId('FixturesObject_');
771 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
772 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $name; public $uid; }');
773 $mockValueObject = new $classNameWithNS();
774 $mockValueObject->name = 'foo';
775 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
776 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
777 $expectedResult = array('name' => 'foo', 'uid' => NULL, 'pid' => NULL);
778 $this->assertEquals($expectedResult, $actualResult);
779 }
780
781 /**
782 * @test
783 */
784 public function tansientObjectsAreRecursivelyConverted() {
785 $className = $this->getUniqueId('FixturesObject_');
786 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
787 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $name; public $uid; }');
788 $mockInnerValueObject2 = new $classNameWithNS();
789 $mockInnerValueObject2->name = 'foo';
790 $mockInnerValueObject2->uid = 99;
791 $className = $this->getUniqueId('FixturesObject_');
792 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
793 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $object; public $uid; }');
794 $mockInnerValueObject1 = new $classNameWithNS();
795 $mockInnerValueObject1->object = $mockInnerValueObject2;
796 $className = $this->getUniqueId('FixturesObject_');
797 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
798 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $object; public $uid; }');
799 $mockValueObject = new $classNameWithNS();
800 $mockValueObject->object = $mockInnerValueObject1;
801 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
802 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
803 $expectedResult = array(
804 'object' => array(
805 'object' => 99,
806 'uid' => NULL,
807 'pid' => NULL
808 ),
809 'uid' => NULL,
810 'pid' => NULL
811 );
812 $this->assertEquals($expectedResult, $actualResult);
813 }
814
815 /**
816 * @test
817 */
818 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
819 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
820 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
821 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
822 $extensionName = 'ExtensionName';
823 $pluginName = 'PluginName';
824 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
825 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
826 $this->assertEquals($expectedResult, $actualResult);
827 }
828
829 /**
830 * @test
831 */
832 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
833 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
834 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
835 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
836 $extensionName = 'ExtensionName';
837 $pluginName = 'PluginName';
838 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
839 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
840 $this->assertEquals($expectedResult, $actualResult);
841 }
842
843 /**
844 * @test
845 */
846 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
847 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
848 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
849 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
850 $extensionName = 'ExtensionName';
851 $pluginName = 'PluginName';
852 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
853 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
854 $this->assertEquals($expectedResult, $actualResult);
855 }
856
857 /**
858 * @test
859 */
860 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
861 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
862 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
863 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
864 $extensionName = 'ExtensionName';
865 $pluginName = 'PluginName';
866 $expectedResult = array('foo' => 'bar');
867 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
868 $this->assertEquals($expectedResult, $actualResult);
869 }
870
871 }