‪TYPO3CMS  10.4
SiteResolverTest.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 Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
33 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
36 class ‪SiteResolverTest extends UnitTestCase
37 {
41  protected ‪$resetSingletonInstances = true;
42 
46  protected ‪$siteFinder;
47 
52 
56  protected ‪$originalLocale;
57 
61  protected function ‪setUp(): void
62  {
63  parent::setUp();
64  $this->originalLocale = setlocale(LC_COLLATE, '0');
65  $this->siteFinder = $this->getAccessibleMock(SiteFinder::class, ['dummy'], [], '', false);
66 
67  // A request handler which expects a site to be found.
68  $this->siteFoundRequestHandler = new class() implements RequestHandlerInterface {
69  public function handle(ServerRequestInterface $request): ResponseInterface
70  {
73  $site = $request->getAttribute('site', false);
74  $language = $request->getAttribute('language', false);
75  if ($site && $language) {
76  return new ‪JsonResponse(
77  [
78  'site' => $site->getIdentifier(),
79  'language-id' => $language->getLanguageId(),
80  'language-base' => (string)$language->getBase(),
81  'rootpage' => $site->getRootPageId()
82  ]
83  );
84  }
85  return new ‪NullResponse();
86  }
87  };
88 
89  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
90  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
91  }
92 
93  protected function ‪tearDown(): void
94  {
95  // restore locale to original setting
96  setlocale(LC_COLLATE, $this->originalLocale);
97  setlocale(LC_MONETARY, $this->originalLocale);
98  setlocale(LC_TIME, $this->originalLocale);
99  parent::tearDown();
100  }
101 
111  {
112  $incomingUrl = 'https://a-random-domain.com/mysite/';
113  $siteIdentifier = 'full-site';
114  $this->siteFinder->_set('sites', [
115  $siteIdentifier => new Site($siteIdentifier, 13, [
116  'base' => '/mysite/',
117  'languages' => [
118  0 => [
119  'languageId' => 0,
120  'locale' => 'fr_FR.UTF-8',
121  'base' => '/'
122  ]
123  ]
124  ])
125  ]);
126 
127  $subject = new SiteResolver(new SiteMatcher($this->siteFinder));
128 
129  $request = new ServerRequest($incomingUrl, 'GET');
130  $response = $subject->process($request, $this->siteFoundRequestHandler);
131 
132  if ($response instanceof NullResponse) {
133  self::fail('No site configuration found in URL ' . $incomingUrl . '.');
134  } else {
135  $result = $response->getBody()->getContents();
136  $result = json_decode($result, true);
137  self::assertEquals($siteIdentifier, $result['site']);
138  self::assertEquals(0, $result['language-id']);
139  self::assertEquals('/mysite/', $result['language-base']);
140  }
141  }
142 
155  {
156  $incomingUrl = 'https://www.random-result.com/mysubsite/you-know-why/';
157  $this->siteFinder->_set('sites', [
158  'outside-site' => new Site('outside-site', 13, [
159  'base' => '/',
160  'languages' => [
161  0 => [
162  'languageId' => 0,
163  'locale' => 'fr_FR.UTF-8',
164  'base' => '/'
165  ]
166  ]
167  ]),
168  'sub-site' => new Site('sub-site', 15, [
169  'base' => '/mysubsite/',
170  'languages' => [
171  0 => [
172  'languageId' => 0,
173  'locale' => 'fr_FR.UTF-8',
174  'base' => '/'
175  ]
176  ]
177  ]),
178  ]);
179 
180  $subject = new SiteResolver(new SiteMatcher($this->siteFinder));
181 
182  $request = new ServerRequest($incomingUrl, 'GET');
183  $response = $subject->process($request, $this->siteFoundRequestHandler);
184  if ($response instanceof NullResponse) {
185  self::fail('No site configuration found in URL ' . $incomingUrl . '.');
186  } else {
187  $result = $response->getBody()->getContents();
188  $result = json_decode($result, true);
189  self::assertEquals('sub-site', $result['site']);
190  self::assertEquals(0, $result['language-id']);
191  self::assertEquals('/mysubsite/', $result['language-base']);
192  }
193  }
194 
196  {
197  return [
198  'matches second site' => [
199  'https://www.random-result.com/mysubsite/you-know-why/',
200  'sub-site',
201  14,
202  '/mysubsite/'
203  ],
204  'matches third site' => [
205  'https://www.random-result.com/mysubsite/micro-site/oh-yes-you-do/',
206  'subsub-site',
207  15,
208  '/mysubsite/micro-site/'
209  ],
210  'matches a subsite in first site' => [
211  'https://www.random-result.com/products/pampers/',
212  'outside-site',
213  13,
214  '/'
215  ],
216  ];
217  }
218 
235  public function ‪detectSubSubsiteInsideNestedUrlStructure($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedBase)
236  {
237  $this->siteFinder->_set('sites', [
238  'outside-site' => new Site('outside-site', 13, [
239  'base' => '/',
240  'languages' => [
241  0 => [
242  'languageId' => 0,
243  'locale' => 'fr_FR.UTF-8',
244  'base' => '/'
245  ]
246  ]
247  ]),
248  'sub-site' => new Site('sub-site', 14, [
249  'base' => '/mysubsite/',
250  'languages' => [
251  0 => [
252  'languageId' => 0,
253  'locale' => 'fr_FR.UTF-8',
254  'base' => '/'
255  ]
256  ]
257  ]),
258  'subsub-site' => new Site('subsub-site', 15, [
259  'base' => '/mysubsite/micro-site/',
260  'languages' => [
261  0 => [
262  'languageId' => 0,
263  'locale' => 'fr_FR.UTF-8',
264  'base' => '/'
265  ]
266  ]
267  ]),
268  ]);
269 
270  $subject = new SiteResolver(new SiteMatcher($this->siteFinder));
271 
272  $request = new ServerRequest($incomingUrl, 'GET');
273  $response = $subject->process($request, $this->siteFoundRequestHandler);
274 
275  if ($response instanceof NullResponse) {
276  self::fail('No site configuration found in URL ' . $incomingUrl . '.');
277  } else {
278  $result = $response->getBody()->getContents();
279  $result = json_decode($result, true);
280  self::assertEquals($expectedSiteIdentifier, $result['site']);
281  self::assertEquals($expectedRootPageId, $result['rootpage']);
282  self::assertEquals($expectedBase, $result['language-base']);
283  }
284  }
285 
287  {
288  return [
289  'matches second site' => [
290  'https://www.random-result.com/mysubsite/you-know-why/',
291  'sub-site',
292  14,
293  2,
294  '/mysubsite/'
295  ],
296  'matches second site in other language' => [
297  'https://www.random-result.com/mysubsite/it/you-know-why/',
298  'sub-site',
299  14,
300  2,
301  '/mysubsite/'
302  ],
303  'matches third site' => [
304  'https://www.random-result.com/mysubsite/micro-site/ru/oh-yes-you-do/',
305  'subsub-site',
306  15,
307  13,
308  '/mysubsite/micro-site/ru/'
309  ],
310  'matches a subpage in first site' => [
311  'https://www.random-result.com/en/products/pampers/',
312  'outside-site',
313  13,
314  0,
315  '/en/'
316  ],
317  'matches a subpage with translation in first site' => [
318  'https://www.random-result.com/fr/products/pampers/',
319  'outside-site',
320  13,
321  1,
322  '/fr/'
323  ],
324  ];
325  }
326 
340  public function ‪detectProperLanguageByIncomingUrl($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedLanguageId, $expectedBase)
341  {
342  $this->siteFinder->_set('sites', [
343  'outside-site' => new Site('outside-site', 13, [
344  'base' => '/',
345  'languages' => [
346  0 => [
347  'languageId' => 0,
348  'locale' => 'en_US.UTF-8',
349  'base' => '/en/'
350  ],
351  1 => [
352  'languageId' => 1,
353  'locale' => 'fr_CA.UTF-8',
354  'base' => '/fr/'
355  ]
356  ]
357  ]),
358  'sub-site' => new Site('sub-site', 14, [
359  'base' => '/mysubsite/',
360  'languages' => [
361  2 => [
362  'languageId' => 2,
363  'locale' => 'it_IT.UTF-8',
364  'base' => '/'
365  ]
366  ]
367  ]),
368  'subsub-site' => new Site('subsub-site', 15, [
369  'base' => '/mysubsite/micro-site/',
370  'languages' => [
371  13 => [
372  'languageId' => 13,
373  'locale' => 'ru_RU.UTF-8',
374  'base' => '/ru/'
375  ]
376  ]
377  ]),
378  ]);
379 
380  $subject = new SiteResolver(new SiteMatcher($this->siteFinder));
381 
382  $request = new ServerRequest($incomingUrl, 'GET');
383  $response = $subject->process($request, $this->siteFoundRequestHandler);
384 
385  if ($response instanceof NullResponse) {
386  self::fail('No site configuration found in URL ' . $incomingUrl . '.');
387  } else {
388  $result = $response->getBody()->getContents();
389  $result = json_decode($result, true);
390  self::assertEquals($expectedSiteIdentifier, $result['site']);
391  self::assertEquals($expectedRootPageId, $result['rootpage']);
392  self::assertEquals($expectedLanguageId, $result['language-id']);
393  self::assertEquals($expectedBase, $result['language-base']);
394  }
395  }
396 }
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectProperLanguageByIncomingUrlDataProvider
‪detectProperLanguageByIncomingUrlDataProvider()
Definition: SiteResolverTest.php:282
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\$siteFoundRequestHandler
‪RequestHandlerInterface $siteFoundRequestHandler
Definition: SiteResolverTest.php:48
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: SiteResolverTest.php:40
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:26
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest
Definition: SiteResolverTest.php:37
‪TYPO3\CMS\Frontend\Middleware\SiteResolver
Definition: SiteResolver.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectASingleSiteWhenProperRequestIsGiven
‪detectASingleSiteWhenProperRequestIsGiven()
Definition: SiteResolverTest.php:106
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\$siteFinder
‪SiteFinder AccessibleObjectInterface $siteFinder
Definition: SiteResolverTest.php:44
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:26
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\tearDown
‪tearDown()
Definition: SiteResolverTest.php:89
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectProperLanguageByIncomingUrl
‪detectProperLanguageByIncomingUrl($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedLanguageId, $expectedBase)
Definition: SiteResolverTest.php:336
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\$originalLocale
‪string $originalLocale
Definition: SiteResolverTest.php:52
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectSubSubsiteInsideNestedUrlStructure
‪detectSubSubsiteInsideNestedUrlStructure($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedBase)
Definition: SiteResolverTest.php:231
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\setUp
‪setUp()
Definition: SiteResolverTest.php:57
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectSubSubsiteInsideNestedUrlStructureDataProvider
‪detectSubSubsiteInsideNestedUrlStructureDataProvider()
Definition: SiteResolverTest.php:191
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Routing\SiteMatcher
Definition: SiteMatcher.php:51
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectSubsiteInsideNestedUrlStructure
‪detectSubsiteInsideNestedUrlStructure()
Definition: SiteResolverTest.php:150