[BUGFIX] Localization ignores extensionName argument for TypoScript labels
authorTymoteusz Motylewski <t.motylewski@gmail.com>
Sat, 13 Apr 2013 11:35:16 +0000 (13:35 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 13 Apr 2013 12:58:25 +0000 (14:58 +0200)
Localization utility should pass extesnionName to configurationManager
when fetching TypoScript _LOCAL_LANG labels.
Without this parameter configurationManager returns configuration
of the current plugin.
For example having
<f:translate key="foo" extensionName="news" />
in the blog_example extension template should load translation
from "news" extesnion.

This patch also makes LocalizationUtility::loadTypoScriptLabels()
testable.

Resolves: #46042
Releases: 4.7, 6.0, 6.1
Change-Id: If3785617defe2894cf3a8dc201e66acd182c18c0
Reviewed-on: https://review.typo3.org/19908
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/extbase/Classes/Utility/LocalizationUtility.php
typo3/sysext/extbase/Tests/Unit/Utility/LocalizationUtilityTest.php

index 4b4246e..77db27d 100644 (file)
@@ -190,17 +190,16 @@ class LocalizationUtility {
        }
 
        /**
-        * Overwrites labels that are set via typoscript.
+        * Overwrites labels that are set via TypoScript.
         * TS locallang labels have to be configured like:
         * plugin.tx_myextension._LOCAL_LANG.languageKey.key = value
         *
         * @param string $extensionName
         * @return void
         */
-       protected function loadTypoScriptLabels($extensionName) {
-               $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
-               $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
-               $frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
+       static protected function loadTypoScriptLabels($extensionName) {
+               $configurationManager = static::getConfigurationManager();
+               $frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, $extensionName);
                if (!is_array($frameworkConfiguration['_LOCAL_LANG'])) {
                        return;
                }
@@ -269,6 +268,17 @@ class LocalizationUtility {
                        return $convertedValue !== NULL ? $convertedValue : $value;
                }
        }
+
+       /**
+        * Returns instance of the configuration manager
+        *
+        * @return \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
+        */
+       static protected function getConfigurationManager() {
+               $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
+               $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
+               return $configurationManager;
+       }
 }
 
 ?>
\ No newline at end of file
index f1cba51..eaef253 100644 (file)
@@ -152,7 +152,7 @@ class LocalizationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
        );
 
        public function setUp() {
-               $this->localization = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility', array('dummy'));
+               $this->localization = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Utility\\LocalizationUtility', array('getConfigurationManager'));
        }
 
        public function tearDown() {
@@ -262,7 +262,27 @@ class LocalizationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
                                                        'key1' => array(
                                                                array(
                                                                        'source' => 'English label for key1',
-                                                                       'target' => 'Dansk label for key1',
+                                                                       'target' => 'Dansk label for key1 extensionKey',
+                                                               )
+                                                       ),
+                                                       'key2' => array(
+                                                               array(
+                                                                       'source' => 'English label for key2',
+                                                               )
+                                                       ),
+                                                       'key3.subkey1' => array(
+                                                               array(
+                                                                       'source' => 'English label for key3',
+                                                               )
+                                                       ),
+                                               ),
+                                       ),
+                                       'extensionKey1' => array(
+                                               'dk' => array(
+                                                       'key1' => array(
+                                                               array(
+                                                                       'source' => 'English label for key1',
+                                                                       'target' => 'Dansk label for key1 extensionKey1',
                                                                )
                                                        ),
                                                        'key2' => array(
@@ -279,15 +299,17 @@ class LocalizationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
                                        ),
                                ),
                                'typoscript LOCAL_LANG' => array(
-                                       'dk' => array(
-                                               'key1' => 'key1 value from TS',
-                                               'key3' => array(
-                                                       'subkey1' => 'key3.subkey1 value from TS',
-                                                       // this key doesn't exist in xml files
-                                                       'subkey2' => array(
-                                                               'subsubkey' => 'key3.subkey2.subsubkey value from TS'
+                                       '_LOCAL_LANG' => array(
+                                               'dk' => array(
+                                                       'key1' => 'key1 value from TS extensionKey',
+                                                       'key3' => array(
+                                                               'subkey1' => 'key3.subkey1 value from TS extensionKey',
+                                                               // this key doesn't exist in xml files
+                                                               'subkey2' => array(
+                                                                       'subsubkey' => 'key3.subkey2.subsubkey value from TS extensionKey'
+                                                               )
                                                        )
-                                               ),
+                                               )
                                        )
                                ),
                                'language key' => 'dk',
@@ -295,7 +317,7 @@ class LocalizationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
                                        'key1' => array(
                                                array(
                                                        'source' => 'English label for key1',
-                                                       'target' => 'key1 value from TS',
+                                                       'target' => 'key1 value from TS extensionKey',
                                                )
                                        ),
                                        'key2' => array(
@@ -306,12 +328,12 @@ class LocalizationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
                                        'key3.subkey1' => array(
                                                array(
                                                        'source' => 'English label for key3',
-                                                       'target' => 'key3.subkey1 value from TS',
+                                                       'target' => 'key3.subkey1 value from TS extensionKey',
                                                )
                                        ),
                                        'key3.subkey2.subsubkey' => array(
                                                array(
-                                                       'target' => 'key3.subkey2.subsubkey value from TS',
+                                                       'target' => 'key3.subkey2.subsubkey value from TS extensionKey',
                                                )
                                        ),
                                ),
@@ -320,27 +342,32 @@ class LocalizationUtilityTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase
        }
 
        /**
+        * Tests whether labels from xml are overwritten by TypoScript labels
+        *
         * @param array $LOCAL_LANG
-        * @param array $typoscriptLocalLang
+        * @param array $typoScriptLocalLang
         * @param string $languageKey
         * @param array $expected
         * @return void
         * @dataProvider loadTypoScriptLabelsProvider
         * @test
         */
-       public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoscriptLocalLang, $languageKey, array $expected) {
+       public function loadTypoScriptLabels(array $LOCAL_LANG, array $typoScriptLocalLang, $languageKey, array $expected) {
+
+               $configurationType = \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK;
+
+               $configurationManager = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager', array('getConfiguration'));
+               $configurationManager->expects($this->at(0))->method('getConfiguration')->with($configurationType, 'extensionKey', NULL)->will($this->returnValue($typoScriptLocalLang));
+
+               $this->localization->staticExpects($this->atLeastOnce())->method('getConfigurationManager')->will($this->returnValue($configurationManager));
+
+               // translations loaded from xml files
                $this->localization->_setStatic('LOCAL_LANG', $LOCAL_LANG);
                $this->localization->_setStatic('languageKey', $languageKey);
 
-               $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
-               $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
-               $frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
-               $frameworkConfiguration['_LOCAL_LANG'] = $typoscriptLocalLang;
-               $configurationManager->setConfiguration($frameworkConfiguration);
-
                $this->localization->_call('loadTypoScriptLabels', 'extensionKey');
                $result = $this->localization->_getStatic('LOCAL_LANG');
-               $this->assertSame($expected, $result['extensionKey'][$languageKey]);
+               $this->assertEquals($expected, $result['extensionKey'][$languageKey]);
        }
 }