PasswordableBehaviorTest.php 19 KB

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