PasswordableBehaviorTest.php 20 KB

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