‪TYPO3CMS  9.5
SiteResolverTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
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 Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\RequestHandlerInterface;
32 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
35 class ‪SiteResolverTest extends UnitTestCase
36 {
40  protected ‪$resetSingletonInstances = true;
41 
45  protected ‪$siteFinder;
46 
51 
55  protected function ‪setUp(): void
56  {
57  $this->siteFinder = $this->getAccessibleMock(SiteFinder::class, ['dummy'], [], '', false);
58 
59  // A request handler which expects a site to be found.
60  $this->siteFoundRequestHandler = new class implements RequestHandlerInterface {
61  public function handle(ServerRequestInterface $request): ResponseInterface
62  {
65  $site = $request->getAttribute('site', false);
66  $language = $request->getAttribute('language', false);
67  if ($site && $language) {
68  return new ‪JsonResponse(
69  [
70  'site' => $site->getIdentifier(),
71  'language-id' => $language->getLanguageId(),
72  'language-base' => (string)$language->getBase(),
73  'rootpage' => $site->getRootPageId()
74  ]
75  );
76  }
77  return new ‪NullResponse();
78  }
79  };
80 
81  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
82  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
83  }
84 
94  {
95  $incomingUrl = 'https://a-random-domain.com/mysite/';
96  $siteIdentifier = 'full-site';
97  $this->siteFinder->_set('sites', [
98  $siteIdentifier => new ‪Site($siteIdentifier, 13, [
99  'base' => '/mysite/',
100  'languages' => [
101  0 => [
102  'languageId' => 0,
103  'locale' => 'fr_FR.UTF-8',
104  'base' => '/'
105  ]
106  ]
107  ])
108  ]);
109 
110  $subject = new ‪SiteResolver(new ‪SiteMatcher($this->siteFinder));
111 
112  $request = new ‪ServerRequest($incomingUrl, 'GET');
113  $response = $subject->process($request, $this->siteFoundRequestHandler);
114 
115  if ($response instanceof ‪NullResponse) {
116  $this->fail('No site configuration found in URL ' . $incomingUrl . '.');
117  } else {
118  $result = $response->getBody()->getContents();
119  $result = json_decode($result, true);
120  $this->assertEquals($siteIdentifier, $result['site']);
121  $this->assertEquals(0, $result['language-id']);
122  $this->assertEquals('/mysite/', $result['language-base']);
123  }
124  }
125 
138  {
139  $incomingUrl = 'https://www.random-result.com/mysubsite/you-know-why/';
140  $this->siteFinder->_set('sites', [
141  'outside-site' => new ‪Site('outside-site', 13, [
142  'base' => '/',
143  'languages' => [
144  0 => [
145  'languageId' => 0,
146  'locale' => 'fr_FR.UTF-8',
147  'base' => '/'
148  ]
149  ]
150  ]),
151  'sub-site' => new ‪Site('sub-site', 15, [
152  'base' => '/mysubsite/',
153  'languages' => [
154  0 => [
155  'languageId' => 0,
156  'locale' => 'fr_FR.UTF-8',
157  'base' => '/'
158  ]
159  ]
160  ]),
161  ]);
162 
163  $subject = new ‪SiteResolver(new ‪SiteMatcher($this->siteFinder));
164 
165  $request = new ‪ServerRequest($incomingUrl, 'GET');
166  $response = $subject->process($request, $this->siteFoundRequestHandler);
167  if ($response instanceof ‪NullResponse) {
168  $this->fail('No site configuration found in URL ' . $incomingUrl . '.');
169  } else {
170  $result = $response->getBody()->getContents();
171  $result = json_decode($result, true);
172  $this->assertEquals('sub-site', $result['site']);
173  $this->assertEquals(0, $result['language-id']);
174  $this->assertEquals('/mysubsite/', $result['language-base']);
175  }
176  }
177 
179  {
180  return [
181  'matches second site' => [
182  'https://www.random-result.com/mysubsite/you-know-why/',
183  'sub-site',
184  14,
185  '/mysubsite/'
186  ],
187  'matches third site' => [
188  'https://www.random-result.com/mysubsite/micro-site/oh-yes-you-do/',
189  'subsub-site',
190  15,
191  '/mysubsite/micro-site/'
192  ],
193  'matches a subsite in first site' => [
194  'https://www.random-result.com/products/pampers/',
195  'outside-site',
196  13,
197  '/'
198  ],
199  ];
200  }
201 
218  public function ‪detectSubSubsiteInsideNestedUrlStructure($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedBase)
219  {
220  $this->siteFinder->_set('sites', [
221  'outside-site' => new Site('outside-site', 13, [
222  'base' => '/',
223  'languages' => [
224  0 => [
225  'languageId' => 0,
226  'locale' => 'fr_FR.UTF-8',
227  'base' => '/'
228  ]
229  ]
230  ]),
231  'sub-site' => new Site('sub-site', 14, [
232  'base' => '/mysubsite/',
233  'languages' => [
234  0 => [
235  'languageId' => 0,
236  'locale' => 'fr_FR.UTF-8',
237  'base' => '/'
238  ]
239  ]
240  ]),
241  'subsub-site' => new Site('subsub-site', 15, [
242  'base' => '/mysubsite/micro-site/',
243  'languages' => [
244  0 => [
245  'languageId' => 0,
246  'locale' => 'fr_FR.UTF-8',
247  'base' => '/'
248  ]
249  ]
250  ]),
251  ]);
252 
253  $subject = new SiteResolver(new SiteMatcher($this->siteFinder));
254 
255  $request = new ServerRequest($incomingUrl, 'GET');
256  $response = $subject->process($request, $this->siteFoundRequestHandler);
257 
258  if ($response instanceof NullResponse) {
259  $this->fail('No site configuration found in URL ' . $incomingUrl . '.');
260  } else {
261  $result = $response->getBody()->getContents();
262  $result = json_decode($result, true);
263  $this->assertEquals($expectedSiteIdentifier, $result['site']);
264  $this->assertEquals($expectedRootPageId, $result['rootpage']);
265  $this->assertEquals($expectedBase, $result['language-base']);
266  }
267  }
268 
270  {
271  return [
272  'matches second site' => [
273  'https://www.random-result.com/mysubsite/you-know-why/',
274  'sub-site',
275  14,
276  2,
277  '/mysubsite/'
278  ],
279  'matches second site in other language' => [
280  'https://www.random-result.com/mysubsite/it/you-know-why/',
281  'sub-site',
282  14,
283  2,
284  '/mysubsite/'
285  ],
286  'matches third site' => [
287  'https://www.random-result.com/mysubsite/micro-site/ru/oh-yes-you-do/',
288  'subsub-site',
289  15,
290  13,
291  '/mysubsite/micro-site/ru/'
292  ],
293  'matches a subpage in first site' => [
294  'https://www.random-result.com/en/products/pampers/',
295  'outside-site',
296  13,
297  0,
298  '/en/'
299  ],
300  'matches a subpage with translation in first site' => [
301  'https://www.random-result.com/fr/products/pampers/',
302  'outside-site',
303  13,
304  1,
305  '/fr/'
306  ],
307  ];
308  }
309 
323  public function ‪detectProperLanguageByIncomingUrl($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedLanguageId, $expectedBase)
324  {
325  $this->siteFinder->_set('sites', [
326  'outside-site' => new Site('outside-site', 13, [
327  'base' => '/',
328  'languages' => [
329  0 => [
330  'languageId' => 0,
331  'locale' => 'en_US.UTF-8',
332  'base' => '/en/'
333  ],
334  1 => [
335  'languageId' => 1,
336  'locale' => 'fr_CA.UTF-8',
337  'base' => '/fr/'
338  ]
339  ]
340  ]),
341  'sub-site' => new Site('sub-site', 14, [
342  'base' => '/mysubsite/',
343  'languages' => [
344  2 => [
345  'languageId' => 2,
346  'locale' => 'it_IT.UTF-8',
347  'base' => '/'
348  ]
349  ]
350  ]),
351  'subsub-site' => new Site('subsub-site', 15, [
352  'base' => '/mysubsite/micro-site/',
353  'languages' => [
354  13 => [
355  'languageId' => 13,
356  'locale' => 'ru_RU.UTF-8',
357  'base' => '/ru/'
358  ]
359  ]
360  ]),
361  ]);
362 
363  $subject = new SiteResolver(new SiteMatcher($this->siteFinder));
364 
365  $request = new ServerRequest($incomingUrl, 'GET');
366  $response = $subject->process($request, $this->siteFoundRequestHandler);
367 
368  if ($response instanceof NullResponse) {
369  $this->fail('No site configuration found in URL ' . $incomingUrl . '.');
370  } else {
371  $result = $response->getBody()->getContents();
372  $result = json_decode($result, true);
373  $this->assertEquals($expectedSiteIdentifier, $result['site']);
374  $this->assertEquals($expectedRootPageId, $result['rootpage']);
375  $this->assertEquals($expectedLanguageId, $result['language-id']);
376  $this->assertEquals($expectedBase, $result['language-base']);
377  }
378  }
379 }
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectProperLanguageByIncomingUrlDataProvider
‪detectProperLanguageByIncomingUrlDataProvider()
Definition: SiteResolverTest.php:266
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\$siteFoundRequestHandler
‪RequestHandlerInterface $siteFoundRequestHandler
Definition: SiteResolverTest.php:47
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: SiteResolverTest.php:39
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:24
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest
Definition: SiteResolverTest.php:36
‪TYPO3\CMS\Frontend\Middleware\SiteResolver
Definition: SiteResolver.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectASingleSiteWhenProperRequestIsGiven
‪detectASingleSiteWhenProperRequestIsGiven()
Definition: SiteResolverTest.php:90
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\$siteFinder
‪SiteFinder AccessibleObjectInterface $siteFinder
Definition: SiteResolverTest.php:43
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:4
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectProperLanguageByIncomingUrl
‪detectProperLanguageByIncomingUrl($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedLanguageId, $expectedBase)
Definition: SiteResolverTest.php:320
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectSubSubsiteInsideNestedUrlStructure
‪detectSubSubsiteInsideNestedUrlStructure($incomingUrl, $expectedSiteIdentifier, $expectedRootPageId, $expectedBase)
Definition: SiteResolverTest.php:215
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\setUp
‪setUp()
Definition: SiteResolverTest.php:52
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectSubSubsiteInsideNestedUrlStructureDataProvider
‪detectSubSubsiteInsideNestedUrlStructureDataProvider()
Definition: SiteResolverTest.php:175
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Routing\SiteMatcher
Definition: SiteMatcher.php:53
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\SiteResolverTest\detectSubsiteInsideNestedUrlStructure
‪detectSubsiteInsideNestedUrlStructure()
Definition: SiteResolverTest.php:134