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