/vendor/php-activerecord/php-activerecord/test/ValidationsTest.php

https://gitlab.com/bandana/Astro-Veda · PHP · 182 lines · 153 code · 28 blank · 1 comment · 2 complexity · 880e2769f9a74027d761b426d5206e99 MD5 · raw file

  1. <?php
  2. use ActiveRecord as AR;
  3. class BookValidations extends ActiveRecord\Model
  4. {
  5. static $table_name = 'books';
  6. static $alias_attribute = array('name_alias' => 'name', 'x' => 'secondary_author_id');
  7. static $validates_presence_of = array();
  8. static $validates_uniqueness_of = array();
  9. static $custom_validator_error_msg = 'failed custom validation';
  10. // fired for every validation - but only used for custom validation test
  11. public function validate()
  12. {
  13. if ($this->name == 'test_custom_validation')
  14. $this->errors->add('name', self::$custom_validator_error_msg);
  15. }
  16. }
  17. class ValuestoreValidations extends ActiveRecord\Model
  18. {
  19. static $table_name = 'valuestore';
  20. static $validates_uniqueness_of = array();
  21. }
  22. class ValidationsTest extends DatabaseTest
  23. {
  24. public function set_up($connection_name=null)
  25. {
  26. parent::set_up($connection_name);
  27. BookValidations::$validates_presence_of[0] = 'name';
  28. BookValidations::$validates_uniqueness_of[0] = 'name';
  29. ValuestoreValidations::$validates_uniqueness_of[0] = 'key';
  30. }
  31. public function test_is_valid_invokes_validations()
  32. {
  33. $book = new Book;
  34. $this->assert_true(empty($book->errors));
  35. $book->is_valid();
  36. $this->assert_false(empty($book->errors));
  37. }
  38. public function test_is_valid_returns_true_if_no_validations_exist()
  39. {
  40. $book = new Book;
  41. $this->assert_true($book->is_valid());
  42. }
  43. public function test_is_valid_returns_false_if_failed_validations()
  44. {
  45. $book = new BookValidations;
  46. $this->assert_false($book->is_valid());
  47. }
  48. public function test_is_invalid()
  49. {
  50. $book = new Book();
  51. $this->assert_false($book->is_invalid());
  52. }
  53. public function test_is_invalid_is_true()
  54. {
  55. $book = new BookValidations();
  56. $this->assert_true($book->is_invalid());
  57. }
  58. public function test_is_iterable()
  59. {
  60. $book = new BookValidations();
  61. $book->is_valid();
  62. foreach ($book->errors as $name => $message)
  63. $this->assert_equals("Name can't be blank",$message);
  64. }
  65. public function test_full_messages()
  66. {
  67. $book = new BookValidations();
  68. $book->is_valid();
  69. $this->assert_equals(array("Name can't be blank"),array_values($book->errors->full_messages(array('hash' => true))));
  70. }
  71. public function test_to_array()
  72. {
  73. $book = new BookValidations();
  74. $book->is_valid();
  75. $this->assert_equals(array("name" => array("Name can't be blank")), $book->errors->to_array());
  76. }
  77. public function test_toString()
  78. {
  79. $book = new BookValidations();
  80. $book->is_valid();
  81. $book->errors->add('secondary_author_id', "is invalid");
  82. $this->assert_equals("Name can't be blank\nSecondary author id is invalid", (string) $book->errors);
  83. }
  84. public function test_validates_uniqueness_of()
  85. {
  86. BookValidations::create(array('name' => 'bob'));
  87. $book = BookValidations::create(array('name' => 'bob'));
  88. $this->assert_equals(array("Name must be unique"),$book->errors->full_messages());
  89. $this->assert_equals(1,BookValidations::count(array('conditions' => "name='bob'")));
  90. }
  91. public function test_validates_uniqueness_of_excludes_self()
  92. {
  93. $book = BookValidations::first();
  94. $this->assert_equals(true,$book->is_valid());
  95. }
  96. public function test_validates_uniqueness_of_with_multiple_fields()
  97. {
  98. BookValidations::$validates_uniqueness_of[0] = array(array('name','special'));
  99. $book1 = BookValidations::first();
  100. $book2 = new BookValidations(array('name' => $book1->name, 'special' => $book1->special+1));
  101. $this->assert_true($book2->is_valid());
  102. }
  103. public function test_validates_uniqueness_of_with_multiple_fields_is_not_unique()
  104. {
  105. BookValidations::$validates_uniqueness_of[0] = array(array('name','special'));
  106. $book1 = BookValidations::first();
  107. $book2 = new BookValidations(array('name' => $book1->name, 'special' => $book1->special));
  108. $this->assert_false($book2->is_valid());
  109. $this->assert_equals(array('Name and special must be unique'),$book2->errors->full_messages());
  110. }
  111. public function test_validates_uniqueness_of_works_with_alias_attribute()
  112. {
  113. BookValidations::$validates_uniqueness_of[0] = array(array('name_alias','x'));
  114. $book = BookValidations::create(array('name_alias' => 'Another Book', 'x' => 2));
  115. $this->assert_false($book->is_valid());
  116. $this->assert_equals(array('Name alias and x must be unique'), $book->errors->full_messages());
  117. }
  118. public function test_validates_uniqueness_of_works_with_mysql_reserved_word_as_column_name()
  119. {
  120. ValuestoreValidations::create(array('key' => 'GA_KEY', 'value' => 'UA-1234567-1'));
  121. $valuestore = ValuestoreValidations::create(array('key' => 'GA_KEY', 'value' => 'UA-1234567-2'));
  122. $this->assert_equals(array("Key must be unique"),$valuestore->errors->full_messages());
  123. $this->assert_equals(1,ValuestoreValidations::count(array('conditions' => "`key`='GA_KEY'")));
  124. }
  125. public function test_get_validation_rules()
  126. {
  127. $validators = BookValidations::first()->get_validation_rules();
  128. $this->assert_true(in_array(array('validator' => 'validates_presence_of'),$validators['name']));
  129. }
  130. public function test_model_is_nulled_out_to_prevent_memory_leak()
  131. {
  132. $book = new BookValidations();
  133. $book->is_valid();
  134. $this->assert_true(strpos(serialize($book->errors),'model";N;') !== false);
  135. }
  136. public function test_validations_takes_strings()
  137. {
  138. BookValidations::$validates_presence_of = array('numeric_test', array('special'), 'name');
  139. $book = new BookValidations(array('numeric_test' => 1, 'special' => 1));
  140. $this->assert_false($book->is_valid());
  141. }
  142. public function test_gh131_custom_validation()
  143. {
  144. $book = new BookValidations(array('name' => 'test_custom_validation'));
  145. $book->save();
  146. $this->assert_true($book->errors->is_invalid('name'));
  147. $this->assert_equals(BookValidations::$custom_validator_error_msg, $book->errors->on('name'));
  148. }
  149. };
  150. ?>