ImageTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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-2010 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-2010 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_ImageTest extends PHPUnit_Framework_TestCase
  38. {
  39. protected $_tmpDir;
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @return void
  44. */
  45. public static function main()
  46. {
  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. /**
  200. * @group ZF-10006
  201. */
  202. public function testCaptchaImageCleanupOnlyCaptchaFilesIdentifiedByTheirSuffix()
  203. {
  204. $this->element->render($this->getView());
  205. $filename = $this->testDir."/".$this->captcha->getId().".png";
  206. $this->assertTrue(file_exists($filename));
  207. //Create other cache file
  208. $otherFile = $this->testDir . "/zf10006.cache";
  209. file_put_contents($otherFile, '');
  210. $this->assertTrue(file_exists($otherFile));
  211. $this->captcha->setExpiration(1);
  212. $this->captcha->setGcFreq(1);
  213. sleep(2);
  214. $this->captcha->generate();
  215. clearstatcache();
  216. $this->assertFalse(file_exists($filename), "File $filename was found even after GC");
  217. $this->assertTrue(file_exists($otherFile), "File $otherFile was not found after GC");
  218. }
  219. public function testGenerateReturnsId()
  220. {
  221. $id = $this->captcha->generate();
  222. $this->assertFalse(empty($id));
  223. $this->assertTrue(is_string($id));
  224. $this->id = $id;
  225. }
  226. public function testGetWordReturnsWord()
  227. {
  228. $this->captcha->generate();
  229. $word = $this->captcha->getWord();
  230. $this->assertFalse(empty($word));
  231. $this->assertTrue(is_string($word));
  232. $this->assertTrue(strlen($word) == 8);
  233. $this->word = $word;
  234. }
  235. public function testGetWordLength()
  236. {
  237. $this->captcha->setWordLen(4);
  238. $this->captcha->generate();
  239. $word = $this->captcha->getWord();
  240. $this->assertTrue(is_string($word));
  241. $this->assertTrue(strlen($word) == 4);
  242. $this->word = $word;
  243. }
  244. public function testAdapterElementName()
  245. {
  246. $this->assertEquals($this->captcha->getName(),
  247. $this->element->getName());
  248. }
  249. public function testGenerateIsRandomised()
  250. {
  251. $id1 = $this->captcha->generate();
  252. $word1 = $this->captcha->getWord();
  253. $id2 = $this->captcha->generate();
  254. $word2 = $this->captcha->getWord();
  255. $this->assertFalse(empty($id1));
  256. $this->assertFalse(empty($id2));
  257. $this->assertFalse($id1 == $id2);
  258. $this->assertFalse($word1 == $word2);
  259. }
  260. public function testRenderSetsValue()
  261. {
  262. $this->testCaptchaIsRendered();
  263. $this->assertEquals($this->captcha->getId(),
  264. $this->element->getValue());
  265. }
  266. public function testLabelIsNull()
  267. {
  268. $this->assertNull($this->element->getLabel());
  269. }
  270. public function testRenderInitializesSessionData()
  271. {
  272. $this->testCaptchaIsRendered();
  273. $session = $this->captcha->getSession();
  274. $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
  275. $this->assertEquals(1, $session->setExpirationHops);
  276. $this->assertEquals($this->captcha->getWord(), $session->word);
  277. }
  278. public function testWordValidates()
  279. {
  280. $this->testCaptchaIsRendered();
  281. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  282. $this->assertTrue($this->element->isValid("", $input));
  283. }
  284. public function testMissingNotValid()
  285. {
  286. $this->testCaptchaIsRendered();
  287. $this->assertFalse($this->element->isValid("", array()));
  288. $input = array($this->element->getName() => array("input" => "blah"));
  289. $this->assertFalse($this->element->isValid("", $input));
  290. }
  291. public function testWrongWordNotValid()
  292. {
  293. $this->testCaptchaIsRendered();
  294. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
  295. $this->assertFalse($this->element->isValid("", $input));
  296. }
  297. /**
  298. * @group ZF-3995
  299. */
  300. public function testIsValidShouldAllowPassingArrayValueWithNoContext()
  301. {
  302. $this->testCaptchaIsRendered();
  303. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  304. $this->assertTrue($this->element->isValid($input));
  305. }
  306. /**
  307. * @group ZF-3995
  308. */
  309. public function testIsValidShouldNotRequireValueToBeNestedArray()
  310. {
  311. $this->testCaptchaIsRendered();
  312. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  313. $this->assertTrue($this->element->isValid($input));
  314. }
  315. }
  316. class Zend_Captcha_ImageTest_SessionContainer
  317. {
  318. protected static $_word;
  319. public function __get($name)
  320. {
  321. if ('word' == $name) {
  322. return self::$_word;
  323. }
  324. return null;
  325. }
  326. public function __set($name, $value)
  327. {
  328. if ('word' == $name) {
  329. self::$_word = $value;
  330. } else {
  331. $this->$name = $value;
  332. }
  333. }
  334. public function __isset($name)
  335. {
  336. if (('word' == $name) && (null !== self::$_word)) {
  337. return true;
  338. }
  339. return false;
  340. }
  341. public function __call($method, $args)
  342. {
  343. switch ($method) {
  344. case 'setExpirationHops':
  345. case 'setExpirationSeconds':
  346. $this->$method = array_shift($args);
  347. break;
  348. default:
  349. }
  350. }
  351. }
  352. // Call Zend_Captcha_ImageTest::main() if this source file is executed directly.
  353. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ImageTest::main") {
  354. Zend_Captcha_ImageTest::main();
  355. }