You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.7 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-27
  8. *
  9. * Unit tests for Captcha class
  10. */
  11. require_once dirname(__FILE__) . '/../../session/Session.php';
  12. require_once dirname(__FILE__) . '/../../captcha/Captcha.php';
  13. class CaptchaTest extends PHPUnit_Framework_TestCase
  14. {
  15. private $captcha;
  16. protected $width = 200;
  17. protected $height = 70;
  18. public function setUp()
  19. {
  20. $this->captcha = new Captcha();
  21. }
  22. /**
  23. * @TODO: give a path to custom font as a param
  24. */
  25. public function testGetImage()
  26. {
  27. $token = $this->captcha->getToken();
  28. $image = $this->captcha->getImage($token);
  29. $this->assertNotNull($image);
  30. $my_image = $this->createBlankImage();
  31. $this->assertNotSame($image, $my_image);
  32. }
  33. public function testGetImageWrongToken()
  34. {
  35. $token = $this->captcha->getToken();
  36. $image = $this->captcha->getImage('tony');
  37. $this->assertNotNull($image);
  38. $my_image = $this->createBlankImage();
  39. $this->assertSame($image, $my_image);
  40. }
  41. public function testGetImageEmptyCode()
  42. {
  43. $token = $this->captcha->getToken();
  44. Session::set('_ccode', '');
  45. $image = $this->captcha->getImage($token);
  46. $this->assertNotNull($image);
  47. $my_image = $this->createBlankImage();
  48. $this->assertSame($image, $my_image);
  49. }
  50. public function testGetToken()
  51. {
  52. $token = $this->captcha->getToken();
  53. $code = Session::get('_ccode');
  54. $this->assertNotEmpty($token);
  55. $this->assertNotEmpty($code);
  56. $this->assertSame(5, strlen($code));
  57. $this->assertSame(Session::get('_ctoken'), $token);
  58. $this->assertSame(32, strlen($token));
  59. }
  60. public function testCheckCode()
  61. {
  62. $token = Session::get('_ctoken');
  63. $code = Session::get('_ccode');
  64. $this->assertFalse($this->captcha->checkCode($token . 'asd', $code));
  65. $this->assertSame(Session::get('_ctoken'), $token);
  66. $this->assertTrue($this->captcha->checkCode($token, $code));
  67. $this->assertNull(Session::get('_ctoken'));
  68. }
  69. public function tearDown()
  70. {
  71. $sid = session_id();
  72. if (!empty($sid)) {
  73. Session::destroy();
  74. }
  75. }
  76. private function createBlankImage()
  77. {
  78. $image = imagecreatetruecolor($this->width, $this->height);
  79. $background = imagecolorallocate($image, 255, 255, 255);
  80. imagefilledrectangle($image, 0, 0, $this->width, $this->height, $background);
  81. ob_start();
  82. imagejpeg($image, '', 100);
  83. return ob_get_clean();
  84. }
  85. }