PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Captcha/FigletTest.php

https://github.com/MontmereLimited/zf2
PHP | 288 lines | 207 code | 30 blank | 51 comment | 6 complexity | fd8bd3fd7a85c3b4eabbf816e9c5a393 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\Captcha;
  25. use Zend\View\Renderer\PhpRenderer as View;
  26. /**
  27. * @category Zend
  28. * @package Zend_Captcha
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Captcha
  33. */
  34. class FigletTest extends CommonWordTest
  35. {
  36. protected $wordClass = '\Zend\Captcha\Figlet';
  37. /**
  38. * Sets up the fixture, for example, open a network connection.
  39. * This method is called before a test is executed.
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. if (isset($this->word)) {
  46. unset($this->word);
  47. }
  48. $this->element = new \Zend\Form\Element\Captcha(
  49. 'captchaF',
  50. array(
  51. 'captcha' => array(
  52. 'Figlet',
  53. 'sessionClass' => 'ZendTest\\Captcha\\TestAsset\\SessionContainer'
  54. )
  55. )
  56. );
  57. $this->captcha = $this->element->getCaptcha();
  58. }
  59. public function testCaptchaAdapterCreated()
  60. {
  61. $this->assertTrue($this->element->getCaptcha() instanceof \Zend\Captcha\Adapter);
  62. }
  63. public function getView()
  64. {
  65. $view = new View();
  66. return $view;
  67. }
  68. public function testCaptchaIsRendered()
  69. {
  70. $html = $this->element->render($this->getView());
  71. $this->assertContains($this->element->getName(), $html);
  72. }
  73. public function testCaptchaHasIdAndInput()
  74. {
  75. $html = $this->element->render($this->getView());
  76. $expect = sprintf('type="hidden" name="%s\[id\]" value="%s"', $this->element->getName(), $this->captcha->getId());
  77. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  78. $expect = sprintf('type="text" name="%s\[input\]"', $this->element->getName());
  79. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  80. }
  81. /*
  82. * @group ZF-8268
  83. */
  84. public function testLabelIdIsCorrect()
  85. {
  86. $form = new \Zend\Form\Form();
  87. $form->setElementsBelongTo('comment');
  88. $this->element->setLabel("My Captcha");
  89. $form->addElement($this->element);
  90. $html = $form->render($this->getView());
  91. $expect = sprintf('for="comment-%s-input"', $this->element->getName());
  92. $this->assertRegexp("/<label [^>]*?$expect/", $html, $html);
  93. }
  94. public function testTimeoutPopulatedByDefault()
  95. {
  96. $ttl = $this->captcha->getTimeout();
  97. $this->assertFalse(empty($ttl));
  98. $this->assertTrue(is_int($ttl));
  99. }
  100. public function testCanSetTimeout()
  101. {
  102. $ttl = $this->captcha->getTimeout();
  103. $this->captcha->setTimeout(3600);
  104. $this->assertNotEquals($ttl, $this->captcha->getTimeout());
  105. $this->assertEquals(3600, $this->captcha->getTimeout());
  106. }
  107. public function testGenerateReturnsId()
  108. {
  109. $id = $this->captcha->generate();
  110. $this->assertFalse(empty($id));
  111. $this->assertTrue(is_string($id));
  112. $this->id = $id;
  113. }
  114. public function testGetWordReturnsWord()
  115. {
  116. $this->captcha->generate();
  117. $word = $this->captcha->getWord();
  118. $this->assertFalse(empty($word));
  119. $this->assertTrue(is_string($word));
  120. $this->assertTrue(strlen($word) == 8);
  121. $this->word = $word;
  122. }
  123. public function testGetWordLength()
  124. {
  125. $this->captcha->setWordLen(4);
  126. $this->captcha->generate();
  127. $word = $this->captcha->getWord();
  128. $this->assertTrue(is_string($word));
  129. $this->assertTrue(strlen($word) == 4);
  130. $this->word = $word;
  131. }
  132. public function testAdapterElementName()
  133. {
  134. $this->assertEquals($this->captcha->getName(),
  135. $this->element->getName());
  136. }
  137. public function testGenerateIsRandomised()
  138. {
  139. $id1 = $this->captcha->generate();
  140. $word1 = $this->captcha->getWord();
  141. $id2 = $this->captcha->generate();
  142. $word2 = $this->captcha->getWord();
  143. $this->assertFalse(empty($id1));
  144. $this->assertFalse(empty($id2));
  145. $this->assertFalse($id1 == $id2);
  146. $this->assertFalse($word1 == $word2);
  147. }
  148. public function testRenderSetsValue()
  149. {
  150. $this->testCaptchaIsRendered();
  151. $this->assertEquals($this->captcha->getId(),
  152. $this->element->getValue());
  153. }
  154. public function testLabelIsNull()
  155. {
  156. $this->assertNull($this->element->getLabel());
  157. }
  158. public function testRenderInitializesSessionData()
  159. {
  160. $this->testCaptchaIsRendered();
  161. $session = $this->captcha->getSession();
  162. $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
  163. $this->assertEquals(1, $session->setExpirationHops);
  164. $this->assertEquals($this->captcha->getWord(), $session->word);
  165. }
  166. public function testWordValidates()
  167. {
  168. $this->testCaptchaIsRendered();
  169. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  170. $this->assertTrue($this->element->isValid("", $input));
  171. }
  172. public function testMissingNotValid()
  173. {
  174. $this->testCaptchaIsRendered();
  175. $this->assertFalse($this->element->isValid("", array()));
  176. $input = array($this->element->getName() => array("input" => "blah"));
  177. $this->assertFalse($this->element->isValid("", $input));
  178. }
  179. public function testWrongWordNotValid()
  180. {
  181. $this->testCaptchaIsRendered();
  182. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
  183. $this->assertFalse($this->element->isValid("", $input));
  184. }
  185. public function testUsesWordCaptchaDecoratorByDefault()
  186. {
  187. $this->assertEquals('Captcha\\Word', $this->element->getCaptcha()->getDecorator());
  188. }
  189. public function testCaptchaShouldBeConfigurableViaConfigObject()
  190. {
  191. $options = array(
  192. 'name' => 'foo',
  193. 'sessionClass' => 'ZendTest\\Captcha\\TestAsset\\SessionContainer',
  194. 'wordLen' => 6,
  195. 'timeout' => 300,
  196. );
  197. $config = new \Zend\Config\Config($options);
  198. $captcha = new \Zend\Captcha\Figlet($config);
  199. $test = $captcha->getOptions();
  200. $this->assertEquals($options, $test);
  201. }
  202. public function testShouldAllowFigletsLargerThanFourteenCharacters()
  203. {
  204. $this->captcha->setName('foo')
  205. ->setWordLen(14);
  206. $id = $this->captcha->generate();
  207. }
  208. public function testShouldNotValidateEmptyInputAgainstEmptySession()
  209. {
  210. // Regression Test for ZF-4245
  211. $this->captcha->setName('foo')
  212. ->setWordLen(6)
  213. ->setTimeout(300);
  214. $id = $this->captcha->generate();
  215. // Unset the generated word
  216. // we have to reset $this->captcha for that
  217. $this->captcha->getSession()->word = null;
  218. $this->setUp();
  219. $this->captcha->setName('foo')
  220. ->setWordLen(6)
  221. ->setTimeout(300);
  222. $empty = array($this->captcha->getName() => array('id' => $id, 'input' => ''));
  223. $this->assertEquals(false, $this->captcha->isValid(null, $empty));
  224. }
  225. /**
  226. * @group ZF-3995
  227. */
  228. public function testIsValidShouldAllowPassingArrayValueAndOmittingContext()
  229. {
  230. $this->testCaptchaIsRendered();
  231. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  232. $this->assertTrue($this->element->isValid($input));
  233. }
  234. /**
  235. * @group ZF-3995
  236. */
  237. public function testIsValidShouldNotRequireValueToBeNestedArray()
  238. {
  239. $this->testCaptchaIsRendered();
  240. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  241. $this->assertTrue($this->element->isValid($input));
  242. }
  243. /**
  244. * @group ZF-5728
  245. */
  246. public function testSetSessionWorks()
  247. {
  248. if(headers_sent($file, $line)) {
  249. $this->markTestSkipped("Cannot use sessions because headers already sent");
  250. }
  251. $session = new \Zend\Session\Container('captcha');
  252. $this->captcha->setSession($session);
  253. $this->testCaptchaIsRendered();
  254. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  255. $this->assertTrue($this->element->isValid($input));
  256. $this->assertEquals($session->word, $this->captcha->getWord());
  257. }
  258. }