CakeFieldTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * CakeFieldTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model.Validator
  16. * @since CakePHP(tm) v 2.2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(dirname(__FILE__)) . DS . 'ModelTestBase.php';
  20. /**
  21. * CakeFieldTest
  22. *
  23. * @package Cake.Test.Case.Model.Validator
  24. */
  25. class CakeFieldTest extends BaseModelTest {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $this->Article = new Article();
  34. $this->Article->set(array('title' => '', 'body' => 'no title'));
  35. $this->Validator = new ModelValidator($this->Article);
  36. $this->Validator->getData();
  37. }
  38. /**
  39. * testConstruct method
  40. *
  41. * @return void
  42. */
  43. public function testConstruct() {
  44. $Field = new CakeField($this->Validator, 'title', 'notEmpty');
  45. $this->assertEquals(array('title' => '', 'body' => 'no title'), $Field->data);
  46. $this->assertEquals('title', $Field->field);
  47. $this->assertEquals(array('notEmpty'), $Field->ruleSet);
  48. }
  49. /**
  50. * testValidate method
  51. *
  52. * @return void
  53. */
  54. public function testValidate() {
  55. $Field = new CakeField($this->Validator, 'title', 'notEmpty');
  56. $result = $Field->validate();
  57. $this->assertFalse($result);
  58. $Field = new CakeField($this->Validator, 'body', 'notEmpty');
  59. $result = $Field->validate();
  60. $this->assertTrue($result);
  61. $Field = new CakeField($this->Validator, 'nothere', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
  62. $result = $Field->validate();
  63. $this->assertFalse($result);
  64. }
  65. /**
  66. * testGetRule method
  67. *
  68. * @return void
  69. */
  70. public function testGetRule() {
  71. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  72. $Field = new CakeField($this->Validator, 'title', $rules);
  73. $result = $Field->getRule('notEmpty');
  74. $this->assertInstanceOf('CakeRule', $result);
  75. $this->assertEquals('notEmpty', $result->rule);
  76. $this->assertEquals(null, $result->required);
  77. $this->assertEquals(false, $result->allowEmpty);
  78. $this->assertEquals(null, $result->on);
  79. $this->assertEquals(true, $result->last);
  80. $this->assertEquals('Can not be empty', $result->message);
  81. $this->assertEquals(array('title' => '', 'body' => 'no title'), $result->data);
  82. }
  83. /**
  84. * testGetRules method
  85. *
  86. * @return void
  87. */
  88. public function testGetRules() {
  89. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  90. $Field = new CakeField($this->Validator, 'title', $rules);
  91. $result = $Field->getRules();
  92. $this->assertEquals(array('notEmpty'), array_keys($result));
  93. $this->assertInstanceOf('CakeRule', $result['notEmpty']);
  94. }
  95. /**
  96. * testSetRule method
  97. *
  98. * @return void
  99. */
  100. public function testSetRule() {
  101. $rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  102. $Field = new CakeField($this->Validator, 'title', $rules);
  103. $Rule = new CakeRule($Field, $rules['notEmpty'], 'notEmpty');
  104. $this->assertEquals($Rule, $Field->getRule('notEmpty'));
  105. $rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
  106. $Rule = new CakeRule($Field, $rules['validEmail'], 'validEmail');
  107. $Field->setRule('validEmail', $Rule);
  108. $result = $Field->getRules();
  109. $this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
  110. $rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
  111. $Rule = new CakeRule($Field, $rules['validEmail'], 'validEmail');
  112. $Field->setRule('validEmail', $Rule);
  113. $result = $Field->getRules();
  114. $this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
  115. $result = $Field->getRule('validEmail');
  116. $this->assertInstanceOf('CakeRule', $result);
  117. $this->assertEquals('email', $result->rule);
  118. $this->assertEquals(null, $result->required);
  119. $this->assertEquals(false, $result->allowEmpty);
  120. $this->assertEquals(null, $result->on);
  121. $this->assertEquals(true, $result->last);
  122. $this->assertEquals('Other message', $result->message);
  123. $this->assertEquals(array('title' => '', 'body' => 'no title'), $result->data);
  124. }
  125. /**
  126. * testSetRules method
  127. *
  128. * @return void
  129. */
  130. public function testSetRules() {
  131. $rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  132. $Field = new CakeField($this->Validator, 'title', $rule);
  133. $RuleEmpty = new CakeRule($Field, $rule['notEmpty'], 'notEmpty');
  134. $rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
  135. $RuleEmail = new CakeRule($Field, $rule['validEmail'], 'validEmail');
  136. $rules = array('validEmail' => $RuleEmail);
  137. $Field->setRules($rules, false);
  138. $result = $Field->getRules();
  139. $this->assertEquals(array('validEmail'), array_keys($result));
  140. $rules = array('notEmpty' => $RuleEmpty);
  141. $Field->setRules($rules, true);
  142. $result = $Field->getRules();
  143. $this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
  144. }
  145. /**
  146. * testGetValidator method
  147. *
  148. * @return void
  149. */
  150. public function testGetValidator() {
  151. $rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
  152. $Field = new CakeField($this->Validator, 'title', $rule);
  153. $result = $Field->getValidator();
  154. $this->assertInstanceOf('ModelValidator', $result);
  155. }
  156. }