[TASK] Increase code coverage of FrontendConfigurationManager
[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 * Testcase for class \TYPO3\CMS\Extbase\Utility\LocalizationUtility
28 */
29 class LocalizationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Utility\LocalizationUtility|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
33 */
34 protected $localization;
35
36 /**
37 * LOCAL_LANG array fixture
38 *
39 * @var array
40 */
41 protected $LOCAL_LANG = array(
42 'extensionKey' => array(
43 'default' => array(
44 'key1' => array(
45 array(
46 'source' => 'English label for key1',
47 'target' => 'English label for key1',
48 )
49 ),
50 'key2' => array(
51 array(
52 'source' => 'English label for key2',
53 'target' => 'English label for key2',
54 )
55 ),
56 'key3' => array(
57 array(
58 'source' => 'English label for key3',
59 'target' => 'English label for key3',
60 )
61 ),
62 'key4' => array(
63 array(
64 'source' => 'English label for key4',
65 'target' => 'English label for key4',
66 )
67 ),
68 'keyWithPlaceholder' => array(
69 array(
70 'source' => 'English label with number %d',
71 'target' => 'English label with number %d',
72 )
73 ),
74 ),
75 'dk' => array(
76 'key1' => array(
77 array(
78 'source' => 'English label for key1',
79 'target' => 'Dansk label for key1',
80 )
81 ),
82 // not translated in dk => no target (llxml)
83 'key2' => array(
84 array(
85 'source' => 'English label for key2',
86 )
87 ),
88 'key3' => array(
89 array(
90 'source' => 'English label for key3',
91 )
92 ),
93 // not translated in dk => empty target (xliff)
94 'key4' => array(
95 array(
96 'source' => 'English label for key4',
97 'target' => '',
98 )
99 ),
100 // not translated in dk => empty target (xliff)
101 'key5' => array(
102 array(
103 'source' => 'English label for key5',
104 'target' => '',
105 )
106 ),
107 'keyWithPlaceholder' => array(
108 array(
109 'source' => 'English label with number %d',
110 )
111 ),
112 ),
113 // fallback language for labels which are not translated in dk
114 'dk_alt' => array(
115 'key1' => array(
116 array(
117 'source' => 'English label for key1',
118 )
119 ),
120 'key2' => array(
121 array(
122 'source' => 'English label for key2',
123 'target' => 'Dansk alternative label for key2',
124 )
125 ),
126 'key3' => array(
127 array(
128 'source' => 'English label for key3',
129 )
130 ),
131 // not translated in dk_alt => empty target (xliff)
132 'key4' => array(
133 array(
134 'source' => 'English label for key4',
135 'target' => '',
136 )
137 ),
138 'key5' => array(
139 array(
140 'source' => 'English label for key5',
141 'target' => 'Dansk alternative label for key5',
142 )
143 ),
144 'keyWithPlaceholder' => array(
145 array(
146 'source' => 'English label with number %d',
147 )
148 ),
149 ),
150
151 ),
152 );
153
154 public function setUp() {
155 $this->localization = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility', array('dummy'));
156 }
157
158 public function tearDown() {
159 $this->localization = NULL;
160 }
161
162 /**
163 * @test
164 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
165 */
166 public function implodeTypoScriptLabelArrayWorks() {
167 $expected = array(
168 'key1' => 'value1',
169 'key2' => 'value2',
170 'key3.subkey1' => 'subvalue1',
171 'key3.subkey2.subsubkey' => 'val'
172 );
173 $actual = $this->localization->_call('flattenTypoScriptLabelArray', array(
174 'key1' => 'value1',
175 'key2' => 'value2',
176 'key3' => array(
177 'subkey1' => 'subvalue1',
178 'subkey2' => array(
179 'subsubkey' => 'val'
180 )
181 )
182 ));
183 $this->assertEquals($expected, $actual);
184 }
185
186 /**
187 * @test
188 */
189 public function translateForEmptyStringKeyReturnsNull() {
190 $this->assertNull(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('', 'extbase'));
191 }
192
193 /**
194 * @test
195 */
196 public function translateForEmptyStringKeyWithArgumentsReturnsNull() {
197 $this->assertNull(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('', 'extbase', array('argument')));
198 }
199
200 /**
201 * @return array
202 */
203 public function translateDataProvider() {
204 return array(
205 'get translated key' =>
206 array('key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1'),
207
208 'fallback to English when translation is missing for key' =>
209 array('key2', $this->LOCAL_LANG, 'dk', 'English label for key2'),
210
211 'fallback to English for non existing language' =>
212 array('key2', $this->LOCAL_LANG, 'xx', 'English label for key2'),
213
214 'replace placeholder with argument' =>
215 array('keyWithPlaceholder', $this->LOCAL_LANG, 'en', 'English label with number 100', array(), array(100)),
216
217 'get translated key from primary language' =>
218 array('key1', $this->LOCAL_LANG, 'dk', 'Dansk label for key1', array('dk_alt')),
219
220 'fallback to alternative language if translation is missing(llxml)' =>
221 array('key2', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key2', array('dk_alt')),
222
223 'fallback to alternative language if translation is missing(xlif)' =>
224 array('key5', $this->LOCAL_LANG, 'dk', 'Dansk alternative label for key5', array('dk_alt')),
225
226 'fallback to English for label not translated in dk and dk_alt(llxml)' =>
227 array('key3', $this->LOCAL_LANG, 'dk', 'English label for key3', array('dk_alt')),
228
229 'fallback to English for label not translated in dk and dk_alt(xlif)' =>
230 array('key4', $this->LOCAL_LANG, 'dk', 'English label for key4', array('dk_alt')),
231 );
232 }
233
234 /**
235 * @param string $key
236 * @param array $LOCAL_LANG
237 * @param string $languageKey
238 * @param string $expected
239 * @param array $altLanguageKeys
240 * @param array $arguments
241 * @return void
242 * @dataProvider translateDataProvider
243 * @test
244 */
245 public function translateTest($key, array $LOCAL_LANG, $languageKey, $expected, array $altLanguageKeys = array(), array $arguments = NULL) {
246 $this->localization->_setStatic('LOCAL_LANG', $LOCAL_LANG);
247 $this->localization->_setStatic('languageKey', $languageKey);
248 $this->localization->_setStatic('alternativeLanguageKeys', $altLanguageKeys);
249
250 $this->assertEquals($expected, $this->localization->translate($key, 'extensionKey', $arguments));
251 }
252
253 /**
254 * @return array
255 */
256 public function loadTypoScriptLabelsProvider() {
257 return array(
258 'override labels with typoscript' => array(
259 'LOCAL_LANG' => array(
260 'extensionKey' => array(
261 'dk' => array(
262 'key1' => array(
263 array(
264 'source' => 'English label for key1',
265 'target' => 'Dansk label for key1',
266 )
267 ),
268 'key2' => array(
269 array(
270 'source' => 'English label for key2',
271 )
272 ),
273 'key3.subkey1' => array(
274 array(
275 'source' => 'English label for key3',
276 )
277 ),
278 ),
279 ),
280 ),
281 'typoscript LOCAL_LANG' => array(
282 'dk' => array(
283 'key1' => 'key1 value from TS',
284 'key3' => array(
285 'subkey1' => 'key3.subkey1 value from TS',
286 // this key doesn't exist in xml files
287 'subkey2' => array(
288 'subsubkey' => 'key3.subkey2.subsubkey value from TS'
289 )
290 ),
291 )
292 ),
293 'language key' => 'dk',
294 'expected' => array(
295 'key1' => array(
296 array(
297 'source' => 'English label for key1',
298 'target' => 'key1 value from TS',
299 )
300 ),
301 'key2' => array(
302 array(
303 'source' => 'English label for key2',
304 )
305 ),
306 'key3.subkey1' => array(
307 array(
308 'source' => 'English label for key3',
309 'target' => 'key3.subkey1 value from TS',
310 )
311 ),
312 'key3.subkey2.subsubkey' => array(
313 array(
314 'target' => 'key3.subkey2.subsubkey value from TS',
315 )
316 ),
317 ),
318 )
319 );
320 }
321
322 /**
323 * @param array $LOCAL_LANG
324 * @param array $typoscriptLocalLang
325 * @param string $languageKey
326 * @param array $expected
327 * @return void
328 * @dataProvider loadTypoScriptLabelsProvider
329 * @test
330 */
331 public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoscriptLocalLang, $languageKey, array $expected) {
332 $this->localization->_setStatic('LOCAL_LANG', $LOCAL_LANG);
333 $this->localization->_setStatic('languageKey', $languageKey);
334
335 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
336 $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
337 $frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
338 $frameworkConfiguration['_LOCAL_LANG'] = $typoscriptLocalLang;
339 $configurationManager->setConfiguration($frameworkConfiguration);
340
341 $this->localization->_call('loadTypoScriptLabels', 'extensionKey');
342 $result = $this->localization->_getStatic('LOCAL_LANG');
343 $this->assertSame($expected, $result['extensionKey'][$languageKey]);
344 }
345 }
346
347 ?>