PasswordableBehaviorTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. <?php
  2. namespace Tools\Model\Behavior;
  3. use Tools\TestSuite\TestCase;
  4. use Cake\ORM\Behavior;
  5. use Cake\ORM\Entity;
  6. use Cake\ORM\Query;
  7. use Cake\ORM\Table;
  8. use Cake\Core\Configure;
  9. use Cake\Auth\DefaultPasswordHasher;
  10. use Cake\ORM\TableRegistry;
  11. use Cake\Utility\Security;
  12. use Cake\Routing\Router;
  13. use Cake\Network\Request;
  14. use Cake\Auth\PasswordHasherFactory;
  15. class PasswordableBehaviorTest extends TestCase {
  16. public $fixtures = array(
  17. 'plugin.tools.tools_users', 'plugin.tools.roles',
  18. );
  19. /**
  20. * SetUp method
  21. *
  22. * @return void
  23. */
  24. public function setUp() {
  25. parent::setUp();
  26. Configure::write('App.namespace', 'TestApp');
  27. Configure::delete('Passwordable');
  28. Configure::write('Passwordable.auth', 'AuthTest');
  29. $this->Users = TableRegistry::get('ToolsUsers');
  30. $this->hasher = PasswordHasherFactory::build('Default');
  31. $user = $this->Users->newEntity();
  32. $data = array(
  33. 'id' => '5',
  34. 'name' => 'admin',
  35. 'password' => $this->hasher->hash('somepwd'),
  36. 'role_id' => '1'
  37. );
  38. $this->Users->patchEntity($user, $data);
  39. $result = $this->Users->save($user);
  40. $this->assertTrue((bool)$result);
  41. Router::setRequestInfo(new Request());
  42. }
  43. public function tearDown() {
  44. TableRegistry::clear();
  45. parent::tearDown();
  46. }
  47. /**
  48. * Make sure validation is triggered correctly
  49. *
  50. * @return void
  51. */
  52. public function testValidate() {
  53. $this->Users->addBehavior('Tools.Passwordable', array());
  54. $user = $this->Users->newEntity();
  55. $data = array(
  56. 'pwd' => '123456',
  57. );
  58. $this->Users->patchEntity($user, $data);
  59. $is = $this->Users->save($user);
  60. //debug($user->errors());
  61. $this->assertFalse($is);
  62. $this->assertEquals(array('pwd_repeat'), array_keys($user->errors()));
  63. $user = $this->Users->newEntity();
  64. $data = array(
  65. 'pwd' => '1234ab',
  66. 'pwd_repeat' => '123456'
  67. );
  68. $this->Users->patchEntity($user, $data);
  69. $is = $this->Users->save($user);
  70. //debug($user->errors());
  71. $this->assertFalse($is);
  72. $this->assertEquals(array('validateIdentical' => __d('tools', 'valErrPwdNotMatch')), $user->errors()['pwd_repeat']);
  73. $user = $this->Users->newEntity();
  74. $data = array(
  75. 'pwd' => '123456',
  76. 'pwd_repeat' => '123456'
  77. );
  78. $this->Users->patchEntity($user, $data);
  79. //debug($this->Users->validate);
  80. $is = $this->Users->validate($user);
  81. $this->assertTrue(!empty($is));
  82. }
  83. /**
  84. * Test that confirm false does not require confirmation
  85. *
  86. * @return void
  87. */
  88. public function testValidateNoConfirm() {
  89. $this->Users->addBehavior('Tools.Passwordable', array('confirm' => false));
  90. $user = $this->Users->newEntity();
  91. $data = array(
  92. 'pwd' => '123456',
  93. );
  94. $this->Users->patchEntity($user, $data);
  95. $is = $this->Users->save($user);
  96. //debug($is);
  97. $this->assertTrue(!empty($is));
  98. }
  99. /**
  100. * Trigger validation and update process if no values are entered but are required
  101. *
  102. * @return void
  103. */
  104. public function testValidateRequired() {
  105. $this->Users->addBehavior('Tools.Passwordable');
  106. $user = $this->Users->newEntity();
  107. $data = array(
  108. 'pwd' => '',
  109. 'pwd_repeat' => ''
  110. );
  111. $this->Users->patchEntity($user, $data);
  112. $is = $this->Users->save($user);
  113. $this->assertFalse($is);
  114. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($user->errors()));
  115. }
  116. /**
  117. * Validation and update process gets skipped if no values are entered
  118. *
  119. * @return void
  120. */
  121. public function testValidateNotRequired() {
  122. $this->Users->addBehavior('Tools.Passwordable', array('require' => false));
  123. $user = $this->Users->newEntity();
  124. $data = array(
  125. 'name' => 'foo', // we need at least one field besides the password on CREATE
  126. 'pwd' => '',
  127. 'pwd_repeat' => ''
  128. );
  129. $this->Users->patchEntity($user, $data);
  130. $is = $this->Users->save($user);
  131. $this->assertTrue((bool)$is);
  132. $this->assertEquals(array('name', 'id'), $is->visibleProperties());
  133. $id = $user->id;
  134. $user = $this->Users->newEntity();
  135. $data = array(
  136. 'id' => $id,
  137. 'pwd' => '',
  138. 'pwd_repeat' => ''
  139. );
  140. $this->Users->patchEntity($user, $data);
  141. $is = $this->Users->save($user);
  142. $this->assertTrue((bool)$is);
  143. $this->assertEquals(array('id'), $is->visibleProperties());
  144. }
  145. /**
  146. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  147. *
  148. * @return void
  149. */
  150. public function testValidateEmptyWithCurrentPassword() {
  151. $this->Users->addBehavior('Tools.Passwordable', array('current' => true));
  152. $user = $this->Users->newEntity();
  153. $data = array(
  154. 'id' => '123',
  155. 'pwd' => '',
  156. 'pwd_repeat' => '',
  157. 'pwd_current' => '123456',
  158. );
  159. $this->Users->patchEntity($user, $data);
  160. $is = $this->Users->save($user);
  161. //debug($user->errors());
  162. $this->assertFalse($is);
  163. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($user->errors()));
  164. $this->tearDown();
  165. $this->setUp();
  166. $this->Users->removeBehavior('Passwordable');
  167. $this->Users->addBehavior('Tools.Passwordable', array('require' => false, 'current' => true));
  168. $user = $this->Users->newEntity();
  169. $data = array(
  170. 'name' => 'foo',
  171. 'pwd' => '',
  172. 'pwd_repeat' => '',
  173. 'pwd_current' => '',
  174. );
  175. $this->Users->patchEntity($user, $data);
  176. $is = $this->Users->save($user);
  177. $this->assertTrue(!empty($is));
  178. }
  179. /**
  180. * Test aliases for field names
  181. */
  182. public function testDifferentFieldNames() {
  183. $this->Users->addBehavior('Tools.Passwordable', array(
  184. 'formField' => 'passw',
  185. 'formFieldRepeat' => 'passw_repeat',
  186. 'formFieldCurrent' => 'passw_current',
  187. ));
  188. $user = $this->Users->newEntity();
  189. $data = array(
  190. 'passw' => '123456',
  191. 'passw_repeat' => '123456'
  192. );
  193. $this->Users->patchEntity($user, $data);
  194. //debug($this->Users->data);
  195. $is = $this->Users->save($user);
  196. $this->assertTrue(!empty($is));
  197. }
  198. /**
  199. * Assert that allowSame false does not allow storing the same password as previously entered
  200. */
  201. public function testNotSame() {
  202. $this->Users->addBehavior('Tools.Passwordable', array(
  203. 'formField' => 'passw',
  204. 'formFieldRepeat' => 'passw_repeat',
  205. 'formFieldCurrent' => 'passw_current',
  206. 'allowSame' => false,
  207. 'current' => true,
  208. ));
  209. $user = $this->Users->newEntity();
  210. $data = array(
  211. 'id' => '5',
  212. 'passw_current' => 'something',
  213. 'passw' => 'somepwd',
  214. 'passw_repeat' => 'somepwd'
  215. );
  216. $this->Users->patchEntity($user, $data);
  217. $is = $this->Users->save($user);
  218. //debug($user->errors());
  219. $this->assertFalse($is);
  220. $user = $this->Users->newEntity([], ['markNew' => false]);
  221. $data = array(
  222. 'id' => '5',
  223. 'passw_current' => 'somepwd',
  224. 'passw' => 'newpwd',
  225. 'passw_repeat' => 'newpwd'
  226. );
  227. $this->Users->patchEntity($user, $data);
  228. $is = $this->Users->save($user);
  229. $this->assertTrue(!empty($is));
  230. }
  231. /**
  232. * Assert that allowSame false does not allow storing the same password as previously entered
  233. */
  234. public function testNotSameWithoutCurrentField() {
  235. $this->Users->addBehavior('Tools.Passwordable', array(
  236. 'formField' => 'passw',
  237. 'formFieldRepeat' => 'passw_repeat',
  238. 'allowSame' => false,
  239. 'current' => false
  240. ));
  241. $user = $this->Users->newEntity();
  242. $data = array(
  243. 'passw' => 'somepwd',
  244. 'passw_repeat' => 'somepwd'
  245. );
  246. $this->Users->patchEntity($user, $data);
  247. $is = $this->Users->save($user);
  248. $this->assertTrue((bool)$is);
  249. $id = $is['id'];
  250. $user = $this->Users->newEntity([], ['markNew' => false]);
  251. $data = array(
  252. 'id' => $id,
  253. 'passw' => 'somepwd',
  254. 'passw_repeat' => 'somepwd'
  255. );
  256. $this->Users->patchEntity($user, $data);
  257. $is = $this->Users->save($user);
  258. $this->assertFalse((bool)$is);
  259. $user = $this->Users->newEntity([], ['markNew' => false]);
  260. $data = array(
  261. 'id' => $id,
  262. 'passw' => 'newpwd',
  263. 'passw_repeat' => 'newpwd'
  264. );
  265. $this->Users->patchEntity($user, $data);
  266. $is = $this->Users->save($user);
  267. $this->assertTrue((bool)$is);
  268. }
  269. /**
  270. * Assert that on edit it does not wrongly pass validation (require => false)
  271. */
  272. public function testRequireFalse() {
  273. $this->Users->addBehavior('Tools.Passwordable', array(
  274. 'formField' => 'passw',
  275. 'formFieldRepeat' => 'passw_repeat',
  276. 'require' => false
  277. ));
  278. $user = $this->Users->newEntity();
  279. $data = array(
  280. 'passw' => 'somepwd',
  281. 'passw_repeat' => 'somepwd'
  282. );
  283. $this->Users->patchEntity($user, $data);
  284. $is = $this->Users->save($user);
  285. $this->assertTrue((bool)$is);
  286. $id = $is['id'];
  287. $user = $this->Users->newEntity([], ['markNew' => false]);
  288. $data = array(
  289. 'id' => $id,
  290. 'passw' => 'somepwd2',
  291. 'passw_repeat' => ''
  292. );
  293. $this->Users->patchEntity($user, $data);
  294. $is = $this->Users->save($user);
  295. $this->assertFalse((bool)$is);
  296. //debug($user->errors());
  297. $user = $this->Users->newEntity([], ['markNew' => false]);
  298. $data = array(
  299. 'id' => $id,
  300. 'passw' => 'somepwd2',
  301. 'passw_repeat' => 'somepwd2'
  302. );
  303. $this->Users->patchEntity($user, $data);
  304. $is = $this->Users->save($user);
  305. $this->assertTrue((bool)$is);
  306. }
  307. /**
  308. * Needs faking of pwd check...
  309. */
  310. public function testValidateCurrent() {
  311. $this->assertFalse($this->Users->behaviors()->has('Passwordable'));
  312. $user = $this->Users->newEntity();
  313. $data = array(
  314. 'name' => 'xyz',
  315. 'password' => $this->hasher->hash('somepwd'));
  316. $this->Users->patchEntity($user, $data);
  317. $result = $this->Users->save($user);
  318. $this->assertTrue(!empty($result));
  319. $uid = (string)$user->id;
  320. $this->Users->removeBehavior('Passwordable');
  321. $this->Users->addBehavior('Tools.Passwordable', array('current' => true));
  322. $user = $this->Users->newEntity([], ['markNew' => false]);
  323. $data = array(
  324. 'id' => $uid,
  325. 'pwd' => '123456',
  326. 'pwd_repeat' => '12345678',
  327. //'pwd_current' => '',
  328. );
  329. $this->Users->patchEntity($user, $data);
  330. $this->assertTrue($this->Users->behaviors()->has('Passwordable'));
  331. $is = $this->Users->save($user);
  332. $this->assertFalse($is);
  333. $user = $this->Users->newEntity([], ['markNew' => false]);
  334. $data = array(
  335. 'id' => $uid,
  336. 'pwd_current' => 'somepwdx',
  337. 'pwd' => '123456',
  338. 'pwd_repeat' => '123456'
  339. );
  340. $this->Users->patchEntity($user, $data);
  341. $is = $this->Users->save($user);
  342. $this->assertFalse($is);
  343. $user = $this->Users->newEntity([], ['markNew' => false]);
  344. $data = array(
  345. 'id' => $uid,
  346. 'name' => 'Yeah',
  347. 'pwd_current' => 'somepwd',
  348. 'pwd' => '123456',
  349. 'pwd_repeat' => '123456'
  350. );
  351. $user->accessible('*', false); // Mark all properties as protected
  352. $user->accessible(array('id', 'pwd', 'pwd_repeat', 'pwd_current'), true);
  353. $this->Users->patchEntity($user, $data);
  354. // Test whitelist setting - only "password" needs to gets auto-added
  355. $options = array('validate' => true, 'fieldList' => array('id', 'pwd', 'pwd_repeat', 'pwd_current'));
  356. $is = $this->Users->save($user, $options);
  357. $this->assertTrue(!empty($is));
  358. //$this->skipIf(true, 'FIXME: whitelisting fieldList');
  359. $user = $this->Users->get($uid);
  360. // The password is updated, the name not
  361. $this->assertSame($is['password'], $user['password']);
  362. $this->assertSame('xyz', $user['name']);
  363. // Proof that we manually need to add pwd, pwd_repeat etc due to a bug in CakePHP<=2.4 allowing behaviors to only modify saving,
  364. // not validating of additional whitelist fields. Validation for those will be just skipped, no matter what the behavior tries
  365. // to set.
  366. $user = $this->Users->newEntity([], ['markNew' => false]);
  367. $data = array(
  368. 'id' => $uid,
  369. 'name' => 'Yeah',
  370. 'pwd_current' => '123', // Obviously wrong
  371. 'pwd' => 'some', // Too short
  372. 'pwd_repeat' => 'somex' // Don't match
  373. );
  374. $user->accessible('*', false); // Mark all properties as protected
  375. $user->accessible(array('id', 'name'), true);
  376. $this->Users->patchEntity($user, $data);
  377. // Test whitelist setting - only "password" gets auto-added, pwd, pwd_repeat etc need to be added manually
  378. // NOTE that I had to remove the code for adding those fields from the behavior (as it was not functional)
  379. // So of course, this won't work now as expected. But feel free to try to add them in the behavior. Results will be the same.
  380. $options = array('validate' => true, 'fieldList' => array('id', 'name'));
  381. $is = $this->Users->save($user, $options);
  382. // Validation errors triggered - as expected
  383. $this->assertFalse($is);
  384. $this->assertSame(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($user->errors()));
  385. }
  386. /**
  387. * Test cake2.4 passwordHasher feature
  388. *
  389. * @return void
  390. */
  391. public function testPasswordHasher() {
  392. $this->skipIf((float)Configure::version() < 2.4, 'Needs 2.4 and above');
  393. $this->Users->addBehavior('Tools.Passwordable', array(
  394. 'formField' => 'pwd',
  395. 'formFieldRepeat' => 'pwd_repeat',
  396. 'allowSame' => false,
  397. 'current' => false,
  398. 'passwordHasher' => 'Complex',
  399. ));
  400. $user = $this->Users->newEntity();
  401. $data = array(
  402. 'pwd' => 'somepwd',
  403. 'pwd_repeat' => 'somepwd'
  404. );
  405. $this->Users->patchEntity($user, $data);
  406. $result = $this->Users->save($user);
  407. $this->assertTrue((bool)$result);
  408. $uid = (string)$user->id;
  409. $this->Users->removeBehavior('Passwordable');
  410. $this->Users->addBehavior('Tools.Passwordable', array('current' => true));
  411. $user = $this->Users->newEntity();
  412. $data = array(
  413. 'id' => $uid,
  414. 'pwd' => '123456',
  415. 'pwd_repeat' => '12345678',
  416. //'pwd_current' => '',
  417. );
  418. $this->Users->patchEntity($user, $data);
  419. $this->assertTrue($this->Users->behaviors()->has('Passwordable'));
  420. $is = $this->Users->save($user);
  421. $this->assertFalse($is);
  422. $user = $this->Users->newEntity();
  423. $data = array(
  424. 'id' => $uid,
  425. 'pwd_current' => 'somepwdx',
  426. 'pwd' => '123456',
  427. 'pwd_repeat' => '123456'
  428. );
  429. $this->Users->patchEntity($user, $data);
  430. $is = $this->Users->save($user);
  431. $this->assertFalse($is);
  432. $user = $this->Users->newEntity();
  433. $data = array(
  434. 'id' => $uid,
  435. 'pwd_current' => 'somepwd',
  436. 'pwd' => '123456',
  437. 'pwd_repeat' => '123456'
  438. );
  439. $this->Users->patchEntity($user, $data);
  440. $is = $this->Users->save($user);
  441. $this->assertTrue(!empty($is));
  442. }
  443. /**
  444. * PasswordableBehaviorTest::testBlowfish()
  445. *
  446. * @return void
  447. */
  448. public function testBlowfish() {
  449. $this->Users->addBehavior('Tools.Passwordable', array(
  450. 'allowSame' => false,
  451. 'current' => false,
  452. 'authType' => 'Blowfish',
  453. ));
  454. $user = $this->Users->newEntity();
  455. $data = array(
  456. 'pwd' => 'somepwd',
  457. 'pwd_repeat' => 'somepwd'
  458. );
  459. $this->Users->patchEntity($user, $data);
  460. $result = $this->Users->save($user);
  461. $this->assertTrue((bool)$result);
  462. $uid = (string)$user->id;
  463. $this->Users->removeBehavior('Passwordable');
  464. $this->Users->addBehavior('Tools.Passwordable', array('current' => true));
  465. // Without the current password it will not continue
  466. $user = $this->Users->newEntity();
  467. $data = array(
  468. 'id' => $uid,
  469. 'pwd' => '123456',
  470. 'pwd_repeat' => '12345678',
  471. );
  472. $this->Users->patchEntity($user, $data);
  473. $this->assertTrue($this->Users->behaviors()->has('Passwordable'));
  474. $result = $this->Users->save($user);
  475. $this->assertFalse($result);
  476. // Without the correct current password it will not continue
  477. $user = $this->Users->newEntity();
  478. $data = array(
  479. 'id' => $uid,
  480. 'pwd_current' => 'somepwdx',
  481. 'pwd' => '123456',
  482. 'pwd_repeat' => '123456'
  483. );
  484. $this->Users->patchEntity($user, $data);
  485. $result = $this->Users->save($user);
  486. $this->assertFalse($result);
  487. // Now it will
  488. $user = $this->Users->newEntity();
  489. $data = array(
  490. 'id' => $uid,
  491. 'pwd_current' => 'somepwd',
  492. 'pwd' => '123456',
  493. 'pwd_repeat' => '123456'
  494. );
  495. $this->Users->patchEntity($user, $data);
  496. $result = $this->Users->save($user);
  497. $this->assertTrue((bool)$result);
  498. }
  499. /**
  500. * Tests needsPasswordRehash()
  501. *
  502. * @return void
  503. */
  504. public function testNeedsPasswordRehash() {
  505. $this->Users->addBehavior('Tools.Passwordable', array(
  506. 'allowSame' => false,
  507. 'current' => false,
  508. 'authType' => 'Blowfish',
  509. 'passwordHasher' => 'Default'
  510. ));
  511. $hash = password_hash('foobar', PASSWORD_BCRYPT);
  512. $result = $this->Users->needsPasswordRehash($hash);
  513. $this->assertFalse($result);
  514. $hash = sha1('foobar');
  515. $result = $this->Users->needsPasswordRehash($hash);
  516. $this->assertTrue($result);
  517. }
  518. /**
  519. * Tests needsPasswordRehash()
  520. *
  521. * @return void
  522. */
  523. public function testNeedsPasswordRehashWithNotSupportedHasher() {
  524. $this->Users->addBehavior('Tools.Passwordable', array(
  525. 'allowSame' => false,
  526. 'current' => false,
  527. 'authType' => 'Blowfish',
  528. ));
  529. $hash = password_hash('foobar', PASSWORD_BCRYPT);
  530. $result = $this->Users->needsPasswordRehash($hash);
  531. $this->assertFalse($result);
  532. $this->Users->removeBehavior('Passwordable');
  533. $this->Users->addBehavior('Tools.Passwordable', array(
  534. 'allowSame' => false,
  535. 'current' => false,
  536. 'authType' => 'Blowfish',
  537. 'passwordHasher' => 'Default'
  538. ));
  539. $hash = password_hash('foobar', PASSWORD_BCRYPT);
  540. $result = $this->Users->needsPasswordRehash($hash);
  541. $this->assertFalse($result);
  542. }
  543. /**
  544. * PasswordableBehaviorTest::testSettings()
  545. *
  546. * @return void
  547. */
  548. public function testSettings() {
  549. // Pwd min and max length
  550. $this->Users->addBehavior('Tools.Passwordable', array(
  551. 'allowSame' => false,
  552. 'current' => false,
  553. 'minLength' => 3,
  554. 'maxLength' => 6,
  555. ));
  556. $user = $this->Users->newEntity();
  557. $data = array(
  558. 'pwd' => '123',
  559. 'pwd_repeat' => '123'
  560. );
  561. $this->Users->patchEntity($user, $data);
  562. $result = $this->Users->save($user);
  563. $this->assertTrue((bool)$result);
  564. $user = $this->Users->newEntity();
  565. $data = array(
  566. 'pwd' => '12345678',
  567. 'pwd_repeat' => '12345678'
  568. );
  569. $this->Users->patchEntity($user, $data);
  570. $result = $this->Users->save($user);
  571. $this->assertFalse($result);
  572. $expected = array(
  573. 'pwd' => array('between' => __d('tools', 'valErrBetweenCharacters {0} {1}', 3, 6)),
  574. );
  575. $this->assertEquals($expected, $user->errors());
  576. }
  577. /**
  578. * Test that validate false also works.
  579. *
  580. * @return void
  581. */
  582. public function testSaveWithValidateFalse() {
  583. $this->Users->addBehavior('Tools.Passwordable');
  584. $user = $this->Users->newEntity();
  585. $data = array(
  586. 'pwd' => '123',
  587. );
  588. $this->Users->patchEntity($user, $data);
  589. $result = $this->Users->save($user, array('validate' => false));
  590. $this->assertTrue((bool)$result);
  591. $uid = (string)$user->id;
  592. $hash = $user['password'];
  593. $data = array(
  594. 'id' => $uid,
  595. 'pwd' => '1234'
  596. );
  597. $this->Users->patchEntity($user, $data);
  598. $result2 = $this->Users->save($user, array('validate' => false));
  599. $this->assertTrue((bool)$result2);
  600. $hash2 = $user['password'];
  601. $this->assertTrue($hash !== $hash2);
  602. }
  603. /**
  604. * PasswordableBehaviorTest::testValidateCustomRule()
  605. *
  606. * @return void
  607. */
  608. public function testValidateCustomRule() {
  609. $rules = array(
  610. 'validateCustom' => array(
  611. 'rule' => array('custom', '#^[a-z0-9]+$#'), // Just a test example, never use this regexp!
  612. 'message' => 'Foo Bar',
  613. 'last' => true,
  614. ),
  615. 'validateCustomExt' => array(
  616. 'rule' => array('custom', '#^[a-z]+$#'), // Just a test example, never use this regexp!
  617. 'message' => 'Foo Bar Ext',
  618. 'last' => true,
  619. )
  620. );
  621. $this->Users->addBehavior('Tools.Passwordable', array(
  622. 'customValidation' => $rules));
  623. $user = $this->Users->newEntity();
  624. $data = array(
  625. 'pwd' => '%123456',
  626. 'pwd_repeat' => '%123456'
  627. );
  628. $this->Users->patchEntity($user, $data);
  629. $is = $this->Users->save($user);
  630. $this->assertFalse($is);
  631. $result = $user->errors();
  632. $expected = array('pwd' => array('validateCustom' => 'Foo Bar'));
  633. $this->assertSame($expected, $result);
  634. $user = $this->Users->newEntity();
  635. $data = array(
  636. 'pwd' => 'abc123',
  637. 'pwd_repeat' => 'abc123'
  638. );
  639. $this->Users->patchEntity($user, $data);
  640. $is = $this->Users->save($user);
  641. $this->assertFalse($is);
  642. $result = $user->errors();
  643. $expected = array('pwd' => array('validateCustomExt' => 'Foo Bar Ext'));
  644. $this->assertSame($expected, $result);
  645. $user = $this->Users->newEntity();
  646. $data = array(
  647. 'pwd' => 'abcdef',
  648. 'pwd_repeat' => 'abcdef'
  649. );
  650. $this->Users->patchEntity($user, $data);
  651. $is = $this->Users->save($user);
  652. $this->assertTrue((bool)$is);
  653. }
  654. }