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