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