ValidationIntegrationTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\Entity;
  17. use Cake\ORM\TableRegistry;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Tests the integration between the ORM and the validator
  21. */
  22. class ValidationIntegrationTest extends TestCase {
  23. /**
  24. * Fixtures to be loaded
  25. *
  26. * @var array
  27. */
  28. public $fixtures = ['core.article'];
  29. /**
  30. * Tear down
  31. *
  32. * @return void
  33. */
  34. public function tearDown() {
  35. parent::tearDown();
  36. TableRegistry::clear();
  37. }
  38. /**
  39. * Tests that Table::validateUnique handle validation params correctly
  40. *
  41. * @return void
  42. */
  43. public function testValidateUnique() {
  44. $articles = TableRegistry::get('articles');
  45. $articles->validator()->add('title', [
  46. 'unique' => [
  47. 'rule' => 'validateUnique',
  48. 'provider' => 'table',
  49. 'message' => 'Y U NO WRITE UNIQUE?'
  50. ]
  51. ]);
  52. $entity = new Entity(['title' => 'First Article', 'body' => 'Foo']);
  53. $this->assertFalse($articles->validate($entity));
  54. $this->assertEquals('Y U NO WRITE UNIQUE?', $entity->errors()['title']['unique']);
  55. $entity = new Entity(['title' => 'New Article', 'body' => 'Foo']);
  56. $this->assertTrue($articles->validate($entity));
  57. }
  58. /**
  59. * Tests that validateUnique can be scoped to another field in the provided data
  60. *
  61. * @return void
  62. */
  63. public function testValidateUniqueWithScope() {
  64. $articles = TableRegistry::get('articles');
  65. $articles->validator()->add('title', [
  66. 'unique' => [
  67. 'rule' => ['validateUnique', ['scope' => 'published']],
  68. 'provider' => 'table'
  69. ]
  70. ]);
  71. $entity = new Entity(['title' => 'First Article', 'published' => 'N']);
  72. $this->assertTrue($articles->validate($entity));
  73. $entity->published = 'Y';
  74. $this->assertFalse($articles->validate($entity));
  75. }
  76. /**
  77. * Tests that uniqueness validation excludes the same record when it exists
  78. *
  79. * @return void
  80. */
  81. public function testValidateUniqueUpdate() {
  82. $articles = TableRegistry::get('articles');
  83. $articles->validator()->add('title', [
  84. 'unique' => [
  85. 'rule' => 'validateUnique',
  86. 'provider' => 'table'
  87. ]
  88. ]);
  89. $entity = new Entity(['id' => 1, 'title' => 'First Article'], ['markNew' => false]);
  90. $this->assertTrue($articles->validate($entity));
  91. $entity = new Entity(['id' => 2, 'title' => 'First Article'], ['markNew' => false]);
  92. $this->assertFalse($articles->validate($entity));
  93. }
  94. }