[TASK] Update to phpunit 4.1
[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 use \TYPO3\CMS\Extbase\Utility\LocalizationUtility;
28
29 /**
30 * Test case
31 */
32 class LocalizationUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * Instance of configurationManager, injected to subject
36 *
37 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
38 */
39 protected $configurationManagerMock;
40
41 /**
42 * LOCAL_LANG array fixture
43 *
44 * @var array
45 */
46 protected $LOCAL_LANG = array(
47 'extensionKey' => array(
48 'default' => array(
49 'key1' => array(
50 array(
51 'source' => 'English label for key1',
52 'target' => 'English label for key1',
53 )
54 ),
55 'key2' => array(
56 array(
57 'source' => 'English label for key2',
58 'target' => 'English label for key2',
59 )
60 ),
61 'key3' => array(
62 array(
63 'source' => 'English label for key3',
64 'target' => 'English label for key3',
65 )
66 ),
67 'key4' => array(
68 array(
69 'source' => 'English label for key4',
70 'target' => 'English label for key4',
71 )
72 ),
73 'keyWithPlaceholder' => array(
74 array(
75 'source' => 'English label with number %d',
76 'target' => 'English label with number %d',
77 )
78 ),
79 ),
80 'dk' => array(
81 'key1' => array(
82 array(
83 'source' => 'English label for key1',
84 'target' => 'Dansk label for key1',
85 )
86 ),
87 // not translated in dk => no target (llxml)
88 'key2' => array(
89 array(
90 'source' => 'English label for key2',
91 )
92 ),
93 'key3' => array(
94 array(
95 'source' => 'English label for key3',
96 )
97 ),
98 // not translated in dk => empty target (xliff)
99 'key4' => array(
100 array(
101 'source' => 'English label for key4',
102 'target' => '',
103 )
104 ),
105 // not translated in dk => empty target (xliff)
106 'key5' => array(
107 array(
108 'source' => 'English label for key5',
109 'target' => '',
110 )
111 ),
112 'keyWithPlaceholder' => array(
113 array(
114 'source' => 'English label with number %d',
115 )
116 ),
117 ),
118 // fallback language for labels which are not translated in dk
119 'dk_alt' => array(
120 'key1' => array(
121 array(
122 'source' => 'English label for key1',
123 )
124 ),
125 'key2' => array(
126 array(
127 'source' => 'English label for key2',
128 'target' => 'Dansk alternative label for key2',
129 )
130 ),
131 'key3' => array(
132 array(
133 'source' => 'English label for key3',
134 )
135 ),
136 // not translated in dk_alt => empty target (xliff)
137 'key4' => array(
138 array(
139 'source' => 'English label for key4',
140 'target' => '',
141 )
142 ),
143 'key5' => array(
144 array(
145 'source' => 'English label for key5',
146 'target' => 'Dansk alternative label for key5',
147 )
148 ),
149 'keyWithPlaceholder' => array(
150 array(
151 'source' => 'English label with number %d',
152 )
153 ),
154 ),
155
156 ),
157 );
158
159 /**
160 * Prepare class mocking some dependencies
161 */
162 public function setUp() {
163 $reflectionClass = new \ReflectionClass('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility');
164
165 $this->configurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('getConfiguration'));
166 $property = $reflectionClass->getProperty('configurationManager');
167 $property->setAccessible(TRUE);
168 $property->setValue($this->configurationManager);
169 }
170
171 /**
172 * Reset static properties
173 */
174 public function tearDown() {
175 $reflectionClass = new \ReflectionClass('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility');
176
177 $property = $reflectionClass->getProperty('configurationManager');
178 $property->setAccessible(TRUE);
179 $property->setValue(NULL);
180
181 $property = $reflectionClass->getProperty('LOCAL_LANG');
182 $property->setAccessible(TRUE);
183 $property->setValue(array());
184
185 $property = $reflectionClass->getProperty('languageKey');
186 $property->setAccessible(TRUE);
187 $property->setValue('default');
188
189 $property = $reflectionClass->getProperty('alternativeLanguageKeys');
190 $property->setAccessible(TRUE);
191 $property->setValue(array());
192 }
193
194 /**
195 * @test
196 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
197 */
198 public function implodeTypoScriptLabelArrayWorks() {
199 $reflectionClass = new \ReflectionClass('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility');
200 $method = $reflectionClass->getMethod('flattenTypoScriptLabelArray');
201 $method->setAccessible(TRUE);
202
203 $expected = array(
204 'key1' => 'value1',
205 'key2' => 'value2',
206 'key3.subkey1' => 'subvalue1',
207 'key3.subkey2.subsubkey' => 'val'
208 );
209 $input = array(
210 'key1' => 'value1',
211 'key2' => 'value2',
212 'key3' => array(
213 'subkey1' => 'subvalue1',
214 'subkey2' => array(
215 'subsubkey' => 'val'
216 )
217 )
218 );
219 $result = $method->invoke(NULL, $input);
220 $this->assertEquals($expected, $result);
221 }
222
223 /**
224 * @test
225 */
226 public function translateForEmptyStringKeyReturnsNull() {
227 $this->assertNull(LocalizationUtility::translate('', 'extbase'));
228 }
229
230 /**
231 * @test
232 */
233 public function translateForEmptyStringKeyWithArgumentsReturnsNull() {
234 $this->assertNull(LocalizationUtility::translate('', 'extbase', array('argument')));
235 }
236
237 /**
238 * @return array
239 */
240 public function translateDataProvider() {
241 return array(
242 'get translated key' =>
243 array('key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1'),
244
245 'fallback to English when translation is missing for key' =>
246 array('key2', $this->LOCAL_LANG, 'dk', 'English label for key2'),
247
248 'fallback to English for non existing language' =>
249 array('key2', $this->LOCAL_LANG, 'xx', 'English label for key2'),
250
251 'replace placeholder with argument' =>
252 array('keyWithPlaceholder', $this->LOCAL_LANG, 'en', 'English label with number 100', array(), array(100)),
253
254 'get translated key from primary language' =>
255 array('key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1', array('dk_alt')),
256
257 'fallback to alternative language if translation is missing(llxml)' =>
258 array('key2', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key2', array('dk_alt')),
259
260 'fallback to alternative language if translation is missing(xlif)' =>
261 array('key5', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key5', array('dk_alt')),
262
263 'fallback to English for label not translated in dk and dk_alt(llxml)' =>
264 array('key3', $this->LOCAL_LANG, 'dk', 'English label for key3', array('dk_alt')),
265
266 'fallback to English for label not translated in dk and dk_alt(xlif)' =>
267 array('key4', $this->LOCAL_LANG, 'dk', 'English label for key4', array('dk_alt')),
268 );
269 }
270
271 /**
272 * @param string $key
273 * @param array $LOCAL_LANG
274 * @param string $languageKey
275 * @param string $expected
276 * @param array $altLanguageKeys
277 * @param array $arguments
278 * @return void
279 * @dataProvider translateDataProvider
280 * @test
281 */
282 public function translateTest($key, array $LOCAL_LANG, $languageKey, $expected, array $altLanguageKeys = array(), array $arguments = NULL) {
283 $reflectionClass = new \ReflectionClass('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility');
284
285 $property = $reflectionClass->getProperty('LOCAL_LANG');
286 $property->setAccessible(TRUE);
287 $property->setValue($LOCAL_LANG);
288
289 $property = $reflectionClass->getProperty('languageKey');
290 $property->setAccessible(TRUE);
291 $property->setValue($languageKey);
292
293 $property = $reflectionClass->getProperty('alternativeLanguageKeys');
294 $property->setAccessible(TRUE);
295 $property->setValue($altLanguageKeys);
296
297 $this->assertEquals($expected, LocalizationUtility::translate($key, 'extensionKey', $arguments));
298 }
299
300 /**
301 * @return array
302 */
303 public function loadTypoScriptLabelsProvider() {
304 return array(
305 'override labels with typoscript' => array(
306 'LOCAL_LANG' => array(
307 'extensionKey' => array(
308 'dk' => array(
309 'key1' => array(
310 array(
311 'source' => 'English label for key1',
312 'target' => 'Dansk label for key1 extensionKey',
313 )
314 ),
315 'key2' => array(
316 array(
317 'source' => 'English label for key2',
318 )
319 ),
320 'key3.subkey1' => array(
321 array(
322 'source' => 'English label for key3',
323 )
324 ),
325 ),
326 ),
327 'extensionKey1' => array(
328 'dk' => array(
329 'key1' => array(
330 array(
331 'source' => 'English label for key1',
332 'target' => 'Dansk label for key1 extensionKey1',
333 )
334 ),
335 'key2' => array(
336 array(
337 'source' => 'English label for key2',
338 )
339 ),
340 'key3.subkey1' => array(
341 array(
342 'source' => 'English label for key3',
343 )
344 ),
345 ),
346 ),
347 ),
348 'typoscript LOCAL_LANG' => array(
349 '_LOCAL_LANG' => array(
350 'dk' => array(
351 'key1' => 'key1 value from TS extensionKey',
352 'key3' => array(
353 'subkey1' => 'key3.subkey1 value from TS extensionKey',
354 // this key doesn't exist in xml files
355 'subkey2' => array(
356 'subsubkey' => 'key3.subkey2.subsubkey value from TS extensionKey'
357 )
358 )
359 )
360 )
361 ),
362 'language key' => 'dk',
363 'expected' => array(
364 'key1' => array(
365 array(
366 'source' => 'English label for key1',
367 'target' => 'key1 value from TS extensionKey',
368 )
369 ),
370 'key2' => array(
371 array(
372 'source' => 'English label for key2',
373 )
374 ),
375 'key3.subkey1' => array(
376 array(
377 'source' => 'English label for key3',
378 'target' => 'key3.subkey1 value from TS extensionKey',
379 )
380 ),
381 'key3.subkey2.subsubkey' => array(
382 array(
383 'target' => 'key3.subkey2.subsubkey value from TS extensionKey',
384 )
385 ),
386 ),
387 )
388 );
389 }
390
391 /**
392 * Tests whether labels from xml are overwritten by TypoScript labels
393 *
394 * @param array $LOCAL_LANG
395 * @param array $typoScriptLocalLang
396 * @param string $languageKey
397 * @param array $expected
398 * @return void
399 * @dataProvider loadTypoScriptLabelsProvider
400 * @test
401 */
402 public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoScriptLocalLang, $languageKey, array $expected) {
403 $reflectionClass = new \ReflectionClass('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility');
404
405 $property = $reflectionClass->getProperty('LOCAL_LANG');
406 $property->setAccessible(TRUE);
407 $property->setValue($LOCAL_LANG);
408
409 $property = $reflectionClass->getProperty('languageKey');
410 $property->setAccessible(TRUE);
411 $property->setValue($languageKey);
412
413 $configurationType = \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK;
414 $this->configurationManager->expects($this->at(0))->method('getConfiguration')->with($configurationType, 'extensionKey', NULL)->will($this->returnValue($typoScriptLocalLang));
415
416 $method = $reflectionClass->getMethod('loadTypoScriptLabels');
417 $method->setAccessible(TRUE);
418 $method->invoke(NULL, 'extensionKey');
419
420 $property = $reflectionClass->getProperty('LOCAL_LANG');
421 $property->setAccessible(TRUE);
422 $result = $property->getValue();
423
424 $this->assertEquals($expected, $result['extensionKey'][$languageKey]);
425 }
426
427 /**
428 * @return void
429 * @test
430 */
431 public function clearLabelWithTypoScript() {
432 $reflectionClass = new \ReflectionClass('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility');
433
434 $property = $reflectionClass->getProperty('LOCAL_LANG');
435 $property->setAccessible(TRUE);
436 $property->setValue($this->LOCAL_LANG);
437
438 $property = $reflectionClass->getProperty('languageKey');
439 $property->setAccessible(TRUE);
440 $property->setValue('dk');
441
442 $typoScriptLocalLang = array(
443 '_LOCAL_LANG' => array(
444 'dk' => array(
445 'key1' => '',
446 )
447 )
448 );
449
450 $configurationType = \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK;
451 $this->configurationManager->expects($this->at(0))->method('getConfiguration')->with($configurationType, 'extensionKey', NULL)->will($this->returnValue($typoScriptLocalLang));
452
453 $method = $reflectionClass->getMethod('loadTypoScriptLabels');
454 $method->setAccessible(TRUE);
455 $method->invoke(NULL, 'extensionKey');
456
457 $result = LocalizationUtility::translate('key1', 'extensionKey');
458 $this->assertNotNull($result);
459 $this->assertEquals('', $result);
460 }
461 }