[BUGFIX] Fix GET parameter order in unit tests
[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 * Uri Builder Test Class
33 */
34 class UriBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
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->objectManager->get('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?id=pageId&foo=bar&foo2=bar2&M=moduleKey&moduleToken=dummyToken';
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?id=pageId&foo=bar&M=moduleKey&moduleToken=dummyToken';
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?foo=bar&foo2=bar2&moduleToken=dummyToken'
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?foo=bar&foo2=bar2&moduleToken=dummyToken'
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?foo2=bar2&M=moduleKey&moduleToken=dummyToken'
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?foo2=bar2&M=moduleKey&moduleToken=dummyToken'
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?id=pageId&M=moduleKey&moduleToken=dummyToken';
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?id=pageId&somePrefix%5Bbar%5D=baz&M=overwrittenModuleKey&moduleToken=dummyToken';
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?somePrefix%5BsomeDomainObject%5D=123&M=moduleKey&moduleToken=dummyToken';
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 buildFrontendUriCreatesTypoLink() {
353 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
354 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
355 $uriBuilder->_set('contentObject', $this->mockContentObject);
356 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
357 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
358 $uriBuilder->buildFrontendUri();
359 }
360
361 /**
362 * @test
363 */
364 public function buildFrontendUriCreatesRelativeUrisByDefault() {
365 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
366 $expectedResult = 'relative/uri';
367 $actualResult = $this->uriBuilder->buildFrontendUri();
368 $this->assertSame($expectedResult, $actualResult);
369 }
370
371 /**
372 * @test
373 */
374 public function buildFrontendUriDoesNotStripLeadingSlashesFromRelativeUris() {
375 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('/relative/uri'));
376 $expectedResult = '/relative/uri';
377 $actualResult = $this->uriBuilder->buildFrontendUri();
378 $this->assertSame($expectedResult, $actualResult);
379 }
380
381 /**
382 * @test
383 */
384 public function buildFrontendUriCreatesAbsoluteUrisIfSpecified() {
385 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
386 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
387 $uriBuilder->_set('contentObject', $this->mockContentObject);
388 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
389 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => TRUE))->will($this->returnValue('http://baseuri/relative/uri'));
390 $uriBuilder->setCreateAbsoluteUri(TRUE);
391 $expectedResult = 'http://baseuri/relative/uri';
392 $actualResult = $uriBuilder->buildFrontendUri();
393 $this->assertSame($expectedResult, $actualResult);
394 }
395
396 /**
397 * @test
398 */
399 public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified() {
400 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
401 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
402 $uriBuilder->_set('contentObject', $this->mockContentObject);
403 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
404 $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'));
405 $uriBuilder->setCreateAbsoluteUri(TRUE);
406 $uriBuilder->setAbsoluteUriScheme('someScheme');
407 $expectedResult = 'http://baseuri/relative/uri';
408 $actualResult = $uriBuilder->buildFrontendUri();
409 $this->assertSame($expectedResult, $actualResult);
410 }
411
412 /**
413 * @test
414 */
415 public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse() {
416 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
417 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
418 $uriBuilder->_set('contentObject', $this->mockContentObject);
419 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
420 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
421 $uriBuilder->setCreateAbsoluteUri(FALSE);
422 $uriBuilder->setAbsoluteUriScheme('someScheme');
423 $expectedResult = 'http://baseuri/relative/uri';
424 $actualResult = $uriBuilder->buildFrontendUri();
425 $this->assertSame($expectedResult, $actualResult);
426 }
427
428 /**
429 * @test
430 */
431 public function resetSetsAllOptionsToTheirDefaultValue() {
432 $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);
433 $this->uriBuilder->reset();
434 $this->assertEquals(array(), $this->uriBuilder->getArguments());
435 $this->assertEquals('', $this->uriBuilder->getSection());
436 $this->assertEquals('', $this->uriBuilder->getFormat());
437 $this->assertEquals(FALSE, $this->uriBuilder->getCreateAbsoluteUri());
438 $this->assertEquals(FALSE, $this->uriBuilder->getAddQueryString());
439 $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
440 $this->assertEquals(NULL, $this->uriBuilder->getAddQueryStringMethod());
441 $this->assertEquals(NULL, $this->uriBuilder->getArgumentPrefix());
442 $this->assertEquals(FALSE, $this->uriBuilder->getLinkAccessRestrictedPages());
443 $this->assertEquals(NULL, $this->uriBuilder->getTargetPageUid());
444 $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
445 $this->assertEquals(FALSE, $this->uriBuilder->getNoCache());
446 $this->assertEquals(TRUE, $this->uriBuilder->getUseCacheHash());
447 }
448
449 /**
450 * @test
451 */
452 public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid() {
453 $GLOBALS['TSFE']->id = 123;
454 $this->uriBuilder->setTargetPageUid(321);
455 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
456 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
457 $this->assertEquals($expectedConfiguration, $actualConfiguration);
458 }
459
460 /**
461 * @test
462 */
463 public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet() {
464 $GLOBALS['TSFE']->id = 123;
465 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
466 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
467 $this->assertEquals($expectedConfiguration, $actualConfiguration);
468 }
469
470 /**
471 * @test
472 */
473 public function buildTypolinkConfigurationProperlySetsAdditionalArguments() {
474 $this->uriBuilder->setTargetPageUid(123);
475 $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
476 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz[extbase]=fluid');
477 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
478 $this->assertEquals($expectedConfiguration, $actualConfiguration);
479 }
480
481 /**
482 * @test
483 */
484 public function buildTypolinkConfigurationProperlySetsAddQueryString() {
485 $this->uriBuilder->setTargetPageUid(123);
486 $this->uriBuilder->setAddQueryString(TRUE);
487 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'useCacheHash' => 1);
488 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
489 $this->assertEquals($expectedConfiguration, $actualConfiguration);
490 }
491
492 /**
493 * @test
494 */
495 public function buildTypolinkConfigurationProperlySetsAddQueryStringMethod() {
496 $this->uriBuilder->setTargetPageUid(123);
497 $this->uriBuilder->setAddQueryString(TRUE);
498 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
499 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'addQueryString.' => array('method' => 'GET,POST'), 'useCacheHash' => 1);
500 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
501 $this->assertEquals($expectedConfiguration, $actualConfiguration);
502 }
503
504 /**
505 * @test
506 */
507 public function buildTypolinkConfigurationConvertsDomainObjects() {
508 $mockDomainObject1 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
509 $mockDomainObject1->_set('uid', '123');
510 $mockDomainObject2 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
511 $mockDomainObject2->_set('uid', '321');
512 $this->uriBuilder->setTargetPageUid(123);
513 $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
514 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject=123&baz[someOtherDomainObject]=321');
515 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
516 $this->assertEquals($expectedConfiguration, $actualConfiguration);
517 }
518
519 /**
520 * @test
521 */
522 public function buildTypolinkConfigurationResolvesPageTypeFromFormat() {
523 $this->uriBuilder->setTargetPageUid(123);
524 $this->uriBuilder->setFormat('txt');
525
526 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
527 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
528 ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
529 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
530
531 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
532 ->with(NULL, 'txt')
533 ->will($this->returnValue(2));
534
535 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
536 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
537 $this->assertEquals($expectedConfiguration, $actualConfiguration);
538 }
539
540 /**
541 * @test
542 */
543 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured() {
544 $this->uriBuilder->setTargetPageUid(123);
545 $this->uriBuilder->setFormat('txt');
546
547 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
548 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
549 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
550
551 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
552 ->with(NULL, 'txt')
553 ->will($this->returnValue(0));
554
555 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
556 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
557
558 $this->assertEquals($expectedConfiguration, $actualConfiguration);
559 }
560
561 /**
562 * @test
563 */
564 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped() {
565 $this->uriBuilder->setTargetPageUid(123);
566 $this->uriBuilder->setFormat('txt');
567
568 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
569 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
570 ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
571 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
572
573 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
574 ->with(NULL, 'txt')
575 ->will($this->returnValue(0));
576
577 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
578 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
579
580 $this->assertEquals($expectedConfiguration, $actualConfiguration);
581 }
582
583
584 /**
585 * @test
586 */
587 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
588 $this->uriBuilder->setTargetPageUid(123);
589 $this->uriBuilder->setNoCache(TRUE);
590 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
591 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
592 $this->assertEquals($expectedConfiguration, $actualConfiguration);
593 }
594
595 /**
596 * @test
597 */
598 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
599 $this->uriBuilder->setTargetPageUid(123);
600 $this->uriBuilder->setUseCacheHash(FALSE);
601 $expectedConfiguration = array('parameter' => 123);
602 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
603 $this->assertEquals($expectedConfiguration, $actualConfiguration);
604 }
605
606 /**
607 * @test
608 */
609 public function buildTypolinkConfigurationConsidersSection() {
610 $this->uriBuilder->setTargetPageUid(123);
611 $this->uriBuilder->setSection('SomeSection');
612 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
613 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
614 $this->assertEquals($expectedConfiguration, $actualConfiguration);
615 }
616
617 /**
618 * @test
619 */
620 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
621 $this->uriBuilder->setTargetPageUid(123);
622 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
623 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
624 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
625 $this->assertEquals($expectedConfiguration, $actualConfiguration);
626 }
627
628 /**
629 * @test
630 */
631 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
632 $mockDomainObject1 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
633 $mockDomainObject1->_set('uid', '123');
634 $mockDomainObject2 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
635 $mockDomainObject2->_set('uid', '321');
636 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
637 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
638 $this->assertEquals($expectedResult, $actualResult);
639 }
640
641 /**
642 * @test
643 */
644 public function conversionOfTansientObjectsIsInvoked() {
645 $className = uniqid('FixturesObject_');
646 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
647 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
648 $mockValueObject = new $classNameWithNS();
649 $mockValueObject->name = 'foo';
650 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('convertTransientObjectToArray'));
651 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
652 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
653 $expectedResult = array('object' => array('foo' => 'bar'));
654 $this->assertEquals($expectedResult, $actualResult);
655 }
656
657 /**
658 * @test
659 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
660 */
661 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
662 $className = uniqid('FixturesObject_');
663 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
664 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity { public $name; public $uid; }');
665 $mockEntity = new $classNameWithNS();
666 $mockEntity->name = 'foo';
667 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
668 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
669 }
670
671 /**
672 * @test
673 */
674 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
675 $className = uniqid('FixturesObject_');
676 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
677 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
678 $mockValueObject = new $classNameWithNS();
679 $mockValueObject->name = 'foo';
680 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
681 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
682 $expectedResult = array('name' => 'foo', 'uid' => NULL, 'pid' => NULL);
683 $this->assertEquals($expectedResult, $actualResult);
684 }
685
686 /**
687 * @test
688 */
689 public function tansientObjectsAreRecursivelyConverted() {
690 $className = uniqid('FixturesObject_');
691 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
692 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
693 $mockInnerValueObject2 = new $classNameWithNS();
694 $mockInnerValueObject2->name = 'foo';
695 $mockInnerValueObject2->uid = 99;
696 $className = uniqid('FixturesObject_');
697 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
698 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $object; public $uid; }');
699 $mockInnerValueObject1 = new $classNameWithNS();
700 $mockInnerValueObject1->object = $mockInnerValueObject2;
701 $className = uniqid('FixturesObject_');
702 $classNameWithNS = __NAMESPACE__ . '\\' . $className;
703 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $object; public $uid; }');
704 $mockValueObject = new $classNameWithNS();
705 $mockValueObject->object = $mockInnerValueObject1;
706 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
707 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
708 $expectedResult = array(
709 'object' => array(
710 'object' => 99,
711 'uid' => NULL,
712 'pid' => NULL
713 ),
714 'uid' => NULL,
715 'pid' => NULL
716 );
717 $this->assertEquals($expectedResult, $actualResult);
718 }
719
720 /**
721 * @test
722 */
723 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
724 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
725 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
726 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
727 $extensionName = 'ExtensionName';
728 $pluginName = 'PluginName';
729 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
730 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
731 $this->assertEquals($expectedResult, $actualResult);
732 }
733
734 /**
735 * @test
736 */
737 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
738 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
739 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
740 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
741 $extensionName = 'ExtensionName';
742 $pluginName = 'PluginName';
743 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
744 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
745 $this->assertEquals($expectedResult, $actualResult);
746 }
747
748 /**
749 * @test
750 */
751 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
752 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
753 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
754 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
755 $extensionName = 'ExtensionName';
756 $pluginName = 'PluginName';
757 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
758 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
759 $this->assertEquals($expectedResult, $actualResult);
760 }
761
762 /**
763 * @test
764 */
765 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
766 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
767 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
768 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
769 $extensionName = 'ExtensionName';
770 $pluginName = 'PluginName';
771 $expectedResult = array('foo' => 'bar');
772 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
773 $this->assertEquals($expectedResult, $actualResult);
774 }
775 }