PasswordableBehaviorTest.php 21 KB

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