‪TYPO3CMS  ‪main
LocalizationUtilityTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
30 final class ‪LocalizationUtilityTest extends FunctionalTestCase
31 {
32  protected array ‪$testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/label_test'];
33 
34  #[Test]
35  public function ‪implodeTypoScriptLabelArrayWorks(): void
36  {
37  $reflectionClass = new \ReflectionClass(LocalizationUtility::class);
38  $method = $reflectionClass->getMethod('flattenTypoScriptLabelArray');
39 
40  $expected = [
41  'key1' => 'value1',
42  'key2' => 'value2',
43  'key3' => 'value3',
44  'key3.subkey1' => 'subvalue1',
45  'key3.subkey2.subsubkey' => 'val',
46  ];
47  $input = [
48  'key1' => 'value1',
49  'key2' => 'value2',
50  'key3' => [
51  '_typoScriptNodeValue' => 'value3',
52  'subkey1' => 'subvalue1',
53  'subkey2' => [
54  'subsubkey' => 'val',
55  ],
56  ],
57  ];
58  $result = $method->invoke(null, $input);
59  self::assertSame($expected, $result);
60  }
61 
62  #[Test]
64  {
65  self::assertNull(‪LocalizationUtility::translate('', 'extbase'));
66  }
67 
68  #[Test]
70  {
71  self::assertNull(‪LocalizationUtility::translate('', 'extbase', ['argument']));
72  }
73 
74  public static function ‪translateDataProvider(): array
75  {
76  return [
77  'get translated key' =>
78  ['key1', 'da', 'Dansk label for key1'],
79 
80  'fallback to English when translation is missing for key' =>
81  ['key2', 'da', 'English label for key2'],
82 
83  'fallback to English for non existing language' =>
84  ['key2', 'xx', 'English label for key2'],
85 
86  'replace placeholder with argument' =>
87  ['keyWithPlaceholder', 'default', 'English label with number 100', [100]],
88 
89  'placeholder and empty arguments in default' =>
90  ['keyWithPlaceholderAndNoArguments', 'default', '%d/%m/%Y', []],
91 
92  'placeholder and empty arguments in translation' =>
93  ['keyWithPlaceholderAndNoArguments', 'da', '%d-%m-%Y', []],
94  ];
95  }
96 
97  #[DataProvider('translateDataProvider')]
98  #[Test]
99  public function ‪translateTestWithBackendUserLanguage(string $key, string $languageKey, string $expected, array $arguments = null): void
100  {
101  // No TypoScript overrides
102  $configurationManagerInterfaceMock = $this->createMock(ConfigurationManagerInterface::class);
103  $configurationManagerInterfaceMock
104  ->method('getConfiguration')
105  ->with('Framework', 'label_test', null)
106  ->willReturn([]);
107  GeneralUtility::setSingletonInstance(ConfigurationManagerInterface::class, $configurationManagerInterfaceMock);
108 
109  ‪$GLOBALS['BE_USER'] = new ‪BackendUserAuthentication();
110  ‪$GLOBALS['BE_USER']->user = ['lang' => $languageKey];
111  self::assertSame($expected, ‪LocalizationUtility::translate($key, 'label_test', $arguments));
112  }
113 
114  #[DataProvider('translateDataProvider')]
115  #[Test]
117  string $key,
118  string $languageKey,
119  string $expected,
120  array $arguments = null
121  ): void {
122  // No TypoScript overrides
123  $configurationManagerInterfaceMock = $this->createMock(ConfigurationManagerInterface::class);
124  $configurationManagerInterfaceMock
125  ->method('getConfiguration')
126  ->with('Framework', 'label_test', null)
127  ->willReturn([]);
128  GeneralUtility::setSingletonInstance(ConfigurationManagerInterface::class, $configurationManagerInterfaceMock);
129 
130  self::assertSame($expected, ‪LocalizationUtility::translate($key, 'label_test', $arguments, $languageKey));
131  }
132 
136  #[Test]
137  public function ‪loadTypoScriptLabels(): void
138  {
139  ‪$GLOBALS['TYPO3_REQUEST'] = new ‪ServerRequest();
140  ‪$GLOBALS['TYPO3_REQUEST'] = ‪$GLOBALS['TYPO3_REQUEST']
141  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
143  $configurationManagerInterfaceMock = $this->createMock(ConfigurationManagerInterface::class);
144  $configurationManagerInterfaceMock
145  ->expects(self::atLeastOnce())
146  ->method('getConfiguration')
147  ->with($configurationType, 'label_test', null)
148  ->willReturn(['_LOCAL_LANG' => [
149  'default' => [
150  'key3' => 'English label for key3 from TypoScript',
151  ],
152  'da' => [
153  'key1' => 'key1 value from TS core',
154  'key3' => [
155  'subkey1' => 'key3.subkey1 value from TypoScript',
156  // this key doesn't exist in XLF files
157  'subkey2' => [
158  'subsubkey' => 'key3.subkey2.subsubkey value from TypoScript',
159  ],
160  ],
161  ],
162  ],
163  ]);
164  GeneralUtility::setSingletonInstance(ConfigurationManagerInterface::class, $configurationManagerInterfaceMock);
165 
166  self::assertSame('key1 value from TS core', ‪LocalizationUtility::translate('key1', 'label_test', languageKey: 'da'));
167  // Label from XLF file, no override
168  self::assertSame('English label for key2', ‪LocalizationUtility::translate('key2', 'label_test', languageKey: 'da'));
169  self::assertSame('English label for key3 from TypoScript', ‪LocalizationUtility::translate('key3', 'label_test', languageKey: 'da'));
170  self::assertSame('key3.subkey1 value from TypoScript', ‪LocalizationUtility::translate('key3.subkey1', 'label_test', languageKey: 'da'));
171  self::assertSame('key3.subkey2.subsubkey value from TypoScript', ‪LocalizationUtility::translate('key3.subkey2.subsubkey', 'label_test', languageKey: 'da'));
172  }
173 
174  #[Test]
175  public function ‪clearLabelWithTypoScript(): void
176  {
177  ‪$GLOBALS['TYPO3_REQUEST'] = new ‪ServerRequest();
178  ‪$GLOBALS['TYPO3_REQUEST'] = ‪$GLOBALS['TYPO3_REQUEST']
179  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
181  $configurationManagerInterfaceMock = $this->createMock(ConfigurationManagerInterface::class);
182  $configurationManagerInterfaceMock
183  ->expects(self::atLeastOnce())
184  ->method('getConfiguration')
185  ->with($configurationType, 'label_test', null)
186  ->willReturn([
187  '_LOCAL_LANG' => [
188  'da' => [
189  'key1' => '',
190  ],
191  ],
192  ]);
193  GeneralUtility::setSingletonInstance(ConfigurationManagerInterface::class, $configurationManagerInterfaceMock);
194 
195  $result = ‪LocalizationUtility::translate('key1', 'label_test', languageKey: 'da');
196 
197  self::assertSame('', $result);
198  }
199 
200  #[Test]
202  {
203  $this->expectException(\InvalidArgumentException::class);
204  $this->expectExceptionCode(1498144052);
205  ‪LocalizationUtility::translate('foo/bar', '');
206  }
207 
208  #[Test]
210  {
211  ‪$GLOBALS['TYPO3_REQUEST'] = new ‪ServerRequest();
212  ‪$GLOBALS['TYPO3_REQUEST'] = ‪$GLOBALS['TYPO3_REQUEST']
213  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
214  $typoScriptLocalLang = [
215  '_LOCAL_LANG' => [
216  'da' => [
217  'key1' => 'I am a new key and there is no xlf file',
218  ],
219  ],
220  ];
221 
223  $configurationManagerInterfaceMock = $this->createMock(ConfigurationManagerInterface::class);
224  $configurationManagerInterfaceMock
225  ->expects(self::atLeastOnce())
226  ->method('getConfiguration')
227  ->with($configurationType, 'core', null)
228  ->willReturn($typoScriptLocalLang);
229  GeneralUtility::setSingletonInstance(ConfigurationManagerInterface::class, $configurationManagerInterfaceMock);
230 
231  $result = ‪LocalizationUtility::translate('key1', 'core', [], 'da');
232 
233  self::assertSame('I am a new key and there is no xlf file', $result);
234  }
235 }
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\translateForEmptyStringKeyReturnsNull
‪translateForEmptyStringKeyReturnsNull()
Definition: LocalizationUtilityTest.php:63
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:35
‪TYPO3\CMS\Extbase\Tests\Functional\Utility
Definition: LocalizationUtilityTest.php:18
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\translateDataProvider
‪static translateDataProvider()
Definition: LocalizationUtilityTest.php:74
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_FRAMEWORK
‪const CONFIGURATION_TYPE_FRAMEWORK
Definition: ConfigurationManagerInterface.php:29
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\clearLabelWithTypoScript
‪clearLabelWithTypoScript()
Definition: LocalizationUtilityTest.php:175
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\translateTestWithBackendUserLanguage
‪translateTestWithBackendUserLanguage(string $key, string $languageKey, string $expected, array $arguments=null)
Definition: LocalizationUtilityTest.php:99
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\translateTestWithExplicitLanguageParameters
‪translateTestWithExplicitLanguageParameters(string $key, string $languageKey, string $expected, array $arguments=null)
Definition: LocalizationUtilityTest.php:116
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, Locale|string $languageKey=null)
Definition: LocalizationUtility.php:47
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\implodeTypoScriptLabelArrayWorks
‪implodeTypoScriptLabelArrayWorks()
Definition: LocalizationUtilityTest.php:35
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\translateThrowsExceptionWithEmptyExtensionNameIfKeyIsNotPrefixedWithLLL
‪translateThrowsExceptionWithEmptyExtensionNameIfKeyIsNotPrefixedWithLLL()
Definition: LocalizationUtilityTest.php:201
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest
Definition: LocalizationUtilityTest.php:31
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\loadTypoScriptLabels
‪loadTypoScriptLabels()
Definition: LocalizationUtilityTest.php:137
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_FE
‪const REQUESTTYPE_FE
Definition: SystemEnvironmentBuilder.php:43
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: LocalizationUtilityTest.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\translateWillReturnLabelsFromTsEvenIfNoXlfFileExists
‪translateWillReturnLabelsFromTsEvenIfNoXlfFileExists()
Definition: LocalizationUtilityTest.php:209
‪TYPO3\CMS\Extbase\Tests\Functional\Utility\LocalizationUtilityTest\translateForEmptyStringKeyWithArgumentsReturnsNull
‪translateForEmptyStringKeyWithArgumentsReturnsNull()
Definition: LocalizationUtilityTest.php:69