CaptchaBehaviorTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. App::uses('CaptchaBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class CaptchaBehaviorTest extends MyCakeTestCase {
  5. public $fixtures = [
  6. 'core.comment'
  7. ];
  8. public $Comment;
  9. public function setUp() {
  10. parent::setUp();
  11. $this->Comment = ClassRegistry::init('Comment');
  12. $this->Comment->Behaviors->load('Tools.Captcha', []);
  13. }
  14. public function tearDown() {
  15. parent::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 = ['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 = ['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(['captcha' => 2, 'captcha_time' => time() - DAY, ''], CaptchaLib::$defaults);
  42. $data = ['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(['captcha' => 2, 'captcha_time' => time() + DAY, ''], CaptchaLib::$defaults);
  49. $data = ['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(['captcha' => 2, 'captcha_time' => time() - 10, ''], CaptchaLib::$defaults);
  59. $data = ['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. }