PasswordableBehaviorTest.php 20 KB

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