ConfirmableBehaviorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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->getErrors());
  35. $this->assertSame(['confirm' => ['notBlank' => __d('tools', 'Please confirm the checkbox')]], $animal->getErrors());
  36. $data = [
  37. 'name' => 'FooBar',
  38. 'confirm' => '1'
  39. ];
  40. $animal = $this->Articles->patchEntity($animal, $data);
  41. $this->assertEmpty($animal->getErrors());
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function testValidationThatHasBeenModifiedBefore() {
  47. $this->Articles = TableRegistry::get('SluggedArticles');
  48. /*
  49. $this->Articles->validator()->add('confirm', 'notBlank', [
  50. 'rule' => function ($value, $context) {
  51. return !empty($value);
  52. },
  53. 'message' => __('Please select checkbox to continue.'),
  54. 'requirePresence' => true,
  55. 'allowEmpty' => false,
  56. 'last' => true,
  57. ]);
  58. $this->Articles->validator()->remove('confirm');
  59. */
  60. $this->Articles->addBehavior('Tools.Confirmable');
  61. $animal = $this->Articles->newEntity();
  62. $data = [
  63. 'name' => 'FooBar',
  64. 'confirm' => '0'
  65. ];
  66. $animal = $this->Articles->patchEntity($animal, $data);
  67. $this->assertNotEmpty($animal->getErrors());
  68. $this->assertSame(['confirm' => ['notBlank' => __d('tools', 'Please confirm the checkbox')]], $animal->getErrors());
  69. $data = [
  70. 'name' => 'FooBar',
  71. 'confirm' => '1'
  72. ];
  73. $animal = $this->Articles->patchEntity($animal, $data);
  74. $this->assertEmpty($animal->getErrors());
  75. }
  76. /**
  77. * @return void
  78. */
  79. public function testValidationFieldMissing() {
  80. $this->Articles = TableRegistry::get('SluggedArticles');
  81. $this->Articles->addBehavior('Tools.Confirmable');
  82. $animal = $this->Articles->newEntity();
  83. $data = [
  84. 'name' => 'FooBar'
  85. ];
  86. $animal = $this->Articles->patchEntity($animal, $data);
  87. $this->assertSame(['confirm' => ['_required' => 'This field is required']], $animal->getErrors());
  88. }
  89. }