[FEATURE] Create interface and enable custom form_postProcessors
authorFranz Geiger <mail@fx-g.de>
Mon, 13 Feb 2012 15:19:37 +0000 (16:19 +0100)
committerSusanne Moog <typo3@susannemoog.de>
Sun, 1 Jul 2012 12:08:32 +0000 (14:08 +0200)
PostProcessors now need to implement the
tx_form_System_Postprocessor_Interface.
All classes implementing this interface can be loaded as a
postProcessor by passing its class-name by typoscript.

Before loading the class in the namespace 'tx_form_System_Postprocessor_'
tx_form_System_Postprocessor first tries to load the class passed by
typoscript directly.

Change-Id: I3bea8ce8287a6713f483d7e019cb5f47cdc68cb8
Resolves: #32701
Releases: 6.0
Reviewed-on: http://review.typo3.org/9006
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
typo3/sysext/form/Classes/System/Postprocessor/Interface.php [new file with mode: 0644]
typo3/sysext/form/Classes/System/Postprocessor/Mail.php
typo3/sysext/form/Classes/System/Postprocessor/Postprocessor.php
typo3/sysext/form/Tests/Unit/System/Postprocessor/PostprocessorTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/form/Classes/System/Postprocessor/Interface.php b/typo3/sysext/form/Classes/System/Postprocessor/Interface.php
new file mode 100644 (file)
index 0000000..4ee7b9b
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2012 Franz Geiger (mail@fx-g.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!
+ ***************************************************************/
+
+/**
+ * Interface for postprocessors
+ *
+ * @author Franz Geiger <mail@fx-g.de>
+ * @package TYPO3
+ * @subpackage form
+ */
+interface tx_form_System_Postprocessor_Interface {
+
+       /**
+        * Constructor
+        *
+        * @param tx_form_Domain_Model_Form $form Form domain model
+        * @param array $typoScript Post processor TypoScript settings
+        */
+       public function __construct(tx_form_Domain_Model_Form $form, array $typoScript);
+
+       /**
+        * The main method called by the post processor
+        *
+        * @return string The post processing HTML
+        */
+       public function process();
+}
+?>
\ No newline at end of file
index 38d5678..005d0f8 100644 (file)
@@ -29,7 +29,7 @@
  * @package TYPO3
  * @subpackage form
  */
-class tx_form_System_Postprocessor_Mail {
+class tx_form_System_Postprocessor_Mail implements tx_form_System_Postprocessor_Interface {
        /**
         * @var tx_form_Domain_Model_Form
         */
@@ -58,9 +58,8 @@ class tx_form_System_Postprocessor_Mail {
        /**
         * Constructor
         *
-        * @param $form tx_form_Domain_Model_Form Form domain model
-        * @param $typoscript array Post processor TypoScript settings
-        * @return void
+        * @param tx_form_Domain_Model_Form $form Form domain model
+        * @param array $typoScript Post processor TypoScript settings
         */
        public function __construct(tx_form_Domain_Model_Form $form, array $typoScript) {
                $this->form = $form;
@@ -300,7 +299,7 @@ class tx_form_System_Postprocessor_Mail {
        /**
         * Checks string for suspicious characters
         *
-        * @param string String to check
+        * @param string $string String to check
         * @return string Valid or empty string
         */
        protected function sanitizeHeaderString($string) {
index 930a157..20c8551 100644 (file)
@@ -54,27 +54,47 @@ class tx_form_System_Postprocessor {
                $html = '';
 
                if (is_array($this->typoScript)) {
-                       $keys = t3lib_TStemplate::sortedKeyList($this->typoScript);
+                       $keys = $this->sortTypoScriptKeyList();
                        foreach ($keys as $key) {
                                if (!intval($key) || strpos($key, '.') !== FALSE) {
                                        continue;
                                }
 
-                               $className = 'tx_form_System_Postprocessor_' . ucfirst(strtolower($this->typoScript[$key]));
+                               $className = FALSE;
                                $processorArguments = array();
 
                                if (isset($this->typoScript[$key . '.'])) {
                                        $processorArguments = $this->typoScript[$key . '.'];
                                }
 
-                               if (class_exists($className, TRUE)) {
+                               if (class_exists($this->typoScript[$key], TRUE)) {
+                                       $className = $this->typoScript[$key];
+                               } else {
+                                       $classNameExpanded = 'tx_form_System_Postprocessor_' . ucfirst(strtolower($this->typoScript[$key]));
+                                       if (class_exists($classNameExpanded, TRUE)) {
+                                               $className = $classNameExpanded;
+                                       }
+                               }
+                               if ($className !== FALSE ) {
                                        $processor = t3lib_div::makeInstance($className, $this->form, $processorArguments);
-                                       $html .= $processor->process();
+                                       if($processor instanceof tx_form_System_Postprocessor_Interface) {
+                                               $html .= $processor->process();
+                                       }
                                }
                        }
                }
 
                return $html;
        }
+
+       /**
+        * Wrapper method for t3lib_TStemplate::sortedKeyList
+        * (makes unit testing possible)
+        *
+        * @return array
+        */
+       public function sortTypoScriptKeyList() {
+               return t3lib_TStemplate::sortedKeyList($this->typoScript);
+       }
 }
 ?>
\ No newline at end of file
diff --git a/typo3/sysext/form/Tests/Unit/System/Postprocessor/PostprocessorTest.php b/typo3/sysext/form/Tests/Unit/System/Postprocessor/PostprocessorTest.php
new file mode 100644 (file)
index 0000000..f5ffe76
--- /dev/null
@@ -0,0 +1,158 @@
+<?php
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2012 Susanne Moog, <typo3@susannemoog.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!
+ ***************************************************************/
+
+/**
+ * Test case for class tx_form_System_Postprocessor
+ *
+ * @author Susanne Moog, <typo3@susannemoog.de>
+ * @package TYPO3
+ * @subpackage form
+ */
+class tx_form_System_Postprocessor_PostprocessorTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
+
+       /**
+        * @var PHPUnit_Framework_MockObject_MockObject
+        */
+       public $postprocessor;
+
+       /**
+        * @var string
+        */
+       public $classNameWithoutPrefix;
+
+       /**
+        * @var string
+        */
+       public $classNameWithPrefix;
+
+       /**
+        * @var string
+        */
+       public $classNameWithoutInterface;
+
+       /**
+        * @var tx_form_Domain_Model_Form
+        */
+       public $form;
+
+       /**
+        * set up
+        */
+       public function setUp() {
+               $this->form = new tx_form_Domain_Model_Form;
+               $this->postprocessor = $this->getMock(
+                       'tx_form_System_Postprocessor',
+                       array('sortTypoScriptKeyList'),
+                       array(
+                               $this->form,
+                               array()
+                       ));
+               $this->classNameWithoutPrefix = uniqid('postprocess');
+               $this->classNameWithPrefix = uniqid('postprocess');
+               $this->classNameWithoutInterface = uniqid('postprocess');
+
+               eval(
+                       'class ' . $this->classNameWithoutPrefix . ' implements tx_form_System_Postprocessor_Interface {
+
+                               public function __construct(tx_form_Domain_Model_Form $form, array $typoScript) {
+
+                               }
+
+                               public function process() {
+                                       return \'processedWithoutPrefix\';
+                               }
+                       }' .
+                       'class tx_form_System_Postprocessor_' . $this->classNameWithPrefix . ' implements tx_form_System_Postprocessor_Interface {
+
+                               public function __construct(tx_form_Domain_Model_Form $form, array $typoScript) {
+
+                               }
+
+                               public function process() {
+                                       return \'processedWithPrefix\';
+                               }
+                       }' .
+                       'class ' . $this->classNameWithoutInterface . '{
+
+                               public function __construct(tx_form_Domain_Model_Form $form, array $typoScript) {
+
+                               }
+
+                               public function process() {
+                                       return \'withoutInterface\';
+                               }
+                       }'
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function processFindsClassSpecifiedByTypoScriptWithoutFormPrefix() {
+               $typoScript = array(
+                       10 => uniqid('postprocess'),
+                       20 => $this->classNameWithoutPrefix
+               );
+               $this->postprocessor->typoScript = $typoScript;
+               $this->postprocessor->expects($this->once())
+                       ->method('sortTypoScriptKeyList')
+                       ->will($this->returnValue(array(10,20)));
+               $returnValue = $this->postprocessor->process();
+               $this->assertEquals('processedWithoutPrefix', $returnValue);
+       }
+
+       /**
+        * @test
+        */
+       public function processFindsClassSpecifiedByTypoScriptWithFormPrefix() {
+               $typoScript = array(
+                       10 => uniqid('postprocess'),
+                       20 => $this->classNameWithPrefix
+               );
+               $this->postprocessor->typoScript = $typoScript;
+               $this->postprocessor->expects($this->once())
+                       ->method('sortTypoScriptKeyList')
+                       ->will($this->returnValue(array(10,20)));
+               $returnValue = $this->postprocessor->process();
+               $this->assertEquals('processedWithPrefix', $returnValue);
+       }
+
+       /**
+        * @test
+        */
+       public function processReturnsEmptyStringIfSpecifiedPostProcessorDoesNotImplementTheInterface() {
+               $typoScript = array(
+                       10 => uniqid('postprocess'),
+                       20 => $this->classNameWithoutInterface
+               );
+               $this->postprocessor->typoScript = $typoScript;
+               $this->postprocessor->expects($this->once())
+                       ->method('sortTypoScriptKeyList')
+                       ->will($this->returnValue(array(10,20)));
+               $returnValue = $this->postprocessor->process();
+               $this->assertEquals('', $returnValue);
+       }
+}
+?>
\ No newline at end of file