PasswordableBehaviorTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. App::uses('ComponentCollection', 'Controller');
  3. class PasswordableBehaviorTest extends CakeTestCase {
  4. public $fixtures = array(
  5. 'core.user',
  6. );
  7. /**
  8. * setUp method
  9. */
  10. public function setUp() {
  11. parent::setUp();
  12. Configure::write('Passwordable.auth', 'AuthTestComponent');
  13. $this->User = ClassRegistry::init('User');
  14. if (isset($this->User->validate['pwd'])) {
  15. unset($this->User->validate['pwd']);
  16. }
  17. if (isset($this->User->validate['pwd_repeat'])) {
  18. unset($this->User->validate['pwd_repeat']);
  19. }
  20. if (isset($this->User->validate['pwd_current'])) {
  21. unset($this->User->validate['pwd_current']);
  22. }
  23. if (isset($this->User->order)) {
  24. unset($this->User->order);
  25. }
  26. }
  27. /**
  28. * Tear-down method. Resets environment state.
  29. */
  30. public function tearDown() {
  31. unset($this->User);
  32. parent::tearDown();
  33. ClassRegistry::flush();
  34. }
  35. public function testObject() {
  36. $this->User->Behaviors->load('Tools.Passwordable', array());
  37. $this->assertInstanceOf('PasswordableBehavior', $this->User->Behaviors->Passwordable);
  38. $res = $this->User->Behaviors->attached('Passwordable');
  39. $this->assertTrue($res);
  40. }
  41. /**
  42. * make sure validation is triggered correctly
  43. */
  44. public function testValidate() {
  45. $this->User->Behaviors->load('Tools.Passwordable', array());
  46. $this->User->create();
  47. $data = array(
  48. 'pwd' => '123456',
  49. );
  50. $this->User->set($data);
  51. $is = $this->User->save();
  52. //debug($this->User->validationErrors); ob_flush();
  53. $this->assertFalse($is);
  54. $this->assertEquals(array('pwd_repeat'), array_keys($this->User->validationErrors));
  55. $this->User->create();
  56. $data = array(
  57. 'pwd' => '1234ab',
  58. 'pwd_repeat' => '123456'
  59. );
  60. $this->User->set($data);
  61. $is = $this->User->save();
  62. //debug($this->User->validationErrors); ob_flush();
  63. $this->assertFalse($is);
  64. $this->assertEquals(array(__('valErrPwdNotMatch')), $this->User->validationErrors['pwd_repeat']);
  65. $this->User->create();
  66. $data = array(
  67. 'pwd' => '123456',
  68. 'pwd_repeat' => '123456'
  69. );
  70. $this->User->set($data);
  71. //debug($this->User->validate);
  72. $is = $this->User->validates();
  73. $this->assertTrue(!empty($is));
  74. }
  75. /**
  76. * test that confirm false does not require confirmation
  77. */
  78. public function testValidateNoConfirm() {
  79. $this->User->Behaviors->load('Tools.Passwordable', array('confirm'=>false));
  80. $this->User->create();
  81. $data = array(
  82. 'pwd' => '123456',
  83. );
  84. $this->User->set($data);
  85. $is = $this->User->save();
  86. debug($is); ob_flush();
  87. $this->assertTrue(!empty($is));
  88. }
  89. /**
  90. * validation and update process gets skipped if no values are entered
  91. */
  92. public function testValidateEmpty() {
  93. $this->User->Behaviors->load('Tools.Passwordable');
  94. $this->User->create();
  95. $data = array(
  96. 'pwd' => '',
  97. 'pwd_repeat' => ''
  98. );
  99. $this->User->set($data);
  100. $is = $this->User->save();
  101. debug($this->User->validationErrors); ob_flush();
  102. $this->assertFalse($is);
  103. $this->assertEquals(array('pwd', 'pwd_repeat'), array_keys($this->User->validationErrors));
  104. $this->User->Behaviors->unload('Passwordable');
  105. $this->User->validate = array();
  106. $this->User->Behaviors->load('Tools.Passwordable', array('current'=>true));
  107. $this->User->create();
  108. $data = array(
  109. 'id' => 123,
  110. 'pwd' => '',
  111. 'pwd_repeat' => '',
  112. 'pwd_current' => '123',
  113. );
  114. $this->User->set($data);
  115. $is = $this->User->save();
  116. //debug($this->User->validationErrors); ob_flush();
  117. $this->assertFalse($is);
  118. $this->assertEquals(array('pwd', 'pwd_repeat', 'pwd_current'), array_keys($this->User->validationErrors));
  119. $this->tearDown();
  120. $this->setUp();
  121. $this->User->Behaviors->load('Tools.Passwordable', array('allowEmpty'=>true, 'current'=>true));
  122. $this->User->create();
  123. $data = array(
  124. 'user' => 'foo',
  125. 'pwd' => '',
  126. 'pwd_repeat' => '',
  127. 'pwd_current' => '',
  128. );
  129. $is = $this->User->save($data);
  130. $this->assertTrue(!empty($is));
  131. }
  132. /**
  133. * test aliases for field names
  134. */
  135. public function testDifferentFieldNames() {
  136. $this->User->Behaviors->load('Tools.Passwordable', array(
  137. 'formField' => 'passw',
  138. 'formFieldRepeat' => 'passw_repeat',
  139. 'formFieldCurrent' => 'passw_current',
  140. ));
  141. $this->User->create();
  142. $data = array(
  143. 'passw' => '123456',
  144. 'passw_repeat' => '123456'
  145. );
  146. $this->User->set($data);
  147. //debug($this->User->data);
  148. $is = $this->User->save();
  149. $this->assertTrue(!empty($is));
  150. }
  151. /**
  152. * assert that allowSame false does not allow storing the same password as previously entered
  153. */
  154. public function testNotSame() {
  155. $this->User->Behaviors->load('Tools.Passwordable', array(
  156. 'formField' => 'passw',
  157. 'formFieldRepeat' => 'passw_repeat',
  158. 'formFieldCurrent' => 'passw_current',
  159. 'allowSame' => false,
  160. 'current' => true
  161. ));
  162. $this->User->create();
  163. $data = array(
  164. 'id' => 5,
  165. 'passw_current' => 'some',
  166. 'passw' => 'some',
  167. 'passw_repeat' => 'some'
  168. );
  169. $this->User->set($data);
  170. $is = $this->User->save();
  171. debug($this->User->validationErrors);
  172. $this->assertFalse($is);
  173. $this->User->create();
  174. $data = array(
  175. 'id' => 5,
  176. 'passw_current' => 'some',
  177. 'passw' => 'new',
  178. 'passw_repeat' => 'new'
  179. );
  180. $this->User->set($data);
  181. debug($this->User->data);
  182. $is = $this->User->save();
  183. $this->assertTrue(!empty($is));
  184. }
  185. /**
  186. * assert that allowSame false does not allow storing the same password as previously entered
  187. */
  188. public function testNotSameWithoutCurrentField() {
  189. $this->User->Behaviors->load('Tools.Passwordable', array(
  190. 'formField' => 'passw',
  191. 'formFieldRepeat' => 'passw_repeat',
  192. 'allowSame' => false,
  193. 'current' => false
  194. ));
  195. $this->User->create();
  196. $data = array(
  197. 'passw' => 'some',
  198. 'passw_repeat' => 'some'
  199. );
  200. $this->User->set($data);
  201. $is = $this->User->save();
  202. $this->assertTrue((bool)$is);
  203. $id = $is['User']['id'];
  204. $this->User->create();
  205. $data = array(
  206. 'id' => $id,
  207. 'passw' => 'some',
  208. 'passw_repeat' => 'some'
  209. );
  210. $this->User->set($data);
  211. $is = $this->User->save();
  212. debug($this->User->validationErrors); ob_flush();
  213. $this->assertFalse((bool)$is);
  214. $this->User->create();
  215. $data = array(
  216. 'id' => $id,
  217. 'passw' => 'new',
  218. 'passw_repeat' => 'new'
  219. );
  220. $this->User->set($data);
  221. $is = $this->User->save();
  222. $this->assertTrue((bool)$is);
  223. }
  224. /**
  225. * needs faking of pwd check...
  226. */
  227. public function testValidateCurrent() {
  228. $this->assertFalse($this->User->Behaviors->attached('Passwordable'));
  229. $this->User->create();
  230. $data = array('user'=>'xyz', 'password'=>Security::hash('some', null, true));
  231. $res = $this->User->save($data);
  232. $this->assertTrue(!empty($res));
  233. $uid = $this->User->id;
  234. # cake bug => attached behavior validation rules cannot be triggered
  235. //$this->tearDown();
  236. //$this->setUp();
  237. $this->User->Behaviors->load('Tools.Passwordable', array('current'=>true));
  238. $this->User->create();
  239. $data = array(
  240. 'id' => $uid,
  241. 'pwd' => '1234',
  242. 'pwd_repeat' => '123456',
  243. //'pwd_current' => '',
  244. );
  245. $this->User->set($data);
  246. $this->assertTrue($this->User->Behaviors->attached('Passwordable'));
  247. //debug($this->User->validate); ob_flush();
  248. $is = $this->User->save();
  249. debug($this->User->validationErrors); ob_flush();
  250. $this->assertFalse($is);
  251. $this->User->create();
  252. $data = array(
  253. 'id' => $uid,
  254. 'pwd_current' => 'somex',
  255. 'pwd' => '123456',
  256. 'pwd_repeat' => '123456'
  257. );
  258. $this->User->set($data);
  259. //debug($this->User->validationErrors); ob_flush();
  260. $is = $this->User->save();
  261. $this->assertFalse($is);
  262. $this->User->create();
  263. $data = array(
  264. 'id' => $uid,
  265. 'pwd_current' => 'some',
  266. 'pwd' => '123456',
  267. 'pwd_repeat' => '123456'
  268. );
  269. $this->User->set($data);
  270. //debug($this->User->validationErrors); ob_flush();
  271. $is = $this->User->save();
  272. $this->assertTrue(!empty($is));
  273. }
  274. }
  275. /**
  276. * FAKER!
  277. * 2011-11-03 ms
  278. */
  279. class AuthTestComponent {
  280. public function identify($request, $response) {
  281. $user = $request->data['User'];
  282. if ($user['id'] == '5' && $user['password'] == 'some') {
  283. return true;
  284. }
  285. return false;
  286. }
  287. }