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