[!!!][BUGFIX] Streamline label processing for inline language files 01/42201/5
authorDaniel Goerz <ervaude@gmail.com>
Sat, 1 Aug 2015 09:24:24 +0000 (11:24 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 6 Aug 2015 17:58:24 +0000 (19:58 +0200)
Change-Id: If75c125743dfe3e7c77d11978b767b432e21a4b6
Releases: master
Resolves: #30863
Reviewed-on: http://review.typo3.org/42201
Reviewed-by: Sascha Wilking <sascha.wilking@hmmh.de>
Tested-by: Sascha Wilking <sascha.wilking@hmmh.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/Page/PageRenderer.php
typo3/sysext/core/Documentation/Changelog/master/Breaking-30863-StreamlineParameterOfInlineLanguageFiles.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Page/PageRendererTest.php

index ae27477..2de3020 100644 (file)
@@ -1623,7 +1623,7 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
         *
         * @param string $fileRef Input is a file-reference (see GeneralUtility::getFileAbsFileName). That file is expected to be a 'locallang.xlf' file containing a valid XML TYPO3 language structure.
         * @param string $selectionPrefix Prefix to select the correct labels (default: '')
-        * @param string $stripFromSelectionName Sub-prefix to be removed from label names in the result (default: '')
+        * @param string $stripFromSelectionName String to be removed from the label names in the output. (default: '')
         * @param int $errorMode Error mode (when file could not be found): 0 - syslog entry, 1 - do nothing, 2 - throw an exception
         * @return void
         */
@@ -2331,8 +2331,6 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                }
                $labelsFromFile = array();
                $allLabels = $this->readLLfile($fileRef, $errorMode);
-               // Regular expression to strip the selection prefix and possibly something from the label name:
-               $labelPattern = '#^' . preg_quote($selectionPrefix, '#') . '(' . preg_quote($stripFromSelectionName, '#') . ')?#';
                if ($allLabels !== FALSE) {
                        // Merge language specific translations:
                        if ($this->lang !== 'default' && isset($allLabels[$this->lang])) {
@@ -2342,10 +2340,10 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                        }
                        // Iterate through all locallang labels:
                        foreach ($labels as $label => $value) {
-                               if ($selectionPrefix === '') {
-                                       $labelsFromFile[$label] = $value;
-                               } elseif (strpos($label, $selectionPrefix) === 0) {
-                                       preg_replace($labelPattern, '', $label);
+                               // If $selectionPrefix is set, only respect labels that start with $selectionPrefix
+                               if ($selectionPrefix === '' || strpos($label, $selectionPrefix) === 0) {
+                                       // Remove substring $stripFromSelectionName from label
+                                       $label = str_replace($stripFromSelectionName, '', $label);
                                        $labelsFromFile[$label] = $value;
                                }
                        }
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-30863-StreamlineParameterOfInlineLanguageFiles.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-30863-StreamlineParameterOfInlineLanguageFiles.rst
new file mode 100644 (file)
index 0000000..d29fa3b
--- /dev/null
@@ -0,0 +1,30 @@
+==========================================================================
+Breaking: #30863 - Streamlined parameters for adding inline language files
+==========================================================================
+
+Description
+===========
+
+The method ``addInlineLanguageLabelFile`` of the ``PageRenderer`` handles the optional parameter ``$stripFromSelectionName``, a string
+that should be removed from any label key in the given file. This did not work until now, so the label keys were never stripped. As this
+functionality is now working it could end up with different label keys in the output.
+
+
+Impact
+======
+
+Inline Javascript label keys could have changed.
+
+
+Affected Installations
+======================
+
+Any third party code using ``PageRenderer->addInlineLanguageLabelFile()`` with the parameter ``$stripFromSelectionName`` set to anything but
+an empty string.
+
+
+Migration
+=========
+
+Change the call to ``PageRenderer->addInlineLanguageLabelFile()`` with ``$stripFromSelectionName = ''`` or adjust your Javascript to handle
+the now correctly rendered label keys.
index bb46585..3f6cd5e 100644 (file)
@@ -148,4 +148,104 @@ class PageRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertSame(1, count($subject->getInlineLanguageLabelFiles()));
        }
 
-}
\ No newline at end of file
+       /**
+        * @test
+        * @expectedException \RuntimeException
+        */
+       public function includeLanguageFileForInlineThrowsExceptionIfLangIsNotSet() {
+               /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
+               $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, array('dummy'), array(), '', FALSE);
+               $subject->_set('charSet', 'utf-8');
+               $subject->_call('includeLanguageFileForInline', 'someLLFile.xml');
+       }
+
+       /**
+        * @test
+        * @expectedException \RuntimeException
+        */
+       public function includeLanguageFileForInlineThrowsExceptionIfCharSetIsNotSet() {
+               /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
+               $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, array('dummy'), array(), '', FALSE);
+               $subject->_set('lang', 'default');
+               $subject->_call('includeLanguageFileForInline', 'someLLFile.xml');
+       }
+
+       /**
+        * @test
+        */
+       public function includeLanguageFileForInlineDoesNotAddToInlineLanguageLabelsIfFileCouldNotBeRead() {
+               /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
+               $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, array('readLLfile'), array(), '', FALSE);
+               $subject->_set('lang', 'default');
+               $subject->_set('charSet', 'utf-8');
+               $subject->_set('inlineLanguageLabels', array());
+               $subject->method('readLLfile')->willReturn(FALSE);
+               $subject->_call('includeLanguageFileForInline', 'someLLFile.xml');
+               $this->assertEquals(array(), $subject->_get('inlineLanguageLabels'));
+       }
+
+       /**
+        * @return array
+        */
+       public function includeLanguageFileForInlineAddsProcessesLabelsToInlineLanguageLabelsProvider() {
+               $llFileContent = array(
+                       'default' => array(
+                               'inline_label_first_Key' => 'first',
+                               'inline_label_second_Key' => 'second',
+                               'thirdKey' => 'third'
+                       )
+               );
+               return array(
+                       'No processing' => array(
+                               $llFileContent,
+                               '',
+                               '',
+                               $llFileContent['default']
+                       ),
+                       'Respect $selectionPrefix' => array(
+                               $llFileContent,
+                               'inline_',
+                               '',
+                               array(
+                                       'inline_label_first_Key' => 'first',
+                                       'inline_label_second_Key' => 'second'
+                               )
+                       ),
+                       'Respect $stripFromSelectionName' => array(
+                               $llFileContent,
+                               '',
+                               'inline_',
+                               array(
+                                       'label_first_Key' => 'first',
+                                       'label_second_Key' => 'second',
+                                       'thirdKey' => 'third'
+                               )
+                       ),
+                       'Respect $selectionPrefix and $stripFromSelectionName' => array(
+                               $llFileContent,
+                               'inline_',
+                               'inline_label_',
+                               array(
+                                       'first_Key' => 'first',
+                                       'second_Key' => 'second'
+                               )
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider includeLanguageFileForInlineAddsProcessesLabelsToInlineLanguageLabelsProvider
+        * @test
+        */
+       public function includeLanguageFileForInlineAddsProcessesLabelsToInlineLanguageLabels($llFileContent, $selectionPrefix, $stripFromSelectionName, $expectation) {
+               /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
+               $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, array('readLLfile'), array(), '', FALSE);
+               $subject->_set('lang', 'default');
+               $subject->_set('charSet', 'utf-8');
+               $subject->_set('inlineLanguageLabels', array());
+               $subject->method('readLLfile')->willReturn($llFileContent);
+               $subject->_call('includeLanguageFileForInline', 'someLLFile.xml', $selectionPrefix, $stripFromSelectionName);
+               $this->assertEquals($expectation, $subject->_get('inlineLanguageLabels'));
+       }
+
+}