PasswordableBehaviorTest.php 19 KB

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