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