ChangePasswordBehaviorTest.php 5.5 KB

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