PasswordableBehaviorTest.php 19 KB

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