PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Users/tests/TestCase/Model/UserTest.php

http://github.com/croogo/croogo
PHP | 348 lines | 255 code | 25 blank | 68 comment | 0 complexity | 2bc63661d5df94ccab73b3d8d545160f MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. namespace Croogo\Users\Test\TestCase\Model;
  3. use Cake\Controller\Component\AuthComponent;
  4. use Croogo\Core\TestSuite\CroogoTestCase;
  5. use Croogo\Users\Model\User;
  6. /**
  7. * TestUser
  8. *
  9. */
  10. class UserTest extends CroogoTestCase
  11. {
  12. /**
  13. * Fixtures
  14. *
  15. * @var array
  16. */
  17. public $fixtures = [
  18. 'plugin.Croogo/Users.Role',
  19. 'plugin.Croogo/Users.User',
  20. 'plugin.Croogo/Users.Aco',
  21. 'plugin.Croogo/Users.Aro',
  22. 'plugin.Croogo/Users.ArosAco',
  23. ];
  24. /**
  25. * User instance
  26. *
  27. * @var TestUser
  28. */
  29. public $User;
  30. /**
  31. * setUp method
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. parent::setUp();
  38. // $this->User = ClassRegistry::init('TestUser');
  39. // $this->User->Aro->useDbConfig = 'test';
  40. }
  41. /**
  42. * tearDown method
  43. *
  44. * @return void
  45. */
  46. public function tearDown()
  47. {
  48. parent::tearDown();
  49. unset($this->User->request, $this->User);
  50. }
  51. /**
  52. * testPasswords method
  53. *
  54. * @return void
  55. */
  56. public function testPasswords()
  57. {
  58. $this->User->create([
  59. 'username' => 'new_user',
  60. 'name' => 'New User',
  61. 'role_id' => 3,
  62. 'email' => 'contact@croogo.org',
  63. 'password' => 'password',
  64. 'website' => 'http://croogo.org',
  65. 'activation_key' => md5(uniqid()),
  66. ]);
  67. $this->User->save();
  68. $this->assertEmpty($this->User->validationErrors, 'Validation error: ' . print_r($this->User->validationErrors, true));
  69. $newUser = $this->User->read();
  70. $this->assertNotEqual($newUser, false);
  71. $this->assertNotEqual($newUser['User']['password'], 'password');
  72. $this->assertEqual($newUser['User']['password'], AuthComponent::password('password'));
  73. $newUser['User']['password'] = '123456';
  74. $this->User->id = $newUser['User']['id'];
  75. $this->User->save($newUser);
  76. $this->assertEmpty($this->User->validationErrors, 'Validation error: ' . print_r($this->User->validationErrors, true));
  77. $newUser = $this->User->read();
  78. $this->assertNotEqual($newUser['User']['password'], '123456');
  79. $this->assertEqual($newUser['User']['password'], AuthComponent::password('123456'));
  80. $oldPassword = $newUser['User']['password'];
  81. $newUser['User']['password'] = '';
  82. $this->User->id = $newUser['User']['id'];
  83. $this->User->save($newUser);
  84. $this->assertContains('Passwords must be at least 6 characters long.', print_r($this->User->validationErrors, true));
  85. $newUser = $this->User->read();
  86. $this->assertEqual($newUser['User']['password'], $oldPassword);
  87. }
  88. /**
  89. * testValidIdenticalPassword method
  90. *
  91. * @return void
  92. */
  93. public function testValidIdenticalPassword()
  94. {
  95. $this->User->data['User'] = ['password' => '123456'];
  96. $this->assertTrue($this->User->validIdentical(['verify_password' => '123456']));
  97. $this->User->data['User'] = ['password' => '123456'];
  98. $this->assertContains('Passwords do not match. Please, try again.', $this->User->validIdentical(['verify_password' => 'other-value']));
  99. }
  100. /**
  101. * testDeleteLastUser method
  102. *
  103. * @return void
  104. */
  105. public function testDeleteLastUser()
  106. {
  107. $this->User->create([
  108. 'username' => 'new_user',
  109. 'name' => 'Admin User',
  110. 'role_id' => 1,
  111. 'email' => 'contact@croogo.org',
  112. 'password' => 'password',
  113. 'website' => 'http://croogo.org',
  114. 'activation_key' => md5(uniqid()),
  115. 'status' => true,
  116. ]);
  117. $this->User->save();
  118. $newUser = $this->User->read();
  119. $this->User->deleteAll(['User.id !=' => $newUser['User']['id']]);
  120. $this->assertFalse($this->User->delete($newUser['User']['id']));
  121. }
  122. /**
  123. * testDeleteAdminUser method
  124. *
  125. * @return void
  126. */
  127. public function testDeleteAdminUser()
  128. {
  129. $this->User->create([
  130. 'username' => 'admin_user',
  131. 'name' => 'Admin User',
  132. 'role_id' => 1,
  133. 'email' => 'contact@croogo.org',
  134. 'password' => 'password',
  135. 'website' => 'http://croogo.org',
  136. 'activation_key' => md5(uniqid()),
  137. 'status' => true,
  138. ]);
  139. $this->User->save();
  140. $newAdmin = $this->User->read();
  141. $this->User->create([
  142. 'username' => 'another_adm',
  143. 'name' => 'Another Admin',
  144. 'role_id' => 1,
  145. 'email' => 'another_adm@croogo.org',
  146. 'password' => 'password',
  147. 'website' => 'http://croogo.org',
  148. 'activation_key' => md5(uniqid()),
  149. 'status' => true,
  150. ]);
  151. $this->User->save();
  152. $anotherAdmin = $this->User->read();
  153. $this->User->deleteAll(['NOT' => ['User.id' => [$newAdmin['User']['id'], $anotherAdmin['User']['id']]]]);
  154. $this->assertTrue($this->User->delete($newAdmin['User']['id']));
  155. }
  156. /**
  157. * testDisplayFields
  158. *
  159. * @return void
  160. */
  161. public function testDisplayFields()
  162. {
  163. $result = $this->User->displayFields();
  164. $expected = [
  165. 'id' => [
  166. 'label' => 'Id',
  167. 'sort' => true,
  168. 'type' => 'text',
  169. 'url' => [],
  170. 'options' => [],
  171. ],
  172. 'username' => [
  173. 'label' => 'Username',
  174. 'sort' => true,
  175. 'type' => 'text',
  176. 'url' => [],
  177. 'options' => [],
  178. ],
  179. 'name' => [
  180. 'label' => 'Name',
  181. 'sort' => true,
  182. 'type' => 'text',
  183. 'url' => [],
  184. 'options' => [],
  185. ],
  186. 'email' => [
  187. 'label' => 'Email',
  188. 'sort' => true,
  189. 'type' => 'text',
  190. 'url' => [],
  191. 'options' => [],
  192. ],
  193. 'status' => [
  194. 'label' => 'Status',
  195. 'sort' => true,
  196. 'type' => 'boolean',
  197. 'url' => [],
  198. 'options' => [],
  199. ],
  200. 'Role.title' => [
  201. 'label' => 'Role',
  202. 'sort' => true,
  203. 'type' => 'text',
  204. 'url' => [],
  205. 'options' => [],
  206. ],
  207. ];
  208. $this->assertEquals($expected, $result);
  209. $result = $this->User->displayFields([
  210. 'one', 'two', 'three',
  211. ]);
  212. $expected = [
  213. 'one' => [
  214. 'label' => 'One',
  215. 'sort' => true,
  216. 'type' => 'text',
  217. 'url' => [],
  218. 'options' => [],
  219. ],
  220. 'two' => [
  221. 'label' => 'Two',
  222. 'sort' => true,
  223. 'type' => 'text',
  224. 'url' => [],
  225. 'options' => [],
  226. ],
  227. 'three' => [
  228. 'label' => 'Three',
  229. 'sort' => true,
  230. 'type' => 'text',
  231. 'url' => [],
  232. 'options' => [],
  233. ],
  234. ];
  235. $this->assertEquals($expected, $result);
  236. }
  237. /**
  238. * testEditFields
  239. *
  240. * @return void
  241. */
  242. public function testEditFields()
  243. {
  244. $result = $this->User->editFields();
  245. $expected = [
  246. 'role_id' => [],
  247. 'username' => [],
  248. 'name' => [],
  249. 'email' => [],
  250. 'website' => [],
  251. 'status' => [],
  252. ];
  253. $this->assertEquals($expected, $result);
  254. $result = $this->User->editFields([]);
  255. $expected = [
  256. 'role_id' => [],
  257. 'username' => [],
  258. 'password' => [],
  259. 'name' => [],
  260. 'email' => [],
  261. 'website' => [],
  262. 'activation_key' => [],
  263. 'image' => [],
  264. 'bio' => [],
  265. 'timezone' => [],
  266. 'status' => [],
  267. 'updated' => [],
  268. 'created' => [],
  269. 'updated_by' => [],
  270. 'created_by' => [],
  271. ];
  272. $this->assertEquals($expected, $result);
  273. $expected = [
  274. 'field' => [
  275. 'label' => 'My Field',
  276. 'type' => 'select',
  277. 'options' => [1, 2, 3],
  278. ],
  279. ];
  280. $result = $this->User->editFields($expected);
  281. $this->assertEquals($expected, $result);
  282. }
  283. /**
  284. * testDeleteAdminUsers
  285. */
  286. public function testDeleteAdminUsers()
  287. {
  288. // delete an admin
  289. $this->User->id = 2;
  290. $result = $this->User->delete();
  291. $this->assertTrue($result);
  292. // delete last remaining admin
  293. $this->User->id = 1;
  294. $result = $this->User->delete();
  295. $this->assertFalse($result);
  296. // delete normal user
  297. $this->User->id = 3;
  298. $result = $this->User->delete();
  299. $this->assertTrue($result);
  300. $count = $this->User->find('count');
  301. $this->assertEquals(1, $count);
  302. }
  303. /**
  304. * testDeleteUsers
  305. */
  306. public function testDeleteUsers()
  307. {
  308. // delete normal user
  309. $this->User->id = 3;
  310. $result = $this->User->delete();
  311. $this->assertTrue($result);
  312. // delete an admin
  313. $this->User->id = 2;
  314. $result = $this->User->delete();
  315. $this->assertTrue($result);
  316. // delete last remaining admin
  317. $this->User->id = 1;
  318. $result = $this->User->delete();
  319. $this->assertFalse($result);
  320. $count = $this->User->find('count');
  321. $this->assertEquals(1, $count);
  322. }
  323. }