FigletTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Captcha
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Captcha_FigletTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Captcha_FigletTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. require_once 'Zend/Form/Element/Captcha.php';
  28. require_once 'Zend/Captcha/Adapter.php';
  29. require_once 'Zend/Config.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Captcha
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Captcha_FigletTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_FigletTest");
  47. $result = PHPUnit_TextUI_TestRunner::run($suite);
  48. }
  49. /**
  50. * Sets up the fixture, for example, open a network connection.
  51. * This method is called before a test is executed.
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. if (isset($this->word)) {
  58. unset($this->word);
  59. }
  60. $this->element = new Zend_Form_Element_Captcha(
  61. 'captchaF',
  62. array(
  63. 'captcha' => array(
  64. 'Figlet',
  65. 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer'
  66. )
  67. )
  68. );
  69. $this->captcha = $this->element->getCaptcha();
  70. }
  71. /**
  72. * Tears down the fixture, for example, close a network connection.
  73. * This method is called after a test is executed.
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. }
  80. public function testCaptchaAdapterCreated()
  81. {
  82. $this->assertTrue($this->element->getCaptcha() instanceof Zend_Captcha_Adapter);
  83. }
  84. public function getView()
  85. {
  86. require_once 'Zend/View.php';
  87. $view = new Zend_View();
  88. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  89. return $view;
  90. }
  91. public function testCaptchaIsRendered()
  92. {
  93. $html = $this->element->render($this->getView());
  94. $this->assertContains($this->element->getName(), $html);
  95. }
  96. public function testCaptchaHasIdAndInput()
  97. {
  98. $html = $this->element->render($this->getView());
  99. $expect = sprintf('type="hidden" name="%s\[id\]" value="%s"', $this->element->getName(), $this->captcha->getId());
  100. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  101. $expect = sprintf('type="text" name="%s\[input\]"', $this->element->getName());
  102. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  103. }
  104. public function testTimeoutPopulatedByDefault()
  105. {
  106. $ttl = $this->captcha->getTimeout();
  107. $this->assertFalse(empty($ttl));
  108. $this->assertTrue(is_int($ttl));
  109. }
  110. public function testCanSetTimeout()
  111. {
  112. $ttl = $this->captcha->getTimeout();
  113. $this->captcha->setTimeout(3600);
  114. $this->assertNotEquals($ttl, $this->captcha->getTimeout());
  115. $this->assertEquals(3600, $this->captcha->getTimeout());
  116. }
  117. public function testGenerateReturnsId()
  118. {
  119. $id = $this->captcha->generate();
  120. $this->assertFalse(empty($id));
  121. $this->assertTrue(is_string($id));
  122. $this->id = $id;
  123. }
  124. public function testGetWordReturnsWord()
  125. {
  126. $this->captcha->generate();
  127. $word = $this->captcha->getWord();
  128. $this->assertFalse(empty($word));
  129. $this->assertTrue(is_string($word));
  130. $this->assertTrue(strlen($word) == 8);
  131. $this->word = $word;
  132. }
  133. public function testGetWordLength()
  134. {
  135. $this->captcha->setWordLen(4);
  136. $this->captcha->generate();
  137. $word = $this->captcha->getWord();
  138. $this->assertTrue(is_string($word));
  139. $this->assertTrue(strlen($word) == 4);
  140. $this->word = $word;
  141. }
  142. public function testAdapterElementName()
  143. {
  144. $this->assertEquals($this->captcha->getName(),
  145. $this->element->getName());
  146. }
  147. public function testGenerateIsRandomised()
  148. {
  149. $id1 = $this->captcha->generate();
  150. $word1 = $this->captcha->getWord();
  151. $id2 = $this->captcha->generate();
  152. $word2 = $this->captcha->getWord();
  153. $this->assertFalse(empty($id1));
  154. $this->assertFalse(empty($id2));
  155. $this->assertFalse($id1 == $id2);
  156. $this->assertFalse($word1 == $word2);
  157. }
  158. public function testRenderSetsValue()
  159. {
  160. $this->testCaptchaIsRendered();
  161. $this->assertEquals($this->captcha->getId(),
  162. $this->element->getValue());
  163. }
  164. public function testLabelIsNull()
  165. {
  166. $this->assertNull($this->element->getLabel());
  167. }
  168. public function testRenderInitializesSessionData()
  169. {
  170. $this->testCaptchaIsRendered();
  171. $session = $this->captcha->getSession();
  172. $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
  173. $this->assertEquals(1, $session->setExpirationHops);
  174. $this->assertEquals($this->captcha->getWord(), $session->word);
  175. }
  176. public function testWordValidates()
  177. {
  178. $this->testCaptchaIsRendered();
  179. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  180. $this->assertTrue($this->element->isValid("", $input));
  181. }
  182. public function testMissingNotValid()
  183. {
  184. $this->testCaptchaIsRendered();
  185. $this->assertFalse($this->element->isValid("", array()));
  186. $input = array($this->element->getName() => array("input" => "blah"));
  187. $this->assertFalse($this->element->isValid("", $input));
  188. }
  189. public function testWrongWordNotValid()
  190. {
  191. $this->testCaptchaIsRendered();
  192. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
  193. $this->assertFalse($this->element->isValid("", $input));
  194. }
  195. public function testUsesWordCaptchaDecoratorByDefault()
  196. {
  197. $this->assertEquals('Captcha_Word', $this->element->getCaptcha()->getDecorator());
  198. }
  199. public function testCaptchaShouldBeConfigurableViaConfigObject()
  200. {
  201. $options = array(
  202. 'name' => 'foo',
  203. 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer',
  204. 'wordLen' => 6,
  205. 'timeout' => 300,
  206. );
  207. $config = new Zend_Config($options);
  208. $captcha = new Zend_Captcha_Figlet($config);
  209. $test = $captcha->getOptions();
  210. $this->assertEquals($options, $test);
  211. }
  212. public function testShouldAllowFigletsLargerThanFourteenCharacters()
  213. {
  214. $this->captcha->setName('foo')
  215. ->setWordLen(14);
  216. $id = $this->captcha->generate();
  217. }
  218. public function testShouldNotValidateEmptyInputAgainstEmptySession()
  219. {
  220. // Regression Test for ZF-4245
  221. $this->captcha->setName('foo')
  222. ->setWordLen(6)
  223. ->setTimeout(300);
  224. $id = $this->captcha->generate();
  225. // Unset the generated word
  226. // we have to reset $this->captcha for that
  227. $this->captcha->getSession()->word = null;
  228. $this->setUp();
  229. $this->captcha->setName('foo')
  230. ->setWordLen(6)
  231. ->setTimeout(300);
  232. $empty = array($this->captcha->getName() => array('id' => $id, 'input' => ''));
  233. $this->assertEquals(false, $this->captcha->isValid(null, $empty));
  234. }
  235. /**
  236. * @group ZF-3995
  237. */
  238. public function testIsValidShouldAllowPassingArrayValueAndOmittingContext()
  239. {
  240. $this->testCaptchaIsRendered();
  241. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  242. $this->assertTrue($this->element->isValid($input));
  243. }
  244. /**
  245. * @group ZF-3995
  246. */
  247. public function testIsValidShouldNotRequireValueToBeNestedArray()
  248. {
  249. $this->testCaptchaIsRendered();
  250. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  251. $this->assertTrue($this->element->isValid($input));
  252. }
  253. }
  254. class Zend_Captcha_FigletTest_SessionContainer
  255. {
  256. protected static $_word;
  257. public function __get($name)
  258. {
  259. if ('word' == $name) {
  260. return self::$_word;
  261. }
  262. return null;
  263. }
  264. public function __set($name, $value)
  265. {
  266. if ('word' == $name) {
  267. self::$_word = $value;
  268. } else {
  269. $this->$name = $value;
  270. }
  271. }
  272. public function __isset($name)
  273. {
  274. if (('word' == $name) && (null !== self::$_word)) {
  275. return true;
  276. }
  277. return false;
  278. }
  279. public function __call($method, $args)
  280. {
  281. switch ($method) {
  282. case 'setExpirationHops':
  283. case 'setExpirationSeconds':
  284. $this->$method = array_shift($args);
  285. break;
  286. default:
  287. }
  288. }
  289. }
  290. // Call Zend_Captcha_FigletTest::main() if this source file is executed directly.
  291. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_FigletTest::main") {
  292. Zend_Captcha_FigletTest::main();
  293. }