PasswordableBehaviorTest.php 21 KB

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