ValidationsTest.php 5.6 KB

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