2 namespace TYPO3\CMS\Extbase\Tests\Unit\Utility
;
4 /***************************************************************
7 * (c) 2011 Extbase Team
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.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
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.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
30 class LocalizationUtilityTest
extends \TYPO3\CMS\Core\Tests\UnitTestCase
{
33 * @var \TYPO3\CMS\Extbase\Utility\LocalizationUtility|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
35 protected $localization;
38 * LOCAL_LANG array fixture
42 protected $LOCAL_LANG = array(
43 'extensionKey' => array(
47 'source' => 'English label for key1',
48 'target' => 'English label for key1',
53 'source' => 'English label for key2',
54 'target' => 'English label for key2',
59 'source' => 'English label for key3',
60 'target' => 'English label for key3',
65 'source' => 'English label for key4',
66 'target' => 'English label for key4',
69 'keyWithPlaceholder' => array(
71 'source' => 'English label with number %d',
72 'target' => 'English label with number %d',
79 'source' => 'English label for key1',
80 'target' => 'Dansk label for key1',
83 // not translated in dk => no target (llxml)
86 'source' => 'English label for key2',
91 'source' => 'English label for key3',
94 // not translated in dk => empty target (xliff)
97 'source' => 'English label for key4',
101 // not translated in dk => empty target (xliff)
104 'source' => 'English label for key5',
108 'keyWithPlaceholder' => array(
110 'source' => 'English label with number %d',
114 // fallback language for labels which are not translated in dk
118 'source' => 'English label for key1',
123 'source' => 'English label for key2',
124 'target' => 'Dansk alternative label for key2',
129 'source' => 'English label for key3',
132 // not translated in dk_alt => empty target (xliff)
135 'source' => 'English label for key4',
141 'source' => 'English label for key5',
142 'target' => 'Dansk alternative label for key5',
145 'keyWithPlaceholder' => array(
147 'source' => 'English label with number %d',
155 public function setUp() {
156 $this->localization
= $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility', array('getConfigurationManager'));
161 * @author Sebastian Kurfürst <sebastian@typo3.org>
163 public function implodeTypoScriptLabelArrayWorks() {
167 'key3.subkey1' => 'subvalue1',
168 'key3.subkey2.subsubkey' => 'val'
170 $actual = $this->localization
->_call('flattenTypoScriptLabelArray', array(
174 'subkey1' => 'subvalue1',
180 $this->assertEquals($expected, $actual);
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'));
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')));
206 public function translateDataProvider() {
208 'get translated key' =>
209 array('key1', $this->LOCAL_LANG
, 'dk', 'Dansk label for key1'),
211 'fallback to English when translation is missing for key' =>
212 array('key2', $this->LOCAL_LANG
, 'dk', 'English label for key2'),
214 'fallback to English for non existing language' =>
215 array('key2', $this->LOCAL_LANG
, 'xx', 'English label for key2'),
217 'replace placeholder with argument' =>
218 array('keyWithPlaceholder', $this->LOCAL_LANG
, 'en', 'English label with number 100', array(), array(100)),
220 'get translated key from primary language' =>
221 array('key1', $this->LOCAL_LANG
, 'dk', 'Dansk label for key1', array('dk_alt')),
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')),
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')),
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')),
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')),
239 * @param array $LOCAL_LANG
240 * @param string $languageKey
241 * @param string $expected
242 * @param array $altLanguageKeys
243 * @param array $arguments
245 * @dataProvider translateDataProvider
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);
253 $this->assertEquals($expected, $this->localization
->translate($key, 'extensionKey', $arguments));
259 public function loadTypoScriptLabelsProvider() {
261 'override labels with typoscript' => array(
262 'LOCAL_LANG' => array(
263 'extensionKey' => array(
267 'source' => 'English label for key1',
268 'target' => 'Dansk label for key1 extensionKey',
273 'source' => 'English label for key2',
276 'key3.subkey1' => array(
278 'source' => 'English label for key3',
283 'extensionKey1' => array(
287 'source' => 'English label for key1',
288 'target' => 'Dansk label for key1 extensionKey1',
293 'source' => 'English label for key2',
296 'key3.subkey1' => array(
298 'source' => 'English label for key3',
304 'typoscript LOCAL_LANG' => array(
305 '_LOCAL_LANG' => array(
307 'key1' => 'key1 value from TS extensionKey',
309 'subkey1' => 'key3.subkey1 value from TS extensionKey',
310 // this key doesn't exist in xml files
312 'subsubkey' => 'key3.subkey2.subsubkey value from TS extensionKey'
318 'language key' => 'dk',
322 'source' => 'English label for key1',
323 'target' => 'key1 value from TS extensionKey',
328 'source' => 'English label for key2',
331 'key3.subkey1' => array(
333 'source' => 'English label for key3',
334 'target' => 'key3.subkey1 value from TS extensionKey',
337 'key3.subkey2.subsubkey' => array(
339 'target' => 'key3.subkey2.subsubkey value from TS extensionKey',
348 * Tests whether labels from xml are overwritten by TypoScript labels
350 * @param array $LOCAL_LANG
351 * @param array $typoScriptLocalLang
352 * @param string $languageKey
353 * @param array $expected
355 * @dataProvider loadTypoScriptLabelsProvider
358 public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoScriptLocalLang, $languageKey, array $expected) {
360 $configurationType = \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
;
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));
365 $this->localization
->staticExpects($this->atLeastOnce())->method('getConfigurationManager')->will($this->returnValue($configurationManager));
367 // translations loaded from xml files
368 $this->localization
->_setStatic('LOCAL_LANG', $LOCAL_LANG);
369 $this->localization
->_setStatic('languageKey', $languageKey);
371 $this->localization
->_call('loadTypoScriptLabels', 'extensionKey');
372 $result = $this->localization
->_getStatic('LOCAL_LANG');
373 $this->assertEquals($expected, $result['extensionKey'][$languageKey]);
380 public function clearLabelWithTypoScript() {
381 $this->localization
->_setStatic('LOCAL_LANG', $this->LOCAL_LANG
);
382 $this->localization
->_setStatic('languageKey', 'dk');
384 $typoScriptLocalLang = array(
385 '_LOCAL_LANG' => array(
392 $configurationType = \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
::CONFIGURATION_TYPE_FRAMEWORK
;
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));
397 $this->localization
->staticExpects($this->atLeastOnce())->method('getConfigurationManager')->will($this->returnValue($configurationManager));
399 $this->localization
->_call('loadTypoScriptLabels', 'extensionKey');
400 $result = $this->localization
->translate('key1', 'extensionKey');
401 $this->assertNotNull($result);
402 $this->assertEquals('', $result);