63492f5e3dc0148e216607111d09b2d2f7ba813c
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Utility / LocalizationUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Extbase Team
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case
29 */
30 class LocalizationUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Utility\LocalizationUtility|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
34 */
35 protected $localization;
36
37 /**
38 * LOCAL_LANG array fixture
39 *
40 * @var array
41 */
42 protected $LOCAL_LANG = array(
43 'extensionKey' => array(
44 'default' => array(
45 'key1' => array(
46 array(
47 'source' => 'English label for key1',
48 'target' => 'English label for key1',
49 )
50 ),
51 'key2' => array(
52 array(
53 'source' => 'English label for key2',
54 'target' => 'English label for key2',
55 )
56 ),
57 'key3' => array(
58 array(
59 'source' => 'English label for key3',
60 'target' => 'English label for key3',
61 )
62 ),
63 'key4' => array(
64 array(
65 'source' => 'English label for key4',
66 'target' => 'English label for key4',
67 )
68 ),
69 'keyWithPlaceholder' => array(
70 array(
71 'source' => 'English label with number %d',
72 'target' => 'English label with number %d',
73 )
74 ),
75 ),
76 'dk' => array(
77 'key1' => array(
78 array(
79 'source' => 'English label for key1',
80 'target' => 'Dansk label for key1',
81 )
82 ),
83 // not translated in dk => no target (llxml)
84 'key2' => array(
85 array(
86 'source' => 'English label for key2',
87 )
88 ),
89 'key3' => array(
90 array(
91 'source' => 'English label for key3',
92 )
93 ),
94 // not translated in dk => empty target (xliff)
95 'key4' => array(
96 array(
97 'source' => 'English label for key4',
98 'target' => '',
99 )
100 ),
101 // not translated in dk => empty target (xliff)
102 'key5' => array(
103 array(
104 'source' => 'English label for key5',
105 'target' => '',
106 )
107 ),
108 'keyWithPlaceholder' => array(
109 array(
110 'source' => 'English label with number %d',
111 )
112 ),
113 ),
114 // fallback language for labels which are not translated in dk
115 'dk_alt' => array(
116 'key1' => array(
117 array(
118 'source' => 'English label for key1',
119 )
120 ),
121 'key2' => array(
122 array(
123 'source' => 'English label for key2',
124 'target' => 'Dansk alternative label for key2',
125 )
126 ),
127 'key3' => array(
128 array(
129 'source' => 'English label for key3',
130 )
131 ),
132 // not translated in dk_alt => empty target (xliff)
133 'key4' => array(
134 array(
135 'source' => 'English label for key4',
136 'target' => '',
137 )
138 ),
139 'key5' => array(
140 array(
141 'source' => 'English label for key5',
142 'target' => 'Dansk alternative label for key5',
143 )
144 ),
145 'keyWithPlaceholder' => array(
146 array(
147 'source' => 'English label with number %d',
148 )
149 ),
150 ),
151
152 ),
153 );
154
155 public function setUp() {
156 $this->localization = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility', array('getConfigurationManager'));
157 }
158
159 /**
160 * @test
161 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
162 */
163 public function implodeTypoScriptLabelArrayWorks() {
164 $expected = array(
165 'key1' => 'value1',
166 'key2' => 'value2',
167 'key3.subkey1' => 'subvalue1',
168 'key3.subkey2.subsubkey' => 'val'
169 );
170 $actual = $this->localization->_call('flattenTypoScriptLabelArray', array(
171 'key1' => 'value1',
172 'key2' => 'value2',
173 'key3' => array(
174 'subkey1' => 'subvalue1',
175 'subkey2' => array(
176 'subsubkey' => 'val'
177 )
178 )
179 ));
180 $this->assertEquals($expected, $actual);
181 }
182
183 /**
184 * @test
185 */
186 public function translateForEmptyStringKeyReturnsNull() {
187 $this->localization->_setStatic('LOCAL_LANG', array());
188 $configurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('getConfiguration'));
189 $this->localization->staticExpects($this->atLeastOnce())->method('getConfigurationManager')->will($this->returnValue($configurationManager));
190 $this->assertNull($this->localization->translate('', 'extbase'));
191 }
192
193 /**
194 * @test
195 */
196 public function translateForEmptyStringKeyWithArgumentsReturnsNull() {
197 $this->localization->_setStatic('LOCAL_LANG', array());
198 $configurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('getConfiguration'));
199 $this->localization->staticExpects($this->atLeastOnce())->method('getConfigurationManager')->will($this->returnValue($configurationManager));
200 $this->assertNull($this->localization->translate('', 'extbase', array('argument')));
201 }
202
203 /**
204 * @return array
205 */
206 public function translateDataProvider() {
207 return array(
208 'get translated key' =>
209 array('key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1'),
210
211 'fallback to English when translation is missing for key' =>
212 array('key2', $this->LOCAL_LANG, 'dk', 'English label for key2'),
213
214 'fallback to English for non existing language' =>
215 array('key2', $this->LOCAL_LANG, 'xx', 'English label for key2'),
216
217 'replace placeholder with argument' =>
218 array('keyWithPlaceholder', $this->LOCAL_LANG, 'en', 'English label with number 100', array(), array(100)),
219
220 'get translated key from primary language' =>
221 array('key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1', array('dk_alt')),
222
223 'fallback to alternative language if translation is missing(llxml)' =>
224 array('key2', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key2', array('dk_alt')),
225
226 'fallback to alternative language if translation is missing(xlif)' =>
227 array('key5', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key5', array('dk_alt')),
228
229 'fallback to English for label not translated in dk and dk_alt(llxml)' =>
230 array('key3', $this->LOCAL_LANG, 'dk', 'English label for key3', array('dk_alt')),
231
232 'fallback to English for label not translated in dk and dk_alt(xlif)' =>
233 array('key4', $this->LOCAL_LANG, 'dk', 'English label for key4', array('dk_alt')),
234 );
235 }
236
237 /**
238 * @param string $key
239 * @param array $LOCAL_LANG
240 * @param string $languageKey
241 * @param string $expected
242 * @param array $altLanguageKeys
243 * @param array $arguments
244 * @return void
245 * @dataProvider translateDataProvider
246 * @test
247 */
248 public function translateTest($key, array $LOCAL_LANG, $languageKey, $expected, array $altLanguageKeys = array(), array $arguments = NULL) {
249 $this->localization->_setStatic('LOCAL_LANG', $LOCAL_LANG);
250 $this->localization->_setStatic('languageKey', $languageKey);
251 $this->localization->_setStatic('alternativeLanguageKeys', $altLanguageKeys);
252
253 $this->assertEquals($expected, $this->localization->translate($key, 'extensionKey', $arguments));
254 }
255
256 /**
257 * @return array
258 */
259 public function loadTypoScriptLabelsProvider() {
260 return array(
261 'override labels with typoscript' => array(
262 'LOCAL_LANG' => array(
263 'extensionKey' => array(
264 'dk' => array(
265 'key1' => array(
266 array(
267 'source' => 'English label for key1',
268 'target' => 'Dansk label for key1 extensionKey',
269 )
270 ),
271 'key2' => array(
272 array(
273 'source' => 'English label for key2',
274 )
275 ),
276 'key3.subkey1' => array(
277 array(
278 'source' => 'English label for key3',
279 )
280 ),
281 ),
282 ),
283 'extensionKey1' => array(
284 'dk' => array(
285 'key1' => array(
286 array(
287 'source' => 'English label for key1',
288 'target' => 'Dansk label for key1 extensionKey1',
289 )
290 ),
291 'key2' => array(
292 array(
293 'source' => 'English label for key2',
294 )
295 ),
296 'key3.subkey1' => array(
297 array(
298 'source' => 'English label for key3',
299 )
300 ),
301 ),
302 ),
303 ),
304 'typoscript LOCAL_LANG' => array(
305 '_LOCAL_LANG' => array(
306 'dk' => array(
307 'key1' => 'key1 value from TS extensionKey',
308 'key3' => array(
309 'subkey1' => 'key3.subkey1 value from TS extensionKey',
310 // this key doesn't exist in xml files
311 'subkey2' => array(
312 'subsubkey' => 'key3.subkey2.subsubkey value from TS extensionKey'
313 )
314 )
315 )
316 )
317 ),
318 'language key' => 'dk',
319 'expected' => array(
320 'key1' => array(
321 array(
322 'source' => 'English label for key1',
323 'target' => 'key1 value from TS extensionKey',
324 )
325 ),
326 'key2' => array(
327 array(
328 'source' => 'English label for key2',
329 )
330 ),
331 'key3.subkey1' => array(
332 array(
333 'source' => 'English label for key3',
334 'target' => 'key3.subkey1 value from TS extensionKey',
335 )
336 ),
337 'key3.subkey2.subsubkey' => array(
338 array(
339 'target' => 'key3.subkey2.subsubkey value from TS extensionKey',
340 )
341 ),
342 ),
343 )
344 );
345 }
346
347 /**
348 * Tests whether labels from xml are overwritten by TypoScript labels
349 *
350 * @param array $LOCAL_LANG
351 * @param array $typoScriptLocalLang
352 * @param string $languageKey
353 * @param array $expected
354 * @return void
355 * @dataProvider loadTypoScriptLabelsProvider
356 * @test
357 */
358 public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoScriptLocalLang, $languageKey, array $expected) {
359
360 $configurationType = \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK;
361
362 $configurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('getConfiguration'));
363 $configurationManager->expects($this->at(0))->method('getConfiguration')->with($configurationType, 'extensionKey', NULL)->will($this->returnValue($typoScriptLocalLang));
364
365 $this->localization->staticExpects($this->atLeastOnce())->method('getConfigurationManager')->will($this->returnValue($configurationManager));
366
367 // translations loaded from xml files
368 $this->localization->_setStatic('LOCAL_LANG', $LOCAL_LANG);
369 $this->localization->_setStatic('languageKey', $languageKey);
370
371 $this->localization->_call('loadTypoScriptLabels', 'extensionKey');
372 $result = $this->localization->_getStatic('LOCAL_LANG');
373 $this->assertEquals($expected, $result['extensionKey'][$languageKey]);
374 }
375
376 /**
377 * @return void
378 * @test
379 */
380 public function clearLabelWithTypoScript() {
381 $this->localization->_setStatic('LOCAL_LANG', $this->LOCAL_LANG);
382 $this->localization->_setStatic('languageKey', 'dk');
383
384 $typoScriptLocalLang = array(
385 '_LOCAL_LANG' => array(
386 'dk' => array(
387 'key1' => '',
388 )
389 )
390 );
391
392 $configurationType = \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK;
393
394 $configurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('getConfiguration'));
395 $configurationManager->expects($this->at(0))->method('getConfiguration')->with($configurationType, 'extensionKey', NULL)->will($this->returnValue($typoScriptLocalLang));
396
397 $this->localization->staticExpects($this->atLeastOnce())->method('getConfigurationManager')->will($this->returnValue($configurationManager));
398
399 $this->localization->_call('loadTypoScriptLabels', 'extensionKey');
400 $result = $this->localization->translate('key1', 'extensionKey');
401 $this->assertNotNull($result);
402 $this->assertEquals('', $result);
403 }
404 }