ImageTest.php 13 KB

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