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