| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- <?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-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- // Call Zend_Form_Element_CaptchaTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_CaptchaTest::main");
- }
- /** Zend_Form_Element_Captcha */
- 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
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Form
- */
- class Zend_Form_Element_CaptchaTest extends PHPUnit_Framework_TestCase
- {
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite('Zend_Form_Element_CaptchaTest');
- PHPUnit_TextUI_TestRunner::run($suite);
- }
- public function setUp()
- {
- $this->element = new Zend_Form_Element_Captcha(
- 'foo',
- array(
- 'captcha' => 'Dumb',
- 'captchaOptions' => array(
- 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
- ),
- )
- );
- }
- public function getCaptcha()
- {
- $captcha = new Zend_Captcha_Dumb(array(
- 'sessionClass' => 'Zend_Form_Element_CaptchaTest_SessionContainer',
- ));
- return $captcha;
- }
- /**
- * @expectedException Zend_Form_Exception
- */
- public function testConstructionShouldRequireCaptchaDetails()
- {
- $this->element = new Zend_Form_Element_Captcha('foo');
- }
- public function testShouldAllowSettingCaptcha()
- {
- $captcha = $this->getCaptcha();
- $this->assertNotSame($this->element->getCaptcha(), $captcha);
- $this->element->setCaptcha($captcha);
- $this->assertSame($captcha, $this->element->getCaptcha());
- }
- public function testShouldAllowAddingCaptchaPrefixPath()
- {
- $this->element->addPrefixPath('My_Captcha', 'My/Captcha/', 'captcha');
- $loader = $this->element->getPluginLoader('captcha');
- $paths = $loader->getPaths('My_Captcha');
- $this->assertTrue(is_array($paths));
- }
- public function testAddingNullPrefixPathShouldAddCaptchaPrefixPath()
- {
- $this->element->addPrefixPath('My', 'My');
- $loader = $this->element->getPluginLoader('captcha');
- $paths = $loader->getPaths('My_Captcha');
- $this->assertTrue(is_array($paths));
- }
- /**
- * @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->assertTrue(
- $element->getCaptcha() instanceof
- Zend_Form_Element_CaptchaTest_Captcha_Foo
- );
- }
- /**
- * @see ZF-4038
- * @group ZF-4038
- */
- public function testCaptchaShouldRenderFullyQualifiedElementName()
- {
- require_once 'Zend/Form.php';
- require_once 'Zend/View.php';
- $form = new Zend_Form();
- $form->addElement($this->element)
- ->setElementsBelongTo('bar');
- $html = $form->render(new Zend_View);
- $this->assertContains('name="bar[foo', $html, $html);
- $this->assertContains('id="bar-foo-', $html, $html);
- $this->form = $form;
- }
- /**
- * @see ZF-4038
- * @group ZF-4038
- */
- public function testCaptchaShouldValidateUsingFullyQualifiedElementName()
- {
- $this->testCaptchaShouldRenderFullyQualifiedElementName();
- $word = $this->element->getCaptcha()->getWord();
- $id = $this->element->getCaptcha()->getId();
- $data = array(
- 'bar' => array(
- 'foo' => array(
- 'id' => $id,
- 'input' => $word,
- )
- )
- );
- $valid = $this->form->isValid($data);
- $this->assertTrue($valid, var_export($this->form->getMessages(), 1));
- }
- /**
- * @group ZF-4822
- */
- public function testDefaultDecoratorsShouldIncludeErrorsDescriptionHtmlTagAndLabel()
- {
- $decorators = $this->element->getDecorators();
- $this->assertTrue(is_array($decorators));
- $this->assertTrue(array_key_exists('Zend_Form_Decorator_Errors', $decorators), 'Missing Errors decorator' . var_export(array_keys($decorators), 1));
- $this->assertTrue(array_key_exists('Zend_Form_Decorator_Description', $decorators), 'Missing Description decorator' . var_export(array_keys($decorators), 1));
- $this->assertTrue(array_key_exists('Zend_Form_Decorator_HtmlTag', $decorators), 'Missing HtmlTag decorator' . var_export(array_keys($decorators), 1));
- $this->assertTrue(array_key_exists('Zend_Form_Decorator_Label', $decorators), 'Missing Label decorator' . var_export(array_keys($decorators), 1));
- }
- /**
- * @group ZF-5855
- */
- public function testHelperDoesNotShowUpInAttribs()
- {
- require_once 'Zend/View.php';
- $this->assertFalse(array_key_exists('helper', $this->element->getAttribs()));
- }
- /**
- * Prove the fluent interface on Zend_Form_Element_Captcha::loadDefaultDecorators
- *
- * @link http://framework.zend.com/issues/browse/ZF-9913
- * @return void
- */
- public function testFluentInterfaceOnLoadDefaultDecorators()
- {
- $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)
- );
- }
-
- /**
- * @group ZF-12173
- */
- public function testShouldAllowAddingCaptchaPrefixPathWithBackslash()
- {
- if (version_compare(PHP_VERSION, '5.3.0', '<')) {
- $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
- return;
- }
- $this->element->addPrefixPath('My\Captcha', 'My/Captcha/', 'captcha');
- $loader = $this->element->getPluginLoader('captcha');
- $paths = $loader->getPaths('My\Captcha');
- $this->assertTrue(is_array($paths));
- }
-
- /**
- * @group ZF-12173
- */
- public function testAddingCaptchaPrefixPathWithBackslash()
- {
- if (version_compare(PHP_VERSION, '5.3.0', '<')) {
- $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
- return;
- }
- $this->element->addPrefixPath('My\\', 'My/');
- $loader = $this->element->getPluginLoader('captcha');
- $paths = $loader->getPaths('My\Captcha');
- $this->assertTrue(is_array($paths));
- }
- }
- /**
- * @category Zend
- * @package Zend_Form
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Form
- */
- class Zend_Form_Element_CaptchaTest_SessionContainer
- {
- protected static $_word;
- public function __get($name)
- {
- if ('word' == $name) {
- return self::$_word;
- }
- return null;
- }
- public function __set($name, $value)
- {
- if ('word' == $name) {
- self::$_word = $value;
- } else {
- $this->$name = $value;
- }
- }
- public function __isset($name)
- {
- if (('word' == $name) && (null !== self::$_word)) {
- return true;
- }
- return false;
- }
- public function __call($method, $args)
- {
- switch ($method) {
- case 'setExpirationHops':
- case 'setExpirationSeconds':
- $this->$method = array_shift($args);
- break;
- default:
- }
- }
- }
- // Call Zend_Form_Element_CaptchaTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_CaptchaTest::main") {
- Zend_Form_Element_CaptchaTest::main();
- }
|