[CLEANUP] Use comments not PHPDoc style comments in copyright block
[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 public 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 * @test
155 */
156 public function uriForDoesNotDisableCacheHashForNonCacheableActions() {
157 $this->mockExtensionService->expects($this->any())->method('isActionCacheable')->will($this->returnValue(FALSE));
158 $this->uriBuilder->uriFor('someNonCacheableAction', array(), 'SomeController', 'SomeExtension');
159 $this->assertTrue($this->uriBuilder->getUseCacheHash());
160 }
161
162 /**
163 * @test
164 */
165 public function buildBackendUriKeepsQueryParametersIfAddQueryStringIsSet() {
166 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
167 $_POST = array();
168 $_POST['foo2'] = 'bar2';
169 $this->uriBuilder->setAddQueryString(TRUE);
170 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
171 $expectedResult = 'mod.php?M=moduleKey&moduleToken=dummyToken&id=pageId&foo=bar&foo2=bar2';
172 $actualResult = $this->uriBuilder->buildBackendUri();
173 $this->assertEquals($expectedResult, $actualResult);
174 }
175
176 /**
177 * @test
178 */
179 public function buildBackendUriKeepsQueryParametersIfAddQueryStringMethodIsNotSet() {
180 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
181 $_POST = array();
182 $_POST['foo2'] = 'bar2';
183 $this->uriBuilder->setAddQueryString(TRUE);
184 $this->uriBuilder->setAddQueryStringMethod(NULL);
185 $expectedResult = 'mod.php?M=moduleKey&moduleToken=dummyToken&id=pageId&foo=bar';
186 $actualResult = $this->uriBuilder->buildBackendUri();
187 $this->assertEquals($expectedResult, $actualResult);
188 }
189
190 /**
191 * return array
192 */
193 public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider() {
194 return array(
195 'Arguments to be excluded in the beginning' => array(
196 array(
197 'M' => 'moduleKey',
198 'id' => 'pageId',
199 'foo' => 'bar'
200 ),
201 array(
202 'foo2' => 'bar2'
203 ),
204 array(
205 'M',
206 'id'
207 ),
208 'mod.php?moduleToken=dummyToken&foo=bar&foo2=bar2'
209 ),
210 'Arguments to be excluded in the end' => array(
211 array(
212 'foo' => 'bar',
213 'id' => 'pageId',
214 'M' => 'moduleKey'
215 ),
216 array(
217 'foo2' => 'bar2'
218 ),
219 array(
220 'M',
221 'id'
222 ),
223 'mod.php?moduleToken=dummyToken&foo=bar&foo2=bar2'
224 ),
225 'Arguments in nested array to be excluded' => array(
226 array(
227 'tx_foo' => array(
228 'bar' => 'baz'
229 ),
230 'id' => 'pageId',
231 'M' => 'moduleKey'
232 ),
233 array(
234 'foo2' => 'bar2'
235 ),
236 array(
237 'id',
238 'tx_foo[bar]'
239 ),
240 'mod.php?M=moduleKey&moduleToken=dummyToken&foo2=bar2'
241 ),
242 'Arguments in multidimensional array to be excluded' => array(
243 array(
244 'tx_foo' => array(
245 'bar' => array(
246 'baz' => 'bay'
247 )
248 ),
249 'id' => 'pageId',
250 'M' => 'moduleKey'
251 ),
252 array(
253 'foo2' => 'bar2'
254 ),
255 array(
256 'id',
257 'tx_foo[bar][baz]'
258 ),
259 'mod.php?M=moduleKey&moduleToken=dummyToken&foo2=bar2'
260 ),
261 );
262 }
263
264 /**
265 * @test
266 * @dataProvider buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider
267 */
268 public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSet(array $parameters, array $postArguments, array $excluded, $expected) {
269 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset($parameters);
270 $_POST = $postArguments;
271 $this->uriBuilder->setAddQueryString(TRUE);
272 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
273 $this->uriBuilder->setArgumentsToBeExcludedFromQueryString($excluded);
274 $actualResult = $this->uriBuilder->buildBackendUri();
275 $this->assertEquals($expected, $actualResult);
276 }
277
278 /**
279 * @test
280 */
281 public function buildBackendUriKeepsModuleQueryParametersIfAddQueryStringIsNotSet() {
282 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
283 $expectedResult = 'mod.php?M=moduleKey&moduleToken=dummyToken&id=pageId';
284 $actualResult = $this->uriBuilder->buildBackendUri();
285 $this->assertEquals($expectedResult, $actualResult);
286 }
287
288 /**
289 * @test
290 */
291 public function buildBackendUriMergesAndOverrulesQueryParametersWithArguments() {
292 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
293 $this->uriBuilder->setArguments(array('M' => 'overwrittenModuleKey', 'somePrefix' => array('bar' => 'baz')));
294 $expectedResult = 'mod.php?M=overwrittenModuleKey&moduleToken=dummyToken&id=pageId&somePrefix%5Bbar%5D=baz';
295 $actualResult = $this->uriBuilder->buildBackendUri();
296 $this->assertEquals($expectedResult, $actualResult);
297 }
298
299 /**
300 * @test
301 */
302 public function buildBackendUriConvertsDomainObjectsAfterArgumentsHaveBeenMerged() {
303 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey'));
304 $mockDomainObject = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
305 $mockDomainObject->_set('uid', '123');
306 $this->uriBuilder->setArguments(array('somePrefix' => array('someDomainObject' => $mockDomainObject)));
307 $expectedResult = 'mod.php?M=moduleKey&moduleToken=dummyToken&somePrefix%5BsomeDomainObject%5D=123';
308 $actualResult = $this->uriBuilder->buildBackendUri();
309 $this->assertEquals($expectedResult, $actualResult);
310 }
311
312 /**
313 * @test
314 */
315 public function buildBackendUriRespectsSection() {
316 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey'));
317 $this->uriBuilder->setSection('someSection');
318 $expectedResult = 'mod.php?M=moduleKey&moduleToken=dummyToken#someSection';
319 $actualResult = $this->uriBuilder->buildBackendUri();
320 $this->assertEquals($expectedResult, $actualResult);
321 }
322
323 /**
324 * @test
325 */
326 public function buildBackendUriCreatesAbsoluteUrisIfSpecified() {
327 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey'));
328 $this->mockRequest->expects($this->any())->method('getBaseUri')->will($this->returnValue('http://baseuri/' . TYPO3_mainDir));
329 $this->uriBuilder->setCreateAbsoluteUri(TRUE);
330 $expectedResult = 'http://baseuri/' . TYPO3_mainDir . 'mod.php?M=moduleKey&moduleToken=dummyToken';
331 $actualResult = $this->uriBuilder->buildBackendUri();
332 $this->assertSame($expectedResult, $actualResult);
333 }
334
335 /**
336 * @test
337 */
338 public function buildBackendUriWithQueryStringMethodPostGetMergesParameters() {
339 $_POST = array(
340 'key1' => 'POST1',
341 'key2' => 'POST2',
342 'key3' => array(
343 'key31' => 'POST31',
344 'key32' => 'POST32',
345 'key33' => array(
346 'key331' => 'POST331',
347 'key332' => 'POST332',
348 )
349 ),
350 );
351 $_GET = array(
352 'key2' => 'GET2',
353 'key3' => array(
354 'key32' => 'GET32',
355 'key33' => array(
356 'key331' => 'GET331',
357 )
358 )
359 );
360 $this->uriBuilder->setAddQueryString(TRUE);
361 $this->uriBuilder->setAddQueryStringMethod('POST,GET');
362 $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');
363 $actualResult = $this->uriBuilder->buildBackendUri();
364 $this->assertEquals($expectedResult, $actualResult);
365 }
366
367 /**
368 * @test
369 */
370 public function buildBackendUriWithQueryStringMethodGetPostMergesParameters() {
371 $_GET = array(
372 'key1' => 'GET1',
373 'key2' => 'GET2',
374 'key3' => array(
375 'key31' => 'GET31',
376 'key32' => 'GET32',
377 'key33' => array(
378 'key331' => 'GET331',
379 'key332' => 'GET332',
380 )
381 ),
382 );
383 $_POST = array(
384 'key2' => 'POST2',
385 'key3' => array(
386 'key32' => 'POST32',
387 'key33' => array(
388 'key331' => 'POST331',
389 )
390 )
391 );
392 $this->uriBuilder->setAddQueryString(TRUE);
393 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
394 $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');
395 $actualResult = $this->uriBuilder->buildBackendUri();
396 $this->assertEquals($expectedResult, $actualResult);
397 }
398
399 /**
400 * Encodes square brackets in URL.
401 *
402 * @param string $string
403 * @return string
404 */
405 private function rawUrlEncodeSquareBracketsInUrl($string) {
406 return str_replace(array('[', ']'), array('%5B', '%5D'), $string);
407 }
408
409 /**
410 * @test
411 */
412 public function buildFrontendUriCreatesTypoLink() {
413 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
414 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
415 $uriBuilder->_set('contentObject', $this->mockContentObject);
416 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
417 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
418 $uriBuilder->buildFrontendUri();
419 }
420
421 /**
422 * @test
423 */
424 public function buildFrontendUriCreatesRelativeUrisByDefault() {
425 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
426 $expectedResult = 'relative/uri';
427 $actualResult = $this->uriBuilder->buildFrontendUri();
428 $this->assertSame($expectedResult, $actualResult);
429 }
430
431 /**
432 * @test
433 */
434 public function buildFrontendUriDoesNotStripLeadingSlashesFromRelativeUris() {
435 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('/relative/uri'));
436 $expectedResult = '/relative/uri';
437 $actualResult = $this->uriBuilder->buildFrontendUri();
438 $this->assertSame($expectedResult, $actualResult);
439 }
440
441 /**
442 * @test
443 */
444 public function buildFrontendUriCreatesAbsoluteUrisIfSpecified() {
445 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
446 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
447 $uriBuilder->_set('contentObject', $this->mockContentObject);
448 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
449 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => TRUE))->will($this->returnValue('http://baseuri/relative/uri'));
450 $uriBuilder->setCreateAbsoluteUri(TRUE);
451 $expectedResult = 'http://baseuri/relative/uri';
452 $actualResult = $uriBuilder->buildFrontendUri();
453 $this->assertSame($expectedResult, $actualResult);
454 }
455
456 /**
457 * @test
458 */
459 public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified() {
460 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
461 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
462 $uriBuilder->_set('contentObject', $this->mockContentObject);
463 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
464 $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'));
465 $uriBuilder->setCreateAbsoluteUri(TRUE);
466 $uriBuilder->setAbsoluteUriScheme('someScheme');
467 $expectedResult = 'http://baseuri/relative/uri';
468 $actualResult = $uriBuilder->buildFrontendUri();
469 $this->assertSame($expectedResult, $actualResult);
470 }
471
472 /**
473 * @test
474 */
475 public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse() {
476 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
477 $uriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('buildTypolinkConfiguration'));
478 $uriBuilder->_set('contentObject', $this->mockContentObject);
479 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
480 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
481 $uriBuilder->setCreateAbsoluteUri(FALSE);
482 $uriBuilder->setAbsoluteUriScheme('someScheme');
483 $expectedResult = 'http://baseuri/relative/uri';
484 $actualResult = $uriBuilder->buildFrontendUri();
485 $this->assertSame($expectedResult, $actualResult);
486 }
487
488 /**
489 * @test
490 */
491 public function resetSetsAllOptionsToTheirDefaultValue() {
492 $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);
493 $this->uriBuilder->reset();
494 $this->assertEquals(array(), $this->uriBuilder->getArguments());
495 $this->assertEquals('', $this->uriBuilder->getSection());
496 $this->assertEquals('', $this->uriBuilder->getFormat());
497 $this->assertEquals(FALSE, $this->uriBuilder->getCreateAbsoluteUri());
498 $this->assertEquals(FALSE, $this->uriBuilder->getAddQueryString());
499 $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
500 $this->assertEquals(NULL, $this->uriBuilder->getAddQueryStringMethod());
501 $this->assertEquals(NULL, $this->uriBuilder->getArgumentPrefix());
502 $this->assertEquals(FALSE, $this->uriBuilder->getLinkAccessRestrictedPages());
503 $this->assertEquals(NULL, $this->uriBuilder->getTargetPageUid());
504 $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
505 $this->assertEquals(FALSE, $this->uriBuilder->getNoCache());
506 $this->assertEquals(TRUE, $this->uriBuilder->getUseCacheHash());
507 }
508
509 /**
510 * @test
511 */
512 public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid() {
513 $GLOBALS['TSFE']->id = 123;
514 $this->uriBuilder->setTargetPageUid(321);
515 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
516 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
517 $this->assertEquals($expectedConfiguration, $actualConfiguration);
518 }
519
520 /**
521 * @test
522 */
523 public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet() {
524 $GLOBALS['TSFE']->id = 123;
525 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
526 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
527 $this->assertEquals($expectedConfiguration, $actualConfiguration);
528 }
529
530 /**
531 * @test
532 */
533 public function buildTypolinkConfigurationProperlySetsAdditionalArguments() {
534 $this->uriBuilder->setTargetPageUid(123);
535 $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
536 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz[extbase]=fluid');
537 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
538 $this->assertEquals($expectedConfiguration, $actualConfiguration);
539 }
540
541 /**
542 * @test
543 */
544 public function buildTypolinkConfigurationProperlySetsAddQueryString() {
545 $this->uriBuilder->setTargetPageUid(123);
546 $this->uriBuilder->setAddQueryString(TRUE);
547 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'useCacheHash' => 1);
548 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
549 $this->assertEquals($expectedConfiguration, $actualConfiguration);
550 }
551
552 /**
553 * @test
554 */
555 public function buildTypolinkConfigurationProperlySetsAddQueryStringMethod() {
556 $this->uriBuilder->setTargetPageUid(123);
557 $this->uriBuilder->setAddQueryString(TRUE);
558 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
559 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'addQueryString.' => array('method' => 'GET,POST'), 'useCacheHash' => 1);
560 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
561 $this->assertEquals($expectedConfiguration, $actualConfiguration);
562 }
563
564 /**
565 * @test
566 */
567 public function buildTypolinkConfigurationConvertsDomainObjects() {
568 $mockDomainObject1 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
569 $mockDomainObject1->_set('uid', '123');
570 $mockDomainObject2 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
571 $mockDomainObject2->_set('uid', '321');
572 $this->uriBuilder->setTargetPageUid(123);
573 $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
574 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject=123&baz[someOtherDomainObject]=321');
575 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
576 $this->assertEquals($expectedConfiguration, $actualConfiguration);
577 }
578
579 /**
580 * @test
581 */
582 public function buildTypolinkConfigurationResolvesPageTypeFromFormat() {
583 $this->uriBuilder->setTargetPageUid(123);
584 $this->uriBuilder->setFormat('txt');
585
586 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
587 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
588 ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
589 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
590
591 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
592 ->with(NULL, 'txt')
593 ->will($this->returnValue(2));
594
595 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
596 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
597 $this->assertEquals($expectedConfiguration, $actualConfiguration);
598 }
599
600 /**
601 * @test
602 */
603 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured() {
604 $this->uriBuilder->setTargetPageUid(123);
605 $this->uriBuilder->setFormat('txt');
606
607 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
608 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
609 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
610
611 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
612 ->with(NULL, 'txt')
613 ->will($this->returnValue(0));
614
615 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
616 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
617
618 $this->assertEquals($expectedConfiguration, $actualConfiguration);
619 }
620
621 /**
622 * @test
623 */
624 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped() {
625 $this->uriBuilder->setTargetPageUid(123);
626 $this->uriBuilder->setFormat('txt');
627
628 $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
629 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
630 ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
631 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
632
633 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
634 ->with(NULL, 'txt')
635 ->will($this->returnValue(0));
636
637 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
638 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
639
640 $this->assertEquals($expectedConfiguration, $actualConfiguration);
641 }
642
643
644 /**
645 * @test
646 */
647 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
648 $this->uriBuilder->setTargetPageUid(123);
649 $this->uriBuilder->setNoCache(TRUE);
650 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
651 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
652 $this->assertEquals($expectedConfiguration, $actualConfiguration);
653 }
654
655 /**
656 * @test
657 */
658 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
659 $this->uriBuilder->setTargetPageUid(123);
660 $this->uriBuilder->setUseCacheHash(FALSE);
661 $expectedConfiguration = array('parameter' => 123);
662 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
663 $this->assertEquals($expectedConfiguration, $actualConfiguration);
664 }
665
666 /**
667 * @test
668 */
669 public function buildTypolinkConfigurationConsidersSection() {
670 $this->uriBuilder->setTargetPageUid(123);
671 $this->uriBuilder->setSection('SomeSection');
672 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
673 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
674 $this->assertEquals($expectedConfiguration, $actualConfiguration);
675 }
676
677 /**
678 * @test
679 */
680 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
681 $this->uriBuilder->setTargetPageUid(123);
682 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
683 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
684 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
685 $this->assertEquals($expectedConfiguration, $actualConfiguration);
686 }
687
688 /**
689 * @test
690 */
691 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
692 $mockDomainObject1 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
693 $mockDomainObject1->_set('uid', '123');
694 $mockDomainObject2 = $this->getAccessibleMock(\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, array('dummy'));
695 $mockDomainObject2->_set('uid', '321');
696 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
697 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
698 $this->assertEquals($expectedResult, $actualResult);
699 }
700
701 /**
702 * @test
703 */
704 public function conversionOfTansientObjectsIsInvoked() {
705 $className = uniqid('FixturesObject_');
706 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
707 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
708 $mockValueObject = new $classNameWithNS();
709 $mockValueObject->name = 'foo';
710 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('convertTransientObjectToArray'));
711 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
712 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
713 $expectedResult = array('object' => array('foo' => 'bar'));
714 $this->assertEquals($expectedResult, $actualResult);
715 }
716
717 /**
718 * @test
719 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
720 */
721 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
722 $className = uniqid('FixturesObject_');
723 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
724 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class . ' { public $name; public $uid; }');
725 $mockEntity = new $classNameWithNS();
726 $mockEntity->name = 'foo';
727 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
728 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
729 }
730
731 /**
732 * @test
733 */
734 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
735 $className = uniqid('FixturesObject_');
736 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
737 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $name; public $uid; }');
738 $mockValueObject = new $classNameWithNS();
739 $mockValueObject->name = 'foo';
740 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
741 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
742 $expectedResult = array('name' => 'foo', 'uid' => NULL, 'pid' => NULL);
743 $this->assertEquals($expectedResult, $actualResult);
744 }
745
746 /**
747 * @test
748 */
749 public function tansientObjectsAreRecursivelyConverted() {
750 $className = uniqid('FixturesObject_');
751 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
752 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $name; public $uid; }');
753 $mockInnerValueObject2 = new $classNameWithNS();
754 $mockInnerValueObject2->name = 'foo';
755 $mockInnerValueObject2->uid = 99;
756 $className = uniqid('FixturesObject_');
757 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
758 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $object; public $uid; }');
759 $mockInnerValueObject1 = new $classNameWithNS();
760 $mockInnerValueObject1->object = $mockInnerValueObject2;
761 $className = uniqid('FixturesObject_');
762 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
763 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\' . \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class . ' { public $object; public $uid; }');
764 $mockValueObject = new $classNameWithNS();
765 $mockValueObject->object = $mockInnerValueObject1;
766 $mockUriBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class, array('dummy'));
767 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
768 $expectedResult = array(
769 'object' => array(
770 'object' => 99,
771 'uid' => NULL,
772 'pid' => NULL
773 ),
774 'uid' => NULL,
775 'pid' => NULL
776 );
777 $this->assertEquals($expectedResult, $actualResult);
778 }
779
780 /**
781 * @test
782 */
783 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
784 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
785 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
786 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
787 $extensionName = 'ExtensionName';
788 $pluginName = 'PluginName';
789 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
790 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
791 $this->assertEquals($expectedResult, $actualResult);
792 }
793
794 /**
795 * @test
796 */
797 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
798 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
799 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
800 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
801 $extensionName = 'ExtensionName';
802 $pluginName = 'PluginName';
803 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
804 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
805 $this->assertEquals($expectedResult, $actualResult);
806 }
807
808 /**
809 * @test
810 */
811 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
812 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
813 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
814 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
815 $extensionName = 'ExtensionName';
816 $pluginName = 'PluginName';
817 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
818 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
819 $this->assertEquals($expectedResult, $actualResult);
820 }
821
822 /**
823 * @test
824 */
825 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
826 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
827 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
828 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
829 $extensionName = 'ExtensionName';
830 $pluginName = 'PluginName';
831 $expectedResult = array('foo' => 'bar');
832 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
833 $this->assertEquals($expectedResult, $actualResult);
834 }
835 }