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 Cake\Network\Request;
  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.tools_users', 'plugin.tools.roles',
  15. ];
  16. /**
  17. * @var \App\Model\Table\ToolsUsersTable
  18. */
  19. public $Users;
  20. /**
  21. * SetUp method
  22. *
  23. * @return void
  24. */
  25. public function setUp() {
  26. parent::setUp();
  27. Configure::delete('Passwordable');
  28. Configure::write('Passwordable.auth', 'AuthTest');
  29. $this->Users = TableRegistry::get('ToolsUsers');
  30. $this->hasher = PasswordHasherFactory::build('Default');
  31. Router::setRequestInfo(new Request());
  32. }
  33. /**
  34. * @return void
  35. */
  36. public function tearDown() {
  37. TableRegistry::clear();
  38. parent::tearDown();
  39. }
  40. /**
  41. * Make sure validation is triggered correctly
  42. *
  43. * @return void
  44. */
  45. public function testValidate() {
  46. $this->Users->addBehavior('Tools.Passwordable', []);
  47. $user = $this->Users->newEntity();
  48. $data = [
  49. 'pwd' => '123456',
  50. ];
  51. $this->Users->patchEntity($user, $data);
  52. $is = $this->Users->save($user);
  53. $this->assertFalse($is);
  54. $this->assertEquals(['pwd_repeat'], array_keys((array)$user->errors()));
  55. $user = $this->Users->newEntity();
  56. $data = [
  57. 'pwd' => '1234ab',
  58. 'pwd_repeat' => '123456'
  59. ];
  60. $this->Users->patchEntity($user, $data);
  61. $is = $this->Users->save($user);
  62. $this->assertFalse($is);
  63. $this->assertEquals(['validateIdentical' => __d('tools', 'valErrPwdNotMatch')], $user->errors()['pwd_repeat']);
  64. $user = $this->Users->newEntity();
  65. $data = [
  66. 'pwd' => '123456',
  67. 'pwd_repeat' => '123456'
  68. ];
  69. $this->Users->patchEntity($user, $data);
  70. $is = $this->Users->save($user);
  71. $this->assertTrue(!empty($is));
  72. }
  73. /**
  74. * Test that confirm false does not require confirmation
  75. *
  76. * @return void
  77. */
  78. public function testValidateNoConfirm() {
  79. $this->Users->addBehavior('Tools.Passwordable', ['confirm' => false]);
  80. $user = $this->Users->newEntity();
  81. $data = [
  82. 'pwd' => '123456',
  83. ];
  84. $this->Users->patchEntity($user, $data);
  85. $is = $this->Users->save($user);
  86. //debug($is);
  87. $this->assertTrue(!empty($is));
  88. }
  89. /**
  90. * Trigger validation and update process if no values are entered but are required
  91. *
  92. * @return void
  93. */
  94. public function testValidateRequired() {
  95. $this->Users->addBehavior('Tools.Passwordable');
  96. $user = $this->Users->newEntity();
  97. $data = [
  98. 'pwd' => '',
  99. 'pwd_repeat' => ''
  100. ];
  101. $this->Users->patchEntity($user, $data);
  102. $is = $this->Users->save($user);
  103. $this->assertFalse($is);
  104. $this->assertEquals(['pwd', 'pwd_repeat'], array_keys((array)$user->errors()));
  105. }
  106. /**
  107. * Validation and update process gets skipped if no values are entered
  108. *
  109. * @return void
  110. */
  111. public function testValidateNotRequired() {
  112. $this->Users->addBehavior('Tools.Passwordable', ['require' => false]);
  113. $user = $this->Users->newEntity();
  114. $data = [
  115. 'name' => 'foo', // we need at least one field besides the password on CREATE
  116. 'pwd' => '',
  117. 'pwd_repeat' => ''
  118. ];
  119. $this->Users->patchEntity($user, $data);
  120. $is = $this->Users->save($user);
  121. $this->assertTrue((bool)$is);
  122. $this->assertEquals(['name', 'created', 'modified', 'id'], $is->visibleProperties());
  123. $id = $user->id;
  124. $user = $this->Users->newEntity();
  125. $data = [
  126. 'id' => $id,
  127. 'pwd' => '',
  128. 'pwd_repeat' => ''
  129. ];
  130. $this->Users->patchEntity($user, $data);
  131. $is = $this->Users->save($user);
  132. $this->assertTrue((bool)$is);
  133. $this->assertEquals(['id', 'modified'], $is->visibleProperties());
  134. }
  135. /**
  136. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  137. *
  138. * @return void
  139. */
  140. public function testValidateEmptyWithCurrentPassword() {
  141. $this->Users->addBehavior('Tools.Passwordable', ['current' => true]);
  142. $user = $this->Users->newEntity();
  143. $data = [
  144. 'id' => '123',
  145. 'pwd' => '',
  146. 'pwd_repeat' => '',
  147. 'pwd_current' => '123456',
  148. ];
  149. $this->Users->patchEntity($user, $data);
  150. $is = $this->Users->save($user);
  151. $this->assertFalse($is);
  152. $this->assertEquals(['pwd', 'pwd_repeat', 'pwd_current'], array_keys((array)$user->errors()));
  153. $this->tearDown();
  154. $this->setUp();
  155. $this->Users->removeBehavior('Passwordable');
  156. $this->Users->addBehavior('Tools.Passwordable', ['require' => false, 'current' => true]);
  157. $user = $this->Users->newEntity();
  158. $data = [
  159. 'name' => 'foo',
  160. 'pwd' => '',
  161. 'pwd_repeat' => '',
  162. 'pwd_current' => '',
  163. ];
  164. $this->Users->patchEntity($user, $data);
  165. $is = $this->Users->save($user);
  166. $this->assertTrue(!empty($is));
  167. }
  168. /**
  169. * Test aliases for field names
  170. *
  171. * @return void
  172. */
  173. public function testDifferentFieldNames() {
  174. $this->Users->addBehavior('Tools.Passwordable', [
  175. 'formField' => 'passw',
  176. 'formFieldRepeat' => 'passw_repeat',
  177. 'formFieldCurrent' => 'passw_current',
  178. ]);
  179. $user = $this->Users->newEntity();
  180. $data = [
  181. 'passw' => '123456',
  182. 'passw_repeat' => '123456'
  183. ];
  184. $this->Users->patchEntity($user, $data);
  185. //debug($this->Users->data);
  186. $is = $this->Users->save($user);
  187. $this->assertTrue(!empty($is));
  188. }
  189. /**
  190. * Assert that allowSame false does not allow storing the same password as previously entered
  191. *
  192. * @return void
  193. */
  194. public function testNotSame() {
  195. $user = $this->Users->newEntity();
  196. $data = [
  197. 'name' => 'admin',
  198. 'password' => $this->hasher->hash('somepwd'),
  199. 'role_id' => '1'
  200. ];
  201. $this->Users->patchEntity($user, $data);
  202. $result = $this->Users->save($user);
  203. $this->assertTrue((bool)$result);
  204. $userCopy = clone($user);
  205. $this->Users->addBehavior('Tools.Passwordable', [
  206. 'formField' => 'passw',
  207. 'formFieldRepeat' => 'passw_repeat',
  208. 'formFieldCurrent' => 'passw_current',
  209. 'allowSame' => false,
  210. 'current' => true,
  211. ]);
  212. $user = clone($userCopy);
  213. $data = [
  214. 'passw_current' => 'something',
  215. 'passw' => 'somepwd',
  216. 'passw_repeat' => 'somepwd'
  217. ];
  218. $this->Users->patchEntity($user, $data);
  219. $is = $this->Users->save($user);
  220. $this->assertFalse($is);
  221. $user = clone($userCopy);
  222. $data = [
  223. 'passw_current' => 'somepwd',
  224. 'passw' => 'newpwd',
  225. 'passw_repeat' => 'newpwd'
  226. ];
  227. $this->Users->patchEntity($user, $data);
  228. $is = $this->Users->save($user);
  229. $this->assertTrue(!empty($is));
  230. }
  231. /**
  232. * Assert that allowSame false does not allow storing the same password as previously entered
  233. *
  234. * @return void
  235. */
  236. public function testNotSameWithoutCurrentField() {
  237. $this->Users->addBehavior('Tools.Passwordable', [
  238. 'formField' => 'passw',
  239. 'formFieldRepeat' => 'passw_repeat',
  240. 'allowSame' => false,
  241. 'current' => false
  242. ]);
  243. $user = $this->Users->newEntity();
  244. $data = [
  245. 'passw' => 'somepwd',
  246. 'passw_repeat' => 'somepwd'
  247. ];
  248. $this->Users->patchEntity($user, $data);
  249. $is = $this->Users->save($user);
  250. $this->assertTrue((bool)$is);
  251. $userCopy = clone($user);
  252. $uid = $user->id;
  253. $user = clone($userCopy);
  254. $data = [
  255. 'passw' => 'somepwd',
  256. 'passw_repeat' => 'somepwd'
  257. ];
  258. $this->Users->patchEntity($user, $data);
  259. $is = $this->Users->save($user);
  260. $this->assertFalse((bool)$is);
  261. $user = clone($userCopy);
  262. $data = [
  263. 'passw' => 'newpwd',
  264. 'passw_repeat' => 'newpwd'
  265. ];
  266. $this->Users->patchEntity($user, $data);
  267. $is = $this->Users->save($user);
  268. $this->assertTrue((bool)$is);
  269. }
  270. /**
  271. * Assert that on edit it does not wrongly pass validation (require => false)
  272. *
  273. * @return void
  274. */
  275. public function testRequireFalse() {
  276. $this->Users->addBehavior('Tools.Passwordable', [
  277. 'formField' => 'passw',
  278. 'formFieldRepeat' => 'passw_repeat',
  279. 'require' => false
  280. ]);
  281. $user = $this->Users->newEntity();
  282. $data = [
  283. 'passw' => 'somepwd',
  284. 'passw_repeat' => 'somepwd'
  285. ];
  286. $this->Users->patchEntity($user, $data);
  287. $is = $this->Users->save($user);
  288. $this->assertTrue((bool)$is);
  289. $userCopy = clone($user);
  290. $user = clone($userCopy);
  291. $data = [
  292. 'passw' => '',
  293. 'passw_repeat' => ''
  294. ];
  295. $this->Users->patchEntity($user, $data);
  296. $is = $this->Users->save($user);
  297. $this->assertTrue((bool)$is);
  298. //debug($user->errors());
  299. $user = clone($userCopy);
  300. $data = [
  301. 'passw' => 'somepwd2',
  302. 'passw_repeat' => ''
  303. ];
  304. $this->Users->patchEntity($user, $data);
  305. $is = $this->Users->save($user);
  306. $this->assertFalse((bool)$is);
  307. $user = clone($userCopy);
  308. $data = [
  309. 'passw' => 'somepwd2',
  310. 'passw_repeat' => 'somepwd2'
  311. ];
  312. $this->Users->patchEntity($user, $data);
  313. $is = $this->Users->save($user);
  314. $this->assertTrue((bool)$is);
  315. }
  316. /**
  317. * Needs faking of pwd check...
  318. *
  319. * @return void
  320. */
  321. public function testValidateCurrent() {
  322. $this->assertFalse($this->Users->behaviors()->has('Passwordable'));
  323. $user = $this->Users->newEntity();
  324. $data = [
  325. 'name' => 'xyz',
  326. 'password' => $this->hasher->hash('somepwd')];
  327. $this->Users->patchEntity($user, $data);
  328. $result = $this->Users->save($user);
  329. $this->assertTrue(!empty($result));
  330. $userCopy = clone($user);
  331. $uid = $user->id;
  332. $this->Users->removeBehavior('Passwordable');
  333. $this->Users->addBehavior('Tools.Passwordable', ['current' => true]);
  334. $user = clone($userCopy);
  335. $data = [
  336. 'pwd' => '123456',
  337. 'pwd_repeat' => '12345678',
  338. ];
  339. $this->Users->patchEntity($user, $data);
  340. $this->assertTrue($this->Users->behaviors()->has('Passwordable'));
  341. $is = $this->Users->save($user);
  342. $this->assertFalse($is);
  343. $user = clone($userCopy);
  344. $data = [
  345. 'pwd_current' => 'somepwdx',
  346. 'pwd' => '123456',
  347. 'pwd_repeat' => '123456'
  348. ];
  349. $this->Users->patchEntity($user, $data);
  350. $is = $this->Users->save($user);
  351. $this->assertFalse($is);
  352. $user = clone($userCopy);
  353. $data = [
  354. 'name' => 'Yeah',
  355. 'pwd_current' => 'somepwd',
  356. 'pwd' => '123456',
  357. 'pwd_repeat' => '123456'
  358. ];
  359. $user->accessible('*', false); // Mark all properties as protected
  360. $user->accessible(['id'], true); // Allow id to be accessible by default
  361. $user = $this->Users->patchEntity($user, $data, ['fields' => ['id']]);
  362. $this->assertNotSame($is['password'], $user['password']);
  363. $this->assertTrue($user->dirty('pwd'));
  364. $options = ['validate' => true];
  365. $is = $this->Users->save($user, $options);
  366. $this->assertTrue(!empty($is));
  367. $user = $this->Users->get($uid);
  368. // The password is updated, the name not
  369. $this->assertSame($is['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->accessible('*', false); // Mark all properties as protected
  382. $user->accessible(['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->errors()));
  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->newEntity();
  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. $uid = $user->id;
  409. $this->Users->removeBehavior('Passwordable');
  410. $this->Users->addBehavior('Tools.Passwordable', ['current' => true, 'require' => false]);
  411. $user = clone($userCopy);
  412. $data = [
  413. 'name' => 'Yeah',
  414. 'current' => '',
  415. 'pwd' => '',
  416. 'pwd_repeat' => '',
  417. ];
  418. $this->Users->patchEntity($user, $data);
  419. $this->assertTrue($this->Users->behaviors()->has('Passwordable'));
  420. $is = $this->Users->save($user);
  421. $this->assertTrue((bool)$is);
  422. $user = clone($userCopy);
  423. $data = [
  424. 'name' => 'Yeah',
  425. 'pwd_current' => '',
  426. 'pwd' => '123456',
  427. 'pwd_repeat' => '123456'
  428. ];
  429. $this->Users->patchEntity($user, $data);
  430. $is = $this->Users->save($user);
  431. $this->assertFalse($is);
  432. $user = clone($userCopy);
  433. $data = [
  434. 'name' => 'Yeah',
  435. 'pwd_current' => 'somepwd',
  436. 'pwd' => '123456',
  437. 'pwd_repeat' => '123456'
  438. ];
  439. $user->accessible('*', false); // Mark all properties as protected
  440. $user->accessible(['id'], true); // Allow id to be accessible by default
  441. $user = $this->Users->patchEntity($user, $data, ['fields' => ['id']]);
  442. $this->assertNotSame($is['password'], $user['password']);
  443. $this->assertTrue($user->dirty('pwd'));
  444. $is = $this->Users->save($user);
  445. $this->assertTrue((bool)$is);
  446. }
  447. /**
  448. * @return void
  449. */
  450. public function testPatchWithFieldList() {
  451. $this->Users->addBehavior('Tools.Passwordable', [
  452. 'formField' => 'pwd',
  453. 'formFieldRepeat' => 'pwd_repeat',
  454. 'current' => false,
  455. 'passwordHasher' => 'Complex',
  456. ]);
  457. $user = $this->Users->newEntity();
  458. $data = [
  459. 'pwd' => 'somepwd',
  460. 'pwd_repeat' => 'somepwd'
  461. ];
  462. $user->accessible('*', false); // Mark all properties as protected
  463. $user->accessible(['id'], true);
  464. $this->Users->patchEntity($user, $data, ['fields' => ['id']]);
  465. $result = $this->Users->save($user);
  466. $this->assertTrue((bool)$result);
  467. }
  468. /**
  469. * @return void
  470. */
  471. public function testPatchWithoutFieldList() {
  472. $this->Users->addBehavior('Tools.Passwordable', [
  473. 'formField' => 'pwd',
  474. 'formFieldRepeat' => 'pwd_repeat',
  475. 'current' => false,
  476. 'passwordHasher' => 'Complex',
  477. 'forceFieldList' => true
  478. ]);
  479. $user = $this->Users->newEntity();
  480. $data = [
  481. 'name' => 'x',
  482. 'pwd' => 'somepwd',
  483. 'pwd_repeat' => 'somepwd'
  484. ];
  485. $user->accessible('*', false); // Mark all properties as protected
  486. $user->accessible(['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->newEntity();
  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->newEntity();
  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->newEntity();
  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->errors());
  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->newEntity();
  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->newEntity();
  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->errors();
  679. $expected = ['pwd' => ['validateCustom' => 'Foo Bar']];
  680. $this->assertSame($expected, $result);
  681. $user = $this->Users->newEntity();
  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->errors();
  690. $expected = ['pwd' => ['validateCustomExt' => 'Foo Bar Ext']];
  691. $this->assertSame($expected, $result);
  692. $user = $this->Users->newEntity();
  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. }