word)) { unset($this->word); } $this->element = new Zend_Form_Element_Captcha( 'captchaF', array( 'captcha' => array( 'Figlet', 'sessionClass' => 'Zend_Captcha_FigletTest_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 testCaptchaAdapterCreated() { $this->assertTrue($this->element->getCaptcha() instanceof Zend_Captcha_Adapter); } public function getView() { require_once 'Zend/View.php'; $view = new Zend_View(); $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper'); return $view; } public function testCaptchaIsRendered() { $html = $this->element->render($this->getView()); $this->assertContains($this->element->getName(), $html); } public function testCaptchaHasIdAndInput() { $html = $this->element->render($this->getView()); $expect = sprintf('type="hidden" name="%s\[id\]" value="%s"', $this->element->getName(), $this->captcha->getId()); $this->assertRegexp("/]*?$expect/", $html, $html); $expect = sprintf('type="text" name="%s\[input\]"', $this->element->getName()); $this->assertRegexp("/]*?$expect/", $html, $html); } public function testTimeoutPopulatedByDefault() { $ttl = $this->captcha->getTimeout(); $this->assertFalse(empty($ttl)); $this->assertTrue(is_int($ttl)); } public function testCanSetTimeout() { $ttl = $this->captcha->getTimeout(); $this->captcha->setTimeout(3600); $this->assertNotEquals($ttl, $this->captcha->getTimeout()); $this->assertEquals(3600, $this->captcha->getTimeout()); } public function testGenerateReturnsId() { $id = $this->captcha->generate(); $this->assertFalse(empty($id)); $this->assertTrue(is_string($id)); $this->id = $id; } public function testGetWordReturnsWord() { $this->captcha->generate(); $word = $this->captcha->getWord(); $this->assertFalse(empty($word)); $this->assertTrue(is_string($word)); $this->assertTrue(strlen($word) == 8); $this->word = $word; } public function testGetWordLength() { $this->captcha->setWordLen(4); $this->captcha->generate(); $word = $this->captcha->getWord(); $this->assertTrue(is_string($word)); $this->assertTrue(strlen($word) == 4); $this->word = $word; } public function testAdapterElementName() { $this->assertEquals($this->captcha->getName(), $this->element->getName()); } public function testGenerateIsRandomised() { $id1 = $this->captcha->generate(); $word1 = $this->captcha->getWord(); $id2 = $this->captcha->generate(); $word2 = $this->captcha->getWord(); $this->assertFalse(empty($id1)); $this->assertFalse(empty($id2)); $this->assertFalse($id1 == $id2); $this->assertFalse($word1 == $word2); } public function testRenderSetsValue() { $this->testCaptchaIsRendered(); $this->assertEquals($this->captcha->getId(), $this->element->getValue()); } public function testLabelIsNull() { $this->assertNull($this->element->getLabel()); } public function testRenderInitializesSessionData() { $this->testCaptchaIsRendered(); $session = $this->captcha->getSession(); $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds); $this->assertEquals(1, $session->setExpirationHops); $this->assertEquals($this->captcha->getWord(), $session->word); } public function testWordValidates() { $this->testCaptchaIsRendered(); $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord())); $this->assertTrue($this->element->isValid("", $input)); } public function testMissingNotValid() { $this->testCaptchaIsRendered(); $this->assertFalse($this->element->isValid("", array())); $input = array($this->element->getName() => array("input" => "blah")); $this->assertFalse($this->element->isValid("", $input)); } public function testWrongWordNotValid() { $this->testCaptchaIsRendered(); $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah")); $this->assertFalse($this->element->isValid("", $input)); } public function testUsesWordCaptchaDecoratorByDefault() { $this->assertEquals('Captcha_Word', $this->element->getCaptcha()->getDecorator()); } public function testCaptchaShouldBeConfigurableViaConfigObject() { $options = array( 'name' => 'foo', 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer', 'wordLen' => 6, 'timeout' => 300, ); $config = new Zend_Config($options); $captcha = new Zend_Captcha_Figlet($config); $test = $captcha->getOptions(); $this->assertEquals($options, $test); } public function testShouldAllowFigletsLargerThanFourteenCharacters() { $this->captcha->setName('foo') ->setWordLen(14); $id = $this->captcha->generate(); } public function testShouldNotValidateEmptyInputAgainstEmptySession() { // Regression Test for ZF-4245 $this->captcha->setName('foo') ->setWordLen(6) ->setTimeout(300); $id = $this->captcha->generate(); // Unset the generated word // we have to reset $this->captcha for that $this->captcha->getSession()->word = null; $this->setUp(); $this->captcha->setName('foo') ->setWordLen(6) ->setTimeout(300); $empty = array($this->captcha->getName() => array('id' => $id, 'input' => '')); $this->assertEquals(false, $this->captcha->isValid(null, $empty)); } /** * @group ZF-3995 */ public function testIsValidShouldAllowPassingArrayValueAndOmittingContext() { $this->testCaptchaIsRendered(); $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord())); $this->assertTrue($this->element->isValid($input)); } /** * @group ZF-3995 */ public function testIsValidShouldNotRequireValueToBeNestedArray() { $this->testCaptchaIsRendered(); $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()); $this->assertTrue($this->element->isValid($input)); } } class Zend_Captcha_FigletTest_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_FigletTest::main() if this source file is executed directly. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_FigletTest::main") { Zend_Captcha_FigletTest::main(); }