Revert "[!!!][BUGFIX] Skip cache hash for URIs to non-cacheable actions"
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
17 use TYPO3\CMS\Extbase\Tests\Unit\Mvc\Web\Routing\Fixtures\ValueObjectFixture;
18 use TYPO3\CMS\Extbase\Tests\Unit\Mvc\Web\Routing\Fixtures\EntityFixture;
19 use TYPO3\CMS\Core\Tests\UnitTestCase;
20 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
21 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22 use TYPO3\CMS\Extbase\Mvc\Web\Request;
23 use TYPO3\CMS\Extbase\Service\ExtensionService;
24 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
25 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
26 use TYPO3\CMS\Extbase\Service\EnvironmentService;
27 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
30 use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
31
32 /**
33 * Test case
34 */
35 class UriBuilderTest extends UnitTestCase {
36
37 /**
38 * @var ConfigurationManagerInterface
39 */
40 protected $mockConfigurationManager;
41
42 /**
43 * @var ContentObjectRenderer|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $mockContentObject;
46
47 /**
48 * @var Request|\PHPUnit_Framework_MockObject_MockObject
49 */
50 protected $mockRequest;
51
52 /**
53 * @var ExtensionService|\PHPUnit_Framework_MockObject_MockObject
54 */
55 protected $mockExtensionService;
56
57 /**
58 * @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
59 */
60 protected $uriBuilder;
61
62 /**
63 * @throws \InvalidArgumentException
64 * @throws \PHPUnit_Framework_Exception
65 */
66 protected function setUp() {
67 $GLOBALS['TSFE'] = $this->getMock(TypoScriptFrontendController::class, array(), array(), '', FALSE);
68 $this->mockContentObject = $this->getMock(ContentObjectRenderer::class);
69 $this->mockRequest = $this->getMock(Request::class);
70 $this->mockExtensionService = $this->getMock(ExtensionService::class);
71 $this->mockConfigurationManager = $this->getMock(ConfigurationManagerInterface::class);
72 $this->uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('build'));
73 $this->uriBuilder->setRequest($this->mockRequest);
74 $this->uriBuilder->_set('contentObject', $this->mockContentObject);
75 $this->uriBuilder->_set('configurationManager', $this->mockConfigurationManager);
76 $this->uriBuilder->_set('extensionService', $this->mockExtensionService);
77 $this->uriBuilder->_set('environmentService', $this->getMock(EnvironmentService::class));
78 // Mocking backend user is required for backend URI generation as BackendUtility::getModuleUrl() is called
79 $backendUserMock = $this->getMock(BackendUserAuthentication::class);
80 $backendUserMock->expects($this->any())->method('check')->will($this->returnValue(TRUE));
81 $GLOBALS['BE_USER'] = $backendUserMock;
82 }
83
84 /**
85 * @test
86 */
87 public function settersAndGettersWorkAsExpected() {
88 $this->uriBuilder
89 ->reset()
90 ->setArguments(array('test' => 'arguments'))
91 ->setSection('testSection')
92 ->setFormat('testFormat')
93 ->setCreateAbsoluteUri(TRUE)
94 ->setAbsoluteUriScheme('https')
95 ->setAddQueryString(TRUE)
96 ->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))
97 ->setAddQueryStringMethod('GET,POST')
98 ->setArgumentPrefix('testArgumentPrefix')
99 ->setLinkAccessRestrictedPages(TRUE)
100 ->setTargetPageUid(123)
101 ->setTargetPageType(321)
102 ->setNoCache(TRUE)
103 ->setUseCacheHash(FALSE);
104 $this->assertEquals(array('test' => 'arguments'), $this->uriBuilder->getArguments());
105 $this->assertEquals('testSection', $this->uriBuilder->getSection());
106 $this->assertEquals('testFormat', $this->uriBuilder->getFormat());
107 $this->assertEquals(TRUE, $this->uriBuilder->getCreateAbsoluteUri());
108 $this->assertEquals('https', $this->uriBuilder->getAbsoluteUriScheme());
109 $this->assertEquals(TRUE, $this->uriBuilder->getAddQueryString());
110 $this->assertEquals(array('test' => 'addQueryStringExcludeArguments'), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
111 $this->assertEquals('GET,POST', $this->uriBuilder->getAddQueryStringMethod());
112 $this->assertEquals('testArgumentPrefix', $this->uriBuilder->getArgumentPrefix());
113 $this->assertEquals(TRUE, $this->uriBuilder->getLinkAccessRestrictedPages());
114 $this->assertEquals(123, $this->uriBuilder->getTargetPageUid());
115 $this->assertEquals(321, $this->uriBuilder->getTargetPageType());
116 $this->assertEquals(TRUE, $this->uriBuilder->getNoCache());
117 $this->assertEquals(FALSE, $this->uriBuilder->getUseCacheHash());
118 }
119
120 /**
121 * @test
122 */
123 public function uriForPrefixesArgumentsWithExtensionAndPluginNameAndSetsControllerArgument() {
124 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
125 $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'));
126 $GLOBALS['TSFE'] = NULL;
127 $this->uriBuilder->uriFor(NULL, array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')), 'SomeController', 'SomeExtension', 'SomePlugin');
128 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
129 }
130
131 /**
132 * @test
133 */
134 public function uriForRecursivelyMergesAndOverrulesControllerArgumentsWithArguments() {
135 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
136 $arguments = array('tx_someextension_someplugin' => array('foo' => 'bar'), 'additionalParam' => 'additionalValue');
137 $controllerArguments = array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'));
138 $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'), 'additionalParam' => 'additionalValue');
139 $this->uriBuilder->setArguments($arguments);
140 $this->uriBuilder->uriFor(NULL, $controllerArguments, 'SomeController', 'SomeExtension', 'SomePlugin');
141 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
142 }
143
144 /**
145 * @test
146 */
147 public function uriForOnlySetsActionArgumentIfSpecified() {
148 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
149 $expectedArguments = array('tx_someextension_someplugin' => array('controller' => 'SomeController'));
150 $this->uriBuilder->uriFor(NULL, array(), 'SomeController', 'SomeExtension', 'SomePlugin');
151 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
152 }
153
154 /**
155 * @test
156 */
157 public function uriForSetsControllerFromRequestIfControllerIsNotSet() {
158 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_someplugin'));
159 $this->mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('SomeControllerFromRequest'));
160 $expectedArguments = array('tx_someextension_someplugin' => array('controller' => 'SomeControllerFromRequest'));
161 $this->uriBuilder->uriFor(NULL, array(), NULL, 'SomeExtension', 'SomePlugin');
162 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
163 }
164
165 /**
166 * @test
167 */
168 public function uriForSetsExtensionNameFromRequestIfExtensionNameIsNotSet() {
169 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_someextensionnamefromrequest_someplugin'));
170 $this->mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
171 $expectedArguments = array('tx_someextensionnamefromrequest_someplugin' => array('controller' => 'SomeController'));
172 $this->uriBuilder->uriFor(NULL, array(), 'SomeController', NULL, 'SomePlugin');
173 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
174 }
175
176 /**
177 * @test
178 */
179 public function uriForSetsPluginNameFromRequestIfPluginNameIsNotSet() {
180 $this->mockExtensionService->expects($this->once())->method('getPluginNamespace')->will($this->returnValue('tx_someextension_somepluginnamefromrequest'));
181 $this->mockRequest->expects($this->once())->method('getPluginName')->will($this->returnValue('SomePluginNameFromRequest'));
182 $expectedArguments = array('tx_someextension_somepluginnamefromrequest' => array('controller' => 'SomeController'));
183 $this->uriBuilder->uriFor(NULL, array(), 'SomeController', 'SomeExtension');
184 $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
185 }
186
187 /**
188 * @test
189 */
190 public function uriForDoesNotDisableCacheHashForNonCacheableActions() {
191 $this->mockExtensionService->expects($this->any())->method('isActionCacheable')->will($this->returnValue(FALSE));
192 $this->uriBuilder->uriFor('someNonCacheableAction', array(), 'SomeController', 'SomeExtension');
193 $this->assertTrue($this->uriBuilder->getUseCacheHash());
194 }
195
196 /**
197 * @test
198 */
199 public function buildBackendUriKeepsQueryParametersIfAddQueryStringIsSet() {
200 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
201 $_POST = array();
202 $_POST['foo2'] = 'bar2';
203 $this->uriBuilder->setAddQueryString(TRUE);
204 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
205 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&id=pageId&foo=bar&foo2=bar2';
206 $actualResult = $this->uriBuilder->buildBackendUri();
207 $this->assertEquals($expectedResult, $actualResult);
208 }
209
210 /**
211 * @test
212 */
213 public function buildBackendUriKeepsQueryParametersIfAddQueryStringMethodIsNotSet() {
214 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
215 $_POST = array();
216 $_POST['foo2'] = 'bar2';
217 $this->uriBuilder->setAddQueryString(TRUE);
218 $this->uriBuilder->setAddQueryStringMethod(NULL);
219 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&id=pageId&foo=bar';
220 $actualResult = $this->uriBuilder->buildBackendUri();
221 $this->assertEquals($expectedResult, $actualResult);
222 }
223
224 /**
225 * return array
226 */
227 public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider() {
228 return array(
229 'Arguments to be excluded in the beginning' => array(
230 array(
231 'M' => 'moduleKey',
232 'id' => 'pageId',
233 'foo' => 'bar'
234 ),
235 array(
236 'foo2' => 'bar2'
237 ),
238 array(
239 'M',
240 'id'
241 ),
242 '/typo3/index.php?moduleToken=dummyToken&foo=bar&foo2=bar2'
243 ),
244 'Arguments to be excluded in the end' => array(
245 array(
246 'foo' => 'bar',
247 'id' => 'pageId',
248 'M' => 'moduleKey'
249 ),
250 array(
251 'foo2' => 'bar2'
252 ),
253 array(
254 'M',
255 'id'
256 ),
257 '/typo3/index.php?moduleToken=dummyToken&foo=bar&foo2=bar2'
258 ),
259 'Arguments in nested array to be excluded' => array(
260 array(
261 'tx_foo' => array(
262 'bar' => 'baz'
263 ),
264 'id' => 'pageId',
265 'M' => 'moduleKey'
266 ),
267 array(
268 'foo2' => 'bar2'
269 ),
270 array(
271 'id',
272 'tx_foo[bar]'
273 ),
274 '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&foo2=bar2'
275 ),
276 'Arguments in multidimensional array to be excluded' => array(
277 array(
278 'tx_foo' => array(
279 'bar' => array(
280 'baz' => 'bay'
281 )
282 ),
283 'id' => 'pageId',
284 'M' => 'moduleKey'
285 ),
286 array(
287 'foo2' => 'bar2'
288 ),
289 array(
290 'id',
291 'tx_foo[bar][baz]'
292 ),
293 '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&foo2=bar2'
294 ),
295 );
296 }
297
298 /**
299 * @test
300 * @dataProvider buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSetDataProvider
301 * @param array $parameters
302 * @param array $postArguments
303 * @param array $excluded
304 * @param string $expected
305 */
306 public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSet(array $parameters, array $postArguments, array $excluded, $expected) {
307 GeneralUtility::_GETset($parameters);
308 $_POST = $postArguments;
309 $this->uriBuilder->setAddQueryString(TRUE);
310 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
311 $this->uriBuilder->setArgumentsToBeExcludedFromQueryString($excluded);
312 $actualResult = $this->uriBuilder->buildBackendUri();
313 $this->assertEquals($expected, $actualResult);
314 }
315
316 /**
317 * @test
318 */
319 public function buildBackendUriKeepsModuleQueryParametersIfAddQueryStringIsNotSet() {
320 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
321 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&id=pageId';
322 $actualResult = $this->uriBuilder->buildBackendUri();
323 $this->assertEquals($expectedResult, $actualResult);
324 }
325
326 /**
327 * @test
328 */
329 public function buildBackendUriMergesAndOverrulesQueryParametersWithArguments() {
330 GeneralUtility::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
331 $this->uriBuilder->setArguments(array('M' => 'overwrittenModuleKey', 'somePrefix' => array('bar' => 'baz')));
332 $expectedResult = '/typo3/index.php?M=overwrittenModuleKey&moduleToken=dummyToken&id=pageId&somePrefix%5Bbar%5D=baz';
333 $actualResult = $this->uriBuilder->buildBackendUri();
334 $this->assertEquals($expectedResult, $actualResult);
335 }
336
337 /**
338 * @test
339 */
340 public function buildBackendUriConvertsDomainObjectsAfterArgumentsHaveBeenMerged() {
341 GeneralUtility::_GETset(array('M' => 'moduleKey'));
342 $mockDomainObject = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
343 $mockDomainObject->_set('uid', '123');
344 $this->uriBuilder->setArguments(array('somePrefix' => array('someDomainObject' => $mockDomainObject)));
345 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken&somePrefix%5BsomeDomainObject%5D=123';
346 $actualResult = $this->uriBuilder->buildBackendUri();
347 $this->assertEquals($expectedResult, $actualResult);
348 }
349
350 /**
351 * @test
352 */
353 public function buildBackendUriRespectsSection() {
354 GeneralUtility::_GETset(array('M' => 'moduleKey'));
355 $this->uriBuilder->setSection('someSection');
356 $expectedResult = '/typo3/index.php?M=moduleKey&moduleToken=dummyToken#someSection';
357 $actualResult = $this->uriBuilder->buildBackendUri();
358 $this->assertEquals($expectedResult, $actualResult);
359 }
360
361 /**
362 * @test
363 */
364 public function buildBackendUriCreatesAbsoluteUrisIfSpecified() {
365 GeneralUtility::flushInternalRuntimeCaches();
366 GeneralUtility::_GETset(array('M' => 'moduleKey'));
367 $_SERVER['HTTP_HOST'] = 'baseuri';
368 $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
369 $this->mockRequest->expects($this->any())->method('getBaseUri')->will($this->returnValue('http://baseuri'));
370 $this->uriBuilder->setCreateAbsoluteUri(TRUE);
371 $expectedResult = 'http://baseuri/' . TYPO3_mainDir . 'index.php?M=moduleKey&moduleToken=dummyToken';
372 $actualResult = $this->uriBuilder->buildBackendUri();
373 $this->assertSame($expectedResult, $actualResult);
374 }
375
376 /**
377 * @test
378 */
379 public function buildBackendUriWithQueryStringMethodPostGetMergesParameters() {
380 $_POST = array(
381 'key1' => 'POST1',
382 'key2' => 'POST2',
383 'key3' => array(
384 'key31' => 'POST31',
385 'key32' => 'POST32',
386 'key33' => array(
387 'key331' => 'POST331',
388 'key332' => 'POST332',
389 )
390 ),
391 );
392 $_GET = array(
393 'key2' => 'GET2',
394 'key3' => array(
395 'key32' => 'GET32',
396 'key33' => array(
397 'key331' => 'GET331',
398 )
399 )
400 );
401 $this->uriBuilder->setAddQueryString(TRUE);
402 $this->uriBuilder->setAddQueryStringMethod('POST,GET');
403 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('/typo3/index.php?moduleToken=dummyToken&key1=POST1&key2=GET2&key3[key31]=POST31&key3[key32]=GET32&key3[key33][key331]=GET331&key3[key33][key332]=POST332');
404 $actualResult = $this->uriBuilder->buildBackendUri();
405 $this->assertEquals($expectedResult, $actualResult);
406 }
407
408 /**
409 * @test
410 */
411 public function buildBackendUriWithQueryStringMethodGetPostMergesParameters() {
412 $_GET = array(
413 'key1' => 'GET1',
414 'key2' => 'GET2',
415 'key3' => array(
416 'key31' => 'GET31',
417 'key32' => 'GET32',
418 'key33' => array(
419 'key331' => 'GET331',
420 'key332' => 'GET332',
421 )
422 ),
423 );
424 $_POST = array(
425 'key2' => 'POST2',
426 'key3' => array(
427 'key32' => 'POST32',
428 'key33' => array(
429 'key331' => 'POST331',
430 )
431 )
432 );
433 $this->uriBuilder->setAddQueryString(TRUE);
434 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
435 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('/typo3/index.php?moduleToken=dummyToken&key1=GET1&key2=POST2&key3[key31]=GET31&key3[key32]=POST32&key3[key33][key331]=POST331&key3[key33][key332]=GET332');
436 $actualResult = $this->uriBuilder->buildBackendUri();
437 $this->assertEquals($expectedResult, $actualResult);
438 }
439
440 /**
441 * Encodes square brackets in URL.
442 *
443 * @param string $string
444 * @return string
445 */
446 private function rawUrlEncodeSquareBracketsInUrl($string) {
447 return str_replace(array('[', ']'), array('%5B', '%5D'), $string);
448 }
449
450 /**
451 * @test
452 */
453 public function buildFrontendUriCreatesTypoLink() {
454 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
455 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
456 $uriBuilder->_set('contentObject', $this->mockContentObject);
457 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
458 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
459 $uriBuilder->buildFrontendUri();
460 }
461
462 /**
463 * @test
464 */
465 public function buildFrontendUriCreatesRelativeUrisByDefault() {
466 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
467 $expectedResult = 'relative/uri';
468 $actualResult = $this->uriBuilder->buildFrontendUri();
469 $this->assertSame($expectedResult, $actualResult);
470 }
471
472 /**
473 * @test
474 */
475 public function buildFrontendUriDoesNotStripLeadingSlashesFromRelativeUris() {
476 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('/relative/uri'));
477 $expectedResult = '/relative/uri';
478 $actualResult = $this->uriBuilder->buildFrontendUri();
479 $this->assertSame($expectedResult, $actualResult);
480 }
481
482 /**
483 * @test
484 */
485 public function buildFrontendUriCreatesAbsoluteUrisIfSpecified() {
486 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
487 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
488 $uriBuilder->_set('contentObject', $this->mockContentObject);
489 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
490 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => TRUE))->will($this->returnValue('http://baseuri/relative/uri'));
491 $uriBuilder->setCreateAbsoluteUri(TRUE);
492 $expectedResult = 'http://baseuri/relative/uri';
493 $actualResult = $uriBuilder->buildFrontendUri();
494 $this->assertSame($expectedResult, $actualResult);
495 }
496
497 /**
498 * @test
499 */
500 public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified() {
501 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
502 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
503 $uriBuilder->_set('contentObject', $this->mockContentObject);
504 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
505 $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'));
506 $uriBuilder->setCreateAbsoluteUri(TRUE);
507 $uriBuilder->setAbsoluteUriScheme('someScheme');
508 $expectedResult = 'http://baseuri/relative/uri';
509 $actualResult = $uriBuilder->buildFrontendUri();
510 $this->assertSame($expectedResult, $actualResult);
511 }
512
513 /**
514 * @test
515 */
516 public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse() {
517 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $uriBuilder */
518 $uriBuilder = $this->getAccessibleMock(UriBuilder::class, array('buildTypolinkConfiguration'));
519 $uriBuilder->_set('contentObject', $this->mockContentObject);
520 $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
521 $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
522 $uriBuilder->setCreateAbsoluteUri(FALSE);
523 $uriBuilder->setAbsoluteUriScheme('someScheme');
524 $expectedResult = 'http://baseuri/relative/uri';
525 $actualResult = $uriBuilder->buildFrontendUri();
526 $this->assertSame($expectedResult, $actualResult);
527 }
528
529 /**
530 * @test
531 */
532 public function resetSetsAllOptionsToTheirDefaultValue() {
533 $this->uriBuilder->setArguments(array('test' => 'arguments'))->setSection('testSection')->setFormat('someFormat')->setCreateAbsoluteUri(TRUE)->setAddQueryString(TRUE)->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))->setAddQueryStringMethod(NULL)->setArgumentPrefix('testArgumentPrefix')->setLinkAccessRestrictedPages(TRUE)->setTargetPageUid(123)->setTargetPageType(321)->setNoCache(TRUE)->setUseCacheHash(FALSE);
534 $this->uriBuilder->reset();
535 $this->assertEquals(array(), $this->uriBuilder->getArguments());
536 $this->assertEquals('', $this->uriBuilder->getSection());
537 $this->assertEquals('', $this->uriBuilder->getFormat());
538 $this->assertEquals(FALSE, $this->uriBuilder->getCreateAbsoluteUri());
539 $this->assertEquals(FALSE, $this->uriBuilder->getAddQueryString());
540 $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
541 $this->assertEquals(NULL, $this->uriBuilder->getAddQueryStringMethod());
542 $this->assertEquals(NULL, $this->uriBuilder->getArgumentPrefix());
543 $this->assertEquals(FALSE, $this->uriBuilder->getLinkAccessRestrictedPages());
544 $this->assertEquals(NULL, $this->uriBuilder->getTargetPageUid());
545 $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
546 $this->assertEquals(FALSE, $this->uriBuilder->getNoCache());
547 $this->assertEquals(TRUE, $this->uriBuilder->getUseCacheHash());
548 }
549
550 /**
551 * @test
552 */
553 public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid() {
554 $GLOBALS['TSFE']->id = 123;
555 $this->uriBuilder->setTargetPageUid(321);
556 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
557 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
558 $this->assertEquals($expectedConfiguration, $actualConfiguration);
559 }
560
561 /**
562 * @test
563 */
564 public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet() {
565 $GLOBALS['TSFE']->id = 123;
566 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
567 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
568 $this->assertEquals($expectedConfiguration, $actualConfiguration);
569 }
570
571 /**
572 * @test
573 */
574 public function buildTypolinkConfigurationProperlySetsAdditionalArguments() {
575 $this->uriBuilder->setTargetPageUid(123);
576 $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
577 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz[extbase]=fluid');
578 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
579 $this->assertEquals($expectedConfiguration, $actualConfiguration);
580 }
581
582 /**
583 * @test
584 */
585 public function buildTypolinkConfigurationProperlySetsAddQueryString() {
586 $this->uriBuilder->setTargetPageUid(123);
587 $this->uriBuilder->setAddQueryString(TRUE);
588 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'useCacheHash' => 1);
589 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
590 $this->assertEquals($expectedConfiguration, $actualConfiguration);
591 }
592
593 /**
594 * @test
595 */
596 public function buildTypolinkConfigurationProperlySetsAddQueryStringMethod() {
597 $this->uriBuilder->setTargetPageUid(123);
598 $this->uriBuilder->setAddQueryString(TRUE);
599 $this->uriBuilder->setAddQueryStringMethod('GET,POST');
600 $expectedConfiguration = array('parameter' => 123, 'addQueryString' => 1, 'addQueryString.' => array('method' => 'GET,POST'), 'useCacheHash' => 1);
601 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
602 $this->assertEquals($expectedConfiguration, $actualConfiguration);
603 }
604
605 /**
606 * @test
607 */
608 public function buildTypolinkConfigurationConvertsDomainObjects() {
609 $mockDomainObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
610 $mockDomainObject1->_set('uid', '123');
611 $mockDomainObject2 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
612 $mockDomainObject2->_set('uid', '321');
613 $this->uriBuilder->setTargetPageUid(123);
614 $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
615 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject=123&baz[someOtherDomainObject]=321');
616 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
617 $this->assertEquals($expectedConfiguration, $actualConfiguration);
618 }
619
620 /**
621 * @test
622 */
623 public function buildTypolinkConfigurationResolvesPageTypeFromFormat() {
624 $this->uriBuilder->setTargetPageUid(123);
625 $this->uriBuilder->setFormat('txt');
626 $this->mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
627
628 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
629 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
630 ->will($this->returnValue(array('view' => array('formatToPageTypeMapping' => array('txt' => 2)))));
631 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
632
633 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
634 ->with('SomeExtensionNameFromRequest', 'txt')
635 ->will($this->returnValue(2));
636
637 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
638 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
639 $this->assertEquals($expectedConfiguration, $actualConfiguration);
640 }
641
642 /**
643 * @test
644 */
645 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfNoMappingIsConfigured() {
646 $this->uriBuilder->setTargetPageUid(123);
647 $this->uriBuilder->setFormat('txt');
648
649 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
650 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue(array()));
651 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
652
653 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
654 ->with(NULL, 'txt')
655 ->will($this->returnValue(0));
656
657 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
658 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
659
660 $this->assertEquals($expectedConfiguration, $actualConfiguration);
661 }
662
663 /**
664 * @test
665 */
666 public function buildTypolinkConfigurationResolvesDefaultPageTypeFromFormatIfFormatIsNotMapped() {
667 $this->uriBuilder->setTargetPageUid(123);
668 $this->uriBuilder->setFormat('txt');
669
670 $mockConfigurationManager = $this->getMock(ConfigurationManager::class);
671 $mockConfigurationManager->expects($this->any())->method('getConfiguration')
672 ->will($this->returnValue(array(array('view' => array('formatToPageTypeMapping' => array('pdf' => 2))))));
673 $this->uriBuilder->_set('configurationManager', $mockConfigurationManager);
674
675 $this->mockExtensionService->expects($this->any())->method('getTargetPageTypeByFormat')
676 ->with(NULL, 'txt')
677 ->will($this->returnValue(0));
678
679 $expectedConfiguration = array('parameter' => '123,0', 'useCacheHash' => 1);
680 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
681
682 $this->assertEquals($expectedConfiguration, $actualConfiguration);
683 }
684
685
686 /**
687 * @test
688 */
689 public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
690 $this->uriBuilder->setTargetPageUid(123);
691 $this->uriBuilder->setNoCache(TRUE);
692 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
693 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
694 $this->assertEquals($expectedConfiguration, $actualConfiguration);
695 }
696
697 /**
698 * @test
699 */
700 public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
701 $this->uriBuilder->setTargetPageUid(123);
702 $this->uriBuilder->setUseCacheHash(FALSE);
703 $expectedConfiguration = array('parameter' => 123);
704 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
705 $this->assertEquals($expectedConfiguration, $actualConfiguration);
706 }
707
708 /**
709 * @test
710 */
711 public function buildTypolinkConfigurationConsidersSection() {
712 $this->uriBuilder->setTargetPageUid(123);
713 $this->uriBuilder->setSection('SomeSection');
714 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
715 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
716 $this->assertEquals($expectedConfiguration, $actualConfiguration);
717 }
718
719 /**
720 * @test
721 */
722 public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
723 $this->uriBuilder->setTargetPageUid(123);
724 $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
725 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
726 $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
727 $this->assertEquals($expectedConfiguration, $actualConfiguration);
728 }
729
730 /**
731 * @test
732 */
733 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
734 $mockDomainObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
735 $mockDomainObject1->_set('uid', '123');
736 $mockDomainObject2 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
737 $mockDomainObject2->_set('uid', '321');
738 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => '123', 'second' => array('domainObject2' => '321'));
739 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
740 $this->assertEquals($expectedResult, $actualResult);
741 }
742
743 /**
744 * @test
745 */
746 public function convertDomainObjectsToIdentityArraysConvertsObjectStoragesWithDomainObjects() {
747 $objectStorage = new ObjectStorage();
748 $mockChildObject1 = $this->getAccessibleMock(AbstractEntity::class, array('dummy'));
749 $mockChildObject1->_set('uid', '123');
750 $objectStorage->attach($mockChildObject1);
751 $expectedResult = array('foo' => array('bar' => 'baz'), 'objectStorage' => array('123'));
752 $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'objectStorage' => $objectStorage));
753 $this->assertEquals($expectedResult, $actualResult);
754 }
755
756 /**
757 * @test
758 */
759 public function conversionOfTansientObjectsIsInvoked() {
760 $mockValueObject = new ValueObjectFixture();
761 $mockValueObject->name = 'foo';
762 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|Object $mockUriBuilder */
763 $mockUriBuilder = $this->getAccessibleMock(UriBuilder::class, array('convertTransientObjectToArray'));
764 $mockUriBuilder->expects($this->once())->method('convertTransientObjectToArray')->will($this->returnValue(array('foo' => 'bar')));
765 $actualResult = $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockValueObject));
766 $expectedResult = array('object' => array('foo' => 'bar'));
767 $this->assertEquals($expectedResult, $actualResult);
768 }
769
770 /**
771 * @test
772 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException
773 */
774 public function conversionOfTansientObjectsThrowsExceptionForOtherThanValueObjects() {
775 $mockEntity = new EntityFixture();
776 $mockEntity->name = 'foo';
777 /** @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|Object $mockUriBuilder */
778 $mockUriBuilder = $this->getAccessibleMock(UriBuilder::class, array('dummy'));
779 $mockUriBuilder->_call('convertDomainObjectsToIdentityArrays', array('object' => $mockEntity));
780 }
781
782 /**
783 * @test
784 */
785 public function tansientObjectsAreConvertedToAnArrayOfProperties() {
786 $mockValueObject = new ValueObjectFixture();
787 $mockValueObject->name = 'foo';
788 $uriBuilder = new UriBuilder();
789 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
790 $expectedResult = array('name' => 'foo', 'object' => NULL, 'uid' => NULL, 'pid' => NULL);
791 $this->assertEquals($expectedResult, $actualResult);
792 }
793
794 /**
795 * @test
796 */
797 public function tansientObjectsWithObjectStorageAreConvertedToAnArrayOfProperties() {
798 $mockValueObject = new ValueObjectFixture();
799 $objectStorage = new ObjectStorage();
800 $mockValueObject->name = 'foo';
801 $mockValueObject2 = new ValueObjectFixture();
802 $mockValueObject2->name = 'bar';
803 $objectStorage->attach($mockValueObject2);
804 $mockValueObject->object = $objectStorage;
805 $uriBuilder = new UriBuilder();
806 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
807 $expectedResult = array(
808 'name' => 'foo',
809 'object' => array(
810 array(
811 'name' => 'bar',
812 'uid' => NULL,
813 'pid' => NULL,
814 'object' => NULL
815 )
816 ),
817 'uid' => NULL,
818 'pid' => NULL
819 );
820 $this->assertEquals($expectedResult, $actualResult);
821 }
822
823 /**
824 * @test
825 */
826 public function tansientObjectsAreRecursivelyConverted() {
827 $mockInnerValueObject2 = new ValueObjectFixture();
828 $mockInnerValueObject2->name = 'foo';
829 $mockInnerValueObject2->uid = 99;
830 $mockInnerValueObject1 = new ValueObjectFixture();
831 $mockInnerValueObject1->object = $mockInnerValueObject2;
832 $mockValueObject = new ValueObjectFixture();
833 $mockValueObject->object = $mockInnerValueObject1;
834 $uriBuilder = new UriBuilder();
835 $actualResult = $uriBuilder->convertTransientObjectToArray($mockValueObject);
836 $expectedResult = array(
837 'name' => NULL,
838 'object' => array(
839 'name' => NULL,
840 'object' => 99,
841 'uid' => NULL,
842 'pid' => NULL
843 ),
844 'uid' => NULL,
845 'pid' => NULL
846 );
847 $this->assertEquals($expectedResult, $actualResult);
848 }
849
850 /**
851 * @test
852 */
853 public function removeDefaultControllerAndActionDoesNotModifyArgumentsifSpecifiedControlerAndActionIsNotEqualToDefaults() {
854 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
855 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
856 $arguments = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
857 $extensionName = 'ExtensionName';
858 $pluginName = 'PluginName';
859 $expectedResult = array('controller' => 'SomeController', 'action' => 'someAction', 'foo' => 'bar');
860 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
861 $this->assertEquals($expectedResult, $actualResult);
862 }
863
864 /**
865 * @test
866 */
867 public function removeDefaultControllerAndActionRemovesControllerIfItIsEqualToTheDefault() {
868 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
869 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
870 $arguments = array('controller' => 'DefaultController', 'action' => 'someAction', 'foo' => 'bar');
871 $extensionName = 'ExtensionName';
872 $pluginName = 'PluginName';
873 $expectedResult = array('action' => 'someAction', 'foo' => 'bar');
874 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
875 $this->assertEquals($expectedResult, $actualResult);
876 }
877
878 /**
879 * @test
880 */
881 public function removeDefaultControllerAndActionRemovesActionIfItIsEqualToTheDefault() {
882 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
883 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'SomeController')->will($this->returnValue('defaultAction'));
884 $arguments = array('controller' => 'SomeController', 'action' => 'defaultAction', 'foo' => 'bar');
885 $extensionName = 'ExtensionName';
886 $pluginName = 'PluginName';
887 $expectedResult = array('controller' => 'SomeController', 'foo' => 'bar');
888 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
889 $this->assertEquals($expectedResult, $actualResult);
890 }
891
892 /**
893 * @test
894 */
895 public function removeDefaultControllerAndActionRemovesControllerAndActionIfBothAreEqualToTheDefault() {
896 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultControllerNameByPlugin')->with('ExtensionName', 'PluginName')->will($this->returnValue('DefaultController'));
897 $this->mockExtensionService->expects($this->atLeastOnce())->method('getDefaultActionNameByPluginAndController')->with('ExtensionName', 'PluginName', 'DefaultController')->will($this->returnValue('defaultAction'));
898 $arguments = array('controller' => 'DefaultController', 'action' => 'defaultAction', 'foo' => 'bar');
899 $extensionName = 'ExtensionName';
900 $pluginName = 'PluginName';
901 $expectedResult = array('foo' => 'bar');
902 $actualResult = $this->uriBuilder->_callRef('removeDefaultControllerAndAction', $arguments, $extensionName, $pluginName);
903 $this->assertEquals($expectedResult, $actualResult);
904 }
905
906 /**
907 * @return array
908 */
909 public function convertIteratorToArrayConvertsIteratorsToArrayProvider() {
910 return array(
911 'Extbase ObjectStorage' => array(new ObjectStorage()),
912 'SplObjectStorage' => array(new \SplObjectStorage()),
913 'ArrayIterator' => array(new \ArrayIterator())
914 );
915 }
916
917 /**
918 * @dataProvider convertIteratorToArrayConvertsIteratorsToArrayProvider
919 * @test
920 */
921 public function convertIteratorToArrayConvertsIteratorsToArray($iterator) {
922 $result = $this->uriBuilder->_call('convertIteratorToArray', $iterator);
923 $this->assertTrue(is_array($result));
924 }
925
926 }