ConfirmableBehaviorTest.php 2.7 KB

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