From 9292390fbdc0c3ab484be08ed12baa754216e14f Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Thu, 27 Oct 2011 13:24:17 +0400 Subject: [PATCH] Captcha classes tested without CaptchaImageAction as it exit() the code --- tests/captcha/CaptchaTest.php | 104 +++++++++++++++++++++++++++++++++ tests/captcha/CaptchaValidatorTest.php | 43 ++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 tests/captcha/CaptchaTest.php create mode 100644 tests/captcha/CaptchaValidatorTest.php diff --git a/tests/captcha/CaptchaTest.php b/tests/captcha/CaptchaTest.php new file mode 100644 index 0000000..a7451ae --- /dev/null +++ b/tests/captcha/CaptchaTest.php @@ -0,0 +1,104 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-10-27 + * + * Unit tests for Captcha class + */ + +require_once dirname(__FILE__) . '/../../session/Session.php'; +require_once dirname(__FILE__) . '/../../captcha/Captcha.php'; + +class CaptchaTest extends PHPUnit_Framework_TestCase +{ + + private $captcha; + + protected $width = 200; + + protected $height = 70; + + + public function setUp() + { + $this->captcha = new Captcha(); + } + + /** + * @TODO: give a path to custom font as a param + */ + public function testGetImage() + { + $token = $this->captcha->getToken(); + $image = $this->captcha->getImage($token); + $this->assertNotNull($image); + + $my_image = $this->createBlankImage(); + $this->assertNotSame($image, $my_image); + } + + public function testGetImageWrongToken() + { + $token = $this->captcha->getToken(); + $image = $this->captcha->getImage('tony'); + $this->assertNotNull($image); + + $my_image = $this->createBlankImage(); + $this->assertSame($image, $my_image); + } + + public function testGetImageEmptyCode() + { + $token = $this->captcha->getToken(); + Session::set('_ccode', ''); + $image = $this->captcha->getImage($token); + $this->assertNotNull($image); + + $my_image = $this->createBlankImage(); + $this->assertSame($image, $my_image); + } + + public function testGetToken() + { + $token = $this->captcha->getToken(); + $code = Session::get('_ccode'); + $this->assertNotEmpty($token); + $this->assertNotEmpty($code); + $this->assertEquals(5, strlen($code)); + $this->assertEquals(Session::get('_ctoken'), $token); + $this->assertEquals(32, strlen($token)); + } + + public function testCheckCode() + { + $token = Session::get('_ctoken'); + $code = Session::get('_ccode'); + $this->assertFalse($this->captcha->checkCode($token . 'asd', $code)); + $this->assertEquals(Session::get('_ctoken'), $token); + $this->assertTrue($this->captcha->checkCode($token, $code)); + $this->assertNull(Session::get('_ctoken')); + } + + public function tearDown() + { + $sid = session_id(); + if (!empty($sid)) { + Session::destroy(); + } + } + + private function createBlankImage() + { + $image = imagecreatetruecolor($this->width, $this->height); + $background = imagecolorallocate($image, 255, 255, 255); + imagefilledrectangle($image, 0, 0, $this->width, $this->height, $background); + ob_start(); + imagejpeg($image, '', 100); + return ob_get_clean(); + } +} + \ No newline at end of file diff --git a/tests/captcha/CaptchaValidatorTest.php b/tests/captcha/CaptchaValidatorTest.php new file mode 100644 index 0000000..1a28172 --- /dev/null +++ b/tests/captcha/CaptchaValidatorTest.php @@ -0,0 +1,43 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-10-27 + * + * Unit tests for CaptchaValidator class + */ + +require_once dirname(__FILE__) . '/../../session/Session.php'; +require_once dirname(__FILE__) . '/../../validator/iValidator.php'; +require_once dirname(__FILE__) . '/../../validator/Validator.php'; +require_once dirname(__FILE__) . '/../../captcha/CaptchaValidator.php'; +require_once dirname(__FILE__) . '/../../captcha/Captcha.php'; + +class CaptchaValidatorTest extends PHPUnit_Framework_TestCase +{ + + /** + * @TODO: CaptchaValidator->isValid() - $value param not used + */ + public function testIsValid() + { + $captcha = new Captcha(); + $token = $captcha->getToken(); + $code = Session::get('_ccode'); + + $validator = new CaptchaValidator(); + $this->assertTrue($validator->isValid(null, array('ctoken' => $token, 'ccode' => $code))); + $this->assertFalse($validator->isValid(null, array('ctoken' => $token . 'asd', 'ccode' => $code))); + $this->assertEquals('Entered code wrong', $validator->getMessage()); + } + + public function testIsValidInvalid() + { + $validator = new CaptchaValidator(); + $this->assertFalse($validator->isValid(null, array())); + $this->assertEquals('Entered code wrong', $validator->getMessage()); + } +} \ No newline at end of file