ConfirmableBehaviorTest.php 2.6 KB

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