157113bd881068b76f0dae9f02add0a927dbf609
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Localization / Parser / LocallangXmlParserTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Localization\Parser;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Localization\LocalizationFactory;
17
18 /**
19 * Testcase for class \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser.
20 */
21 class LocallangXmlParserTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
22 {
23 /**
24 * @var \TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser
25 */
26 protected $parser;
27
28 /**
29 * @var array
30 */
31 protected $locallangXMLOverride;
32
33 /**
34 * @var string
35 */
36 protected $l10nPriority;
37
38 protected static function getFixtureFilePath($filename)
39 {
40 // We have to take the whole relative path as otherwise this test fails on Windows systems
41 return PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/' . $filename;
42 }
43
44 /**
45 * Prepares the environment before running a test.
46 */
47 protected function setUp()
48 {
49 // Backup locallangXMLOverride and localization format priority
50 $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
51 $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
52 $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Parser\LocallangXmlParser::class);
53
54 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xml';
55 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
56 // Clear localization cache
57 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
58 }
59
60 /**
61 * Cleans up the environment after running a test.
62 */
63 protected function tearDown()
64 {
65 // Restore locallangXMLOverride and localization format priority
66 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
67 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
68 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
69 parent::tearDown();
70 }
71
72 /**
73 * @test
74 */
75 public function canParseLlxmlInEnglish()
76 {
77 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default');
78 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
79 $expectedLabels = [
80 'label1' => 'This is label #1',
81 'label2' => 'This is label #2',
82 'label3' => 'This is label #3'
83 ];
84 foreach ($expectedLabels as $key => $expectedLabel) {
85 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
86 }
87 }
88
89 /**
90 * @test
91 */
92 public function canParseLlxmlInMd5Code()
93 {
94 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5');
95 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
96 $expectedLabels = [
97 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
98 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
99 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
100 ];
101 foreach ($expectedLabels as $key => $expectedLabel) {
102 $this->assertEquals($expectedLabel, $LOCAL_LANG['md5'][$key][0]['target']);
103 }
104 }
105
106 /**
107 * @test
108 */
109 public function canParseLlxmlInFrenchAndReturnsNullLabelsIfNoTranslationIsFound()
110 {
111 $LOCAL_LANG = $this->parser->getParsedData(self::getFixtureFilePath('locallangOnlyDefaultLanguage.xml'), 'fr');
112 $expectedLabels = [
113 'label1' => null,
114 'label2' => null,
115 'label3' => null
116 ];
117 foreach ($expectedLabels as $key => $expectedLabel) {
118 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
119 }
120 }
121
122 /**
123 * @test
124 */
125 public function canOverrideLlxml()
126 {
127 /** @var $factory LocalizationFactory */
128 $factory = new LocalizationFactory;
129
130 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][self::getFixtureFilePath('locallang.xml')][] = self::getFixtureFilePath('locallang_override.xml');
131 $LOCAL_LANG = array_merge(
132 $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'default'),
133 $factory->getParsedData(self::getFixtureFilePath('locallang.xml'), 'md5')
134 );
135 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
136 $this->assertArrayHasKey('md5', $LOCAL_LANG, 'md5 key not found in $LOCAL_LANG');
137 $expectedLabels = [
138 'default' => [
139 'label1' => 'This is my 1st label',
140 'label2' => 'This is my 2nd label',
141 'label3' => 'This is label #3'
142 ],
143 'md5' => [
144 'label1' => '409a6edbc70dbeeccbfe5f1e569d6717',
145 'label2' => 'b5dc71ae9f52ecb9e7704c50562e39b0',
146 'label3' => '51eac55fa5ca15789ce9bbb0cf927296'
147 ]
148 ];
149 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
150 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
151 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
152 }
153 }
154 }
155
156 public function numericKeysDataProvider()
157 {
158 /** @var $factory LocalizationFactory */
159 $factory = new LocalizationFactory;
160
161 $LOCAL_LANG = $factory->getParsedData(self::getFixtureFilePath('locallangNumericKeys.xml'), 'default');
162 $translations = [];
163
164 foreach ($LOCAL_LANG['default'] as $key => $labelData) {
165 $translations['Numerical key ' . $key] = [$key, $labelData[0]['source'] . ' [FR]'];
166 }
167
168 return $translations;
169 }
170
171 /**
172 * @test
173 * @dataProvider numericKeysDataProvider
174 */
175 public function canTranslateNumericKeys($key, $expectedResult)
176 {
177 /** @var $factory LocalizationFactory */
178 $factory = new LocalizationFactory;
179
180 $LOCAL_LANG = $factory->getParsedData(self::getFixtureFilePath('locallangNumericKeys.xml'), 'fr');
181
182 $this->assertEquals($expectedResult, $LOCAL_LANG['fr'][$key][0]['target']);
183 }
184 }