[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Localization / Parser / XliffParserTest.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\XliffParser.
20 */
21 class XliffParserTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
22 {
23 /**
24 * @var \TYPO3\CMS\Core\Localization\Parser\XliffParser
25 */
26 protected $parser;
27
28 /**
29 * @var array
30 */
31 protected $locallangXMLOverride;
32
33 /**
34 * @var string
35 */
36 protected $l10nPriority;
37
38 /**
39 * @var array
40 */
41 protected $xliffFileNames;
42
43 /**
44 * Prepares the environment before running a test.
45 */
46 protected function setUp()
47 {
48 // Backup locallangXMLOverride and localization format priority
49 $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
50 $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
51 $this->parser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Parser\XliffParser::class);
52
53 // We have to take the whole relative path as otherwise this test fails on Windows systems
54 $fixturePath = PATH_site . 'typo3/sysext/core/Tests/Unit/Localization/Parser/Fixtures/';
55 $this->xliffFileNames = [
56 'locallang' => $fixturePath . 'locallang.xlf',
57 'locallang_override' => $fixturePath . 'locallang_override.xlf',
58 'locallang_override_fr' => $fixturePath . 'fr.locallang_override.xlf'
59 ];
60 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xlf';
61 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
62 // Clear localization cache
63 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
64 }
65
66 /**
67 * Cleans up the environment after running a test.
68 */
69 protected function tearDown()
70 {
71 // Restore locallangXMLOverride and localization format priority
72 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
73 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
74 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class)->initialize();
75 parent::tearDown();
76 }
77
78 /**
79 * @test
80 */
81 public function canParseXliffInEnglish()
82 {
83 $LOCAL_LANG = $this->parser->getParsedData($this->xliffFileNames['locallang'], 'default');
84 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
85 $expectedLabels = [
86 'label1' => 'This is label #1',
87 'label2' => 'This is label #2',
88 'label3' => 'This is label #3'
89 ];
90 foreach ($expectedLabels as $key => $expectedLabel) {
91 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
92 }
93 }
94
95 /**
96 * @test
97 */
98 public function canParseXliffInFrench()
99 {
100 $LOCAL_LANG = $this->parser->getParsedData($this->xliffFileNames['locallang'], 'fr');
101 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
102 $expectedLabels = [
103 'label1' => 'Ceci est le libellé no. 1',
104 'label2' => 'Ceci est le libellé no. 2',
105 'label3' => 'Ceci est le libellé no. 3'
106 ];
107 foreach ($expectedLabels as $key => $expectedLabel) {
108 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
109 }
110 }
111
112 /**
113 * @test
114 */
115 public function canOverrideXliff()
116 {
117 /** @var $factory LocalizationFactory */
118 $factory = new LocalizationFactory;
119
120 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override'];
121 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
122 $LOCAL_LANG = array_merge(
123 $factory->getParsedData($this->xliffFileNames['locallang'], 'default'),
124 $factory->getParsedData($this->xliffFileNames['locallang'], 'fr')
125 );
126 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
127 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
128 $expectedLabels = [
129 'default' => [
130 'label1' => 'This is my 1st label',
131 'label2' => 'This is my 2nd label',
132 'label3' => 'This is label #3'
133 ],
134 'fr' => [
135 'label1' => 'Ceci est mon 1er libellé',
136 'label2' => 'Ceci est le libellé no. 2',
137 'label3' => 'Ceci est mon 3e libellé'
138 ]
139 ];
140 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
141 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
142 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
143 }
144 }
145 }
146
147 /**
148 * This test will make sure method \TYPO3\CMS\Core\Utility\GeneralUtility::llXmlAutoFileName() will not prefix twice the
149 * language key to the localization file.
150 *
151 * @test
152 */
153 public function canOverrideXliffWithFrenchOnly()
154 {
155 /** @var $factory LocalizationFactory */
156 $factory = new LocalizationFactory;
157
158 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
159 $LOCAL_LANG = $factory->getParsedData($this->xliffFileNames['locallang'], 'fr');
160 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
161 $expectedLabels = [
162 'label1' => 'Ceci est mon 1er libellé',
163 'label2' => 'Ceci est le libellé no. 2',
164 'label3' => 'Ceci est mon 3e libellé'
165 ];
166 foreach ($expectedLabels as $key => $expectedLabel) {
167 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
168 }
169 }
170 }