PasswordableBehaviorTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\Auth\PasswordHasherFactory;
  4. use Cake\Core\Configure;
  5. use Cake\Http\ServerRequest;
  6. use Cake\ORM\TableRegistry;
  7. use Cake\Routing\Router;
  8. use Tools\TestSuite\TestCase;
  9. class PasswordableBehaviorTest extends TestCase {
  10. /**
  11. * @var array
  12. */
  13. public $fixtures = [
  14. 'plugin.Tools.ToolsUsers',
  15. 'plugin.Tools.Roles'
  16. ];
  17. /**
  18. * @var \App\Model\Table\ToolsUsersTable
  19. */
  20. public $Users;
  21. /**
  22. * SetUp method
  23. *
  24. * @return void
  25. */
  26. public function setUp() {
  27. parent::setUp();
  28. Configure::delete('Passwordable');
  29. Configure::write('Passwordable.auth', 'AuthTest');
  30. $this->Users = TableRegistry::getTableLocator()->get('ToolsUsers');
  31. $this->hasher = PasswordHasherFactory::build('Default');
  32. Router::setRequestInfo(new ServerRequest());
  33. }
  34. /**
  35. * @return void
  36. */
  37. public function tearDown() {
  38. TableRegistry::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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->visibleProperties());
  129. $id = $user->id;
  130. $user = $this->Users->newEntity();
  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->visibleProperties());
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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. $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->assertNotSame($is['password'], $user['password']);
  372. $this->assertTrue($user->isDirty('pwd'));
  373. $options = ['validate' => true];
  374. $is = $this->Users->save($user, $options);
  375. $this->assertTrue(!empty($is));
  376. $user = $this->Users->get($uid);
  377. // The password is updated, the name not
  378. $this->assertSame($is['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->newEntity();
  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. $uid = $user->id;
  418. $this->Users->addBehavior('Tools.Passwordable', ['current' => true, 'require' => false]);
  419. $user = clone($userCopy);
  420. $data = [
  421. 'name' => 'Yeah',
  422. 'current' => '',
  423. 'pwd' => '',
  424. 'pwd_repeat' => '',
  425. ];
  426. $this->Users->patchEntity($user, $data);
  427. $this->assertTrue($this->Users->behaviors()->has('Passwordable'));
  428. $is = $this->Users->save($user);
  429. $this->assertTrue((bool)$is);
  430. $user = clone($userCopy);
  431. $data = [
  432. 'name' => 'Yeah',
  433. 'pwd_current' => '',
  434. 'pwd' => '123456',
  435. 'pwd_repeat' => '123456'
  436. ];
  437. $this->Users->patchEntity($user, $data);
  438. $is = $this->Users->save($user);
  439. $this->assertFalse($is);
  440. $user = clone($userCopy);
  441. $data = [
  442. 'name' => 'Yeah',
  443. 'pwd_current' => 'somepwd',
  444. 'pwd' => '123456',
  445. 'pwd_repeat' => '123456'
  446. ];
  447. $user->setAccess('*', false); // Mark all properties as protected
  448. $user->setAccess(['id'], true); // Allow id to be accessible by default
  449. $user = $this->Users->patchEntity($user, $data, ['fields' => ['id']]);
  450. $this->assertNotSame($is['password'], $user['password']);
  451. $this->assertTrue($user->isDirty('pwd'));
  452. $is = $this->Users->save($user);
  453. $this->assertTrue((bool)$is);
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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->newEntity();
  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. }