PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/application/tests/suite/Controllers/UsersFormTest.php

https://github.com/waynegraham/Omeka
PHP | 199 lines | 168 code | 14 blank | 17 comment | 2 complexity | 05acb6d6012956590ef144fd7af3ee37 MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Roy Rosenzweig Center for History and New Media, 2007-2010
  4. * @license http://www.gnu.org/licenses/gpl-3.0.txt
  5. * @package Omeka
  6. */
  7. /**
  8. *
  9. *
  10. * @package Omeka
  11. * @copyright Roy Rosenzweig Center for History and New Media, 2007-2010
  12. */
  13. class Omeka_Controllers_UsersFormTest extends Omeka_Test_AppTestCase
  14. {
  15. public function setUp()
  16. {
  17. parent::setUp();
  18. $this->adminUser = $this->_addNewUserWithRole('admin');
  19. $this->superUser = $this->_addNewUserWithRole('super');
  20. self::dbChanged(false);
  21. }
  22. public static function tearDownAfterClass()
  23. {
  24. self::dbChanged(true);
  25. }
  26. public function testSuperCanAccessForm()
  27. {
  28. $this->_authenticateUser($this->superUser);
  29. $this->dispatch('/users/edit/' . $this->currentuser->id);
  30. $this->assertController('users');
  31. $this->assertAction('edit', "Super users should be able to reach the 'edit' action for their user account.");
  32. }
  33. public static function formXPaths()
  34. {
  35. return array(
  36. array('//input[@id="username"][@value="adminuser"]',
  37. "There should be a 'username' element on this form with a default "
  38. . "value."),
  39. array(
  40. '//input[@id="name"][@value="Admin User"]',
  41. "There should be a 'name' element on this form with a default "
  42. . "value."),
  43. array(
  44. '//input[@id="email"][@value="admin@example.com"]',
  45. "There should be a 'email' element on this form with a default value.")
  46. );
  47. }
  48. public static function formQueries()
  49. {
  50. return array(
  51. array("form select#role", "There should be a 'role' select on this "
  52. . "form."),
  53. array('form input[name="active"]', "There should be an 'active' "
  54. . "element on this form."),
  55. array('form input[type="submit"]', "There should be a submit button on "
  56. . "this form."),
  57. );
  58. }
  59. /**
  60. * @dataProvider formXPaths
  61. */
  62. public function testFormXPath($xPath, $failMsg)
  63. {
  64. $this->_authenticateUser($this->superUser);
  65. $this->dispatch('/users/edit/' . $this->adminUser->id);
  66. $this->assertXpath($xPath, $failMsg);
  67. }
  68. /**
  69. * @dataProvider formQueries
  70. */
  71. public function testFormQuery($query, $failMsg)
  72. {
  73. $this->_authenticateUser($this->superUser);
  74. $this->dispatch('/users/edit/' . $this->adminUser->id);
  75. $this->assertQuery($query, $failMsg);
  76. }
  77. public function testChangeOtherUsersAccountInfoAsSuperUser()
  78. {
  79. $expectedUsername = 'newuser' . mt_rand();
  80. $this->_authenticateUser($this->superUser);
  81. $this->request->setPost(array(
  82. 'username' => $expectedUsername,
  83. 'name' => 'foobar',
  84. 'email' => 'admin' . mt_rand() . '@example.com',
  85. 'role' => 'admin',
  86. 'active' => '1'
  87. ));
  88. $this->request->setMethod('post');
  89. $this->dispatch('/users/edit/' . $this->adminUser->id);
  90. $newUsername = $this->db->getTable('User')->find($this->adminUser->id)->username;
  91. $this->assertEquals($expectedUsername, $newUsername);
  92. $this->assertRedirectTo('/users/browse');
  93. }
  94. public function testChangeOwnUserAccountInfo()
  95. {
  96. $user = $this->superUser;
  97. $this->_authenticateUser($user);
  98. $this->request->setPost(array(
  99. 'username' => 'newusername',
  100. 'name' => 'foobar foobar',
  101. 'email' => 'foobar' . mt_rand() . '@example.com',
  102. 'active' => '1',
  103. ));
  104. $this->request->setMethod('post');
  105. $this->dispatch('/users/edit/' . $this->currentuser->id);
  106. $this->assertRedirectTo('/');
  107. $changedUser = $this->db->getTable('User')->find($user->id);
  108. $this->assertEquals("newusername", $changedUser->username);
  109. }
  110. public function testGivingInvalidEmailCausesValidationError()
  111. {
  112. $this->_authenticateUser($this->superUser);
  113. $this->request->setPost(array(
  114. 'username' => 'newusername',
  115. 'first_name' => 'foobar foobar',
  116. 'email' => 'invalid.email',
  117. 'role' => 'super',
  118. 'active' => '1'
  119. ));
  120. $this->request->setMethod('post');
  121. $this->dispatch('/users/edit/' . $this->adminUser->id);
  122. $this->assertNotRedirect("This should not have redirected since the form submission was invalid.");
  123. $this->assertQueryContentContains('ul.errors', "email address is invalid",
  124. "Form should contain an error message indicating that the email address provided was invalid.");
  125. }
  126. public function testCannotSetActiveFlagOrRoleFieldWithoutAdequatePermissions()
  127. {
  128. $this->_authenticateUser($this->adminUser);
  129. $this->request->setPost(array(
  130. 'username' => 'newusername',
  131. 'name' => 'foobar foobar',
  132. 'email' => 'foobar@example.com',
  133. 'role' => 'super',
  134. 'active' => '0'
  135. ));
  136. $this->request->setMethod('post');
  137. $this->dispatch('/users/edit/' . $this->adminUser->id);
  138. $newAdminUser = $this->db->getTable('User')->find($this->adminUser->id);
  139. $this->assertEquals($newAdminUser->role, 'admin', "User role should not have been changed from admin to super.");
  140. $this->assertEquals($newAdminUser->active, 1, "User status should not have been changed from active to inactive.");
  141. }
  142. public function testCannotEverChangeSaltOrPasswordFields()
  143. {
  144. $user = $this->adminUser;
  145. $this->_authenticateUser($user);
  146. $this->request->setPost(array(
  147. 'username' => 'newusername',
  148. 'name' => 'foobar foobar',
  149. 'email' => 'foobar@example.com',
  150. 'role' => 'super',
  151. 'active' => '1',
  152. 'salt' => 'foobar',
  153. 'password' => 'some-arbitrary-hash'
  154. ));
  155. $this->request->setMethod('post');
  156. $this->dispatch('/users/edit/' . $this->currentuser->id);
  157. $changedUser = $this->db->getTable('User')->find($user->id);
  158. $this->assertEquals($user->salt, $changedUser->salt,
  159. "Salt should not have changed.");
  160. $this->assertEquals($user->password, $changedUser->password,
  161. "Hashed password should not have changed.");
  162. }
  163. private function _addNewUserWithRole($role)
  164. {
  165. $username = $role . 'user';
  166. $existingUser = $this->_getUser($username);
  167. if ($existingUser) {
  168. $existingUser->delete();
  169. release_object($existingUser);
  170. }
  171. $newUser = new User;
  172. $newUser->username = $username;
  173. $newUser->setPassword('foobar');
  174. $newUser->role = $role;
  175. $newUser->active = 1;
  176. $newUser->name = ucwords($role) . ' User';
  177. $newUser->email = $role . '@example.com';
  178. $newUser->save();
  179. return $newUser;
  180. }
  181. private function _getUser($username)
  182. {
  183. return $this->db->getTable('User')->findBySql("username = ?", array($username), true);
  184. }
  185. }