ChangePasswordBehaviorTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. parent::setUp();
  14. $this->User = ClassRegistry::init('User');
  15. }
  16. /**
  17. * Tear-down method. Resets environment state.
  18. */
  19. public function tearDown() {
  20. unset($this->User);
  21. parent::tearDown();
  22. ClassRegistry::flush();
  23. }
  24. public function testObject() {
  25. $this->User->Behaviors->attach('Tools.ChangePassword', array());
  26. $this->assertInstanceOf('ChangePasswordBehavior', $this->User->Behaviors->ChangePassword);
  27. $res = $this->User->Behaviors->attached('ChangePassword');
  28. $this->assertTrue($res);
  29. }
  30. /**
  31. * make sure validation is triggered correctly
  32. */
  33. public function testValidate() {
  34. $this->User->Behaviors->attach('Tools.ChangePassword', array());
  35. $this->User->create();
  36. $data = array(
  37. 'pwd' => '1234',
  38. );
  39. $this->User->set($data);
  40. $is = $this->User->save();
  41. //debug($this->User->validationErrors); ob_flush();
  42. $this->assertFalse($is);
  43. $this->assertEquals(array('pwd_repeat'), array_keys($this->User->validationErrors));
  44. $this->User->create();
  45. $data = array(
  46. 'pwd' => '1234',
  47. 'pwd_repeat' => '123456'
  48. );
  49. $this->User->set($data);
  50. $is = $this->User->save();
  51. //debug($this->User->validationErrors); ob_flush();
  52. $this->assertFalse($is);
  53. $this->assertEquals(array(__('valErrPwdNotMatch')), $this->User->validationErrors['pwd_repeat']);
  54. $this->User->create();
  55. $data = array(
  56. 'pwd' => '123456',
  57. 'pwd_repeat' => '123456'
  58. );
  59. $this->User->set($data);
  60. //debug($this->User->validate);
  61. $is = $this->User->validates();
  62. $this->assertTrue(!empty($is));
  63. }
  64. /**
  65. * test that confirm false does not require confirmation
  66. */
  67. public function testValidateNoConfirm() {
  68. $this->User->Behaviors->attach('Tools.ChangePassword', array('confirm'=>false));
  69. $this->User->create();
  70. $data = array(
  71. 'pwd' => '123456',
  72. );
  73. $this->User->set($data);
  74. $is = $this->User->save();
  75. debug($is); ob_flush();
  76. $this->assertTrue(!empty($is));
  77. }
  78. /**
  79. * validation and update process gets skipped if no values are entered
  80. */
  81. public function testValidateEmpty() {
  82. $this->User->Behaviors->attach('Tools.ChangePassword');
  83. $this->User->create();
  84. $data = array(
  85. 'pwd' => '',
  86. 'pwd_repeat' => ''
  87. );
  88. $this->User->set($data);
  89. $is = $this->User->save();
  90. debug($this->User->validationErrors); ob_flush();
  91. $this->assertFalse($is);
  92. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  93. $this->User->Behaviors->detach('ChangePassword');
  94. $this->User->validate = array();
  95. $this->User->Behaviors->attach('Tools.ChangePassword', array('current'=>true));
  96. $this->User->create();
  97. $data = array(
  98. 'id' => 123,
  99. 'pwd' => '',
  100. 'pwd_repeat' => '',
  101. 'pwd_current' => '123',
  102. );
  103. $this->User->set($data);
  104. $is = $this->User->save();
  105. //debug($this->User->validationErrors); ob_flush();
  106. $this->assertFalse($is);
  107. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  108. $this->tearDown();
  109. $this->setUp();
  110. $this->User->Behaviors->attach('Tools.ChangePassword', array('allowEmpty'=>true, 'current'=>true));
  111. $this->User->create();
  112. $data = array(
  113. 'user' => 'foo',
  114. 'pwd' => '',
  115. 'pwd_repeat' => '',
  116. 'pwd_current' => '',
  117. );
  118. $is = $this->User->save($data);
  119. debug($this->User->data);
  120. debug($this->User->validate);
  121. debug($this->User->validationErrors); ob_flush();
  122. $this->assertTrue(!empty($is));
  123. }
  124. /**
  125. * test aliases for field names
  126. */
  127. public function testDifferentFieldNames() {
  128. $this->User->Behaviors->attach('Tools.ChangePassword', array(
  129. 'formField' => 'passw',
  130. 'formFieldRepeat' => 'passw_repeat',
  131. 'formFieldCurrent' => 'passw_current',
  132. ));
  133. $this->User->create();
  134. $data = array(
  135. 'passw' => '123456',
  136. 'passw_repeat' => '123456'
  137. );
  138. $this->User->set($data);
  139. //debug($this->User->data);
  140. $is = $this->User->save();
  141. $this->assertTrue(!empty($is));
  142. }
  143. /**
  144. * assert that allowSame false does not allow storing the same password as previously entered
  145. */
  146. public function testNotSame() {
  147. $this->User->Behaviors->attach('Tools.ChangePassword', array(
  148. 'formField' => 'passw',
  149. 'formFieldRepeat' => 'passw_repeat',
  150. 'formFieldCurrent' => 'passw_current',
  151. 'allowSame' => false,
  152. 'current' => true
  153. ));
  154. $this->User->create();
  155. $data = array(
  156. 'id' => 5,
  157. 'passw_current' => 'some',
  158. 'passw' => 'some',
  159. 'passw_repeat' => 'some'
  160. );
  161. $this->User->set($data);
  162. $is = $this->User->save();
  163. debug($this->User->validationErrors);
  164. $this->assertFalse($is);
  165. $this->User->create();
  166. $data = array(
  167. 'id' => 5,
  168. 'passw_current' => 'some',
  169. 'passw' => 'new',
  170. 'passw_repeat' => 'new'
  171. );
  172. $this->User->set($data);
  173. debug($this->User->data);
  174. $is = $this->User->save();
  175. $this->assertTrue(!empty($is));
  176. }
  177. /**
  178. * needs faking of pwd check...
  179. */
  180. public function testValidateCurrent() {
  181. $this->assertFalse($this->User->Behaviors->attached('ChangePassword'));
  182. $this->User->create();
  183. $data = array('user'=>'xyz', 'password'=>Security::hash('some', null, true));
  184. $res = $this->User->save($data);
  185. $this->assertTrue(!empty($res));
  186. $uid = $this->User->id;
  187. # cake bug => attached behavior validation rules cannot be triggered
  188. //$this->tearDown();
  189. //$this->setUp();
  190. $this->User->Behaviors->attach('Tools.ChangePassword', array('current'=>true));
  191. $this->User->create();
  192. $data = array(
  193. 'id' => $uid,
  194. 'pwd' => '1234',
  195. 'pwd_repeat' => '123456',
  196. //'pwd_current' => '',
  197. );
  198. $this->User->set($data);
  199. $this->assertTrue($this->User->Behaviors->attached('ChangePassword'));
  200. debug($this->User->validate); ob_flush();
  201. $is = $this->User->save();
  202. //debug($this->User->validationErrors); ob_flush();
  203. $this->assertFalse($is);
  204. $this->User->create();
  205. $data = array(
  206. 'id' => $uid,
  207. 'pwd_current' => 'somex',
  208. 'pwd' => '123456',
  209. 'pwd_repeat' => '123456'
  210. );
  211. $this->User->set($data);
  212. //debug($this->User->validationErrors); ob_flush();
  213. $is = $this->User->save();
  214. $this->assertFalse($is);
  215. $this->User->create();
  216. $data = array(
  217. 'id' => $uid,
  218. 'pwd_current' => 'some',
  219. 'pwd' => '123456',
  220. 'pwd_repeat' => '123456'
  221. );
  222. $this->User->set($data);
  223. //debug($this->User->validationErrors); ob_flush();
  224. $is = $this->User->save();
  225. $this->assertTrue(!empty($is));
  226. }
  227. }
  228. /**
  229. * FAKER!
  230. * 2011-11-03 ms
  231. */
  232. class AuthComponent {
  233. public function identify($request, $response) {
  234. $user = $request->data['User'];
  235. if ($user['id'] == '5' && $user['password'] == 'some') {
  236. return true;
  237. }
  238. return false;
  239. }
  240. }