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