PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Users/Test/Case/Controller/UsersControllerTest.php

https://github.com/kareypowell/croogo
PHP | 418 lines | 297 code | 26 blank | 95 comment | 0 complexity | cab82c28b4e748987ab06cc729fbc1dd MD5 | raw file
  1. <?php
  2. App::uses('UsersController', 'Users.Controller');
  3. App::uses('CroogoControllerTestCase', 'Croogo.TestSuite');
  4. /**
  5. * UsersController Test
  6. */
  7. class UsersControllerTest extends CroogoControllerTestCase {
  8. /**
  9. * fixtures
  10. *
  11. * @var array
  12. */
  13. public $fixtures = array(
  14. 'plugin.users.aco',
  15. 'plugin.users.aro',
  16. 'plugin.users.aros_aco',
  17. 'plugin.blocks.block',
  18. 'plugin.comments.comment',
  19. 'plugin.contacts.contact',
  20. 'plugin.translate.i18n',
  21. 'plugin.settings.language',
  22. 'plugin.menus.link',
  23. 'plugin.menus.menu',
  24. 'plugin.contacts.message',
  25. 'plugin.meta.meta',
  26. 'plugin.nodes.node',
  27. 'plugin.taxonomy.model_taxonomy',
  28. 'plugin.blocks.region',
  29. 'plugin.users.role',
  30. 'plugin.settings.setting',
  31. 'plugin.taxonomy.taxonomy',
  32. 'plugin.taxonomy.term',
  33. 'plugin.taxonomy.type',
  34. 'plugin.taxonomy.types_vocabulary',
  35. 'plugin.users.user',
  36. 'plugin.taxonomy.vocabulary',
  37. );
  38. /**
  39. * setUp
  40. *
  41. * @return void
  42. */
  43. public function setUp() {
  44. parent::setUp();
  45. $this->UsersController = $this->generate('Users.Users', array(
  46. 'methods' => array(
  47. 'redirect',
  48. 'onAdminLoginFailure',
  49. ),
  50. 'components' => array(
  51. 'Auth' => array('user', 'identify', 'login'),
  52. 'Session',
  53. 'Security',
  54. ),
  55. ));
  56. $this->controller->helpers = array(
  57. 'Html' => array(
  58. 'className' => 'Croogo.CroogoHtml',
  59. ),
  60. );
  61. $this->controller->Auth
  62. ->staticExpects($this->any())
  63. ->method('identify')
  64. ->will($this->returnCallback(array($this, 'authIdentifyFalse')));
  65. }
  66. protected function _setupAuthUser() {
  67. $this->controller->Auth
  68. ->staticExpects($this->any())
  69. ->method('user')
  70. ->will($this->returnCallback(array($this, 'authUserCallback')));
  71. }
  72. public function authIdentifyFalse() {
  73. return false;
  74. }
  75. /**
  76. * tearDown
  77. *
  78. * @return void
  79. */
  80. public function tearDown() {
  81. parent::tearDown();
  82. unset($this->UsersController);
  83. }
  84. /**
  85. * testAdminIndex
  86. *
  87. * @return void
  88. */
  89. public function testAdminIndex() {
  90. $this->_setupAuthUser();
  91. $this->testAction('/admin/users/users/index');
  92. $this->assertNotEmpty($this->vars['displayFields']);
  93. $this->assertNotEmpty($this->vars['users']);
  94. $this->assertEquals(3, count($this->vars['users']));
  95. }
  96. /**
  97. * testAdminIndexSearch
  98. *
  99. * @return void
  100. */
  101. public function testAdminIndexSearch() {
  102. $this->_setupAuthUser();
  103. $this->testAction('/admin/users/users/index?name=admin');
  104. $this->assertEquals(1, count($this->vars['users']));
  105. }
  106. /**
  107. * testAddtestAddInvalidPassword
  108. *
  109. * @return void
  110. */
  111. public function testAddInvalidPassword() {
  112. $this->_setupAuthUser();
  113. $_SERVER['SERVER_NAME'] = 'croogo.dev';
  114. $this->UsersController->Session
  115. ->expects($this->once())
  116. ->method('setFlash')
  117. ->with(
  118. $this->equalTo('The User could not be saved. Please, try again.'),
  119. $this->equalTo('default'),
  120. $this->equalTo(array('class' => 'error'))
  121. );
  122. $this->testAction('/users/users/add', array(
  123. 'data' => array(
  124. 'User' => array(
  125. 'username' => 'new_user',
  126. 'password' => '',
  127. 'email' => 'new_user@croogo.dev',
  128. 'name' => 'New User',
  129. 'website' => '',
  130. 'role_id' => 3,
  131. ),
  132. ),
  133. ));
  134. $errors = print_r($this->UsersController->User->validationErrors, true);
  135. $this->assertContains('at least 6 characters', $errors);
  136. }
  137. /**
  138. * testAddtestAddOtherErrors
  139. *
  140. * @return void
  141. */
  142. public function testAddtestAddOtherErrors() {
  143. $this->_setupAuthUser();
  144. $_SERVER['SERVER_NAME'] = 'croogo.dev';
  145. $this->UsersController->Session
  146. ->expects($this->once())
  147. ->method('setFlash')
  148. ->with(
  149. $this->equalTo('The User could not be saved. Please, try again.'),
  150. $this->equalTo('default'),
  151. $this->equalTo(array('class' => 'error'))
  152. );
  153. $this->testAction('/users/users/add', array(
  154. 'data' => array(
  155. 'User' => array(
  156. 'username' => 'admin',
  157. 'password' => 'yvonne',
  158. 'verify_password' => 'strahovski',
  159. 'email' => '123456',
  160. 'name' => 'New User',
  161. 'website' => '',
  162. 'role_id' => 3,
  163. ),
  164. ),
  165. ));
  166. $errors = print_r($this->UsersController->User->validationErrors, true);
  167. $this->assertContains('do not match', $errors);
  168. $this->assertContains('valid email', $errors);
  169. $this->assertContains('been taken', $errors);
  170. }
  171. /**
  172. * testAdminAdd
  173. *
  174. * @return void
  175. */
  176. public function testAdminAdd() {
  177. $this->_setupAuthUser();
  178. $this->expectFlashAndRedirect('The User has been saved');
  179. $this->testAction('/admin/users/users/add', array(
  180. 'data' => array(
  181. 'User' => array(
  182. 'username' => 'new_user',
  183. 'password' => uniqid(),
  184. 'email' => 'new_user@croogo.dev',
  185. 'name' => 'New User',
  186. 'role_id' => 3,
  187. ),
  188. ),
  189. ));
  190. $newUser = $this->UsersController->User->findByUsername('new_user');
  191. $this->assertEqual($newUser['User']['name'], 'New User');
  192. }
  193. /**
  194. * testAdminEdit
  195. *
  196. * @return void
  197. */
  198. public function testAdminEdit() {
  199. $this->_setupAuthUser();
  200. $this->expectFlashAndRedirect('The User has been saved');
  201. $this->testAction('/admin/users/users/edit/1', array(
  202. 'data' => array(
  203. 'User' => array(
  204. 'id' => 1, // admin
  205. 'name' => 'Administrator [modified]',
  206. 'role_id' => 1,
  207. ),
  208. ),
  209. ));
  210. $expected = 'Administrator [modified]';
  211. $this->assertEquals($expected, $this->controller->request->data['User']['name']);
  212. $result = $this->controller->User->findByUsername('admin');
  213. $this->assertEquals($expected, $result['User']['name']);
  214. }
  215. /**
  216. * testAdminResetPassword
  217. *
  218. * @return void
  219. */
  220. public function testAdminResetPassword() {
  221. $this->_setupAuthUser();
  222. $this->expectFlashAndRedirect('Password has been reset.');
  223. $this->testAction('/admin/users/users/reset_password/1', array(
  224. 'data' => array(
  225. 'User' => array(
  226. 'id' => 1,
  227. 'password' => 'foobar',
  228. 'verify_password' => 'foobar',
  229. ),
  230. ),
  231. ));
  232. }
  233. /**
  234. * testAdminResetPasswordValidationErrors
  235. *
  236. * @return void
  237. */
  238. public function testAdminResetPasswordValidationErrors() {
  239. $this->_setupAuthUser();
  240. $result = $this->testAction('/admin/users/users/reset_password/1', array(
  241. 'data' => array(
  242. 'User' => array(
  243. 'id' => 1,
  244. 'password' => '123',
  245. 'verify_password' => '123',
  246. ),
  247. ),
  248. 'return' => 'view',
  249. ));
  250. $this->assertContains('Passwords must be at least 6 characters long.', $result);
  251. }
  252. /**
  253. * testAdminDelete
  254. *
  255. * @return void
  256. */
  257. public function testAdminDelete() {
  258. $this->_setupAuthUser();
  259. $this->expectFlashAndRedirect('User deleted');
  260. $this->testAction('/admin/users/users/delete/2'); // ID of rchavik
  261. $hasAny = $this->UsersController->User->hasAny(array(
  262. 'User.username' => 'rchavik',
  263. ));
  264. $this->assertFalse($hasAny);
  265. }
  266. /**
  267. * testAdminDeleteCurrentUser
  268. *
  269. * @return void
  270. */
  271. public function testAdminDeleteCurrentUser() {
  272. $this->_setupAuthUser();
  273. // check that another admin exists
  274. $hasAny = $this->UsersController->User->hasAny(array(
  275. 'User.username' => 'rchavik',
  276. 'User.role_id' => 1,
  277. ));
  278. $this->assertTrue($hasAny);
  279. // delete admin
  280. $this->expectFlashAndRedirect('User deleted');
  281. $this->testAction('/admin/users/users/delete/1'); // ID of admin
  282. $hasAny = $this->UsersController->User->hasAny(array(
  283. 'User.role_id' => 1,
  284. ));
  285. $this->assertTrue($hasAny);
  286. }
  287. /**
  288. * testResetPasswordWithValidInfo
  289. *
  290. * @return void
  291. */
  292. public function testResetPasswordWithValidInfo() {
  293. $this->_setupAuthUser();
  294. $this->testAction(
  295. sprintf('/users/users/reset/%s/%s', 'yvonne', '92e35177eba73c6524d4561d3047c0c2')
  296. );
  297. $this->assertTrue(isset($this->vars['key']));
  298. }
  299. /**
  300. * testResetPasswordWithInvalidInfo
  301. *
  302. * @return void
  303. */
  304. public function testResetPasswordWithInvalidInfo() {
  305. $this->_setupAuthUser();
  306. $this->UsersController->Session
  307. ->expects($this->once())
  308. ->method('setFlash')
  309. ->with(
  310. $this->equalTo('An error occurred.'),
  311. $this->equalTo('default'),
  312. $this->equalTo(array('class' => 'error'))
  313. );
  314. $this->UsersController
  315. ->expects($this->once())
  316. ->method('redirect');
  317. $this->testAction(
  318. sprintf('/users/users/reset/%s/%s', 'yvonne', 'invalid')
  319. );
  320. }
  321. /**
  322. * testResetPasswordUpdatesPassword
  323. *
  324. * @return void
  325. */
  326. public function testResetPasswordUpdatesPassword() {
  327. $this->_setupAuthUser();
  328. $this->testAction(
  329. sprintf('/users/users/reset/%s/%s', 'yvonne', '92e35177eba73c6524d4561d3047c0c2'),
  330. array(
  331. 'data' => array(
  332. 'User' => array(
  333. 'password' => 'newpassword',
  334. 'verify_password' => 'newpassword',
  335. )
  336. )
  337. )
  338. );
  339. $user = $this->UsersController->User->findByUsername('yvonne');
  340. $expected = AuthComponent::password('newpassword');
  341. $this->assertEqual($expected, $user['User']['password'], sprintf("%s to be %s", $user['User']['password'], $expected));
  342. }
  343. /**
  344. * testResetPasswordWithMismatchValues
  345. *
  346. * @return void
  347. */
  348. public function testResetPasswordWithMismatchValues() {
  349. $this->_setupAuthUser();
  350. $this->testAction(
  351. sprintf('/users/users/reset/%s/%s', 'yvonne', '92e35177eba73c6524d4561d3047c0c2'),
  352. array(
  353. 'return' => 'contents',
  354. 'data' => array(
  355. 'User' => array(
  356. 'id' => 3,
  357. 'password' => 'otherpassword',
  358. 'verify_password' => 'other password',
  359. )
  360. )
  361. )
  362. );
  363. $this->assertContains('Passwords do not match', $this->contents);
  364. }
  365. /**
  366. * testAdminLoginFailureEvent
  367. *
  368. * @return void
  369. */
  370. public function testAdminLoginFailureEvent() {
  371. $this->controller->Auth->request = $this->controller->request;
  372. $this->controller->Auth->response = $this->controller->response;
  373. $this->controller->Auth->Session = $this->controller->Session;
  374. $this->controller->expects($this->once())
  375. ->method('onAdminLoginFailure')
  376. ->will($this->returnValue(true));
  377. $this->testAction(
  378. '/admin/users/users/login',
  379. array(
  380. 'method' => 'POST',
  381. 'return' => 'result',
  382. 'data' => array(
  383. 'User' => array(
  384. 'username' => 'orange',
  385. 'password' => 'banana',
  386. 'verify_password' => 'banana',
  387. )
  388. )
  389. )
  390. );
  391. }
  392. }