PasswordableBehaviorTest.php 7.8 KB

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