소스 검색

ZF-11609: Fix rendering of Word-based captchas

- Applied patches from Kai Uwe

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24293 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 14 년 전
부모
커밋
ca0c8ff8c8
2개의 변경된 파일232개의 추가작업 그리고 8개의 파일을 삭제
  1. 15 8
      library/Zend/Form/Element/Captcha.php
  2. 217 0
      tests/Zend/Form/Element/CaptchaTest.php

+ 15 - 8
library/Zend/Form/Element/Captcha.php

@@ -178,17 +178,24 @@ class Zend_Form_Element_Captcha extends Zend_Form_Element_Xhtml
         $captcha    = $this->getCaptcha();
         $captcha->setName($this->getFullyQualifiedName());
 
-        $decorators = $this->getDecorators();
+        if (!$this->loadDefaultDecoratorsIsDisabled()) {
+            $decorators = $this->getDecorators();
+            $decorator  = $captcha->getDecorator();
+            $key        = get_class($this->_getDecorator($decorator, null));
+
+            if (!empty($decorator) && !array_key_exists($key, $decorators)) {
+                array_unshift($decorators, $decorator);
+            }
 
-        $decorator  = $captcha->getDecorator();
-        if (!empty($decorator)) {
-            array_unshift($decorators, $decorator);
-        } else {
             $decorator = array('Captcha', array('captcha' => $captcha));
-            array_unshift($decorators, $decorator);
-        }
+            $key       = get_class($this->_getDecorator($decorator[0], $decorator[1]));
+
+            if ($captcha instanceof Zend_Captcha_Word && !array_key_exists($key, $decorators)) {
+                array_unshift($decorators, $decorator);
+            }
 
-        $this->setDecorators($decorators);
+            $this->setDecorators($decorators);
+        }
 
         $this->setValue($this->getCaptcha()->generate());
 

+ 217 - 0
tests/Zend/Form/Element/CaptchaTest.php

@@ -31,6 +31,9 @@ require_once 'Zend/Form/Element/Captcha.php';
 /** Zend_Captcha_Dumb */
 require_once 'Zend/Captcha/Dumb.php';
 
+/** Zend_Captcha_ReCaptcha */
+require_once 'Zend/Captcha/ReCaptcha.php';
+
 /**
  * @category   Zend
  * @package    Zend_Form
@@ -171,6 +174,220 @@ class Zend_Form_Element_CaptchaTest extends PHPUnit_Framework_TestCase
     {
         $this->assertSame($this->element, $this->element->loadDefaultDecorators());
     }
+    
+    /**
+     * @group ZF-11609
+     */
+    public function testDefaultDecoratorsBeforeAndAfterRendering()
+    {
+        /**
+         * Dumb captcha
+         */
+        
+        // Before rendering
+        $decorators = array_keys($this->element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_HtmlTag',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+        
+        $this->element->render();
+        
+        // After rendering
+        $decorators = array_keys($this->element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Captcha',
+                'Zend_Form_Decorator_Captcha_Word',
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_HtmlTag',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+   
+        /**
+         * ReCaptcha
+         */
+        
+        // Reset element
+        $this->setUp();
+        
+        $options = array(
+            'privKey' => 'privateKey',
+            'pubKey'  => 'publicKey',
+            'ssl'     => true,
+            'xhtml'   => true,
+        );
+        $this->element->setCaptcha(new Zend_Captcha_ReCaptcha($options));
+        
+        // Before rendering
+        $decorators = array_keys($this->element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_HtmlTag',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+        
+        $this->element->render();
+        
+        // After rendering
+        $decorators = array_keys($this->element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Captcha_ReCaptcha',
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_HtmlTag',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+    }
+    
+    /**
+     * @group ZF-11609
+     */
+    public function testDefaultDecoratorsBeforeAndAfterRenderingWhenDefaultDecoratorsAreDisabled()
+    {
+        $element = new Zend_Form_Element_Captcha(
+            'foo',
+            array(
+                'captcha'        => 'Dumb',
+                'captchaOptions' => array(
+                    'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
+                ),
+                'disableLoadDefaultDecorators' => true,
+            )
+        );
+        
+        // Before rendering
+        $decorators = $element->getDecorators();
+        $this->assertTrue(empty($decorators));
+        
+        $element->render();
+        
+        // After rendering
+        $decorators = $element->getDecorators();
+        $this->assertTrue(empty($decorators));
+    }
+    
+    /**
+     * @group ZF-11609
+     */
+    public function testIndividualDecoratorsBeforeAndAfterRendering()
+    {
+        // Disable default decorators is true
+        $element = new Zend_Form_Element_Captcha(
+            'foo',
+            array(
+                'captcha'        => 'Dumb',
+                'captchaOptions' => array(
+                    'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
+                ),
+                'disableLoadDefaultDecorators' => true,
+                'decorators'                   => array(
+                    'Description',
+                    'Errors',
+                    'Captcha_Word',
+                    'Captcha',
+                    'Label',
+                ),
+            )
+        );
+        
+        // Before rendering
+        $decorators = array_keys($element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Captcha_Word',
+                'Zend_Form_Decorator_Captcha',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+        
+        $element->render();
+        
+        // After rendering
+        $decorators = array_keys($element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Captcha_Word',
+                'Zend_Form_Decorator_Captcha',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+        
+        // Disable default decorators is false
+        $element = new Zend_Form_Element_Captcha(
+            'foo',
+            array(
+                'captcha'        => 'Dumb',
+                'captchaOptions' => array(
+                    'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
+                ),
+                'decorators' => array(
+                    'Description',
+                    'Errors',
+                    'Captcha_Word',
+                    'Captcha',
+                    'Label',
+                ),
+            )
+        );
+        
+        // Before rendering
+        $decorators = array_keys($element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Captcha_Word',
+                'Zend_Form_Decorator_Captcha',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+        
+        $element->render();
+        
+        // After rendering
+        $decorators = array_keys($element->getDecorators());
+        $this->assertSame(
+            array(
+                'Zend_Form_Decorator_Description',
+                'Zend_Form_Decorator_Errors',
+                'Zend_Form_Decorator_Captcha_Word',
+                'Zend_Form_Decorator_Captcha',
+                'Zend_Form_Decorator_Label',
+            ),
+            $decorators,
+            var_export($decorators, true)
+        );
+    }
 }
 
 /**