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