PasswordableBehaviorTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. namespace Tools\Model\Behavior;
  3. use Cake\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 = array(
  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. /*
  31. if (isset($this->Users->validate['pwd'])) {
  32. unset($this->Users->validate['pwd']);
  33. }
  34. if (isset($this->Users->validate['pwd_repeat'])) {
  35. unset($this->Users->validate['pwd_repeat']);
  36. }
  37. if (isset($this->Users->validate['pwd_current'])) {
  38. unset($this->Users->validate['pwd_current']);
  39. }
  40. if (isset($this->Users->order)) {
  41. unset($this->Users->order);
  42. }
  43. */
  44. $this->hasher = PasswordHasherFactory::build('Default');
  45. $user = $this->Users->newEntity();
  46. $data = array(
  47. 'id' => '5',
  48. 'name' => 'admin',
  49. 'password' => $this->hasher->hash('somepwd'),
  50. 'role_id' => '1'
  51. );
  52. $this->Users->patchEntity($user, $data);
  53. $result = $this->Users->save($user);
  54. //$this->assertTrue();
  55. Router::setRequestInfo(new Request());
  56. }
  57. public function tearDown() {
  58. TableRegistry::clear();
  59. parent::tearDown();
  60. }
  61. /**
  62. * Make sure validation is triggered correctly
  63. *
  64. * @return void
  65. */
  66. public function testValidate() {
  67. $this->Users->addBehavior('Tools.Passwordable', array());
  68. $user = $this->Users->newEntity();
  69. $data = array(
  70. 'pwd' => '123456',
  71. );
  72. $this->Users->patchEntity($user, $data);
  73. $is = $this->Users->save($user);
  74. //debug($user->errors());
  75. $this->assertFalse($is);
  76. $this->assertEquals(array('pwd_repeat'), array_keys($user->errors()));
  77. $user = $this->Users->newEntity();
  78. $data = array(
  79. 'pwd' => '1234ab',
  80. 'pwd_repeat' => '123456'
  81. );
  82. $this->Users->patchEntity($user, $data);
  83. $is = $this->Users->save($user);
  84. //debug($user->errors());
  85. $this->assertFalse($is);
  86. $this->assertEquals(array('validateIdentical' => __d('tools', 'valErrPwdNotMatch')), $user->errors()['pwd_repeat']);
  87. $user = $this->Users->newEntity();
  88. $data = array(
  89. 'pwd' => '123456',
  90. 'pwd_repeat' => '123456'
  91. );
  92. $this->Users->patchEntity($user, $data);
  93. //debug($this->Users->validate);
  94. $is = $this->Users->validate($user);
  95. $this->assertTrue(!empty($is));
  96. }
  97. /**
  98. * Test that confirm false does not require confirmation
  99. *
  100. * @return void
  101. */
  102. public function testValidateNoConfirm() {
  103. $this->Users->addBehavior('Tools.Passwordable', array('confirm' => false));
  104. $user = $this->Users->newEntity();
  105. $data = array(
  106. 'pwd' => '123456',
  107. );
  108. $this->Users->patchEntity($user, $data);
  109. $is = $this->Users->save($user);
  110. //debug($is);
  111. $this->assertTrue(!empty($is));
  112. }
  113. /**
  114. * Trigger validation and update process if no values are entered but are required
  115. *
  116. * @return void
  117. */
  118. public function testValidateRequired() {
  119. $this->Users->addBehavior('Tools.Passwordable');
  120. $user = $this->Users->newEntity();
  121. $data = array(
  122. 'pwd' => '',
  123. 'pwd_repeat' => ''
  124. );
  125. $this->Users->patchEntity($user, $data);
  126. $is = $this->Users->save($user);
  127. $this->assertFalse($is);
  128. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($user->errors()));
  129. }
  130. /**
  131. * Validation and update process gets skipped if no values are entered
  132. *
  133. * @return void
  134. */
  135. public function testValidateNotRequired() {
  136. $this->Users->addBehavior('Tools.Passwordable', array('require' => false));
  137. $user = $this->Users->newEntity();
  138. $data = array(
  139. 'name' => 'foo', // we need at least one field besides the password on CREATE
  140. 'pwd' => '',
  141. 'pwd_repeat' => ''
  142. );
  143. $this->Users->patchEntity($user, $data);
  144. $is = $this->Users->save($user);
  145. $this->assertTrue((bool)$is);
  146. $this->assertEquals(array('name', 'id'), $is->visibleProperties());
  147. $id = $user->id;
  148. $user = $this->Users->newEntity();
  149. $data = array(
  150. 'id' => $id,
  151. 'pwd' => '',
  152. 'pwd_repeat' => ''
  153. );
  154. $this->Users->patchEntity($user, $data);
  155. $is = $this->Users->save($user);
  156. $this->assertTrue((bool)$is);
  157. $this->assertEquals(array('id'), $is->visibleProperties());
  158. }
  159. /**
  160. * PasswordableBehaviorTest::testValidateEmptyWithCurrentPassword()
  161. *
  162. * @return void
  163. */
  164. public function testValidateEmptyWithCurrentPassword() {
  165. $this->Users->addBehavior('Tools.Passwordable', array('current' => true));
  166. $user = $this->Users->newEntity();
  167. $data = array(
  168. 'id' => '123',
  169. 'pwd' => '',
  170. 'pwd_repeat' => '',
  171. 'pwd_current' => '123456',
  172. );
  173. $this->Users->patchEntity($user, $data);
  174. $is = $this->Users->save($user);
  175. //debug($user->errors());
  176. $this->assertFalse($is);
  177. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($user->errors()));
  178. $this->tearDown();
  179. $this->setUp();
  180. $this->Users->removeBehavior('Passwordable');
  181. $this->Users->addBehavior('Tools.Passwordable', array('require' => false, 'current' => true));
  182. $user = $this->Users->newEntity();
  183. $data = array(
  184. 'name' => 'foo',
  185. 'pwd' => '',
  186. 'pwd_repeat' => '',
  187. 'pwd_current' => '',
  188. );
  189. $this->Users->patchEntity($user, $data);
  190. $is = $this->Users->save($user);
  191. $this->assertTrue(!empty($is));
  192. }
  193. /**
  194. * Test aliases for field names
  195. */
  196. public function testDifferentFieldNames() {
  197. $this->Users->addBehavior('Tools.Passwordable', array(
  198. 'formField' => 'passw',
  199. 'formFieldRepeat' => 'passw_repeat',
  200. 'formFieldCurrent' => 'passw_current',
  201. ));
  202. $user = $this->Users->newEntity();
  203. $data = array(
  204. 'passw' => '123456',
  205. 'passw_repeat' => '123456'
  206. );
  207. $this->Users->patchEntity($user, $data);
  208. //debug($this->Users->data);
  209. $is = $this->Users->save($user);
  210. $this->assertTrue(!empty($is));
  211. }
  212. /**
  213. * Assert that allowSame false does not allow storing the same password as previously entered
  214. */
  215. public function testNotSame() {
  216. $this->Users->addBehavior('Tools.Passwordable', array(
  217. 'formField' => 'passw',
  218. 'formFieldRepeat' => 'passw_repeat',
  219. 'formFieldCurrent' => 'passw_current',
  220. 'allowSame' => false,
  221. 'current' => true,
  222. ));
  223. $user = $this->Users->newEntity();
  224. $data = array(
  225. 'id' => '5',
  226. 'passw_current' => 'something',
  227. 'passw' => 'somepwd',
  228. 'passw_repeat' => 'somepwd'
  229. );
  230. $this->Users->patchEntity($user, $data);
  231. $is = $this->Users->save($user);
  232. //debug($user->errors());
  233. $this->assertFalse($is);
  234. $user = $this->Users->newEntity([], ['markNew' => false]);
  235. $data = array(
  236. 'id' => '5',
  237. 'passw_current' => 'somepwd',
  238. 'passw' => 'newpwd',
  239. 'passw_repeat' => 'newpwd'
  240. );
  241. $this->Users->patchEntity($user, $data);
  242. $is = $this->Users->save($user);
  243. $this->assertTrue(!empty($is));
  244. }
  245. /**
  246. * Assert that allowSame false does not allow storing the same password as previously entered
  247. */
  248. public function testNotSameWithoutCurrentField() {
  249. $this->Users->addBehavior('Tools.Passwordable', array(
  250. 'formField' => 'passw',
  251. 'formFieldRepeat' => 'passw_repeat',
  252. 'allowSame' => false,
  253. 'current' => false
  254. ));
  255. $user = $this->Users->newEntity();
  256. $data = array(
  257. 'passw' => 'somepwd',
  258. 'passw_repeat' => 'somepwd'
  259. );
  260. $this->Users->patchEntity($user, $data);
  261. $is = $this->Users->save($user);
  262. $this->assertTrue((bool)$is);
  263. $id = $is['id'];
  264. $user = $this->Users->newEntity([], ['markNew' => false]);
  265. $data = array(
  266. 'id' => $id,
  267. 'passw' => 'somepwd',
  268. 'passw_repeat' => 'somepwd'
  269. );
  270. $this->Users->patchEntity($user, $data);
  271. $is = $this->Users->save($user);
  272. $this->assertFalse((bool)$is);
  273. $user = $this->Users->newEntity([], ['markNew' => false]);
  274. $data = array(
  275. 'id' => $id,
  276. 'passw' => 'newpwd',
  277. 'passw_repeat' => 'newpwd'
  278. );
  279. $this->Users->patchEntity($user, $data);
  280. $is = $this->Users->save($user);
  281. $this->assertTrue((bool)$is);
  282. }
  283. /**
  284. * Assert that on edit it does not wrongly pass validation (require => false)
  285. */
  286. public function testRequireFalse() {
  287. $this->Users->addBehavior('Tools.Passwordable', array(
  288. 'formField' => 'passw',
  289. 'formFieldRepeat' => 'passw_repeat',
  290. 'require' => false
  291. ));
  292. $user = $this->Users->newEntity();
  293. $data = array(
  294. 'passw' => 'somepwd',
  295. 'passw_repeat' => 'somepwd'
  296. );
  297. $this->Users->patchEntity($user, $data);
  298. $is = $this->Users->save($user);
  299. $this->assertTrue((bool)$is);
  300. $id = $is['id'];
  301. $user = $this->Users->newEntity([], ['markNew' => false]);
  302. $data = array(
  303. 'id' => $id,
  304. 'passw' => 'somepwd2',
  305. 'passw_repeat' => ''
  306. );
  307. $this->Users->patchEntity($user, $data);
  308. $is = $this->Users->save($user);
  309. $this->assertFalse((bool)$is);
  310. //debug($user->errors());
  311. $user = $this->Users->newEntity([], ['markNew' => false]);
  312. $data = array(
  313. 'id' => $id,
  314. 'passw' => 'somepwd2',
  315. 'passw_repeat' => 'somepwd2'
  316. );
  317. $this->Users->patchEntity($user, $data);
  318. $is = $this->Users->save($user);
  319. $this->assertTrue((bool)$is);
  320. }
  321. /**
  322. * Needs faking of pwd check...
  323. */
  324. public function testValidateCurrent() {
  325. $this->assertFalse($this->Users->behaviors()->loaded('Passwordable'));
  326. $user = $this->Users->newEntity();
  327. $data = array(
  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', array('current' => true));
  336. $user = $this->Users->newEntity([], ['markNew' => false]);
  337. $data = array(
  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()->loaded('Passwordable'));
  345. $is = $this->Users->save($user);
  346. $this->assertFalse($is);
  347. $user = $this->Users->newEntity([], ['markNew' => false]);
  348. $data = array(
  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 = array(
  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(array('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 = array('validate' => true, 'fieldList' => array('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 = array(
  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(array('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 = array('validate' => true, 'fieldList' => array('id', 'name'));
  395. $is = $this->Users->save($user, $options);
  396. // Validation errors triggered - as expected
  397. $this->assertFalse($is);
  398. $this->assertSame(array('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', array(
  408. 'formField' => 'pwd',
  409. 'formFieldRepeat' => 'pwd_repeat',
  410. 'allowSame' => false,
  411. 'current' => false,
  412. 'passwordHasher' => 'Complex',
  413. ));
  414. $user = $this->Users->newEntity();
  415. $data = array(
  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', array('current' => true));
  425. $user = $this->Users->newEntity();
  426. $data = array(
  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()->loaded('Passwordable'));
  434. $is = $this->Users->save($user);
  435. $this->assertFalse($is);
  436. $user = $this->Users->newEntity();
  437. $data = array(
  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 = array(
  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', array(
  464. 'allowSame' => false,
  465. 'current' => false,
  466. 'authType' => 'Blowfish',
  467. ));
  468. $user = $this->Users->newEntity();
  469. $data = array(
  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', array('current' => true));
  479. // Without the current password it will not continue
  480. $user = $this->Users->newEntity();
  481. $data = array(
  482. 'id' => $uid,
  483. 'pwd' => '123456',
  484. 'pwd_repeat' => '12345678',
  485. );
  486. $this->Users->patchEntity($user, $data);
  487. $this->assertTrue($this->Users->behaviors()->loaded('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 = array(
  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 = array(
  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', array(
  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', array(
  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', array(
  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', array(
  565. 'allowSame' => false,
  566. 'current' => false,
  567. 'minLength' => 3,
  568. 'maxLength' => 6,
  569. ));
  570. $user = $this->Users->newEntity();
  571. $data = array(
  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 = array(
  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 = array(
  587. 'pwd' => array('between' => __d('tools', 'valErrBetweenCharacters {0} {1}', 3, 6)),
  588. 'pwd_repeat' => array('between' =>__d('tools', 'valErrBetweenCharacters {0} {1}', 3, 6))
  589. );
  590. $this->assertEquals($expected, $user->errors());
  591. }
  592. /**
  593. * Test that validate false also works.
  594. *
  595. * @return void
  596. */
  597. public function testSaveWithValidateFalse() {
  598. $this->Users->addBehavior('Tools.Passwordable');
  599. $user = $this->Users->newEntity();
  600. $data = array(
  601. 'pwd' => '123',
  602. );
  603. $this->Users->patchEntity($user, $data);
  604. $result = $this->Users->save($user, array('validate' => false));
  605. $this->assertTrue((bool)$result);
  606. $uid = (string)$user->id;
  607. $hash = $user['password'];
  608. $data = array(
  609. 'id' => $uid,
  610. 'pwd' => '1234'
  611. );
  612. $this->Users->patchEntity($user, $data);
  613. $result2 = $this->Users->save($user, array('validate' => false));
  614. $this->assertTrue((bool)$result2);
  615. $hash2 = $user['password'];
  616. $this->assertTrue($hash !== $hash2);
  617. }
  618. }