ConfirmableBehaviorTest.php 2.4 KB

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