2
0

ImageTest.php 13 KB

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