[FEATURE] Use symfony/routing for Site Resolving
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Site / Entity / SiteTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\Site\Entity;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Crypto\Random;
20 use TYPO3\CMS\Core\Error\PageErrorHandler\FluidPageErrorHandler;
21 use TYPO3\CMS\Core\Error\PageErrorHandler\InvalidPageErrorHandlerException;
22 use TYPO3\CMS\Core\Error\PageErrorHandler\PageContentErrorHandler;
23 use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
24 use TYPO3\CMS\Core\Site\Entity\Site;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28 class SiteTest extends UnitTestCase
29 {
30 public function getBaseReturnsProperUriDataProvider()
31 {
32 return [
33 'URL with scheme and domain' => ['https://www.typo3.org', 'https://www.typo3.org'],
34 'URL with scheme and domain and path' => ['https://www.typo3.org/howdy', 'https://www.typo3.org/howdy'],
35 'URL with scheme and domain and path with trailing slash' => ['https://www.typo3.org/howdy/', 'https://www.typo3.org/howdy/'],
36 'URL without scheme, but with domain' => ['www.typo3.org', '//www.typo3.org'],
37 'URL without scheme, but with domain and path' => ['www.typo3.org/partner', '//www.typo3.org/partner'],
38 'URL without scheme, but with domain and path and trailing slash' => ['www.typo3.org/partner/', '//www.typo3.org/partner/'],
39 'URL without scheme and domain but with absolute path' => ['/partner', '/partner'],
40 'URL without scheme and domain but with absolute path and trailing slash' => ['/partner/', '/partner/'],
41 'URL without scheme, domain but with random path receives a scheme divider' => ['partner/', '/partner/'],
42 'URL with ID query parameter' => ['/partner/?id=nice-to-see-you', '/partner/?id=nice-to-see-you'],
43 'URL with unknown query parameter' => ['/partner/?in-crime=nice-to-see-you', '/partner/?in-crime=nice-to-see-you'],
44 ];
45 }
46
47 /**
48 * @test
49 * @dataProvider getBaseReturnsProperUriDataProvider
50 */
51 public function getBaseReturnsProperUri($input, $expected)
52 {
53 $subject = new Site('all-your-base-belongs-to-us', 13, [
54 'base' => $input,
55 'languages' => []
56 ]);
57 $this->assertEquals($expected, $subject->getBase());
58 }
59
60 /**
61 * Consists of three parts:
62 * - input "site" base
63 * - input "site language" base
64 * - expected "site language" base after it is glued together
65 */
66 public function getBaseForSiteLanguageReturnsProperUriDataProvider()
67 {
68 return [
69 'Language as a regular path segment' => [
70 'https://www.typo3.org',
71 'en',
72 'https://www.typo3.org/en',
73 ],
74 'Language with two path segments' => [
75 'https://www.typo3.org',
76 'us/en',
77 'https://www.typo3.org/us/en',
78 ],
79 'Site base is added to absolute path segment' => [
80 'https://www.typo3.com/microsites/',
81 '/onboarding/',
82 'https://www.typo3.com/microsites/onboarding/',
83 ],
84 'Site base is prefixed to absolute path segment' => [
85 'https://www.typo3.com/microsites/',
86 'onboarding/',
87 'https://www.typo3.com/microsites/onboarding/',
88 ],
89 'Language with domain and scheme, do not care about site base' => [
90 'https://www.typo3.org',
91 'https://www.typo3.it',
92 'https://www.typo3.it',
93 ],
94 'Language with domain but no scheme, do not care about site base' => [
95 'blabla.car',
96 'www.typo3.fr',
97 '//www.typo3.fr',
98 ],
99 ];
100 }
101
102 /**
103 * This test shows that the a base from a site language is properly "inheriting" the base
104 * from a site if it isn't absolute.
105 *
106 * @test
107 * @dataProvider getBaseForSiteLanguageReturnsProperUriDataProvider
108 */
109 public function getBaseForSiteLanguageReturnsProperUri($siteBase, $languageBase, $expected)
110 {
111 $subject = new Site('all-of-base', 13, [
112 'base' => $siteBase,
113 'languages' => [
114 [
115 'languageId' => 0,
116 'base' => $languageBase,
117 'locale' => 'it_IT.UTF-8',
118 ]
119 ]
120 ]);
121 $this->assertEquals($expected, $subject->getLanguageById(0)->getBase());
122 }
123
124 /**
125 * @test
126 */
127 public function getErrorHandlerReturnsConfiguredErrorHandler()
128 {
129 $subject = new Site('aint-misbehaving', 13, [
130 'languages' => [],
131 'errorHandling' => [
132 [
133 'errorCode' => 123,
134 'errorHandler' => 'Fluid',
135 ],
136 [
137 'errorCode' => 124,
138 'errorContentSource' => 123,
139 'errorHandler' => 'Page'
140 ],
141 [
142 'errorCode' => 125,
143 'errorHandler' => 'PHP',
144 'errorContentSource' => 123,
145 'errorPhpClassFQCN' => PageContentErrorHandler::class
146 ]
147 ]
148 ]);
149
150 $fluidProphecy = $this->prophesize(FluidPageErrorHandler::class);
151 GeneralUtility::addInstance(FluidPageErrorHandler::class, $fluidProphecy->reveal());
152
153 $this->assertEquals(true, $subject->getErrorHandler(123) instanceof PageErrorHandlerInterface);
154 $this->assertEquals(true, $subject->getErrorHandler(124) instanceof PageErrorHandlerInterface);
155 $this->assertEquals(true, $subject->getErrorHandler(125) instanceof PageErrorHandlerInterface);
156 }
157
158 /**
159 * @test
160 */
161 public function getErrorHandlerThrowsExceptionOnInvalidErrorHandler()
162 {
163 $this->expectException(InvalidPageErrorHandlerException::class);
164 $this->expectExceptionCode(1527432330);
165 $this->expectExceptionMessage('The configured error handler "' . Random::class . '" for status code 404 must implement the PageErrorHandlerInterface.');
166 $subject = new Site('aint-misbehaving', 13, [
167 'languages' => [],
168 'errorHandling' => [
169 [
170 'errorCode' => 404,
171 'errorHandler' => 'PHP',
172 'errorPhpClassFQCN' => Random::class
173 ],
174 ]
175 ]);
176 $subject->getErrorHandler(404);
177 }
178
179 /**
180 * @test
181 */
182 public function getErrorHandlerThrowsExceptionWhenNoErrorHandlerIsConfigured()
183 {
184 $this->expectException(\RuntimeException::class);
185 $this->expectExceptionCode(1522495914);
186 $this->expectExceptionMessage('No error handler given for the status code "404".');
187 $subject = new Site('aint-misbehaving', 13, ['languages' => []]);
188 $subject->getErrorHandler(404);
189 }
190
191 /**
192 * @test
193 */
194 public function getErrorHandlerThrowsExceptionWhenNoErrorHandlerForStatusCodeIsConfigured()
195 {
196 $this->expectException(\RuntimeException::class);
197 $this->expectExceptionCode(1522495914);
198 $this->expectExceptionMessage('No error handler given for the status code "404".');
199 $subject = new Site('aint-misbehaving', 13, [
200 'languages' => [],
201 'errorHandling' => [
202 [
203 'errorCode' => 403,
204 'errorHandler' => 'Does it really matter?'
205 ],
206 ]
207 ]);
208 $subject->getErrorHandler(404);
209 }
210 }