ConfirmableBehaviorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\ORM\TableRegistry;
  4. use Tools\TestSuite\TestCase;
  5. //use Cake\Core\Configure;
  6. use Tools\Model\Behavior\ConfirmableBehavior;
  7. class ConfirmableBehaviorTest extends TestCase {
  8. public $ConfirmableBehavior;
  9. public $fixtures = array('plugin.Tools.SluggedArticles');
  10. public function setUp() {
  11. parent::setUp();
  12. }
  13. /**
  14. * ConfirmableBehaviorTest::testBasicValidation()
  15. *
  16. * @return void
  17. */
  18. public function testBasicValidation() {
  19. $this->Animals = TableRegistry::get('SluggedArticles');
  20. $this->Animals->addBehavior('Tools.Confirmable');
  21. $animal = $this->Animals->newEntity();
  22. $data = array(
  23. 'name' => 'FooBar',
  24. 'confirm' => '0'
  25. );
  26. $animal = $this->Animals->patchEntity($animal, $data);
  27. $this->assertNotEmpty($animal->errors());
  28. $this->assertSame(array('confirm' => array('notEmpty' => __d('tools', 'Please confirm the checkbox'))), $animal->errors());
  29. $data = array(
  30. 'name' => 'FooBar',
  31. 'confirm' => '1'
  32. );
  33. $animal = $this->Animals->patchEntity($animal, $data);
  34. $this->assertEmpty($animal->errors());
  35. }
  36. /**
  37. * ConfirmableBehaviorTest::testBasicValidation()
  38. *
  39. * @return void
  40. */
  41. public function testValidationThatHasBeenModifiedBefore() {
  42. $this->Animals = TableRegistry::get('SluggedArticles');
  43. /*
  44. $this->Animals->validator()->add('confirm', 'notEmpty', [
  45. 'rule' => function ($value, $context) {
  46. return !empty($value);
  47. },
  48. 'message' => __('Please select checkbox to continue.'),
  49. 'requirePresence' => true,
  50. 'allowEmpty' => false,
  51. 'last' => true,
  52. ]);
  53. $this->Animals->validator()->remove('confirm');
  54. */
  55. $this->Animals->addBehavior('Tools.Confirmable');
  56. $animal = $this->Animals->newEntity();
  57. $data = array(
  58. 'name' => 'FooBar',
  59. 'confirm' => '0'
  60. );
  61. $animal = $this->Animals->patchEntity($animal, $data);
  62. $this->assertNotEmpty($animal->errors());
  63. $this->assertSame(array('confirm' => array('notEmpty' => __d('tools', 'Please confirm the checkbox'))), $animal->errors());
  64. $data = array(
  65. 'name' => 'FooBar',
  66. 'confirm' => '1'
  67. );
  68. $animal = $this->Animals->patchEntity($animal, $data);
  69. $this->assertEmpty($animal->errors());
  70. }
  71. /**
  72. * ConfirmableBehaviorTest::testValidationFieldMissing()
  73. *
  74. * @return void
  75. */
  76. public function testValidationFieldMissing() {
  77. $this->Animals = TableRegistry::get('SluggedArticles');
  78. $this->Animals->addBehavior('Tools.Confirmable');
  79. $animal = $this->Animals->newEntity();
  80. $data = array(
  81. 'name' => 'FooBar'
  82. );
  83. $animal = $this->Animals->patchEntity($animal, $data);
  84. $this->assertSame(array('confirm' => array('This field is required')), $animal->errors());
  85. }
  86. }