ChangePasswordBehaviorTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. App::uses('Model', 'Model');
  3. App::uses('AppModel', 'Model');
  4. App::uses('ComponentCollection', 'Controller');
  5. class ChangePasswordBehaviorTest extends CakeTestCase {
  6. public $fixtures = array(
  7. 'core.user',
  8. );
  9. /**
  10. * setUp method
  11. */
  12. public function setUp() {
  13. $this->User = ClassRegistry::init('User');
  14. }
  15. /**
  16. * Tear-down method. Resets environment state.
  17. */
  18. public function tearDown() {
  19. $this->User->Behaviors->detach('ChangePassword');
  20. unset($this->User);
  21. }
  22. public function testObject() {
  23. $this->User->Behaviors->attach('Tools.ChangePassword', array());
  24. $this->assertIsA($this->User->Behaviors->ChangePassword, 'ChangePasswordBehavior');
  25. $res = $this->User->Behaviors->attached('ChangePassword');
  26. $this->assertTrue($res);
  27. }
  28. public function testValidate() {
  29. $this->User->Behaviors->attach('Tools.ChangePassword', array());
  30. $this->User->create();
  31. $data = array(
  32. 'pwd' => '1234',
  33. );
  34. $this->User->set($data);
  35. //debug($this->User->data);
  36. $is = $this->User->save();
  37. debug($this->User->validationErrors);
  38. //debug($this->User->validate);
  39. $this->assertFalse($is);
  40. $this->User->create();
  41. $data = array(
  42. 'pwd' => '1234',
  43. 'pwd_repeat' => '123456'
  44. );
  45. $this->User->set($data);
  46. //debug($this->User->data);
  47. $is = $this->User->save();
  48. debug($this->User->validationErrors);
  49. //debug($this->User->validate);
  50. $this->assertFalse($is);
  51. $this->User->create();
  52. $data = array(
  53. 'pwd' => '123456',
  54. 'pwd_repeat' => '123456'
  55. );
  56. $this->User->set($data);
  57. //debug($this->User->validate);
  58. $is = $this->User->validates();
  59. $this->assertTrue(!empty($is));
  60. }
  61. /**
  62. * needs faking of pwd check...
  63. */
  64. public function testValidateCurrent() {
  65. $this->User->create();
  66. $data = array('user'=>'xyz', 'password'=>Security::hash('some', null, true));
  67. $res = $this->User->save($data);
  68. $uid = $this->User->id;
  69. $this->User->Behaviors->attach('Tools.ChangePassword', array('current'=>true));
  70. $this->User->create();
  71. $data = array(
  72. 'id' => $uid,
  73. 'pwd' => '1234',
  74. 'pwd_repeat' => '123456'
  75. );
  76. $this->User->set($data);
  77. $is = $this->User->save();
  78. //debug($this->User->validationErrors); ob_flush();
  79. $this->assertFalse($is);
  80. $this->User->create();
  81. $data = array(
  82. 'id' => $uid,
  83. 'pwd_current' => 'somex',
  84. 'pwd' => '123456',
  85. 'pwd_repeat' => '123456'
  86. );
  87. $this->User->set($data);
  88. //debug($this->User->validationErrors); ob_flush();
  89. $is = $this->User->save();
  90. $this->assertFalse($is);
  91. $this->User->create();
  92. $data = array(
  93. 'id' => $uid,
  94. 'pwd_current' => 'some',
  95. 'pwd' => '123456',
  96. 'pwd_repeat' => '123456'
  97. );
  98. $this->User->set($data);
  99. //debug($this->User->validationErrors); ob_flush();
  100. $is = $this->User->save();
  101. $this->assertTrue(!empty($is));
  102. }
  103. public function testValidateNoConfirm() {
  104. $this->User->Behaviors->attach('Tools.ChangePassword', array('confirm'=>false));
  105. $this->User->create();
  106. $data = array(
  107. 'pwd' => '123456',
  108. );
  109. $this->User->set($data);
  110. $is = $this->User->save();
  111. debug($is);
  112. $this->assertTrue(!empty($is));
  113. }
  114. public function testValidateNonEmptyToEmpty() {
  115. $this->User->Behaviors->attach('Tools.ChangePassword', array('nonEmptyToEmpty'=>false));
  116. $this->User->create();
  117. $data = array(
  118. 'pwd' => '',
  119. 'pwd_repeat' => ''
  120. );
  121. $this->User->set($data);
  122. $is = $this->User->save();
  123. debug($this->User->validationErrors);
  124. debug($is);
  125. $this->assertFalse($is);
  126. //TODO:
  127. $this->User->Behaviors->detach('ChangePassword');
  128. $this->User->Behaviors->attach('Tools.ChangePassword', array('nonEmptyToEmpty'=>true));
  129. $this->User->create();
  130. $data = array(
  131. 'pwd' => '',
  132. 'pwd_repeat' => ''
  133. );
  134. $this->User->set($data);
  135. //debug($this->User->data);
  136. $is = $this->User->save();
  137. debug($this->User->validationErrors);
  138. $this->assertFalse($is);
  139. }
  140. public function testDifferentFieldNames() {
  141. $this->User->Behaviors->attach('Tools.ChangePassword', array(
  142. 'formField' => 'passw',
  143. 'formFieldRepeat' => 'passw_repeat',
  144. 'formFieldCurrent' => 'passw_current',
  145. ));
  146. $this->User->create();
  147. $data = array(
  148. 'passw' => '123456',
  149. 'passw_repeat' => '123456'
  150. );
  151. $this->User->set($data);
  152. //debug($this->User->data);
  153. $is = $this->User->save();
  154. $this->assertTrue(!empty($is));
  155. }
  156. public function testNotSame() {
  157. $this->User->Behaviors->attach('Tools.ChangePassword', array(
  158. 'formField' => 'passw',
  159. 'formFieldRepeat' => 'passw_repeat',
  160. 'formFieldCurrent' => 'passw_current',
  161. 'allowSame' => false,
  162. 'current' => true
  163. ));
  164. $this->User->create();
  165. $data = array(
  166. 'id' => 5,
  167. 'passw_current' => 'some',
  168. 'passw' => 'some',
  169. 'passw_repeat' => 'some'
  170. );
  171. $this->User->set($data);
  172. $is = $this->User->save();
  173. debug($this->User->validationErrors);
  174. $this->assertFalse($is);
  175. $this->User->create();
  176. $data = array(
  177. 'id' => 5,
  178. 'passw_current' => 'some',
  179. 'passw' => 'new',
  180. 'passw_repeat' => 'new'
  181. );
  182. $this->User->set($data);
  183. debug($this->User->data);
  184. $is = $this->User->save();
  185. $this->assertTrue(!empty($is));
  186. }
  187. }
  188. /**
  189. * FAKER!
  190. * 2011-11-03 ms
  191. */
  192. class AuthComponent {
  193. public function identify($request, $response) {
  194. $user = $request->data['User'];
  195. if ($user['id'] == '5' && $user['password'] == 'some') {
  196. return true;
  197. }
  198. return false;
  199. }
  200. }