[FEATURE] Configurable Route Parameters
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Routing / UriBuilderTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\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
17 use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
18 use TYPO3\CMS\Backend\Routing\Route;
19 use TYPO3\CMS\Backend\Routing\UriBuilder;
20 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22
23 /**
24 * Test case
25 */
26 class UriBuilderTest extends UnitTestCase
27 {
28 /**
29 * @var UriBuilder|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface
30 */
31 protected $uriBuilder;
32
33 protected function setUp()
34 {
35 parent::setUp();
36 $this->uriBuilder = $this->getAccessibleMock(UriBuilder::class, ['loadBackendRoutes']);
37 }
38
39 protected function tearDown()
40 {
41 unset($this->uriBuilder);
42 }
43
44 /**
45 * @return array
46 */
47 public function validRoutesAreBuiltDataProvider()
48 {
49 return [
50 'plain route' => [
51 [ 'route' => new Route('/test/route', []) ],
52 'route',
53 [],
54 '/typo3/index.php?route=%2Ftest%2Froute&token=dummyToken',
55 ],
56 'AJAX route' => [
57 [ 'route' => new Route('/test/route', [ 'ajax' => true ]) ],
58 'route',
59 [],
60 '/typo3/index.php?ajaxID=%2Ftest%2Froute&ajaxToken=dummyToken',
61 ],
62 'plain route with default parameters' => [
63 [ 'route' => new Route('/test/route', [ 'parameters' => [ 'key' => 'value' ] ]) ],
64 'route',
65 [],
66 '/typo3/index.php?route=%2Ftest%2Froute&token=dummyToken&key=value',
67 ],
68 'AJAX route with default parameters' => [
69 [ 'route' => new Route('/test/route', [ 'ajax' => true, 'parameters' => [ 'key' => 'value' ] ]) ],
70 'route',
71 [],
72 '/typo3/index.php?ajaxID=%2Ftest%2Froute&ajaxToken=dummyToken&key=value',
73 ],
74 'plain route with overridden parameters' => [
75 [ 'route' => new Route('/test/route', [ 'parameters' => [ 'key' => 'value' ] ]) ],
76 'route',
77 ['key' => 'overridden'],
78 '/typo3/index.php?route=%2Ftest%2Froute&token=dummyToken&key=overridden',
79 ],
80 'AJAX route with overridden parameters' => [
81 [ 'route' => new Route('/test/route', [ 'ajax' => true, 'parameters' => [ 'key' => 'value' ] ]) ],
82 'route',
83 ['key' => 'overridden'],
84 '/typo3/index.php?ajaxID=%2Ftest%2Froute&ajaxToken=dummyToken&key=overridden',
85 ],
86 ];
87 }
88
89 /**
90 * @param Route[] $routes
91 * @param string $routeName
92 * @param array $routeParameters
93 * @param string $expectation
94 *
95 * @test
96 * @dataProvider validRoutesAreBuiltDataProvider
97 */
98 public function validRoutesAreBuilt(
99 array $routes,
100 string $routeName,
101 array $routeParameters,
102 string $expectation
103 ) {
104 $this->uriBuilder->_set('routes', $routes);
105
106 $uri = $this->uriBuilder->buildUriFromRoute(
107 $routeName,
108 $routeParameters
109 );
110
111 $this->assertEquals($expectation, $uri->__toString());
112 }
113
114 /**
115 * @test
116 */
117 public function nonExistingRouteThrowsException()
118 {
119 $this->expectException(RouteNotFoundException::class);
120 $this->expectExceptionCode(1476050190);
121 $this->uriBuilder->buildUriFromRoute(uniqid('any'));
122 }
123 }