ConfirmableBehaviorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 array
  8. */
  9. public $fixtures = [
  10. 'plugin.Tools.SluggedArticles',
  11. ];
  12. /**
  13. * @var \Tools\Model\Behavior\ConfirmableBehavior
  14. */
  15. protected $ConfirmableBehavior;
  16. /**
  17. * @return void
  18. */
  19. public function setUp(): void {
  20. parent::setUp();
  21. }
  22. /**
  23. * ConfirmableBehaviorTest::testBasicValidation()
  24. *
  25. * @return void
  26. */
  27. public function testBasicValidation() {
  28. $this->Articles = TableRegistry::getTableLocator()->get('SluggedArticles');
  29. $this->Articles->addBehavior('Tools.Confirmable');
  30. $animal = $this->Articles->newEmptyEntity();
  31. $data = [
  32. 'name' => 'FooBar',
  33. 'confirm' => '0',
  34. ];
  35. $animal = $this->Articles->patchEntity($animal, $data);
  36. $this->assertNotEmpty($animal->getErrors());
  37. $this->assertSame(['confirm' => ['notBlank' => __d('tools', 'Please confirm the checkbox')]], $animal->getErrors());
  38. $data = [
  39. 'name' => 'FooBar',
  40. 'confirm' => '1',
  41. ];
  42. $animal = $this->Articles->patchEntity($animal, $data);
  43. $this->assertEmpty($animal->getErrors());
  44. }
  45. /**
  46. * @return void
  47. */
  48. public function testValidationThatHasBeenModifiedBefore() {
  49. $this->Articles = TableRegistry::getTableLocator()->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->newEmptyEntity();
  64. $data = [
  65. 'name' => 'FooBar',
  66. 'confirm' => '0',
  67. ];
  68. $animal = $this->Articles->patchEntity($animal, $data);
  69. $this->assertNotEmpty($animal->getErrors());
  70. $this->assertSame(['confirm' => ['notBlank' => __d('tools', 'Please confirm the checkbox')]], $animal->getErrors());
  71. $data = [
  72. 'name' => 'FooBar',
  73. 'confirm' => '1',
  74. ];
  75. $animal = $this->Articles->patchEntity($animal, $data);
  76. $this->assertEmpty($animal->getErrors());
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function testValidationFieldMissing() {
  82. $this->Articles = TableRegistry::getTableLocator()->get('SluggedArticles');
  83. $this->Articles->addBehavior('Tools.Confirmable');
  84. $animal = $this->Articles->newEmptyEntity();
  85. $data = [
  86. 'name' => 'FooBar',
  87. ];
  88. $animal = $this->Articles->patchEntity($animal, $data);
  89. $this->assertSame(['confirm' => ['_required' => 'This field is required']], $animal->getErrors());
  90. }
  91. }