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