[BUGFIX] Return string on non-existent CASE cObject 84/35784/3
authorBenjamin Mack <benni@typo3.org>
Sun, 4 Jan 2015 15:12:59 +0000 (16:12 +0100)
committerPhilipp Gampe <philipp.gampe@typo3.org>
Sun, 4 Jan 2015 23:41:13 +0000 (00:41 +0100)
Currently, when using a TypoScript CASE cObject
and the key property is not available then the default
key is used. If the default key is not existent
a TS warning is thrown. According to the documentation
an empty string should be returned.

Releases: master, 6.2
Resolves: #39772
Change-Id: I2c7872b066523b8075983fb7cabb6821fdfef54d
Reviewed-on: http://review.typo3.org/35784
Reviewed-by: Philipp Gampe <philipp.gampe@typo3.org>
Tested-by: Philipp Gampe <philipp.gampe@typo3.org>
typo3/sysext/frontend/Classes/ContentObject/CaseContentObject.php
typo3/sysext/frontend/Tests/Unit/ContentObject/CaseContentObjectTest.php [new file with mode: 0644]

index 846e9cb..8d92347 100644 (file)
@@ -38,8 +38,12 @@ class CaseContentObject extends \TYPO3\CMS\Frontend\ContentObject\AbstractConten
                }
                $key = isset($conf['key.']) ? $this->cObj->stdWrap($conf['key'], $conf['key.']) : $conf['key'];
                $key = strlen($conf[$key]) ? $key : 'default';
-               $name = $conf[$key];
-               $theValue = $this->cObj->cObjGetSingle($name, $conf[$key . '.'], $key);
+               // If no "default" property is available, then an empty string is returned
+               if ($key === 'default' && $conf['default'] === NULL) {
+                       $theValue = '';
+               } else {
+                       $theValue = $this->cObj->cObjGetSingle($conf[$key], $conf[$key . '.'], $key);
+               }
                if (isset($conf['stdWrap.'])) {
                        $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
                }
diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/CaseContentObjectTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/CaseContentObjectTest.php
new file mode 100644 (file)
index 0000000..1f3a35f
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+use TYPO3\CMS\Frontend\ContentObject\CaseContentObject;
+use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
+use TYPO3\CMS\Core\TypoScript\TemplateService;
+use TYPO3\CMS\Frontend\Page\PageRepository;
+use TYPO3\CMS\Core\Charset\CharsetConverter;
+
+/**
+ * Testcase for TYPO3\CMS\Frontend\ContentObject\CaseContentObject
+ *
+ * @author Philipp Gampe <philipp.gampe@typo3.org>
+ */
+class CaseContentObjectTest extends UnitTestCase {
+
+       /**
+        * @var CaseContentObject|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $subject = NULL;
+
+       /**
+        * Set up
+        */
+       public function setUp() {
+               $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
+               $this->contentObjectRenderer = $this->getMock(
+                       'TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer',
+                       array('dummy')
+               );
+               $this->subject = $this->getAccessibleMock(
+                       'TYPO3\\CMS\\Frontend\\ContentObject\\CaseContentObject',
+                       array('dummy'),
+                       array($this->contentObjectRenderer)
+               );
+               /** @var $tsfe \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
+               $tsfe = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
+               $tsfe->tmpl = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
+               $GLOBALS['TSFE'] = $tsfe;
+       }
+
+
+       /**
+        * @test
+        */
+       public function renderReturnsEmptyStringIfNoKeyMatchesAndIfNoDefaultObjectIsSet() {
+               $conf = array(
+                       'key' => 'not existing'
+               );
+               $this->assertSame('', $this->subject->render($conf));
+       }
+
+       /**
+        * @test
+        */
+       public function renderReturnsContentFromDefaultObjectIfKeyDoesNotExist() {
+               $conf = array(
+                       'key' => 'not existing',
+                       'default' => 'TEXT',
+                       'default.' => array(
+                               'value' => 'expected value'
+                       ),
+               );
+               $this->assertSame('expected value', $this->subject->render($conf));
+       }
+}