PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ZendFramework-1.10.0/tests/Zend/Captcha/ImageTest.php

https://bitbucket.org/sebs/mosolar
PHP | 379 lines | 275 code | 43 blank | 61 comment | 30 complexity | f6dd2a659762a3a92593f81b2d1eb229 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, BSD-3-Clause, LGPL-2.0, MIT, GPL-2.0
  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: ImageTest.php 20096 2010-01-06 02:05:09Z bkarwin $
  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. public function testGenerateReturnsId()
  201. {
  202. $id = $this->captcha->generate();
  203. $this->assertFalse(empty($id));
  204. $this->assertTrue(is_string($id));
  205. $this->id = $id;
  206. }
  207. public function testGetWordReturnsWord()
  208. {
  209. $this->captcha->generate();
  210. $word = $this->captcha->getWord();
  211. $this->assertFalse(empty($word));
  212. $this->assertTrue(is_string($word));
  213. $this->assertTrue(strlen($word) == 8);
  214. $this->word = $word;
  215. }
  216. public function testGetWordLength()
  217. {
  218. $this->captcha->setWordLen(4);
  219. $this->captcha->generate();
  220. $word = $this->captcha->getWord();
  221. $this->assertTrue(is_string($word));
  222. $this->assertTrue(strlen($word) == 4);
  223. $this->word = $word;
  224. }
  225. public function testAdapterElementName()
  226. {
  227. $this->assertEquals($this->captcha->getName(),
  228. $this->element->getName());
  229. }
  230. public function testGenerateIsRandomised()
  231. {
  232. $id1 = $this->captcha->generate();
  233. $word1 = $this->captcha->getWord();
  234. $id2 = $this->captcha->generate();
  235. $word2 = $this->captcha->getWord();
  236. $this->assertFalse(empty($id1));
  237. $this->assertFalse(empty($id2));
  238. $this->assertFalse($id1 == $id2);
  239. $this->assertFalse($word1 == $word2);
  240. }
  241. public function testRenderSetsValue()
  242. {
  243. $this->testCaptchaIsRendered();
  244. $this->assertEquals($this->captcha->getId(),
  245. $this->element->getValue());
  246. }
  247. public function testLabelIsNull()
  248. {
  249. $this->assertNull($this->element->getLabel());
  250. }
  251. public function testRenderInitializesSessionData()
  252. {
  253. $this->testCaptchaIsRendered();
  254. $session = $this->captcha->getSession();
  255. $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
  256. $this->assertEquals(1, $session->setExpirationHops);
  257. $this->assertEquals($this->captcha->getWord(), $session->word);
  258. }
  259. public function testWordValidates()
  260. {
  261. $this->testCaptchaIsRendered();
  262. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  263. $this->assertTrue($this->element->isValid("", $input));
  264. }
  265. public function testMissingNotValid()
  266. {
  267. $this->testCaptchaIsRendered();
  268. $this->assertFalse($this->element->isValid("", array()));
  269. $input = array($this->element->getName() => array("input" => "blah"));
  270. $this->assertFalse($this->element->isValid("", $input));
  271. }
  272. public function testWrongWordNotValid()
  273. {
  274. $this->testCaptchaIsRendered();
  275. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
  276. $this->assertFalse($this->element->isValid("", $input));
  277. }
  278. /**
  279. * @group ZF-3995
  280. */
  281. public function testIsValidShouldAllowPassingArrayValueWithNoContext()
  282. {
  283. $this->testCaptchaIsRendered();
  284. $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
  285. $this->assertTrue($this->element->isValid($input));
  286. }
  287. /**
  288. * @group ZF-3995
  289. */
  290. public function testIsValidShouldNotRequireValueToBeNestedArray()
  291. {
  292. $this->testCaptchaIsRendered();
  293. $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
  294. $this->assertTrue($this->element->isValid($input));
  295. }
  296. }
  297. class Zend_Captcha_ImageTest_SessionContainer
  298. {
  299. protected static $_word;
  300. public function __get($name)
  301. {
  302. if ('word' == $name) {
  303. return self::$_word;
  304. }
  305. return null;
  306. }
  307. public function __set($name, $value)
  308. {
  309. if ('word' == $name) {
  310. self::$_word = $value;
  311. } else {
  312. $this->$name = $value;
  313. }
  314. }
  315. public function __isset($name)
  316. {
  317. if (('word' == $name) && (null !== self::$_word)) {
  318. return true;
  319. }
  320. return false;
  321. }
  322. public function __call($method, $args)
  323. {
  324. switch ($method) {
  325. case 'setExpirationHops':
  326. case 'setExpirationSeconds':
  327. $this->$method = array_shift($args);
  328. break;
  329. default:
  330. }
  331. }
  332. }
  333. // Call Zend_Captcha_ImageTest::main() if this source file is executed directly.
  334. if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ImageTest::main") {
  335. Zend_Captcha_ImageTest::main();
  336. }