DumbTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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-2015 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_DumbTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Captcha_DumbTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Captcha.php';
  27. require_once 'Zend/View.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Captcha
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Captcha
  35. */
  36. class Zend_Captcha_DumbTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Runs the test methods of this class.
  40. *
  41. * @return void
  42. */
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_DumbTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. /**
  49. * Sets up the fixture, for example, open a network connection.
  50. * This method is called before a test is executed.
  51. *
  52. * @return void
  53. */
  54. public function setUp()
  55. {
  56. if (isset($this->word)) {
  57. unset($this->word);
  58. }
  59. $this->element = new Zend_Form_Element_Captcha(
  60. 'captchaD',
  61. array(
  62. 'captcha' => array(
  63. 'Dumb',
  64. 'sessionClass' => 'Zend_Captcha_DumbTest_SessionContainer'
  65. )
  66. )
  67. );
  68. $this->captcha = $this->element->getCaptcha();
  69. }
  70. /**
  71. * Tears down the fixture, for example, close a network connection.
  72. * This method is called after a test is executed.
  73. *
  74. * @return void
  75. */
  76. public function tearDown()
  77. {
  78. }
  79. public function testRendersWordInReverse()
  80. {
  81. $id = $this->captcha->generate('test');
  82. $word = $this->captcha->getWord();
  83. $html = $this->captcha->render(new Zend_View);
  84. $this->assertContains(strrev($word), $html);
  85. $this->assertNotContains($word, $html);
  86. }
  87. /**
  88. * @group ZF-11522
  89. */
  90. public function testDefaultLabelIsUsedWhenNoAlternateLabelSet()
  91. {
  92. $this->assertEquals('Please type this word backwards', $this->captcha->getLabel());
  93. }
  94. /**
  95. * @group ZF-11522
  96. */
  97. public function testChangeLabelViaSetterMethod()
  98. {
  99. $this->captcha->setLabel('Testing');
  100. $this->assertEquals('Testing', $this->captcha->getLabel());
  101. }
  102. /**
  103. * @group ZF-11522
  104. */
  105. public function testRendersLabelUsingProvidedValue()
  106. {
  107. $this->captcha->setLabel('Testing 123');
  108. $id = $this->captcha->generate('test');
  109. $html = $this->captcha->render(new Zend_View);
  110. $this->assertContains('Testing 123', $html);
  111. }
  112. }
  113. class Zend_Captcha_DumbTest_SessionContainer
  114. {
  115. protected static $_word;
  116. public function __get($name)
  117. {
  118. if ('word' == $name) {
  119. return self::$_word;
  120. }
  121. return null;
  122. }
  123. public function __set($name, $value)
  124. {
  125. if ('word' == $name) {
  126. self::$_word = $value;
  127. } else {
  128. $this->$name = $value;
  129. }
  130. }
  131. public function __isset($name)
  132. {
  133. if (('word' == $name) && (null !== self::$_word)) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. public function __call($method, $args)
  139. {
  140. switch ($method) {
  141. case 'setExpirationHops':
  142. case 'setExpirationSeconds':
  143. $this->$method = array_shift($args);
  144. break;
  145. default:
  146. }
  147. }
  148. }
  149. // Call Zend_Captcha_DumbTest::main() if this source file is executed directly.
  150. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_DumbTest::main") {
  151. Zend_Captcha_DumbTest::main();
  152. }