ImageTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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-2008 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_ImageTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Captcha_ImageTest::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. /**
  30. * @category Zend
  31. * @package Zend_Captcha
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Captcha_ImageTest extends PHPUnit_Framework_TestCase
  37. {
  38. protected $_tmpDir;
  39. /**
  40. * Runs the test methods of this class.
  41. *
  42. * @return void
  43. */
  44. public static function main()
  45. {
  46. require_once "PHPUnit/TextUI/TestRunner.php";
  47. $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_ImageTest");
  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 (!extension_loaded('gd')) {
  59. $this->markTestSkipped('The GD extension is not available.');
  60. return;
  61. }
  62. if(!function_exists("imagepng")) {
  63. $this->markTestSkipped("Image CAPTCHA requires PNG support");
  64. }
  65. if(!function_exists("imageftbbox")) {
  66. $this->markTestSkipped("Image CAPTCHA requires FT fonts support");
  67. }
  68. if (isset($this->word)) {
  69. unset($this->word);
  70. }
  71. $this->testDir = $this->_getTmpDir() . '/ZF_test_images';
  72. if(!is_dir($this->testDir)) {
  73. @mkdir($this->testDir);
  74. }
  75. $this->element = new Zend_Form_Element_Captcha('captchaI',
  76. array('captcha' => array('Image',
  77. 'sessionClass' => 'Zend_Captcha_ImageTest_SessionContainer',
  78. 'imgDir' => $this->testDir,
  79. 'font' => dirname(__FILE__). '/../Pdf/_fonts/Vera.ttf')
  80. ));
  81. $this->captcha = $this->element->getCaptcha();
  82. }
  83. /**
  84. * Tears down the fixture, for example, close a network connection.
  85. * This method is called after a test is executed.
  86. *
  87. * @return void
  88. */
  89. public function tearDown()
  90. {
  91. // remove chaptcha images
  92. foreach(new DirectoryIterator($this->testDir) as $file) {
  93. if(!$file->isDot() && !$file->isDir()) {
  94. unlink($file->getPathname());
  95. }
  96. }
  97. }
  98. /**
  99. * Determine system TMP directory
  100. *
  101. * @return string
  102. * @throws Zend_File_Transfer_Exception if unable to determine directory
  103. */
  104. protected function _getTmpDir()
  105. {
  106. if (null === $this->_tmpDir) {
  107. if (function_exists('sys_get_temp_dir')) {
  108. $tmpdir = sys_get_temp_dir();
  109. } elseif (!empty($_ENV['TMP'])) {
  110. $tmpdir = realpath($_ENV['TMP']);
  111. } elseif (!empty($_ENV['TMPDIR'])) {
  112. $tmpdir = realpath($_ENV['TMPDIR']);
  113. } else if (!empty($_ENV['TEMP'])) {
  114. $tmpdir = realpath($_ENV['TEMP']);
  115. } else {
  116. // Attemp to detect by creating a temporary file
  117. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  118. if ($tempFile) {
  119. $tmpdir = realpath(dirname($tempFile));
  120. unlink($tempFile);
  121. } else {
  122. require_once 'Zend/File/Transfer/Exception.php';
  123. throw new Zend_File_Transfer_Exception('Could not determine temp directory');
  124. }
  125. }
  126. $this->_tmpDir = rtrim($tmpdir, "/\\");
  127. }
  128. return $this->_tmpDir;
  129. }
  130. public function getView()
  131. {
  132. require_once 'Zend/View.php';
  133. $view = new Zend_View();
  134. $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
  135. return $view;
  136. }
  137. public function testCaptchaIsRendered()
  138. {
  139. $html = $this->element->render($this->getView());
  140. $this->assertContains($this->element->getName(), $html);
  141. }
  142. public function testCaptchaHasIdAndInput()
  143. {
  144. $html = $this->element->render($this->getView());
  145. $expect = sprintf('type="hidden" name="%s\[id\]" value="%s"', $this->element->getName(), $this->captcha->getId());
  146. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  147. $expect = sprintf('type="text" name="%s\[input\]"', $this->element->getName());
  148. $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
  149. }
  150. public function testCaptchaHasImage()
  151. {
  152. $html = $this->element->render($this->getView());
  153. $id = $this->captcha->getId();
  154. $this->assertRegexp("|<img[^>]*?src=\"/images/captcha/$id.png\"|", $html, "Expected $id in HTML:\n" . $html);
  155. }
  156. public function testCaptchaHasAlt()
  157. {
  158. $html = $this->element->render($this->getView());
  159. $this->assertRegexp('|<img[^>]*? alt=""|', $html, "Expected alt= in HTML:\n" . $html);
  160. $this->captcha->setImgAlt("Test Image");
  161. $html = $this->element->render($this->getView());
  162. $this->assertRegexp('|<img[^>]*? alt="Test Image"|', $html, "Wrong alt in HTML:\n" . $html);
  163. }
  164. public function testCaptchaSetSuffix()
  165. {
  166. $this->captcha->setSuffix(".jpeg");
  167. $html = $this->element->render($this->getView());
  168. $this->assertContains(".jpeg", $html, $html);
  169. }
  170. public function testCaptchaSetImgURL()
  171. {
  172. $this->captcha->setImgURL("/some/other/URL/");
  173. $html = $this->element->render($this->getView());
  174. $this->assertContains("/some/other/URL/", $html, $html);
  175. }
  176. public function testCaptchaCreatesImage()
  177. {
  178. $this->element->render($this->getView());
  179. $this->assertTrue(file_exists($this->testDir."/".$this->captcha->getId().".png"));
  180. }
  181. public function testCaptchaSetExpiration()
  182. {
  183. $this->assertEquals($this->captcha->getExpiration(), 600);
  184. $this->captcha->setExpiration(3600);
  185. $this->assertEquals($this->captcha->getExpiration(), 3600);
  186. }
  187. public function testCaptchaImageCleanup()
  188. {
  189. $this->element->render($this->getView());
  190. $filename = $this->testDir."/".$this->captcha->getId().".png";
  191. $this->assertTrue(file_exists($filename));
  192. $this->captcha->setExpiration(1);
  193. $this->captcha->setGcFreq(1);
  194. sleep(2);
  195. $this->captcha->generate();
  196. clearstatcache();
  197. $this->assertFalse(file_exists($filename), "File $filename was found even after GC");
  198. }
  199. public function testGenerateReturnsId()
  200. {
  201. $id = $this->captcha->generate();
  202. $this->assertFalse(empty($id));
  203. $this->assertTrue(is_string($id));
  204. $this->id = $id;
  205. }
  206. public function testGetWordReturnsWord()
  207. {
  208. $this->captcha->generate();
  209. $word = $this->captcha->getWord();
  210. $this->assertFalse(empty($word));
  211. $this->assertTrue(is_string($word));
  212. $this->assertTrue(strlen($word) == 8);
  213. $this->word = $word;
  214. }
  215. public function testGetWordLength()
  216. {
  217. $this->captcha->setWordLen(4);
  218. $this->captcha->generate();
  219. $word = $this->captcha->getWord();
  220. $this->assertTrue(is_string($word));
  221. $this->assertTrue(strlen($word) == 4);
  222. $this->word = $word;
  223. }
  224. public function testAdapterElementName()
  225. {
  226. $this->assertEquals($this->captcha->getName(),
  227. $this->element->getName());
  228. }
  229. public function testGenerateIsRandomised()
  230. {
  231. $id1 = $this->captcha->generate();
  232. $word1 = $this->captcha->getWord();
  233. $id2 = $this->captcha->generate();
  234. $word2 = $this->captcha->getWord();
  235. $this->assertFalse(empty($id1));
  236. $this->assertFalse(empty($id2));
  237. $this->assertFalse($id1 == $id2);
  238. $this->assertFalse($word1 == $word2);
  239. }
  240. public function testRenderSetsValue()
  241. {
  242. $this->testCaptchaIsRendered();
  243. $this->assertEquals($this->captcha->getId(),
  244. $this->element->getValue());
  245. }
  246. public function testLabelIsNull()
  247. {
  248. $this->assertNull($this->element->getLabel());
  249. }
  250. public function testRenderInitializesSessionData()
  251. {
  252. $this->testCaptchaIsRendered();
  253. $session = $this->captcha->getSession();
  254. $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
  255. $this->assertEquals(1, $session->setExpirationHops);
  256. $this->assertEquals($this->captcha->getWord(), $session->word);
  257. }
  258. public function testWordValidates()
  259. {
  260. $this->testCaptchaIsRendered();
  261. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  262. $this->assertTrue($this->element->isValid("", $input));
  263. }
  264. public function testMissingNotValid()
  265. {
  266. $this->testCaptchaIsRendered();
  267. $this->assertFalse($this->element->isValid("", array()));
  268. $input = array($this->element->getName() => array("input" => "blah"));
  269. $this->assertFalse($this->element->isValid("", $input));
  270. }
  271. public function testWrongWordNotValid()
  272. {
  273. $this->testCaptchaIsRendered();
  274. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
  275. $this->assertFalse($this->element->isValid("", $input));
  276. }
  277. /**
  278. * @group ZF-3995
  279. */
  280. public function testIsValidShouldAllowPassingArrayValueWithNoContext()
  281. {
  282. $this->testCaptchaIsRendered();
  283. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  284. $this->assertTrue($this->element->isValid($input));
  285. }
  286. /**
  287. * @group ZF-3995
  288. */
  289. public function testIsValidShouldNotRequireValueToBeNestedArray()
  290. {
  291. $this->testCaptchaIsRendered();
  292. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  293. $this->assertTrue($this->element->isValid($input));
  294. }
  295. }
  296. class Zend_Captcha_ImageTest_SessionContainer
  297. {
  298. protected static $_word;
  299. public function __get($name)
  300. {
  301. if ('word' == $name) {
  302. return self::$_word;
  303. }
  304. return null;
  305. }
  306. public function __set($name, $value)
  307. {
  308. if ('word' == $name) {
  309. self::$_word = $value;
  310. } else {
  311. $this->$name = $value;
  312. }
  313. }
  314. public function __isset($name)
  315. {
  316. if (('word' == $name) && (null !== self::$_word)) {
  317. return true;
  318. }
  319. return false;
  320. }
  321. public function __call($method, $args)
  322. {
  323. switch ($method) {
  324. case 'setExpirationHops':
  325. case 'setExpirationSeconds':
  326. $this->$method = array_shift($args);
  327. break;
  328. default:
  329. }
  330. }
  331. }
  332. // Call Zend_Captcha_ImageTest::main() if this source file is executed directly.
  333. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ImageTest::main") {
  334. Zend_Captcha_ImageTest::main();
  335. }