‪TYPO3CMS  10.4
LocalesTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
25 class ‪LocalesTest extends UnitTestCase
26 {
27  protected ‪$resetSingletonInstances = true;
28 
32  protected ‪$originalLocale;
33 
34  protected function ‪setUp(): void
35  {
36  parent::setUp();
37  $this->originalLocale = setlocale(LC_COLLATE, 0);
38  }
39 
40  protected function ‪tearDown(): void
41  {
42  // Restore original locale
43  setlocale(LC_COLLATE, $this->originalLocale);
44  setlocale(LC_MONETARY, $this->originalLocale);
45  setlocale(LC_TIME, $this->originalLocale);
46  parent::tearDown();
47  }
48 
52  public function ‪browserLanguageDetectionWorksDataProvider(): array
53  {
54  return [
55  'german' => [
56  'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4',
57  'de',
58  ],
59  'english as default' => [
60  'en-US;q=0.8,en;q=0.6;de-DE,de;q=0.4',
61  'default',
62  ],
63  'chinese simplified' => [
64  'zh-CN,en-US;q=0.5,en;q=0.3',
65  'ch'
66  ],
67  'chinese simplified han' => [
68  'zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3',
69  'ch'
70  ],
71  ];
72  }
73 
81  public function ‪browserLanguageDetectionWorks(string $acceptLanguageHeader, string $expected)
82  {
83  $detectedLanguage = (new Locales())->getPreferredClientLanguage(
84  $acceptLanguageHeader
85  );
86  self::assertSame($expected, $detectedLanguage);
87  }
88 
93  {
94  $site = $this->‪createSiteWithDefaultLanguage('');
95  $result = ‪Locales::setSystemLocaleFromSiteLanguage($site->getLanguageById(0));
96  self::assertFalse($result);
97  $currentLocale = setlocale(LC_COLLATE, 0);
98  // Check that the locale was not overridden
99  self::assertEquals($this->originalLocale, $currentLocale);
100  }
101 
106  {
107  $locale = 'en_US';
108  $site = $this->‪createSiteWithDefaultLanguage($locale);
109  $result = ‪Locales::setSystemLocaleFromSiteLanguage($site->getLanguageById(0));
110  self::assertTrue($result);
111  $currentLocale = setlocale(LC_COLLATE, 0);
112  // Check that the locale was overridden
113  self::assertEquals($locale, $currentLocale);
114  }
115 
121  {
122  $locale = 'af_EUR';
123  $site = $this->‪createSiteWithDefaultLanguage($locale);
124  $result = ‪Locales::setSystemLocaleFromSiteLanguage($site->getLanguageById(0));
125  self::assertFalse($result);
126  $currentLocale = setlocale(LC_COLLATE, 0);
127  // Check that the locale was not overridden
128  self::assertEquals($this->originalLocale, $currentLocale);
129  }
130 
131  private function ‪createSiteWithDefaultLanguage(string $locale): ‪Site
132  {
133  return new ‪Site('test', 1, [
134  'identifier' => 'test',
135  'rootPageId' => 1,
136  'base' => '/',
137  'languages' => [
138  [
139  'languageId' => 0,
140  'locale' => $locale,
141  'base' => '/',
142  ],
143  ]
144  ]);
145  }
146 }
‪TYPO3\CMS\Core\Localization\Locales\setSystemLocaleFromSiteLanguage
‪static bool setSystemLocaleFromSiteLanguage(SiteLanguage $siteLanguage)
Definition: Locales.php:270
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\createSiteWithDefaultLanguage
‪createSiteWithDefaultLanguage(string $locale)
Definition: LocalesTest.php:130
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: LocalesTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\$originalLocale
‪string $originalLocale
Definition: LocalesTest.php:31
‪TYPO3\CMS\Core\Localization\Locales
Definition: Locales.php:30
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\setSystemLocaleFromSiteLanguageWithProperLocaleSetsLocale
‪setSystemLocaleFromSiteLanguageWithProperLocaleSetsLocale()
Definition: LocalesTest.php:104
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\setUp
‪setUp()
Definition: LocalesTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\browserLanguageDetectionWorksDataProvider
‪array browserLanguageDetectionWorksDataProvider()
Definition: LocalesTest.php:51
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\tearDown
‪tearDown()
Definition: LocalesTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\browserLanguageDetectionWorks
‪browserLanguageDetectionWorks(string $acceptLanguageHeader, string $expected)
Definition: LocalesTest.php:80
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\setSystemLocaleFromSiteLanguageWithoutLocaleDoesNotSetLocale
‪setSystemLocaleFromSiteLanguageWithoutLocaleDoesNotSetLocale()
Definition: LocalesTest.php:91
‪TYPO3\CMS\Core\Tests\Unit\Localization
Definition: LocalesTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest\setSystemLocaleFromSiteLanguageWithInvalidLocaleDoesNotSetLocale
‪setSystemLocaleFromSiteLanguageWithInvalidLocaleDoesNotSetLocale()
Definition: LocalesTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Localization\LocalesTest
Definition: LocalesTest.php:26