Permalink
Browse files

some tests

  • Loading branch information...
1 parent ae03f52 commit d67432d904e274e69ddc332efdd3f047ddaf014a @cweiske cweiske committed Jul 27, 2010
Showing with 131 additions and 3 deletions.
  1. +131 −3 tests/HTML_QuickForm2_Element_NumeralCaptchaTest.php
@@ -26,6 +26,7 @@ protected function setUp()
}
$this->nc = new HTML_QuickForm2_Element_NumeralCaptcha();
+ $this->nc->clearCaptchaSession();
}
/**
@@ -65,10 +66,12 @@ public function testSetNumeral()
public function testGetCaptchaHtml()
{
//we cannot test getCaptchaHtml() alone because
- // verifyCaptcha() is not called there.
- // Using __toString() is the only way.
+ // verifyCaptcha() is not called yet at that place.
+ // Using __toString() before is needed to generate
+ // the captcha question and answer itself.
(string)$this->nc;
- $str = (string)$this->nc->getCaptchaHtml();
+
+ $str = $this->nc->getCaptchaHtml();
$xml = '<?xml version="1.0" encoding="utf-8"?>'
. "\n<test>\n"
. $str
@@ -78,12 +81,137 @@ public function testGetCaptchaHtml()
$this->assertTag(array('test'), $xml, '', false);
}
+
+
/**
* Tests if __toString() renders the captcha question
* and input element in the normal case (form not filled yet)
+ * when the captcha is not solved yet.
*/
public function test__toStringNormal()
{
+ //make sure we have a known captcha question
+ $cap = new Text_CAPTCHA_Numeral(
+ Text_CAPTCHA_Numeral::TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY,
+ 10, 11
+ );
+ $this->nc->setNumeral($cap);
+
+ $str = (string)$this->nc;
+ $xml = '<?xml version="1.0" encoding="utf-8"?>'
+ . "\n<test>\n"
+ . $str
+ . "\n</test>";
+
+ //this is a cheap way to see if the xml is well-formed
+ $this->assertTag(array('test'), $xml, '', false);
+ }
+
+
+
+ /**
+ * Tests if __toString() renders the "captcha solved" message
+ * when the captcha is solved.
+ */
+ public function test__toStringNormalSolved()
+ {
+ //make sure we have a known captcha question
+ $cap = new Text_CAPTCHA_Numeral(
+ Text_CAPTCHA_Numeral::TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY,
+ 10, 11
+ );
+ $this->nc->setNumeral($cap);
+
+ //force session and question intialisation
+ (string)$this->nc;
+ $this->nc->setValue($this->nc->getSession()->answer);
+
+ $str = (string)$this->nc;
+
+ //not empty string
+ $this->assertNotEquals('', $str, 'Solved string is empty');
+
+ $data = $this->nc->getData();
+ $this->assertEquals($data['captchaSolved'], $str);
+ }
+
+
+
+ /**
+ * Tests if __toString() renders the captcha question
+ * and input element in the frozen case when the captcha
+ * is not solved yet.
+ */
+ public function test__toStringFrozen()
+ {
+ $this->nc->toggleFrozen(true);
+ //make sure we have a known captcha question
+ $cap = new Text_CAPTCHA_Numeral(
+ Text_CAPTCHA_Numeral::TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY,
+ 10, 11
+ );
+ $this->nc->setNumeral($cap);
+
+ $str = (string)$this->nc;
+ //not empty string
+ $this->assertNotEquals('', $str, 'Frozen string is empty');
+
+ //FIXME: check for equation when we get a new numeral captcha
+ // version
+ }
+
+
+
+ /**
+ * Tests if __toString() renders the "captcha solved" message
+ * when the captcha is solved and frozen.
+ */
+ public function test__toStringFrozenSolved()
+ {
+ $this->nc->toggleFrozen(true);
+
+ //make sure we have a known captcha question
+ $cap = new Text_CAPTCHA_Numeral(
+ Text_CAPTCHA_Numeral::TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY,
+ 10, 11
+ );
+ $this->nc->setNumeral($cap);
+
+ //force session and question intialisation
+ (string)$this->nc;
+ $this->nc->setValue($this->nc->getSession()->answer);
+
+ $str = (string)$this->nc;
+
+ //not empty string
+ $this->assertNotEquals('', $str, 'Frozen solved string is empty');
+
+ $data = $this->nc->getData();
+ $this->assertEquals($data['captchaSolved'], $str);
+ }
+
+
+
+ /**
+ * Tests if __toString() does not render the captcha question
+ * when question rendering is turned off and the element is frozen.
+ */
+ public function test__toStringFrozenNoRender()
+ {
+ $this->nc->toggleFrozen(true);
+ //make sure we have a known captcha question
+ $cap = new Text_CAPTCHA_Numeral(
+ Text_CAPTCHA_Numeral::TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY,
+ 10, 11
+ );
+ $this->nc->setNumeral($cap);
+
+ $str = (string)$this->nc;
+ //not empty string
+ $this->assertEquals('', $str, 'Frozen string is not empty');
+
+ //FIXME: check for equation when we get a new numeral captcha
+ // version
}
}
?>

0 comments on commit d67432d

Please sign in to comment.