[FEATURE] Use EnvironmentService to detect TYPO3_MODE in several places
[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(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'), array('M', 'id'), 'mod.php?foo=bar'),
204 'Arguments to be excluded in the end' => array(array('foo' => 'bar', 'id' => 'pageId', 'M' => 'moduleKey'), array('M', 'id'), 'mod.php?foo=bar'),
205 'Arguments in nested array to be excluded' => array(array('tx_foo' => array('bar' => 'baz'), 'id' => 'pageId', 'M' => 'moduleKey'), array('id', 'tx_foo[bar]'), 'mod.php?M=moduleKey'),
206 );
207 }
208
209 /**
210 * @test
211 * @dataProvider buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider
212 */
213 public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSet(array $parameters, array $excluded, $expected) {
214 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset($parameters);
215 $this->uriBuilder->setAddQueryString(TRUE);
216 $this->uriBuilder->setArgumentsToBeExcludedFromQueryString($excluded);
217 $actualResult = $this->uriBuilder->buildBackendUri();
218 $this->assertEquals($expected, $actualResult);
219 }
220
221 /**
222 * @test
223 */
224 public function buildBackendUriKeepsModuleQueryParametersIfAddQueryStringIsNotSet() {
225 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
226 $expectedResult = 'mod.php?M=moduleKey&id=pageId';
227 $actualResult = $this->uriBuilder->buildBackendUri();
228 $this->assertEquals($expectedResult, $actualResult);
229 }
230
231 /**
232 * @test
233 */
234 public function buildBackendUriMergesAndOverrulesQueryParametersWithArguments() {
235 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
236 $this->uriBuilder->setArguments(array('M' => 'overwrittenModuleKey', 'somePrefix' => array('bar' => 'baz')));
237 $expectedResult = 'mod.php?M=overwrittenModuleKey&id=pageId&somePrefix%5Bbar%5D=baz';
238 $actualResult = $this->uriBuilder->buildBackendUri();
239 $this->assertEquals($expectedResult, $actualResult);
240 }
241
242 /**
243 * @test
244 */
245 public function buildBackendUriConvertsDomainObjectsAfterArgumentsHaveBeenMerged() {
246 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey'));
247 $mockDomainObject = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
248 $mockDomainObject->_set('uid', '123');
249 $this->uriBuilder->setArguments(array('somePrefix' => array('someDomainObject' => $mockDomainObject)));
250 $expectedResult = 'mod.php?M=moduleKey&somePrefix%5BsomeDomainObject%5D=123';
251 $actualResult = $this->uriBuilder->buildBackendUri();
252 $this->assertEquals($expectedResult, $actualResult);
253 }
254
255 /**
256 * @test
257 */
258 public function buildBackendUriRespectsSection() {
259 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey'));
260 $this->uriBuilder->setSection('someSection');
261 $expectedResult = 'mod.php?M=moduleKey#someSection';
262 $actualResult = $this->uriBuilder->buildBackendUri();
263 $this->assertEquals($expectedResult, $actualResult);
264 }
265
266 /**
267 * @test
268 */
269 public function buildBackendUriCreatesAbsoluteUrisIfSpecified() {
270 \TYPO3\CMS\Core\Utility\GeneralUtility::_GETset(array('M' => 'moduleKey'));
271 $this->mockRequest->expects($this->any())->method('getBaseUri')->will($this->returnValue('http://baseuri/' . TYPO3_mainDir));
272 $this->uriBuilder->setCreateAbsoluteUri(TRUE);
273 $expectedResult = 'http://baseuri/' . TYPO3_mainDir . 'mod.php?M=moduleKey';
274 $actualResult = $this->uriBuilder->buildBackendUri();
275 $this->assertSame($expectedResult, $actualResult);
276 }
277
278 /**
279 * @test
280 */
281 public function buildFrontendUriCreatesTypoLink() {
282 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
283 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
284 $uriBuilder->_set('contentObject', $this->mockContentObject);
285 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
286 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
287 $uriBuilder->buildFrontendUri();
288 }
289
290 /**
291 * @test
292 */
293 public function buildFrontendUriCreatesRelativeUrisByDefault() {
294 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
295 $expectedResult = 'relative/uri';
296 $actualResult = $this->uriBuilder->buildFrontendUri();
297 $this->assertSame($expectedResult, $actualResult);
298 }
299
300 /**
301 * @test
302 */
303 public function buildFrontendUriDoesNotStripLeadingSlashesFromRelativeUris() {
304 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('/relative/uri'));
305 $expectedResult = '/relative/uri';
306 $actualResult = $this->uriBuilder->buildFrontendUri();
307 $this->assertSame($expectedResult, $actualResult);
308 }
309
310 /**
311 * @test
312 */
313 public function buildFrontendUriCreatesAbsoluteUrisIfSpecified() {
314 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
315 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
316 $uriBuilder->_set('contentObject', $this->mockContentObject);
317 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
318 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => TRUE))->will($this->returnValue('http://baseuri/relative/uri'));
319 $uriBuilder->setCreateAbsoluteUri(TRUE);
320 $expectedResult = 'http://baseuri/relative/uri';
321 $actualResult = $uriBuilder->buildFrontendUri();
322 $this->assertSame($expectedResult, $actualResult);
323 }
324
325 /**
326 * @test
327 */
328 public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified() {
329 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
330 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
331 $uriBuilder->_set('contentObject', $this->mockContentObject);
332 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
333 $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'));
334 $uriBuilder->setCreateAbsoluteUri(TRUE);
335 $uriBuilder->setAbsoluteUriScheme('someScheme');
336 $expectedResult = 'http://baseuri/relative/uri';
337 $actualResult = $uriBuilder->buildFrontendUri();
338 $this->assertSame($expectedResult, $actualResult);
339 }
340
341 /**
342 * @test
343 */
344 public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse() {
345 /** @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
346 $uriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('buildTypolinkConfiguration'));
347 $uriBuilder->_set('contentObject', $this->mockContentObject);
348 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
349 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
350 $uriBuilder->setCreateAbsoluteUri(FALSE);
351 $uriBuilder->setAbsoluteUriScheme('someScheme');
352 $expectedResult = 'http://baseuri/relative/uri';
353 $actualResult = $uriBuilder->buildFrontendUri();
354 $this->assertSame($expectedResult, $actualResult);
355 }
356
357 /**
358 * @test
359 */
360 public function resetSetsAllOptionsToTheirDefaultValue() {
361 $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);
362 $this->uriBuilder->reset();
363 $this->assertEquals(array(), $this->uriBuilder->getArguments());
364 $this->assertEquals('', $this->uriBuilder->getSection());
365 $this->assertEquals('', $this->uriBuilder->getFormat());
366 $this->assertEquals(FALSE, $this->uriBuilder->getCreateAbsoluteUri());
367 $this->assertEquals(FALSE, $this->uriBuilder->getAddQueryString());
368 $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
369 $this->assertEquals(NULL, $this->uriBuilder->getArgumentPrefix());
370 $this->assertEquals(FALSE, $this->uriBuilder->getLinkAccessRestrictedPages());
371 $this->assertEquals(NULL, $this->uriBuilder->getTargetPageUid());
372 $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
373 $this->assertEquals(FALSE, $this->uriBuilder->getNoCache());
374 $this->assertEquals(TRUE, $this->uriBuilder->getUseCacheHash());
375 }
376
377 /**
378 * @test
379 */
380 public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid() {
381 $GLOBALS['TSFE']->id = 123;
382 $this->uriBuilder->setTargetPageUid(321);
383 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
384 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
385 $this->assertEquals($expectedConfiguration, $actualConfiguration);
386 }
387
388 /**
389 * @test
390 */
391 public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet() {
392 $GLOBALS['TSFE']->id = 123;
393 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
394 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
395 $this->assertEquals($expectedConfiguration, $actualConfiguration);
396 }
397
398 /**
399 * @test
400 */
401 public function buildTypolinkConfigurationProperlySetsAdditionalArguments() {
402 $this->uriBuilder->setTargetPageUid(123);
403 $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
404 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz[extbase]=fluid');
405 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
406 $this->assertEquals($expectedConfiguration, $actualConfiguration);
407 }
408
409 /**
410 * @test
411 */
412 public function buildTypolinkConfigurationConvertsDomainObjects() {
413 $mockDomainObject1 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
414 $mockDomainObject1->_set('uid', '123');
415 $mockDomainObject2 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
416 $mockDomainObject2->_set('uid', '321');
417 $this->uriBuilder->setTargetPageUid(123);
418 $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
419 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject=123&baz[someOtherDomainObject]=321');
420 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
421 $this->assertEquals($expectedConfiguration, $actualConfiguration);
422 }
423
424 /**
425 * @test
426 */
427 public function buildTypolinkConfigurationConsidersPageType() {
428 $this->uriBuilder->setTargetPageUid(123);
429 $this->uriBuilder->setTargetPageType(2);
430 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
431 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
432 $this->assertEquals($expectedConfiguration, $actualConfiguration);
433 }
434
435 /**
436 * @test
437 */
438 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
439 $this->uriBuilder->setTargetPageUid(123);
440 $this->uriBuilder->setNoCache(TRUE);
441 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
442 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
443 $this->assertEquals($expectedConfiguration, $actualConfiguration);
444 }
445
446 /**
447 * @test
448 */
449 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
450 $this->uriBuilder->setTargetPageUid(123);
451 $this->uriBuilder->setUseCacheHash(FALSE);
452 $expectedConfiguration = array('parameter' => 123);
453 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
454 $this->assertEquals($expectedConfiguration, $actualConfiguration);
455 }
456
457 /**
458 * @test
459 */
460 public function buildTypolinkConfigurationConsidersSection() {
461 $this->uriBuilder->setTargetPageUid(123);
462 $this->uriBuilder->setSection('SomeSection');
463 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
464 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
465 $this->assertEquals($expectedConfiguration, $actualConfiguration);
466 }
467
468 /**
469 * @test
470 */
471 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
472 $this->uriBuilder->setTargetPageUid(123);
473 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
474 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
475 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
476 $this->assertEquals($expectedConfiguration, $actualConfiguration);
477 }
478
479 /**
480 * @test
481 */
482 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
483 $mockDomainObject1 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
484 $mockDomainObject1->_set('uid', '123');
485 $mockDomainObject2 = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity', array('dummy'));
486 $mockDomainObject2->_set('uid', '321');
487 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
488 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
489 $this->assertEquals($expectedResult, $actualResult);
490 }
491
492 /**
493 * @test
494 */
495 public function conversionOfTansientObjectsIsInvoked() {
496 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
497 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
498 $mockValueObject = new $className();
499 $mockValueObject->name = 'foo';
500 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('convertTransientObjectToArray'));
501 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
502 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
503 $expectedResult = array('object' => array('foo' => 'bar'));
504 $this->assertEquals($expectedResult, $actualResult);
505 }
506
507 /**
508 * @test
509 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
510 */
511 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
512 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
513 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity { public $name; public $uid; }');
514 $mockEntity = new $className();
515 $mockEntity->name = 'foo';
516 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
517 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
518 }
519
520 /**
521 * @test
522 */
523 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
524 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
525 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
526 $mockValueObject = new $className();
527 $mockValueObject->name = 'foo';
528 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
529 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
530 $expectedResult = array('name' => 'foo', 'uid' => NULL, 'pid' => NULL);
531 $this->assertEquals($expectedResult, $actualResult);
532 }
533
534 /**
535 * @test
536 */
537 public function tansientObjectsAreRecursivelyConverted() {
538 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
539 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $name; public $uid; }');
540 $mockInnerValueObject2 = new $className();
541 $mockInnerValueObject2->name = 'foo';
542 $mockInnerValueObject2->uid = 99;
543 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
544 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $object; public $uid; }');
545 $mockInnerValueObject1 = new $className();
546 $mockInnerValueObject1->object = $mockInnerValueObject2;
547 $className = uniqid('Tx_Extbase_Tests_Fixtures_Object');
548 eval('class ' . $className . ' extends TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject { public $object; public $uid; }');
549 $mockValueObject = new $className();
550 $mockValueObject->object = $mockInnerValueObject1;
551 $mockUriBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder', array('dummy'));
552 $actualResult = $mockUriBuilder->_call('convertTransientObjectToArray', $mockValueObject);
553 $expectedResult = array(
554 'object' => array(
555 'object' => 99,
556 'uid' => NULL,
557 'pid' => NULL
558 ),
559 'uid' => NULL,
560 'pid' => NULL
561 );
562 $this->assertEquals($expectedResult, $actualResult);
563 }
564
565 /**
566 * @test
567 */
568 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
569 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
570 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
571 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
572 $extensionName = 'ExtensionName';
573 $pluginName = 'PluginName';
574 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
575 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
576 $this->assertEquals($expectedResult, $actualResult);
577 }
578
579 /**
580 * @test
581 */
582 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
583 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
584 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
585 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
586 $extensionName = 'ExtensionName';
587 $pluginName = 'PluginName';
588 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
589 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
590 $this->assertEquals($expectedResult, $actualResult);
591 }
592
593 /**
594 * @test
595 */
596 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
597 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
598 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
599 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
600 $extensionName = 'ExtensionName';
601 $pluginName = 'PluginName';
602 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
603 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
604 $this->assertEquals($expectedResult, $actualResult);
605 }
606
607 /**
608 * @test
609 */
610 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
611 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
612 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
613 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
614 $extensionName = 'ExtensionName';
615 $pluginName = 'PluginName';
616 $expectedResult = array('foo' => 'bar');
617 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
618 $this->assertEquals($expectedResult, $actualResult);
619 }
620 }
621
622 ?>