‪TYPO3CMS  ‪main
RouterTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
30 final class ‪RouterTest extends FunctionalTestCase
31 {
32  protected bool ‪$initializeDatabase = false;
33 
37  public function ‪routerReturnsRouteForAlias(): void
38  {
39  $subject = $this->get(Router::class);
40  $subject->addRoute(
41  'new_route_identifier',
42  new ‪Route('/new/route/path', []),
43  ['old_route_identifier']
44  );
45  self::assertTrue($subject->hasRoute('new_route_identifier'));
46  self::assertTrue($subject->hasRoute('old_route_identifier'));
47  }
48 
52  public function ‪matchResultFindsProperRoute(): void
53  {
54  $subject = $this->get(Router::class);
55  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
56  $request = new ‪ServerRequest('https://example.com/typo3/login', 'GET', null, [], $serverParams);
57  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
58  $result = $subject->matchResult($request);
59  self::assertEquals('/login', $result->getRoute()->getPath());
60  }
61 
66  {
67  $subject = $this->get(Router::class);
68  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
69  $request = new ‪ServerRequest('https://example.com/typo3/this-path/does-not-exist', 'GET', null, [], $serverParams);
70  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
71  $this->expectException(ResourceNotFoundException::class);
72  $subject->matchResult($request);
73  }
74 
79  {
80  $subject = $this->get(Router::class);
81  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
82  $request = new ‪ServerRequest('https://example.com/typo3/login/password-reset/initiate-reset', 'GET', null, [], $serverParams);
83  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
84  $this->expectException(MethodNotAllowedException::class);
85  $subject->matchResult($request);
86  }
87 
92  {
93  $subject = $this->get(Router::class);
94  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
95  $request = new ‪ServerRequest('https://example.com/typo3/login/password-reset/initiate-reset', 'POST', null, [], $serverParams);
96  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
97  $result = $subject->matchResult($request);
98  self::assertEquals('/login/password-reset/initiate-reset', $result->getRoute()->getPath());
99  }
100 
105  {
106  $subject = $this->get(Router::class);
107  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
108  $request = new ‪ServerRequest('https://example.com/typo3/module/site/configuration/delete', 'POST', null, [], $serverParams);
109  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
110  $result = $subject->matchResult($request);
111  self::assertEquals('/module/site/configuration/delete', $result->getRoute()->getPath());
112  self::assertInstanceOf(ModuleInterface::class, $result->getRoute()->getOption('module'));
113  }
114 
119  {
120  $this->expectException(MethodNotAllowedException::class);
121  $this->expectExceptionCode(1612649842);
122 
123  $subject = $this->get(Router::class);
124  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
125  $request = new ‪ServerRequest('https://example.com/typo3/module/site/configuration/delete', 'GET', null, [], $serverParams);
126  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
127  $subject->matchResult($request);
128  }
129 
134  {
135  $subject = $this->get(Router::class);
136  $subject->addRoute('custom-route', (new ‪Route('/my-path/{identifier}', []))->setMethods(['POST']));
137  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
138  $request = new ‪ServerRequest('https://example.com/typo3/my-path/my-identifier', 'POST', null, [], $serverParams);
139  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
140  $result = $subject->matchResult($request);
141  self::assertEquals('custom-route', $result->getRouteName());
142  self::assertEquals(['identifier' => 'my-identifier'], $result->getArguments());
143  }
144 
149  {
150  $subject = $this->get(Router::class);
151  $subject->addRoute('main_module', new ‪Route('/module/main/module', []));
152  $routeCollection = new ‪RouteCollection();
153  $routeCollection->add('subroute', new ‪Route('/subroute', []));
154  $routeCollection->addNamePrefix('main_module.');
155  $routeCollection->addPrefix('/module/main/module');
156  $subject->addRouteCollection($routeCollection);
157 
158  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
159 
160  $request = new ‪ServerRequest('/typo3/module/main/module', 'GET', null, [], $serverParams);
161  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
162  $resultMainModule = $subject->matchResult($request);
163  self::assertEquals('main_module', $resultMainModule->getRouteName());
164  self::assertEquals('/module/main/module', $resultMainModule->getRoute()->getPath());
165 
166  $request = new ‪ServerRequest('/typo3/module/main/module/subroute', 'GET', null, [], $serverParams);
167  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
168  $resultSubRoute = $subject->matchResult($request);
169  self::assertEquals('main_module.subroute', $resultSubRoute->getRouteName());
170  self::assertEquals('/module/main/module/subroute', $resultSubRoute->getRoute()->getPath());
171  }
172 }
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultReturnsRouteWithMethodLimitation
‪matchResultReturnsRouteWithMethodLimitation()
Definition: RouterTest.php:91
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\routerReturnsRouteForAlias
‪routerReturnsRouteForAlias()
Definition: RouterTest.php:37
‪TYPO3\CMS\Backend\Routing\Exception\MethodNotAllowedException
Definition: MethodNotAllowedException.php:23
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultReturnsRouteForSubRoute
‪matchResultReturnsRouteForSubRoute()
Definition: RouterTest.php:148
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:30
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest
Definition: RouterTest.php:31
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultThrowsExceptionForWrongHttpMethod
‪matchResultThrowsExceptionForWrongHttpMethod()
Definition: RouterTest.php:118
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultThrowsInvalidMethodForValidRoute
‪matchResultThrowsInvalidMethodForValidRoute()
Definition: RouterTest.php:78
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultFindsProperRoute
‪matchResultFindsProperRoute()
Definition: RouterTest.php:52
‪TYPO3\CMS\Backend\Tests\Functional\Routing
Definition: RouterTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\$initializeDatabase
‪bool $initializeDatabase
Definition: RouterTest.php:32
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultThrowsExceptionOnInvalidRoute
‪matchResultThrowsExceptionOnInvalidRoute()
Definition: RouterTest.php:65
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultReturnsRouteWithPlaceholderAndMethodLimitation
‪matchResultReturnsRouteWithPlaceholderAndMethodLimitation()
Definition: RouterTest.php:133
‪TYPO3\CMS\Backend\Module\ModuleInterface
Definition: ModuleInterface.php:24
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Backend\Routing\Exception\ResourceNotFoundException
Definition: ResourceNotFoundException.php:23
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:40
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:840
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultReturnsRouteForBackendModuleWithMethodLimitation
‪matchResultReturnsRouteForBackendModuleWithMethodLimitation()
Definition: RouterTest.php:104
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38