[BUGFIX] locallangXMLOverride is broken since the switch to XLIFF
[Packages/TYPO3.CMS.git] / tests / t3lib / l10n / parser / class.t3lib_l10n_parser_xliffTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Xavier Perseguers <xavier@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for class t3lib_l10n_parser_xliff.
27 *
28 * @author Xavier Perseguers <xavier@typo3.org>
29 * @package TYPO3
30 */
31 class t3lib_l10n_parser_xliffTest extends tx_phpunit_testcase {
32
33 /**
34 * @var t3lib_l10n_parser_xliff
35 */
36 protected $parser;
37
38 /**
39 * @var array
40 */
41 protected $locallangXMLOverride;
42
43 /**
44 * @var string
45 */
46 protected $l10nPriority;
47
48 /**
49 * @var array
50 */
51 protected $xliffFileNames;
52
53 /**
54 * Prepares the environment before running a test.
55 */
56 public function setUp() {
57 // Backup locallangXMLOverride and localization format priority
58 $this->locallangXMLOverride = $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'];
59 $this->l10nPriority = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'];
60
61 $this->parser = t3lib_div::makeInstance('t3lib_l10n_parser_xliff');
62 $this->xliffFileNames = array(
63 'locallang' => PATH_site . 'typo3_src/tests/t3lib/l10n/parser/fixtures/locallang.xlf',
64 'locallang_override' => PATH_site . 'typo3_src/tests/t3lib/l10n/parser/fixtures/locallang_override.xlf',
65 'locallang_override_fr' => PATH_site . 'typo3_src/tests/t3lib/l10n/parser/fixtures/fr.locallang_override.xlf',
66 );
67 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = 'xlf';
68 t3lib_div::makeInstance('t3lib_l10n_Store')->initialize();
69
70 // Clear localization cache
71 $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n')->flush();
72 }
73
74 /**
75 * Cleans up the environment after running a test.
76 */
77 public function tearDown() {
78 unset($this->parser);
79
80 // Restore locallangXMLOverride and localization format priority
81 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'] = $this->locallangXMLOverride;
82 $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['format']['priority'] = $this->l10nPriority;
83 t3lib_div::makeInstance('t3lib_l10n_Store')->initialize();
84 }
85
86 /**
87 * @test
88 */
89 public function canParseXliffInEnglish() {
90 $LOCAL_LANG = $this->parser->getParsedData($this->xliffFileNames['locallang'], 'default');
91
92 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
93
94 $expectedLabels = array(
95 'label1' => 'This is label #1',
96 'label2' => 'This is label #2',
97 'label3' => 'This is label #3',
98 );
99
100 foreach ($expectedLabels as $key => $expectedLabel) {
101 $this->assertEquals($expectedLabel, $LOCAL_LANG['default'][$key][0]['target']);
102 }
103 }
104
105 /**
106 * @test
107 */
108 public function canParseXliffInFrench() {
109 $LOCAL_LANG = $this->parser->getParsedData($this->xliffFileNames['locallang'], 'fr');
110
111 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
112
113 $expectedLabels = array(
114 'label1' => 'Ceci est le libellé no. 1',
115 'label2' => 'Ceci est le libellé no. 2',
116 'label3' => 'Ceci est le libellé no. 3',
117 );
118
119 foreach ($expectedLabels as $key => $expectedLabel) {
120 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
121 }
122 }
123
124 /**
125 * @test
126 */
127 public function canOverrideXliff() {
128 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override'];
129 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
130
131 $LOCAL_LANG = array_merge(
132 t3lib_div::readLLfile($this->xliffFileNames['locallang'], 'default'),
133 t3lib_div::readLLfile($this->xliffFileNames['locallang'], 'fr')
134 );
135
136 $this->assertArrayHasKey('default', $LOCAL_LANG, 'default key not found in $LOCAL_LANG');
137 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
138
139 $expectedLabels = array(
140 'default' => array(
141 'label1' => 'This is my 1st label',
142 'label2' => 'This is my 2nd label',
143 'label3' => 'This is label #3',
144 ),
145 'fr' => array(
146 'label1' => 'Ceci est mon 1er libellé',
147 'label2' => 'Ceci est le libellé no. 2',
148 'label3' => 'Ceci est mon 3e libellé',
149 )
150 );
151
152 foreach ($expectedLabels as $languageKey => $expectedLanguageLabels) {
153 foreach ($expectedLanguageLabels as $key => $expectedLabel) {
154 $this->assertEquals($expectedLabel, $LOCAL_LANG[$languageKey][$key][0]['target']);
155 }
156 }
157 }
158
159 /**
160 * This test will make sure method t3lib_div::llXmlAutoFileName() will not prefix twice the
161 * language key to the localization file.
162 *
163 * @test
164 */
165 public function canOverrideXliffWithFrenchOnly() {
166 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['fr'][$this->xliffFileNames['locallang']][] = $this->xliffFileNames['locallang_override_fr'];
167
168 $LOCAL_LANG = t3lib_div::readLLfile($this->xliffFileNames['locallang'], 'fr');
169
170 $this->assertArrayHasKey('fr', $LOCAL_LANG, 'fr key not found in $LOCAL_LANG');
171
172 $expectedLabels = array(
173 'label1' => 'Ceci est mon 1er libellé',
174 'label2' => 'Ceci est le libellé no. 2',
175 'label3' => 'Ceci est mon 3e libellé',
176 );
177
178 foreach ($expectedLabels as $key => $expectedLabel) {
179 $this->assertEquals($expectedLabel, $LOCAL_LANG['fr'][$key][0]['target']);
180 }
181 }
182
183 }
184
185 ?>