FigletTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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-2010 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Captcha
  37. */
  38. class Zend_Captcha_FigletTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @return void
  44. */
  45. public static function main()
  46. {
  47. $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_FigletTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. /**
  51. * Sets up the fixture, for example, open a network connection.
  52. * This method is called before a test is executed.
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. if (isset($this->word)) {
  59. unset($this->word);
  60. }
  61. $this->element = new Zend_Form_Element_Captcha(
  62. 'captchaF',
  63. array(
  64. 'captcha' => array(
  65. 'Figlet',
  66. 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer'
  67. )
  68. )
  69. );
  70. $this->captcha = $this->element->getCaptcha();
  71. }
  72. /**
  73. * Tears down the fixture, for example, close a network connection.
  74. * This method is called after a test is executed.
  75. *
  76. * @return void
  77. */
  78. public function tearDown()
  79. {
  80. }
  81. public function testCaptchaAdapterCreated()
  82. {
  83. $this->assertTrue($this->element->getCaptcha() instanceof Zend_Captcha_Adapter);
  84. }
  85. public function getView()
  86. {
  87. require_once 'Zend/View.php';
  88. $view = new Zend_View();
  89. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  90. return $view;
  91. }
  92. public function testCaptchaIsRendered()
  93. {
  94. $html = $this->element->render($this->getView());
  95. $this->assertContains($this->element->getName(), $html);
  96. }
  97. public function testCaptchaHasIdAndInput()
  98. {
  99. $html = $this->element->render($this->getView());
  100. $expect = sprintf('type="hidden" name="%s\[id\]" value="%s"', $this->element->getName(), $this->captcha->getId());
  101. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  102. $expect = sprintf('type="text" name="%s\[input\]"', $this->element->getName());
  103. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  104. }
  105. /*
  106. * @see ZF-8268
  107. * @group ZF-8268
  108. */
  109. public function testLabelIdIsCorrect()
  110. {
  111. require_once 'Zend/Form.php';
  112. $form = new Zend_Form();
  113. $form->setElementsBelongTo('comment');
  114. $this->element->setLabel("My Captcha");
  115. $form->addElement($this->element);
  116. $html = $form->render($this->getView());
  117. $expect = sprintf('for="comment-%s-input"', $this->element->getName());
  118. $this->assertRegexp("/<label [^>]*?$expect/", $html, $html);
  119. }
  120. public function testTimeoutPopulatedByDefault()
  121. {
  122. $ttl = $this->captcha->getTimeout();
  123. $this->assertFalse(empty($ttl));
  124. $this->assertTrue(is_int($ttl));
  125. }
  126. public function testCanSetTimeout()
  127. {
  128. $ttl = $this->captcha->getTimeout();
  129. $this->captcha->setTimeout(3600);
  130. $this->assertNotEquals($ttl, $this->captcha->getTimeout());
  131. $this->assertEquals(3600, $this->captcha->getTimeout());
  132. }
  133. public function testGenerateReturnsId()
  134. {
  135. $id = $this->captcha->generate();
  136. $this->assertFalse(empty($id));
  137. $this->assertTrue(is_string($id));
  138. $this->id = $id;
  139. }
  140. public function testGetWordReturnsWord()
  141. {
  142. $this->captcha->generate();
  143. $word = $this->captcha->getWord();
  144. $this->assertFalse(empty($word));
  145. $this->assertTrue(is_string($word));
  146. $this->assertTrue(strlen($word) == 8);
  147. $this->word = $word;
  148. }
  149. public function testGetWordLength()
  150. {
  151. $this->captcha->setWordLen(4);
  152. $this->captcha->generate();
  153. $word = $this->captcha->getWord();
  154. $this->assertTrue(is_string($word));
  155. $this->assertTrue(strlen($word) == 4);
  156. $this->word = $word;
  157. }
  158. public function testAdapterElementName()
  159. {
  160. $this->assertEquals($this->captcha->getName(),
  161. $this->element->getName());
  162. }
  163. public function testGenerateIsRandomised()
  164. {
  165. $id1 = $this->captcha->generate();
  166. $word1 = $this->captcha->getWord();
  167. $id2 = $this->captcha->generate();
  168. $word2 = $this->captcha->getWord();
  169. $this->assertFalse(empty($id1));
  170. $this->assertFalse(empty($id2));
  171. $this->assertFalse($id1 == $id2);
  172. $this->assertFalse($word1 == $word2);
  173. }
  174. public function testRenderSetsValue()
  175. {
  176. $this->testCaptchaIsRendered();
  177. $this->assertEquals($this->captcha->getId(),
  178. $this->element->getValue());
  179. }
  180. public function testLabelIsNull()
  181. {
  182. $this->assertNull($this->element->getLabel());
  183. }
  184. public function testRenderInitializesSessionData()
  185. {
  186. $this->testCaptchaIsRendered();
  187. $session = $this->captcha->getSession();
  188. $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
  189. $this->assertEquals(1, $session->setExpirationHops);
  190. $this->assertEquals($this->captcha->getWord(), $session->word);
  191. }
  192. public function testWordValidates()
  193. {
  194. $this->testCaptchaIsRendered();
  195. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  196. $this->assertTrue($this->element->isValid("", $input));
  197. }
  198. public function testMissingNotValid()
  199. {
  200. $this->testCaptchaIsRendered();
  201. $this->assertFalse($this->element->isValid("", array()));
  202. $input = array($this->element->getName() => array("input" => "blah"));
  203. $this->assertFalse($this->element->isValid("", $input));
  204. }
  205. public function testWrongWordNotValid()
  206. {
  207. $this->testCaptchaIsRendered();
  208. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
  209. $this->assertFalse($this->element->isValid("", $input));
  210. }
  211. public function testUsesWordCaptchaDecoratorByDefault()
  212. {
  213. $this->assertEquals('Captcha_Word', $this->element->getCaptcha()->getDecorator());
  214. }
  215. public function testCaptchaShouldBeConfigurableViaConfigObject()
  216. {
  217. $options = array(
  218. 'name' => 'foo',
  219. 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer',
  220. 'wordLen' => 6,
  221. 'timeout' => 300,
  222. );
  223. $config = new Zend_Config($options);
  224. $captcha = new Zend_Captcha_Figlet($config);
  225. $test = $captcha->getOptions();
  226. $this->assertEquals($options, $test);
  227. }
  228. public function testShouldAllowFigletsLargerThanFourteenCharacters()
  229. {
  230. $this->captcha->setName('foo')
  231. ->setWordLen(14);
  232. $id = $this->captcha->generate();
  233. }
  234. public function testShouldNotValidateEmptyInputAgainstEmptySession()
  235. {
  236. // Regression Test for ZF-4245
  237. $this->captcha->setName('foo')
  238. ->setWordLen(6)
  239. ->setTimeout(300);
  240. $id = $this->captcha->generate();
  241. // Unset the generated word
  242. // we have to reset $this->captcha for that
  243. $this->captcha->getSession()->word = null;
  244. $this->setUp();
  245. $this->captcha->setName('foo')
  246. ->setWordLen(6)
  247. ->setTimeout(300);
  248. $empty = array($this->captcha->getName() => array('id' => $id, 'input' => ''));
  249. $this->assertEquals(false, $this->captcha->isValid(null, $empty));
  250. }
  251. /**
  252. * @group ZF-3995
  253. */
  254. public function testIsValidShouldAllowPassingArrayValueAndOmittingContext()
  255. {
  256. $this->testCaptchaIsRendered();
  257. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  258. $this->assertTrue($this->element->isValid($input));
  259. }
  260. /**
  261. * @group ZF-3995
  262. */
  263. public function testIsValidShouldNotRequireValueToBeNestedArray()
  264. {
  265. $this->testCaptchaIsRendered();
  266. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  267. $this->assertTrue($this->element->isValid($input));
  268. }
  269. /**
  270. * @group ZF-5728
  271. */
  272. public function testSetSessionWorks()
  273. {
  274. if(headers_sent($file, $line)) {
  275. $this->markTestSkipped("Cannot use sessions because headers already sent");
  276. }
  277. require_once 'Zend/Session/Namespace.php';
  278. $session = new Zend_Session_Namespace('captcha');
  279. $this->captcha->setSession($session);
  280. $this->testCaptchaIsRendered();
  281. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  282. $this->assertTrue($this->element->isValid($input));
  283. $this->assertEquals($session->word, $this->captcha->getWord());
  284. }
  285. }
  286. class Zend_Captcha_FigletTest_SessionContainer
  287. {
  288. protected static $_word;
  289. public function __get($name)
  290. {
  291. if ('word' == $name) {
  292. return self::$_word;
  293. }
  294. return null;
  295. }
  296. public function __set($name, $value)
  297. {
  298. if ('word' == $name) {
  299. self::$_word = $value;
  300. } else {
  301. $this->$name = $value;
  302. }
  303. }
  304. public function __isset($name)
  305. {
  306. if (('word' == $name) && (null !== self::$_word)) {
  307. return true;
  308. }
  309. return false;
  310. }
  311. public function __call($method, $args)
  312. {
  313. switch ($method) {
  314. case 'setExpirationHops':
  315. case 'setExpirationSeconds':
  316. $this->$method = array_shift($args);
  317. break;
  318. default:
  319. }
  320. }
  321. }
  322. // Call Zend_Captcha_FigletTest::main() if this source file is executed directly.
  323. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_FigletTest::main") {
  324. Zend_Captcha_FigletTest::main();
  325. }