PasswordableBehaviorTest.php 19 KB

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