PasswordableBehaviorTest.php 20 KB

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