PasswordableBehaviorTest.php 20 KB

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