CaptchaBehaviorTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 testInvalid() {
  39. App::uses('CaptchaLib', 'Tools.Lib');
  40. $Captcha = new CaptchaLib();
  41. $hash = $Captcha->buildHash(array('captcha'=>2, 'captcha_time'=>time()-DAY, ''), CaptchaLib::$defaults);
  42. $data = array('title'=>'xyz', 'captcha'=>'2', 'homepage'=>'', 'captcha_hash'=>$hash, 'captcha_time'=>time()-DAY);
  43. $this->Comment->set($data);
  44. $is = $this->Comment->validates();
  45. debug($this->Comment->invalidFields());
  46. //$this->assertTrue($is);
  47. $Captcha = new CaptchaLib();
  48. $hash = $Captcha->buildHash(array('captcha'=>2, 'captcha_time'=>time()+DAY, ''), CaptchaLib::$defaults);
  49. $data = array('title'=>'xyz', 'captcha'=>'2', 'homepage'=>'', 'captcha_hash'=>$hash, 'captcha_time'=>time()+DAY);
  50. $this->Comment->set($data);
  51. $is = $this->Comment->validates();
  52. debug($this->Comment->invalidFields());
  53. //$this->assertTrue($is);
  54. }
  55. public function testCorrect() {
  56. App::uses('CaptchaLib', 'Tools.Lib');
  57. $Captcha = new CaptchaLib();
  58. $hash = $Captcha->buildHash(array('captcha'=>2, 'captcha_time'=>time()-10, ''), CaptchaLib::$defaults);
  59. $data = array('title'=>'xyz', 'captcha'=>'2', 'homepage'=>'', 'captcha_hash'=>$hash, 'captcha_time'=>time()-10);
  60. $this->Comment->set($data);
  61. $is = $this->Comment->validates();
  62. //debug($this->Comment->invalidFields());
  63. $this->assertTrue($is);
  64. }
  65. //TODO
  66. }