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