ConfirmableBehaviorTest.php 2.6 KB

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