CaptchaBehaviorTest.php 2.4 KB

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