[FEATURE] Add stdWrap for _DEFAULT_PI_VARS 83/14283/14
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Sun, 2 Sep 2012 10:00:54 +0000 (12:00 +0200)
committerPhilipp Gampe <philipp.gampe@typo3.org>
Fri, 21 Jun 2013 11:41:30 +0000 (13:41 +0200)
This feature allows multilevel variables as well.

Example:
plugin.tt_news._DEFAULT_PI_VARS {
  year.stdWrap.data = date:Y
}

Change-Id: I49ae6b420f98cf959f95289c99c49d7636a4ba52
Resolves: #22045
Releases: 6.2
Reviewed-on: https://review.typo3.org/14283
Reviewed-by: Markus Klein
Tested-by: Markus Klein
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
typo3/sysext/frontend/Classes/Plugin/AbstractPlugin.php
typo3/sysext/frontend/Tests/Unit/Plugin/AbstractPluginTest.php [new file with mode: 0644]

index d9d809f..638be66 100644 (file)
@@ -254,13 +254,48 @@ class AbstractPlugin {
        }
 
        /**
+        * Recursively looks for stdWrap and executes it
+        *
+        * @param array $conf Current section of configuration to work on
+        * @param integer $level Current level being processed (currently just for tracking; no limit enforced)
+        * @return array Current section of configuration after stdWrap applied
+        */
+       protected function applyStdWrapRecursive(array $conf, $level = 0) {
+               foreach ($conf as $key => $confNextLevel) {
+                       if (strpos($key, '.') !== FALSE) {
+                               $key = substr($key, 0, -1);
+
+                               // descend into all non-stdWrap-subelements first
+                               foreach ($confNextLevel as $subKey => $subConfNextLevel) {
+                                       if (is_array($subConfNextLevel) && strpos($subKey, '.') !== FALSE && $subKey !== 'stdWrap.') {
+                                               $subKey = substr($subKey, 0, -1);
+                                               $conf[$key . '.'] = $this->applyStdWrapRecursive($confNextLevel, $level + 1);
+                                       }
+                               }
+
+                               // now for stdWrap
+                               foreach ($confNextLevel as $subKey => $subConfNextLevel) {
+                                       if (is_array($subConfNextLevel) && $subKey === 'stdWrap.') {
+                                               $conf[$key] = $this->cObj->stdWrap($conf[$key], $conf[$key . '.']['stdWrap.']);
+                                               unset($conf[$key . '.']['stdWrap.']);
+                                               if (!count($conf[$key . '.'])) {
+                                                       unset($conf[$key . '.']);
+                                               }
+                                       }
+                               }
+                       }
+               }
+               return $conf;
+       }
+
+       /**
         * If internal TypoScript property "_DEFAULT_PI_VARS." is set then it will merge the current $this->piVars array onto these default values.
         *
         * @return void
-        * @todo Define visibility
         */
        public function pi_setPiVarDefaults() {
-               if (is_array($this->conf['_DEFAULT_PI_VARS.'])) {
+               if (isset($this->conf['_DEFAULT_PI_VARS.']) && is_array($this->conf['_DEFAULT_PI_VARS.'])) {
+                       $this->conf['_DEFAULT_PI_VARS.'] = $this->applyStdWrapRecursive($this->conf['_DEFAULT_PI_VARS.']);
                        $this->piVars = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($this->conf['_DEFAULT_PI_VARS.'], is_array($this->piVars) ? $this->piVars : array());
                }
        }
@@ -1283,4 +1318,4 @@ class AbstractPlugin {
 }
 
 
-?>
\ No newline at end of file
+?>
diff --git a/typo3/sysext/frontend/Tests/Unit/Plugin/AbstractPluginTest.php b/typo3/sysext/frontend/Tests/Unit/Plugin/AbstractPluginTest.php
new file mode 100644 (file)
index 0000000..bbc5ecb
--- /dev/null
@@ -0,0 +1,152 @@
+<?php
+namespace TYPO3\CMS\Frontend\Tests\Unit\Plugin;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2013 Stefan Neufeind <info (at) speedpartner.de>
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+/**
+ * Testcase for TYPO3\CMS\Frontend\Plugin\AbstractPlugin
+ *
+ * @author Stefan Neufeind <info (at) speedpartner.de>
+ */
+class AbstractPluginTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Frontend\Plugin\AbstractPlugin
+        */
+       protected $abstractPlugin;
+
+       /**
+        * @var array
+        */
+       protected $defaultPiVars;
+
+       /**
+        * Sets up this testcase
+        */
+       public function setUp() {
+               parent::setUp();
+
+               // Allow objects until 100 levels deep when executing the stdWrap
+               $GLOBALS['TSFE']->cObjectDepthCounter = 100;
+
+               $this->abstractPlugin = new \TYPO3\CMS\Frontend\Plugin\AbstractPlugin;
+               $this->abstractPlugin->cObj = new \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer();
+               $this->defaultPiVars = $this->abstractPlugin->piVars;
+       }
+
+       /**
+        * Tears down this testcase
+        */
+       public function tearDown() {
+               parent::tearDown();
+
+               $this->abstractPlugin = NULL;
+       }
+
+       /**
+        * Data provider for piSetPiVarDefaultsStdWrap
+        *
+        * @return array input-array with configuration and stdWrap, expected output-array in piVars
+        */
+       public function piSetPiVarDefaultsStdWrapProvider() {
+               return array(
+                       'stdWrap on conf, non-recursive, stdWrap 1 level deep' => array(
+                               array(
+                                       'abc' => 'DEF',
+                                       'abc.' => array(
+                                               'stdWrap.' => array(
+                                                       'wrap' => 'test | test'
+                                               ),
+                                       ),
+                               ),
+                               array(
+                                       'abc' => 'testDEFtest',
+                                       'pointer' => '',
+                                       'mode' => '',
+                                       'sword' => '',
+                                       'sort' => '',
+                               ),
+                       ),
+                       'stdWrap on conf, non-recursive, stdWrap 2 levels deep' => array(
+                               array(
+                                       'xyz.' => array(
+                                               'stdWrap.' => array(
+                                                       'cObject' => 'TEXT',
+                                                       'cObject.' => array(
+                                                               'data' => 'date:U',
+                                                               'strftime' => '%Y',
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                               array(
+                                       'xyz' => date('Y'),
+                                       'pointer' => '',
+                                       'mode' => '',
+                                       'sword' => '',
+                                       'sort' => '',
+                               ),
+                       ),
+                       'stdWrap on conf, recursive' => array(
+                               array(
+                                       'abc.' => array(
+                                               'def' => 'DEF',
+                                               'def.' => array(
+                                                       'ghi' => '123',
+                                                       'stdWrap.' => array(
+                                                               'wrap' => 'test | test'
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                               array(
+                                       'abc.' => array(
+                                               'def' => 'testDEFtest',
+                                               'def.' => array(
+                                                       'ghi' => '123',
+                                               ),
+                                       ),
+                                       'pointer' => '',
+                                       'mode' => '',
+                                       'sword' => '',
+                                       'sort' => '',
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider piSetPiVarDefaultsStdWrapProvider
+        */
+       public function piSetPiVarDefaultsStdWrap($input, $expected) {
+               $this->abstractPlugin->piVars = $this->defaultPiVars;
+
+               $this->abstractPlugin->conf['_DEFAULT_PI_VARS.'] = $input;
+               $this->abstractPlugin->pi_setPiVarDefaults();
+               $this->assertEquals($expected, $this->abstractPlugin->piVars);
+       }
+
+}
+
+?>
\ No newline at end of file