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