PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Captcha/FigletTest.php

https://bitbucket.org/ksekar/campus
PHP | 363 lines | 261 code | 40 blank | 62 comment | 18 complexity | 395b0442f77da0c89db12e5b3723e12a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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. * @version $Id: FigletTest.php 24594 2012-01-05 21:27:01Z matthew $
  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 'Zend/Form/Element/Captcha.php';
  27. require_once 'Zend/Captcha/Adapter.php';
  28. require_once 'Zend/Config.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Captcha
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Captcha
  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. /*
  105. * @group ZF-8268
  106. */
  107. public function testLabelIdIsCorrect()
  108. {
  109. require_once 'Zend/Form.php';
  110. $form = new Zend_Form();
  111. $form->setElementsBelongTo('comment');
  112. $this->element->setLabel("My Captcha");
  113. $form->addElement($this->element);
  114. $html = $form->render($this->getView());
  115. $expect = sprintf('for="comment-%s-input"', $this->element->getName());
  116. $this->assertRegexp("/<label [^>]*?$expect/", $html, $html);
  117. }
  118. public function testTimeoutPopulatedByDefault()
  119. {
  120. $ttl = $this->captcha->getTimeout();
  121. $this->assertFalse(empty($ttl));
  122. $this->assertTrue(is_int($ttl));
  123. }
  124. public function testCanSetTimeout()
  125. {
  126. $ttl = $this->captcha->getTimeout();
  127. $this->captcha->setTimeout(3600);
  128. $this->assertNotEquals($ttl, $this->captcha->getTimeout());
  129. $this->assertEquals(3600, $this->captcha->getTimeout());
  130. }
  131. public function testGenerateReturnsId()
  132. {
  133. $id = $this->captcha->generate();
  134. $this->assertFalse(empty($id));
  135. $this->assertTrue(is_string($id));
  136. $this->id = $id;
  137. }
  138. public function testGetWordReturnsWord()
  139. {
  140. $this->captcha->generate();
  141. $word = $this->captcha->getWord();
  142. $this->assertFalse(empty($word));
  143. $this->assertTrue(is_string($word));
  144. $this->assertTrue(strlen($word) == 8);
  145. $this->word = $word;
  146. }
  147. public function testGetWordLength()
  148. {
  149. $this->captcha->setWordLen(4);
  150. $this->captcha->generate();
  151. $word = $this->captcha->getWord();
  152. $this->assertTrue(is_string($word));
  153. $this->assertTrue(strlen($word) == 4);
  154. $this->word = $word;
  155. }
  156. public function testAdapterElementName()
  157. {
  158. $this->assertEquals($this->captcha->getName(),
  159. $this->element->getName());
  160. }
  161. public function testGenerateIsRandomised()
  162. {
  163. $id1 = $this->captcha->generate();
  164. $word1 = $this->captcha->getWord();
  165. $id2 = $this->captcha->generate();
  166. $word2 = $this->captcha->getWord();
  167. $this->assertFalse(empty($id1));
  168. $this->assertFalse(empty($id2));
  169. $this->assertFalse($id1 == $id2);
  170. $this->assertFalse($word1 == $word2);
  171. }
  172. public function testRenderSetsValue()
  173. {
  174. $this->testCaptchaIsRendered();
  175. $this->assertEquals($this->captcha->getId(),
  176. $this->element->getValue());
  177. }
  178. public function testLabelIsNull()
  179. {
  180. $this->assertNull($this->element->getLabel());
  181. }
  182. public function testRenderInitializesSessionData()
  183. {
  184. $this->testCaptchaIsRendered();
  185. $session = $this->captcha->getSession();
  186. $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
  187. $this->assertEquals(1, $session->setExpirationHops);
  188. $this->assertEquals($this->captcha->getWord(), $session->word);
  189. }
  190. public function testWordValidates()
  191. {
  192. $this->testCaptchaIsRendered();
  193. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  194. $this->assertTrue($this->element->isValid("", $input));
  195. }
  196. public function testMissingNotValid()
  197. {
  198. $this->testCaptchaIsRendered();
  199. $this->assertFalse($this->element->isValid("", array()));
  200. $input = array($this->element->getName() => array("input" => "blah"));
  201. $this->assertFalse($this->element->isValid("", $input));
  202. }
  203. public function testWrongWordNotValid()
  204. {
  205. $this->testCaptchaIsRendered();
  206. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
  207. $this->assertFalse($this->element->isValid("", $input));
  208. }
  209. public function testUsesWordCaptchaDecoratorByDefault()
  210. {
  211. $this->assertEquals('Captcha_Word', $this->element->getCaptcha()->getDecorator());
  212. }
  213. public function testCaptchaShouldBeConfigurableViaConfigObject()
  214. {
  215. $options = array(
  216. 'name' => 'foo',
  217. 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer',
  218. 'wordLen' => 6,
  219. 'timeout' => 300,
  220. );
  221. $config = new Zend_Config($options);
  222. $captcha = new Zend_Captcha_Figlet($config);
  223. $test = $captcha->getOptions();
  224. $this->assertEquals($options, $test);
  225. }
  226. public function testShouldAllowFigletsLargerThanFourteenCharacters()
  227. {
  228. $this->captcha->setName('foo')
  229. ->setWordLen(14);
  230. $id = $this->captcha->generate();
  231. }
  232. public function testShouldNotValidateEmptyInputAgainstEmptySession()
  233. {
  234. // Regression Test for ZF-4245
  235. $this->captcha->setName('foo')
  236. ->setWordLen(6)
  237. ->setTimeout(300);
  238. $id = $this->captcha->generate();
  239. // Unset the generated word
  240. // we have to reset $this->captcha for that
  241. $this->captcha->getSession()->word = null;
  242. $this->setUp();
  243. $this->captcha->setName('foo')
  244. ->setWordLen(6)
  245. ->setTimeout(300);
  246. $empty = array($this->captcha->getName() => array('id' => $id, 'input' => ''));
  247. $this->assertEquals(false, $this->captcha->isValid(null, $empty));
  248. }
  249. /**
  250. * @group ZF-3995
  251. */
  252. public function testIsValidShouldAllowPassingArrayValueAndOmittingContext()
  253. {
  254. $this->testCaptchaIsRendered();
  255. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  256. $this->assertTrue($this->element->isValid($input));
  257. }
  258. /**
  259. * @group ZF-3995
  260. */
  261. public function testIsValidShouldNotRequireValueToBeNestedArray()
  262. {
  263. $this->testCaptchaIsRendered();
  264. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  265. $this->assertTrue($this->element->isValid($input));
  266. }
  267. /**
  268. * @group ZF-5728
  269. */
  270. public function testSetSessionWorks()
  271. {
  272. if(headers_sent($file, $line)) {
  273. $this->markTestSkipped("Cannot use sessions because headers already sent");
  274. }
  275. require_once 'Zend/Session/Namespace.php';
  276. $session = new Zend_Session_Namespace('captcha');
  277. $this->captcha->setSession($session);
  278. $this->testCaptchaIsRendered();
  279. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  280. $this->assertTrue($this->element->isValid($input));
  281. $this->assertEquals($session->word, $this->captcha->getWord());
  282. }
  283. }
  284. class Zend_Captcha_FigletTest_SessionContainer
  285. {
  286. protected static $_word;
  287. public function __get($name)
  288. {
  289. if ('word' == $name) {
  290. return self::$_word;
  291. }
  292. return null;
  293. }
  294. public function __set($name, $value)
  295. {
  296. if ('word' == $name) {
  297. self::$_word = $value;
  298. } else {
  299. $this->$name = $value;
  300. }
  301. }
  302. public function __isset($name)
  303. {
  304. if (('word' == $name) && (null !== self::$_word)) {
  305. return true;
  306. }
  307. return false;
  308. }
  309. public function __call($method, $args)
  310. {
  311. switch ($method) {
  312. case 'setExpirationHops':
  313. case 'setExpirationSeconds':
  314. $this->$method = array_shift($args);
  315. break;
  316. default:
  317. }
  318. }
  319. }
  320. // Call Zend_Captcha_FigletTest::main() if this source file is executed directly.
  321. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_FigletTest::main") {
  322. Zend_Captcha_FigletTest::main();
  323. }