‪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 
20 use PHPUnit\Framework\Attributes\Test;
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
30 
31 final class ‪RouterTest extends FunctionalTestCase
32 {
33  protected bool ‪$initializeDatabase = false;
34 
35  #[Test]
36  public function ‪routerReturnsRouteForAlias(): void
37  {
38  $subject = $this->get(Router::class);
39  $subject->addRoute(
40  'new_route_identifier',
41  new ‪Route('/new/route/path', []),
42  ['old_route_identifier']
43  );
44  self::assertTrue($subject->hasRoute('new_route_identifier'));
45  self::assertTrue($subject->hasRoute('old_route_identifier'));
46  }
47 
48  #[Test]
49  public function ‪matchResultFindsProperRoute(): void
50  {
51  $subject = $this->get(Router::class);
52  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
53  $request = new ‪ServerRequest('https://example.com/typo3/login', 'GET', null, [], $serverParams);
54  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
55  $result = $subject->matchResult($request);
56  self::assertEquals('/login', $result->getRoute()->getPath());
57  }
58 
59  #[Test]
61  {
62  $subject = $this->get(Router::class);
63  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
64  $request = new ‪ServerRequest('https://example.com/typo3/this-path/does-not-exist', 'GET', null, [], $serverParams);
65  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
66  $this->expectException(ResourceNotFoundException::class);
67  $subject->matchResult($request);
68  }
69 
70  #[Test]
72  {
73  $subject = $this->get(Router::class);
74  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
75  $request = new ‪ServerRequest('https://example.com/typo3/login/password-reset/initiate-reset', 'GET', null, [], $serverParams);
76  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
77  $this->expectException(MethodNotAllowedException::class);
78  $subject->matchResult($request);
79  }
80 
81  #[Test]
83  {
84  $subject = $this->get(Router::class);
85  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
86  $request = new ‪ServerRequest('https://example.com/typo3/login/password-reset/initiate-reset', 'POST', null, [], $serverParams);
87  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
88  $result = $subject->matchResult($request);
89  self::assertEquals('/login/password-reset/initiate-reset', $result->getRoute()->getPath());
90  }
91 
92  #[Test]
94  {
95  $subject = $this->get(Router::class);
96  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
97  $request = new ‪ServerRequest('https://example.com/typo3/module/site/configuration/delete', 'POST', null, [], $serverParams);
98  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
99  $result = $subject->matchResult($request);
100  self::assertEquals('/module/site/configuration/delete', $result->getRoute()->getPath());
101  self::assertInstanceOf(ModuleInterface::class, $result->getRoute()->getOption('module'));
102  }
103 
104  #[Test]
106  {
107  $this->expectException(MethodNotAllowedException::class);
108  $this->expectExceptionCode(1612649842);
109 
110  $subject = $this->get(Router::class);
111  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
112  $request = new ‪ServerRequest('https://example.com/typo3/module/site/configuration/delete', 'GET', null, [], $serverParams);
113  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
114  $subject->matchResult($request);
115  }
116 
117  #[Test]
119  {
120  $subject = $this->get(Router::class);
121  $subject->addRoute('custom-route', (new ‪Route('/my-path/{identifier}', []))->setMethods(['POST']));
122  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
123  $request = new ‪ServerRequest('https://example.com/typo3/my-path/my-identifier', 'POST', null, [], $serverParams);
124  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
125  $result = $subject->matchResult($request);
126  self::assertEquals('custom-route', $result->getRouteName());
127  self::assertEquals(['identifier' => 'my-identifier'], $result->getArguments());
128  }
129 
130  #[Test]
132  {
133  $subject = $this->get(Router::class);
134  $subject->addRoute('main_module', new ‪Route('/module/main/module', []));
135  $routeCollection = new ‪RouteCollection();
136  $routeCollection->add('subroute', new ‪Route('/subroute', []));
137  $routeCollection->addNamePrefix('main_module.');
138  $routeCollection->addPrefix('/module/main/module');
139  $subject->addRouteCollection($routeCollection);
140 
141  $serverParams = array_replace(‪$_SERVER, ['HTTP_HOST' => 'example.com', 'HTTPS' => 'on', 'SCRIPT_NAME' => '/index.php']);
142 
143  $request = new ‪ServerRequest('/typo3/module/main/module', 'GET', null, [], $serverParams);
144  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
145  $resultMainModule = $subject->matchResult($request);
146  self::assertEquals('main_module', $resultMainModule->getRouteName());
147  self::assertEquals('/module/main/module', $resultMainModule->getRoute()->getPath());
148 
149  $request = new ‪ServerRequest('/typo3/module/main/module/subroute', 'GET', null, [], $serverParams);
150  $request = $request->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
151  $resultSubRoute = $subject->matchResult($request);
152  self::assertEquals('main_module.subroute', $resultSubRoute->getRouteName());
153  self::assertEquals('/module/main/module/subroute', $resultSubRoute->getRoute()->getPath());
154  }
155 }
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultReturnsRouteWithMethodLimitation
‪matchResultReturnsRouteWithMethodLimitation()
Definition: RouterTest.php:82
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\routerReturnsRouteForAlias
‪routerReturnsRouteForAlias()
Definition: RouterTest.php:36
‪TYPO3\CMS\Backend\Routing\Exception\MethodNotAllowedException
Definition: MethodNotAllowedException.php:23
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultReturnsRouteForSubRoute
‪matchResultReturnsRouteForSubRoute()
Definition: RouterTest.php:131
‪TYPO3\CMS\Core\Routing\RouteCollection
Definition: RouteCollection.php:30
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest
Definition: RouterTest.php:32
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultThrowsExceptionForWrongHttpMethod
‪matchResultThrowsExceptionForWrongHttpMethod()
Definition: RouterTest.php:105
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultThrowsInvalidMethodForValidRoute
‪matchResultThrowsInvalidMethodForValidRoute()
Definition: RouterTest.php:71
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultFindsProperRoute
‪matchResultFindsProperRoute()
Definition: RouterTest.php:49
‪TYPO3\CMS\Backend\Tests\Functional\Routing
Definition: RouterTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\$initializeDatabase
‪bool $initializeDatabase
Definition: RouterTest.php:33
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultThrowsExceptionOnInvalidRoute
‪matchResultThrowsExceptionOnInvalidRoute()
Definition: RouterTest.php:60
‪TYPO3\CMS\Backend\Tests\Functional\Routing\RouterTest\matchResultReturnsRouteWithPlaceholderAndMethodLimitation
‪matchResultReturnsRouteWithPlaceholderAndMethodLimitation()
Definition: RouterTest.php:118
‪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:93
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38