‪TYPO3CMS  11.5
SiteTest.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 Prophecy\PhpUnit\ProphecyTrait;
21 use Psr\Http\Message\ResponseFactoryInterface;
22 use Symfony\Component\DependencyInjection\Container;
40 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
41 
42 class ‪SiteTest extends UnitTestCase
43 {
44  use ProphecyTrait;
45 
46  public function ‪tearDown(): void
47  {
48  parent::tearDown();
49  GeneralUtility::purgeInstances();
50  }
51 
52  public function ‪getBaseReturnsProperUriDataProvider(): array
53  {
54  return [
55  'URL with scheme and domain' => ['https://www.typo3.org', 'https://www.typo3.org'],
56  'URL with scheme and domain and path' => ['https://www.typo3.org/howdy', 'https://www.typo3.org/howdy'],
57  'URL with scheme and domain and path with trailing slash' => ['https://www.typo3.org/howdy/', 'https://www.typo3.org/howdy/'],
58  'URL without scheme, but with domain' => ['www.typo3.org', '//www.typo3.org'],
59  'URL without scheme, but with domain and path' => ['www.typo3.org/partner', '//www.typo3.org/partner'],
60  'URL without scheme, but with domain and path and trailing slash' => ['www.typo3.org/partner/', '//www.typo3.org/partner/'],
61  'URL without scheme and domain but with absolute path' => ['/partner', '/partner'],
62  'URL without scheme and domain but with absolute path and trailing slash' => ['/partner/', '/partner/'],
63  'URL without scheme, domain but with random path receives a scheme divider' => ['partner/', '/partner/'],
64  'URL with ID query parameter' => ['/partner/?id=nice-to-see-you', '/partner/?id=nice-to-see-you'],
65  'URL with unknown query parameter' => ['/partner/?in-crime=nice-to-see-you', '/partner/?in-crime=nice-to-see-you'],
66  ];
67  }
68 
73  public function ‪getBaseReturnsProperUri($input, $expected): void
74  {
75  $subject = new ‪Site('all-your-base-belongs-to-us', 13, [
76  'base' => $input,
77  'languages' => [],
78  ]);
79  self::assertEquals(new ‪Uri($expected), $subject->getBase());
80  }
81 
89  {
90  return [
91  'Language as a regular path segment' => [
92  'https://www.typo3.org',
93  'en',
94  'https://www.typo3.org/en',
95  ],
96  'Language with two path segments' => [
97  'https://www.typo3.org',
98  'us/en',
99  'https://www.typo3.org/us/en',
100  ],
101  'Site base is added to absolute path segment' => [
102  'https://www.typo3.com/microsites/',
103  '/onboarding/',
104  'https://www.typo3.com/microsites/onboarding/',
105  ],
106  'Site base is prefixed to absolute path segment' => [
107  'https://www.typo3.com/microsites/',
108  'onboarding/',
109  'https://www.typo3.com/microsites/onboarding/',
110  ],
111  'Language with domain and scheme, do not care about site base' => [
112  'https://www.typo3.org',
113  'https://www.typo3.it',
114  'https://www.typo3.it',
115  ],
116  'Language with domain but no scheme, do not care about site base' => [
117  'blabla.car',
118  'www.typo3.fr',
119  '//www.typo3.fr',
120  ],
121  ];
122  }
123 
131  public function ‪getBaseForSiteLanguageReturnsProperUri($siteBase, $languageBase, $expected): void
132  {
133  $subject = new Site('all-of-base', 13, [
134  'base' => $siteBase,
135  'languages' => [
136  [
137  'languageId' => 0,
138  'base' => $languageBase,
139  'locale' => 'it_IT.UTF-8',
140  ],
141  ],
142  ]);
143  self::assertEquals(new Uri($expected), $subject->getLanguageById(0)->getBase());
144  }
145 
150  {
151  $subject = new Site('aint-misbehaving', 13, [
152  'languages' => [],
153  'errorHandling' => [
154  [
155  'errorCode' => 123,
156  'errorHandler' => 'Fluid',
157  ],
158  [
159  'errorCode' => 124,
160  'errorContentSource' => 123,
161  'errorHandler' => 'Page',
162  ],
163  [
164  'errorCode' => 125,
165  'errorHandler' => 'PHP',
166  'errorContentSource' => 123,
167  'errorPhpClassFQCN' => PageContentErrorHandler::class,
168  ],
169  ],
170  ]);
171 
172  $fluidProphecy = $this->prophesize(FluidPageErrorHandler::class);
173  GeneralUtility::addInstance(FluidPageErrorHandler::class, $fluidProphecy->reveal());
174 
175  $app = new class () extends Application {
176  // This is ugly but php-cs-fixer insists.
177  public function __construct() {}
178  };
179  $link = new class () extends LinkService {
180  // This is ugly but php-cs-fixer insists.
181  public function __construct() {}
182  };
183  $siteFinder = new class () extends SiteFinder {
184  // This is ugly but php-cs-fixer insists.
185  public function __construct() {}
186  };
187  $cacheManager = new class () extends CacheManager {
188  public function getCache($identifier)
189  {
190  return new class () extends PhpFrontend {
191  // This is ugly but php-cs-fixer insists.
192  public function __construct() {}
193  };
194  }
195  };
196  $container = new Container();
197  $container->set(Application::class, $app);
198  $container->set(Features::class, new Features());
199  $container->set(RequestFactory::class, new RequestFactory());
200  $container->set(ResponseFactoryInterface::class, new ResponseFactory());
201  $container->set(LinkService::class, $link);
202  $container->set(SiteFinder::class, $siteFinder);
203  $container->set(CacheManager::class, $cacheManager);
204  GeneralUtility::setContainer($container);
205 
206  self::assertInstanceOf(PageErrorHandlerInterface::class, $subject->getErrorHandler(123));
207  self::assertInstanceOf(PageErrorHandlerInterface::class, $subject->getErrorHandler(124));
208  self::assertInstanceOf(PageErrorHandlerInterface::class, $subject->getErrorHandler(125));
209  }
210 
215  {
216  $this->expectException(InvalidPageErrorHandlerException::class);
217  $this->expectExceptionCode(1527432330);
218  $this->expectExceptionMessage('The configured error handler "' . Random::class . '" for status code 404 must implement the PageErrorHandlerInterface.');
219  $subject = new Site('aint-misbehaving', 13, [
220  'languages' => [],
221  'errorHandling' => [
222  [
223  'errorCode' => 404,
224  'errorHandler' => 'PHP',
225  'errorPhpClassFQCN' => Random::class,
226  ],
227  ],
228  ]);
229  $subject->getErrorHandler(404);
230  }
231 
236  {
237  $this->expectException(PageErrorHandlerNotConfiguredException::class);
238  $this->expectExceptionCode(1522495914);
239  $this->expectExceptionMessage('No error handler given for the status code "404".');
240  $subject = new Site('aint-misbehaving', 13, ['languages' => []]);
241  $subject->getErrorHandler(404);
242  }
243 
248  {
249  $this->expectException(PageErrorHandlerNotConfiguredException::class);
250  $this->expectExceptionCode(1522495914);
251  $this->expectExceptionMessage('No error handler given for the status code "404".');
252  $subject = new Site('aint-misbehaving', 13, [
253  'languages' => [],
254  'errorHandling' => [
255  [
256  'errorCode' => 403,
257  'errorHandler' => 'Does it really matter?',
258  ],
259  ],
260  ]);
261  $subject->getErrorHandler(404);
262  }
263 }
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getErrorHandlerThrowsExceptionOnInvalidErrorHandler
‪getErrorHandlerThrowsExceptionOnInvalidErrorHandler()
Definition: SiteTest.php:213
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getBaseForSiteLanguageReturnsProperUriDataProvider
‪getBaseForSiteLanguageReturnsProperUriDataProvider()
Definition: SiteTest.php:87
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getErrorHandlerReturnsConfiguredErrorHandler
‪getErrorHandlerReturnsConfiguredErrorHandler()
Definition: SiteTest.php:148
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity
Definition: SiteLanguageTest.php:18
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getBaseReturnsProperUriDataProvider
‪getBaseReturnsProperUriDataProvider()
Definition: SiteTest.php:51
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Error\PageErrorHandler\FluidPageErrorHandler
Definition: FluidPageErrorHandler.php:32
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\tearDown
‪tearDown()
Definition: SiteTest.php:45
‪TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerNotConfiguredException
Definition: PageErrorHandlerNotConfiguredException.php:26
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getErrorHandlerThrowsExceptionWhenNoErrorHandlerForStatusCodeIsConfigured
‪getErrorHandlerThrowsExceptionWhenNoErrorHandlerForStatusCodeIsConfigured()
Definition: SiteTest.php:246
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Core\Error\PageErrorHandler\InvalidPageErrorHandlerException
Definition: InvalidPageErrorHandlerException.php:26
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getBaseForSiteLanguageReturnsProperUri
‪getBaseForSiteLanguageReturnsProperUri($siteBase, $languageBase, $expected)
Definition: SiteTest.php:130
‪TYPO3\CMS\Core\Configuration\Features
Definition: Features.php:56
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Error\PageErrorHandler\PageContentErrorHandler
Definition: PageContentErrorHandler.php:51
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest
Definition: SiteTest.php:43
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:31
‪TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface
Definition: PageErrorHandlerInterface.php:29
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getBaseReturnsProperUri
‪getBaseReturnsProperUri($input, $expected)
Definition: SiteTest.php:72
‪TYPO3\CMS\Core\Http\ResponseFactory
Definition: ResponseFactory.php:27
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:24
‪TYPO3\CMS\Core\Tests\Unit\Site\Entity\SiteTest\getErrorHandlerThrowsExceptionWhenNoErrorHandlerIsConfigured
‪getErrorHandlerThrowsExceptionWhenNoErrorHandlerIsConfigured()
Definition: SiteTest.php:234
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\Http\Application
Definition: Application.php:38