Ver Fonte

ZF-12161: Allow setting custom prefix path for CAPTCHA adapters in Zend_Form_Element_Captcha

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24773 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan há 13 anos atrás
pai
commit
67a886f248

+ 9 - 3
library/Zend/Form/Element/Captcha.php

@@ -154,16 +154,22 @@ class Zend_Form_Element_Captcha extends Zend_Form_Element_Xhtml
      */
     public function setOptions(array $options)
     {
+        $captcha        = null;
+        $captchaOptions = array();
+
         if (array_key_exists('captcha', $options)) {
+            $captcha = $options['captcha'];
             if (array_key_exists('captchaOptions', $options)) {
-                $this->setCaptcha($options['captcha'], $options['captchaOptions']);
+                $captchaOptions = $options['captchaOptions'];
                 unset($options['captchaOptions']);
-            } else {
-                $this->setCaptcha($options['captcha']);
             }
             unset($options['captcha']);
         }
         parent::setOptions($options);
+
+        if(null !== $captcha) {
+            $this->setCaptcha($captcha, $captchaOptions);
+        }
         return $this;
     }
 

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

@@ -105,6 +105,28 @@ class Zend_Form_Element_CaptchaTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-12161
+     */
+    public function testSettingCustomCaptchaAdapterPerConstructor()
+    {
+        $element = new Zend_Form_Element_Captcha(
+            'foo',
+            array(
+                'prefixPath' => array(
+                    'prefix' => 'Zend_Form_Element_CaptchaTest',
+                    'path'   => dirname(__FILE__) . '/_files',
+                ),
+                'captcha' => 'Foo',
+            )
+        );
+
+        $this->assertType(
+            'Zend_Form_Element_CaptchaTest_Captcha_Foo',
+            $element->getCaptcha()
+        );
+    }
+
+    /**
      * @see   ZF-4038
      * @group ZF-4038
      */

+ 33 - 0
tests/Zend/Form/Element/_files/Captcha/Foo.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Form
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Foo.php 24593 2012-01-05 20:35:02Z matthew $
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Form
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Form_Element_CaptchaTest_Captcha_Foo extends Zend_Captcha_Dumb
+{
+
+}