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