‪TYPO3CMS  9.5
SiteMatcherTest.php
Go to the documentation of this file.
1 <?php
2 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 
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 class ‪SiteMatcherTest extends UnitTestCase
28 {
29  public function ‪setUp()
30  {
31  $pseudoSiteFinderMock = $this->getMockBuilder(PseudoSiteFinder::class)
32  ->setMethods(['findAll'])
33  ->disableOriginalConstructor()
34  ->getMock();
35  GeneralUtility::addInstance(PseudoSiteFinder::class, $pseudoSiteFinderMock);
36  parent::setUp();
37  }
38 
43  {
44  $site = new ‪Site('main', 1, [
45  'base' => '/',
46  'languages' => [
47  0 => [
48  'title' => 'English',
49  'languageId' => 0,
50  'base' => 'http://9-5.typo3.test/',
51  'locale' => 'en_US-UTF-8'
52  ],
53  2 => [
54  'title' => 'Deutsch',
55  'languageId' => 2,
56  'base' => 'http://de.9-5.typo3.test/',
57  'locale' => 'en_US-UTF-8'
58  ],
59  1 => [
60  'title' => 'Dansk',
61  'languageId' => 1,
62  'base' => 'http://9-5.typo3.test/da/',
63  'locale' => 'da_DK.UTF-8'
64  ]
65  ]
66  ]);
67  $secondSite = new ‪Site('second', 13, [
68  'base' => '/',
69  'languages' => [
70  0 => [
71  'title' => 'English',
72  'languageId' => 0,
73  'base' => '/en/',
74  'locale' => 'en_US-UTF-8'
75  ],
76  1 => [
77  'title' => 'Dansk',
78  'languageId' => 1,
79  'base' => '/da/',
80  'locale' => 'da_DK.UTF-8'
81  ],
82  ]
83  ]);
85  $finderMock = $this
86  ->getMockBuilder(SiteFinder::class)
87  ->setMethods(['getAllSites'])
88  ->disableOriginalConstructor()
89  ->getMock();
90  $finderMock->method('getAllSites')->willReturn(['main' => $site, 'second' => $secondSite]);
91  $subject = new ‪SiteMatcher($finderMock);
92 
93  $request = new ‪ServerRequest('http://9-5.typo3.test/da/my-page/');
95  $result = $subject->matchRequest($request);
96  self::assertEquals(1, $result->getLanguage()->getLanguageId());
97 
98  $request = new ‪ServerRequest('http://9-5.typo3.test/da');
100  $result = $subject->matchRequest($request);
101  // Matches danish, as path fits
102  self::assertEquals(1, $result->getLanguage()->getLanguageId());
103 
104  $request = new ‪ServerRequest('https://9-5.typo3.test/da');
106  $result = $subject->matchRequest($request);
107  // Matches the second site, as this is HTTPS and HTTP
108  self::assertEquals('second', $result->getSite()->getIdentifier());
109  self::assertEquals(1, $result->getLanguage()->getLanguageId());
110 
111  $request = new ‪ServerRequest('http://de.9-5.typo3.test/da');
113  $result = $subject->matchRequest($request);
114  // Matches german, as the domain fits!
115  self::assertEquals(2, $result->getLanguage()->getLanguageId());
116 
117  $request = new ‪ServerRequest('http://9-5.typo3.test/');
119  $result = $subject->matchRequest($request);
120  // Matches english
121  self::assertEquals(0, $result->getLanguage()->getLanguageId());
122 
123  $request = new ‪ServerRequest('http://www.example.com/');
125  $result = $subject->matchRequest($request);
126  // Nothing found, only the empty site, but finds the last site ("second") according to the algorithm
127  self::assertNull($result->getLanguage());
128  self::assertEquals('second', $result->getSite()->getIdentifier());
129  }
130 
136  {
137  $site = new ‪Site('main', 1, [
138  'base' => 'https://www.example.com/',
139  'languages' => [
140  0 => [
141  'title' => 'English',
142  'languageId' => 0,
143  'base' => 'http://example.us/',
144  'locale' => 'en_US-UTF-8'
145  ],
146  2 => [
147  'title' => 'Deutsch',
148  'languageId' => 2,
149  'base' => 'http://www.example.de/',
150  'locale' => 'en_US-UTF-8'
151  ],
152  1 => [
153  'title' => 'Dansk',
154  'languageId' => 1,
155  'base' => 'http://www.example.com/da/',
156  'locale' => 'da_DK.UTF-8'
157  ],
158  3 => [
159  'title' => 'French',
160  'languageId' => 3,
161  'base' => '/fr/',
162  'locale' => 'fr_FR.UTF-8'
163  ]
164  ]
165  ]);
166  $secondSite = new ‪Site('second', 13, [
167  'base' => '/',
168  'languages' => [
169  0 => [
170  'title' => 'English',
171  'languageId' => 0,
172  'base' => '/en/',
173  'locale' => 'en_US-UTF-8'
174  ],
175  1 => [
176  'title' => 'Dansk',
177  'languageId' => 1,
178  'base' => '/da/',
179  'locale' => 'da_DK.UTF-8'
180  ],
181  ]
182  ]);
184  $finderMock = $this
185  ->getMockBuilder(SiteFinder::class)
186  ->setMethods(['getAllSites'])
187  ->disableOriginalConstructor()
188  ->getMock();
189  $finderMock->method('getAllSites')->willReturn(['main' => $site, 'second' => $secondSite]);
190  $subject = new ‪SiteMatcher($finderMock);
191 
192  $request = new ‪ServerRequest('https://www.example.com/de');
194  $result = $subject->matchRequest($request);
195  // Site found, but no language
196  self::assertEquals($site, $result->getSite());
197  self::assertNull($result->getLanguage());
198 
199  $request = new ‪ServerRequest('http://www.other-domain.com/da');
201  $result = $subject->matchRequest($request);
202  self::assertEquals($secondSite, $result->getSite());
203  self::assertEquals(1, $result->getLanguage()->getLanguageId());
204 
205  $request = new ‪ServerRequest('http://www.other-domain.com/de');
207  $result = $subject->matchRequest($request);
208  // No language for this solution
209  self::assertEquals($secondSite, $result->getSite());
210  self::assertNull($result->getLanguage());
211  }
212 }
‪TYPO3\CMS\Core\Tests\Unit\Routing
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Tests\Unit\Routing\SiteMatcherTest\fullUrlMatchesSpecificLanguageWithSubdomainsAndPathSuffixes
‪fullUrlMatchesSpecificLanguageWithSubdomainsAndPathSuffixes()
Definition: SiteMatcherTest.php:135
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Site\PseudoSiteFinder
Definition: PseudoSiteFinder.php:39
‪TYPO3\CMS\Core\Routing\SiteRouteResult
Definition: SiteRouteResult.php:29
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Routing\SiteMatcherTest\fullUrlMatchesSpecificLanguageWithSubdomainsAndDomainSuffixes
‪fullUrlMatchesSpecificLanguageWithSubdomainsAndDomainSuffixes()
Definition: SiteMatcherTest.php:42
‪TYPO3\CMS\Core\Tests\Unit\Routing\SiteMatcherTest
Definition: SiteMatcherTest.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Routing\SiteMatcher
Definition: SiteMatcher.php:53
‪TYPO3\CMS\Core\Tests\Unit\Routing\SiteMatcherTest\setUp
‪setUp()
Definition: SiteMatcherTest.php:29