| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?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_Captcha
- * @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_Captcha_ReCaptchaTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_Captcha_ReCaptchaTest::main");
- }
- require_once 'Zend/Form/Element/Captcha.php';
- require_once 'Zend/View.php';
- /**
- * @category Zend
- * @package Zend_Captcha
- * @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_Captcha
- */
- class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Runs the test methods of this class.
- *
- * @return void
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_ReCaptchaTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- /**
- * Sets up the fixture, for example, open a network connection.
- * This method is called before a test is executed.
- *
- * @return void
- */
- public function setUp()
- {
- if (isset($this->word)) {
- unset($this->word);
- }
- $this->element = new Zend_Form_Element_Captcha(
- 'captchaR',
- array(
- 'captcha' => array(
- 'ReCaptcha',
- 'sessionClass' => 'Zend_Captcha_ReCaptchaTest_SessionContainer'
- )
- )
- );
- $this->captcha = $this->element->getCaptcha();
- }
- /**
- * Tears down the fixture, for example, close a network connection.
- * This method is called after a test is executed.
- *
- * @return void
- */
- public function tearDown()
- {
- }
- public function testConstructorShouldSetOptions()
- {
- $options = array(
- 'privKey' => 'privateKey',
- 'pubKey' => 'publicKey',
- 'ssl' => true,
- 'xhtml' => true,
- );
- $captcha = new Zend_Captcha_ReCaptcha($options);
- $test = $captcha->getOptions();
- $compare = array('privKey' => $options['privKey'], 'pubKey' => $options['pubKey']);
- $this->assertEquals($compare, $test);
- $service = $captcha->getService();
- $test = $service->getParams();
- $compare = array('ssl' => $options['ssl'], 'xhtml' => $options['xhtml']);
- foreach ($compare as $key => $value) {
- $this->assertTrue(array_key_exists($key, $test));
- $this->assertSame($value, $test[$key]);
- }
- }
- public function testShouldAllowSpecifyingServiceObject()
- {
- $captcha = new Zend_Captcha_ReCaptcha();
- $try = new Zend_Service_ReCaptcha();
- $this->assertNotSame($captcha->getService(), $try);
- $captcha->setService($try);
- $this->assertSame($captcha->getService(), $try);
- }
- public function testSetAndGetPublicAndPrivateKeys()
- {
- $captcha = new Zend_Captcha_ReCaptcha();
- $pubKey = 'pubKey';
- $privKey = 'privKey';
- $captcha->setPubkey($pubKey)
- ->setPrivkey($privKey);
- $this->assertSame($pubKey, $captcha->getPubkey());
- $this->assertSame($privKey, $captcha->getPrivkey());
- $this->assertSame($pubKey, $captcha->getService()->getPublicKey());
- $this->assertSame($privKey, $captcha->getService()->getPrivateKey());
- }
- /** @group ZF-7654 */
- public function testConstructorShouldAllowSettingLangOptionOnServiceObject()
- {
- $options = array('lang'=>'fr');
- $captcha = new Zend_Captcha_ReCaptcha($options);
- $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
- }
- /** @group ZF-7654 */
- public function testConstructorShouldAllowSettingThemeOptionOnServiceObject()
- {
- $options = array('theme'=>'black');
- $captcha = new Zend_Captcha_ReCaptcha($options);
- $this->assertEquals('black', $captcha->getService()->getOption('theme'));
- }
- /** @group ZF-7654 */
- public function testAllowsSettingLangOptionOnServiceObject()
- {
- $captcha = new Zend_Captcha_ReCaptcha;
- $captcha->setOption('lang', 'fr');
- $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
- }
- /** @group ZF-7654 */
- public function testAllowsSettingThemeOptionOnServiceObject()
- {
- $captcha = new Zend_Captcha_ReCaptcha;
- $captcha->setOption('theme', 'black');
- $this->assertEquals('black', $captcha->getService()->getOption('theme'));
- }
- /** @group ZF-10991 */
- public function testRenderWillUseElementNameWhenRenderingNoScriptFields()
- {
- $captcha = new Zend_Captcha_ReCaptcha;
- $pubKey = 'pubKey';
- $privKey = 'privKey';
- $captcha->setPubkey($pubKey)
- ->setPrivkey($privKey);
- $element = new Zend_Form_Element_Captcha('captcha', array(
- 'captcha' => $captcha,
- 'belongsTo' => 'contact',
- ));
- $view = new Zend_View();
- $html = $captcha->render($view, $element);
- $this->assertContains('contact[recaptcha_challenge_field]', $html);
- $this->assertContains('contact[recaptcha_response_field]', $html);
- }
- /** @group ZF-10991 */
- public function testUsesReCaptchaSpecificDecorator()
- {
- $captcha = new Zend_Captcha_ReCaptcha;
- $this->assertEquals('Captcha_ReCaptcha', $captcha->getDecorator());
- }
- /**
- * @group ZF-12086
- */
- public function testAllowsSettingCustomTranslationsOptionOnServiceObject()
- {
- $options = array(
- 'instructions_visual' => 'Wpisz dwa wyrazy:',
- 'instructions_audio' => 'Wpisz uslyszany tekst:'
- );
- $captcha = new Zend_Captcha_ReCaptcha;
- $captcha->setOption('custom_translations', $options);
- $this->assertEquals(
- $options,
- $captcha->getService()->getOption('custom_translations')
- );
- }
- }
- class Zend_Captcha_ReCaptchaTest_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_Captcha_ReCaptchaTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ReCaptchaTest::main") {
- Zend_Captcha_ReCaptchaTest::main();
- }
|