ChangePasswordBehaviorTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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->invalidFields());
  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->invalidFields());
  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('username'=>'xyz', 'password'=>Security::hash('some', null, true));
  67. $res = $this->User->save($data);
  68. $uid = $this->User->id;
  69. debug($res);
  70. //App::import('Component', 'Tools.AuthExt');
  71. $this->User->Behaviors->attach('Tools.ChangePassword', array('current'=>true));
  72. $this->User->create();
  73. $data = array(
  74. 'id' => $uid,
  75. 'pwd' => '1234',
  76. 'pwd_repeat' => '123456'
  77. );
  78. $this->User->set($data);
  79. //debug($this->User->data);
  80. $is = $this->User->save();
  81. debug($this->User->invalidFields());
  82. //debug($this->User->validate);
  83. $this->assertFalse($is);
  84. $this->User->create();
  85. $data = array(
  86. 'id' => $uid,
  87. 'pwd_current' => 'somex',
  88. 'pwd' => '123456',
  89. 'pwd_repeat' => '123456'
  90. );
  91. $this->User->set($data);
  92. debug($this->User->invalidFields());
  93. $is = $this->User->save();
  94. $this->assertFalse($is);
  95. $this->User->create();
  96. $data = array(
  97. 'id' => $uid,
  98. 'pwd_current' => 'some',
  99. 'pwd' => '123456',
  100. 'pwd_repeat' => '123456'
  101. );
  102. $this->User->set($data);
  103. debug($this->User->invalidFields());
  104. $is = $this->User->save();
  105. $this->assertTrue(!empty($is));
  106. }
  107. public function testValidateNoConfirm() {
  108. $this->User->Behaviors->attach('Tools.ChangePassword', array('confirm'=>false));
  109. $this->User->create();
  110. $data = array(
  111. 'pwd' => '123456',
  112. );
  113. $this->User->set($data);
  114. $is = $this->User->save();
  115. debug($is);
  116. $this->assertTrue(!empty($is));
  117. }
  118. public function testValidateNonEmptyToEmpty() {
  119. $this->User->Behaviors->attach('Tools.ChangePassword', array('nonEmptyToEmpty'=>false));
  120. $this->User->create();
  121. $data = array(
  122. 'pwd' => '',
  123. 'pwd_repeat' => ''
  124. );
  125. $this->User->set($data);
  126. $is = $this->User->save();
  127. debug($this->User->invalidFields());
  128. debug($is);
  129. $this->assertFalse($is);
  130. //TODO:
  131. $this->User->Behaviors->detach('ChangePassword');
  132. $this->User->Behaviors->attach('Tools.ChangePassword', array('nonEmptyToEmpty'=>true));
  133. $this->User->create();
  134. $data = array(
  135. 'pwd' => '',
  136. 'pwd_repeat' => ''
  137. );
  138. $this->User->set($data);
  139. //debug($this->User->data);
  140. $is = $this->User->save();
  141. debug($this->User->invalidFields());
  142. $this->assertFalse($is);
  143. }
  144. public function testDifferentFieldNames() {
  145. $this->User->Behaviors->attach('Tools.ChangePassword', array(
  146. 'formField' => 'passw',
  147. 'formFieldRepeat' => 'passw_repeat',
  148. 'formFieldCurrent' => 'passw_current',
  149. ));
  150. $this->User->create();
  151. $data = array(
  152. 'passw' => '123456',
  153. 'passw_repeat' => '123456'
  154. );
  155. $this->User->set($data);
  156. //debug($this->User->data);
  157. $is = $this->User->save();
  158. $this->assertTrue(!empty($is));
  159. }
  160. public function testNotSame() {
  161. $this->User->Behaviors->attach('Tools.ChangePassword', array(
  162. 'formField' => 'passw',
  163. 'formFieldRepeat' => 'passw_repeat',
  164. 'formFieldCurrent' => 'passw_current',
  165. 'allowSame' => false,
  166. 'current' => true
  167. ));
  168. $this->User->create();
  169. $data = array(
  170. 'id' => 5,
  171. 'passw_current' => 'some',
  172. 'passw' => 'some',
  173. 'passw_repeat' => 'some'
  174. );
  175. $this->User->set($data);
  176. $is = $this->User->save();
  177. debug($this->User->invalidFields());
  178. $this->assertFalse($is);
  179. $this->User->create();
  180. $data = array(
  181. 'id' => 5,
  182. 'passw_current' => 'some',
  183. 'passw' => 'new',
  184. 'passw_repeat' => 'new'
  185. );
  186. $this->User->set($data);
  187. debug($this->User->data);
  188. $is = $this->User->save();
  189. $this->assertTrue(!empty($is));
  190. }
  191. }
  192. /**
  193. * FAKER!
  194. * 2011-11-03 ms
  195. */
  196. class AuthComponent {
  197. public function identify($request, $response) {
  198. $user = $request->data['User'];
  199. if ($user['id'] == '5' && $user['password'] == 'some') {
  200. return true;
  201. }
  202. return false;
  203. }
  204. }