[TASK] Merge submodule linkvalidator into core
[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 buildTypolinkConfigurationConsidersPageType() {
479 $this->uriBuilder->setTargetPageUid(123);
480 $this->uriBuilder->setTargetPageType(2);
481 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
482 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
483 $this->assertEquals($expectedConfiguration, $actualConfiguration);
484 }
485
486 /**
487 * @test
488 */
489 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
490 $this->uriBuilder->setTargetPageUid(123);
491 $this->uriBuilder->setNoCache(TRUE);
492 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
493 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
494 $this->assertEquals($expectedConfiguration, $actualConfiguration);
495 }
496
497 /**
498 * @test
499 */
500 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
501 $this->uriBuilder->setTargetPageUid(123);
502 $this->uriBuilder->setUseCacheHash(FALSE);
503 $expectedConfiguration = array('parameter' => 123);
504 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
505 $this->assertEquals($expectedConfiguration, $actualConfiguration);
506 }
507
508 /**
509 * @test
510 */
511 public function buildTypolinkConfigurationConsidersSection() {
512 $this->uriBuilder->setTargetPageUid(123);
513 $this->uriBuilder->setSection('SomeSection');
514 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
515 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
516 $this->assertEquals($expectedConfiguration, $actualConfiguration);
517 }
518
519 /**
520 * @test
521 */
522 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
523 $this->uriBuilder->setTargetPageUid(123);
524 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
525 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
526 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
527 $this->assertEquals($expectedConfiguration, $actualConfiguration);
528 }
529
530 /**
531 * @test
532 */
533 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
534 $mockDomainObject1 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
535 $mockDomainObject1->_set('uid', '123');
536 $mockDomainObject2 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
537 $mockDomainObject2->_set('uid', '321');
538 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
539 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
540 $this->assertEquals($expectedResult, $actualResult);
541 }
542
543 /**
544 * @test
545 */
546 public function conversionOfTansientObjectsIsInvoked() {
547 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
548 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
549 $mockValueObject = new $className();
550 $mockValueObject->name = 'foo';
551 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('convertTransientObjectToArray'));
552 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
553 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
554 $expectedResult = array('object' => array('foo' => 'bar'));
555 $this->assertEquals($expectedResult, $actualResult);
556 }
557
558 /**
559 * @test
560 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
561 */
562 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
563 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
564 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity { public $name; public $uid; }');
565 $mockEntity = new $className();
566 $mockEntity->name = 'foo';
567 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
568 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
569 }
570
571 /**
572 * @test
573 */
574 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
575 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
576 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
577 $mockValueObject = new $className();
578 $mockValueObject->name = 'foo';
579 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
580 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
581 $expectedResult = array('name' => 'foo', 'uid' => NULL, 'pid' => NULL);
582 $this->assertEquals($expectedResult, $actualResult);
583 }
584
585 /**
586 * @test
587 */
588 public function tansientObjectsAreRecursivelyConverted() {
589 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
590 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
591 $mockInnerValueObject2 = new $className();
592 $mockInnerValueObject2->name = 'foo';
593 $mockInnerValueObject2->uid = 99;
594 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
595 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $object; public $uid; }');
596 $mockInnerValueObject1 = new $className();
597 $mockInnerValueObject1->object = $mockInnerValueObject2;
598 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
599 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $object; public $uid; }');
600 $mockValueObject = new $className();
601 $mockValueObject->object = $mockInnerValueObject1;
602 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
603 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
604 $expectedResult = array(
605 'object' => array(
606 'object' => 99,
607 'uid' => NULL,
608 'pid' => NULL
609 ),
610 'uid' => NULL,
611 'pid' => NULL
612 );
613 $this->assertEquals($expectedResult, $actualResult);
614 }
615
616 /**
617 * @test
618 */
619 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
620 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
621 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
622 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
623 $extensionName = 'ExtensionName';
624 $pluginName = 'PluginName';
625 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
626 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
627 $this->assertEquals($expectedResult, $actualResult);
628 }
629
630 /**
631 * @test
632 */
633 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
634 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
635 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
636 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
637 $extensionName = 'ExtensionName';
638 $pluginName = 'PluginName';
639 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
640 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
641 $this->assertEquals($expectedResult, $actualResult);
642 }
643
644 /**
645 * @test
646 */
647 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
648 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
649 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
650 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
651 $extensionName = 'ExtensionName';
652 $pluginName = 'PluginName';
653 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
654 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
655 $this->assertEquals($expectedResult, $actualResult);
656 }
657
658 /**
659 * @test
660 */
661 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
662 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
663 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
664 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
665 $extensionName = 'ExtensionName';
666 $pluginName = 'PluginName';
667 $expectedResult = array('foo' => 'bar');
668 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
669 $this->assertEquals($expectedResult, $actualResult);
670 }
671 }
672
673 ?>