[TASK] Make LocallangXmlParserTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Localization / Parser / LocallangXmlParserTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Localization\Parser;
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
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Cache\CacheManager;
20 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
21 use TYPO3\CMS\Core\Localization\LanguageStore;
22 use TYPO3\CMS\Core\Localization\LocalizationFactory;
23 use TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class LocallangXmlParserTest extends UnitTestCase
31 {
32 /**
33 * Prepares the environment before running a test.
34 */
35 protected function setUp()
36 {
37 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
38
39 $cacheManagerProphecy = $this->prophesize(CacheManager::class);
40 GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
41 $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
42 $cacheManagerProphecy->getCache('l10n')->willReturn($cacheFrontendProphecy->reveal());
43 $cacheFrontendProphecy->get(Argument::cetera())->willReturn(false);
44 $cacheFrontendProphecy->set(Argument::cetera())->willReturn(null);
45
46 GeneralUtility::makeInstance(LanguageStore::class)->initialize();
47 }
48 /**
49 * Cleans up the environment after running a test.
50 */
51 protected function tearDown()
52 {
53 GeneralUtility::purgeInstances();
54 parent::tearDown();
55 }
56
57 protected static function getFixtureFilePath($filename)
58 {
59 // We have to take the whole relative path as otherwise this test fails on Windows systems
60 return PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/' . $filename;
61 }
62
63 /**
64 * @test
65 */
66 public function canParseLlxmlInEnglish()
67 {
68 $LOCAL_LANG = (new LocallangXmlParser)->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
69 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
70 $expectedLabels = [
71 'label1' => 'This is label #1',
72 'label2' => 'This is label #2',
73 'label3' => 'This is label #3'
74 ];
75 foreach ($expectedLabels as $key => $expectedLabel) {
76 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
77 }
78 }
79
80 /**
81 * @test
82 */
83 public function canParseLlxmlInMd5Code()
84 {
85 $LOCAL_LANG = (new LocallangXmlParser)->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5');
86 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
87 $expectedLabels = [
88 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
89 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
90 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
91 ];
92 foreach ($expectedLabels as $key => $expectedLabel) {
93 $this->assertEquals($expectedLabel, $LOCAL_LANG['md5'][$key][0]['target']);
94 }
95 }
96
97 /**
98 * @test
99 */
100 public function canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound()
101 {
102 $localLang = (new LocallangXmlParser)->getParsedData(
103 self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'),
104 'fr'
105 );
106 // This test case is odd: The system under test does NOT
107 // return 'target' at all if there is no such translation.
108 // @todo: Either change / fix subject, or adapt test and test name!
109 $this->assertNull($localLang['fr']['label1'][0]['target'] ?? null);
110 $this->assertNull($localLang['fr']['label2'][0]['target'] ?? null);
111 $this->assertNull($localLang['fr']['label3'][0]['target'] ?? null);
112 }
113
114 /**
115 * @test
116 */
117 public function canOverrideLlxml()
118 {
119 /** @var $factory LocalizationFactory */
120 $factory = new LocalizationFactory;
121
122 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][self::getFixtureFilePath('locallang.xml')][] = self::getFixtureFilePath('locallang_override.xml');
123 $LOCAL_LANG = array_merge(
124 $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default'),
125 $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5')
126 );
127 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
128 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
129 $expectedLabels = [
130 'default' => [
131 'label1' => 'This is my 1st label',
132 'label2' => 'This is my 2nd label',
133 'label3' => 'This is label #3'
134 ],
135 'md5' => [
136 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
137 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
138 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
139 ]
140 ];
141 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
142 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
143 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
144 }
145 }
146 }
147
148 public function numericKeysDataProvider()
149 {
150 return [
151 'Numeric key 1' => [
152 1,
153 'This is label #1 [FR]'
154 ],
155 'Numeric key 2' => [
156 2,
157 'This is label #2 [FR]'
158 ],
159 'Numeric key 3' => [
160 3,
161 'This is label #3 [FR]'
162 ],
163 'Numeric key 5' => [
164 5,
165 'This is label #5 [FR]'
166 ],
167 'Numeric key 10' => [
168 10,
169 'This is label #10 [FR]'
170 ],
171 ];
172 }
173
174 /**
175 * @test
176 * @dataProvider numericKeysDataProvider
177 */
178 public function canTranslateNumericKeys($key, $expectedResult)
179 {
180 /** @var $factory LocalizationFactory */
181 $factory = new LocalizationFactory;
182
183 $LOCAL_LANG = $factory->getParsedData(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
184
185 $this->assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
186 }
187 }