CaptchaBehaviorTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. App::uses('CaptchaBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.Lib');
  4. class CaptchaBehaviorTest extends MyCakeTestCase {
  5. public $fixtures = array(
  6. 'core.comment'
  7. );
  8. public $Comment;
  9. public function startTest() {
  10. }
  11. public function setUp() {
  12. $this->Comment = ClassRegistry::init('Comment');
  13. $this->Comment->Behaviors->attach('Tools.Captcha', array());
  14. }
  15. public function tearDown() {
  16. unset($this->Comment);
  17. }
  18. /**
  19. * test if nothing has been
  20. */
  21. public function testEmpty() {
  22. $is = $this->Comment->validates();
  23. debug($this->Comment->invalidFields());
  24. $this->assertFalse($is);
  25. }
  26. public function testWrong() {
  27. $data = array('title'=>'xyz', 'captcha'=>'x', 'captcha_hash'=>'y', 'captcha_time'=>'123');
  28. $this->Comment->set($data);
  29. $is = $this->Comment->validates();
  30. debug($this->Comment->invalidFields());
  31. $this->assertFalse($is);
  32. $data = array('title'=>'xyz', 'captcha'=>'x', 'homepage'=>'', 'captcha_hash'=>'y', 'captcha_time'=>'123');
  33. $this->Comment->set($data);
  34. $is = $this->Comment->validates();
  35. debug($this->Comment->invalidFields());
  36. $this->assertFalse($is);
  37. }
  38. public function testCorrect() {
  39. App::import('Lib', 'Tools.CaptchaLib');
  40. $Captcha = new CaptchaLib();
  41. $hash = $Captcha->buildHash(array('captcha'=>2, 'captcha_time'=>time()-10, ''), CaptchaLib::$defaults);
  42. $data = array('title'=>'xyz', 'captcha'=>'2', 'homepage'=>'', 'captcha_hash'=>$hash, 'captcha_time'=>time()-10);
  43. $this->Comment->set($data);
  44. $is = $this->Comment->validates();
  45. debug($this->Comment->invalidFields());
  46. $this->assertTrue($is);
  47. }
  48. //TODO
  49. }