ConfirmableBehaviorTest.php 2.7 KB

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