/engine/tests/objects/users.php

https://github.com/wangaiying/elgg4ysu · PHP · 234 lines · 143 code · 49 blank · 42 comment · 0 complexity · adf5568100dccfc4c7c21b8351e13fd4 MD5 · raw file

  1. <?php
  2. /**
  3. * Elgg Test ElggUser
  4. *
  5. * @package Elgg
  6. * @subpackage Test
  7. */
  8. class ElggCoreUserTest extends ElggCoreUnitTest {
  9. /**
  10. * Called before each test object.
  11. */
  12. public function __construct() {
  13. parent::__construct();
  14. // all code should come after here
  15. }
  16. /**
  17. * Called before each test method.
  18. */
  19. public function setUp() {
  20. $this->user = new ElggUserTest();
  21. }
  22. /**
  23. * Called after each test method.
  24. */
  25. public function tearDown() {
  26. // do not allow SimpleTest to interpret Elgg notices as exceptions
  27. $this->swallowErrors();
  28. unset($this->user);
  29. }
  30. /**
  31. * Called after each test object.
  32. */
  33. public function __destruct() {
  34. // all code should go above here
  35. parent::__destruct();
  36. }
  37. /**
  38. * A basic test that will be called and fail.
  39. */
  40. public function testElggUserConstructor() {
  41. $attributes = array();
  42. $attributes['guid'] = NULL;
  43. $attributes['type'] = 'user';
  44. $attributes['subtype'] = NULL;
  45. $attributes['owner_guid'] = elgg_get_logged_in_user_guid();
  46. $attributes['container_guid'] = elgg_get_logged_in_user_guid();
  47. $attributes['site_guid'] = NULL;
  48. $attributes['access_id'] = ACCESS_PRIVATE;
  49. $attributes['time_created'] = NULL;
  50. $attributes['time_updated'] = NULL;
  51. $attributes['last_action'] = NULL;
  52. $attributes['enabled'] = 'yes';
  53. $attributes['tables_split'] = 2;
  54. $attributes['tables_loaded'] = 0;
  55. $attributes['name'] = NULL;
  56. $attributes['username'] = NULL;
  57. $attributes['password'] = NULL;
  58. $attributes['salt'] = NULL;
  59. $attributes['email'] = NULL;
  60. $attributes['language'] = NULL;
  61. $attributes['code'] = NULL;
  62. $attributes['banned'] = 'no';
  63. $attributes['admin'] = 'no';
  64. ksort($attributes);
  65. $entity_attributes = $this->user->expose_attributes();
  66. ksort($entity_attributes);
  67. $this->assertIdentical($entity_attributes, $attributes);
  68. }
  69. public function testElggUserLoad() {
  70. // new object
  71. $object = new ElggObject();
  72. $this->AssertEqual($object->getGUID(), 0);
  73. $guid = $object->save();
  74. $this->AssertNotEqual($guid, 0);
  75. // fail on wrong type
  76. try {
  77. $error = new ElggUserTest($guid);
  78. $this->assertTrue(FALSE);
  79. } catch (Exception $e) {
  80. $this->assertIsA($e, 'InvalidClassException');
  81. $message = sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $guid, 'ElggUser');
  82. $this->assertIdentical($e->getMessage(), $message);
  83. }
  84. // clean up
  85. $object->delete();
  86. }
  87. public function testElggUserConstructorByGuid() {
  88. $user = new ElggUser(elgg_get_logged_in_user_guid());
  89. $this->assertIdentical($user, $_SESSION['user']);
  90. // fail with garbage
  91. try {
  92. $error = new ElggUserTest(array('invalid'));
  93. $this->assertTrue(FALSE);
  94. } catch (Exception $e) {
  95. $this->assertIsA($e, 'InvalidParameterException');
  96. $message = sprintf(elgg_echo('InvalidParameterException:UnrecognisedValue'));
  97. $this->assertIdentical($e->getMessage(), $message);
  98. }
  99. }
  100. public function testElggUserConstructorByDbRow() {
  101. $row = $this->fetchUser(elgg_get_logged_in_user_guid());
  102. $user = new ElggUser($row);
  103. $this->assertIdentical($user, $_SESSION['user']);
  104. }
  105. public function testElggUserConstructorByUsername() {
  106. $row = $this->fetchUser(elgg_get_logged_in_user_guid());
  107. $user = new ElggUser($row->username);
  108. $this->assertIdentical($user, $_SESSION['user']);
  109. }
  110. public function testElggUserSave() {
  111. // new object
  112. $this->AssertEqual($this->user->getGUID(), 0);
  113. $guid = $this->user->save();
  114. $this->AssertNotEqual($guid, 0);
  115. // clean up
  116. $this->user->delete();
  117. }
  118. public function testElggUserDelete() {
  119. $guid = $this->user->save();
  120. // delete object
  121. $this->assertTrue($this->user->delete());
  122. // check GUID not in database
  123. $this->assertFalse($this->fetchUser($guid));
  124. }
  125. public function testElggUserNameCache() {
  126. // Trac #1305
  127. // very unlikely a user would have this username
  128. $name = (string)time();
  129. $this->user->username = $name;
  130. $guid = $this->user->save();
  131. $user = get_user_by_username($name);
  132. $user->delete();
  133. $user = get_user_by_username($name);
  134. $this->assertFalse($user);
  135. }
  136. public function testElggUserMakeAdmin() {
  137. global $CONFIG;
  138. // need to save user to have a guid
  139. $guid = $this->user->save();
  140. $this->assertTrue($this->user->makeAdmin());
  141. $q = "SELECT admin FROM {$CONFIG->dbprefix}users_entity WHERE guid = $guid";
  142. $r = mysql_query($q);
  143. $admin = mysql_fetch_assoc($r);
  144. $this->assertEqual($admin['admin'], 'yes');
  145. $this->user->delete();
  146. }
  147. public function testElggUserRemoveAdmin() {
  148. global $CONFIG;
  149. // need to save user to have a guid
  150. $guid = $this->user->save();
  151. $this->assertTrue($this->user->removeAdmin());
  152. $q = "SELECT admin FROM {$CONFIG->dbprefix}users_entity WHERE guid = $guid";
  153. $r = mysql_query($q);
  154. $admin = mysql_fetch_assoc($r);
  155. $this->assertEqual($admin['admin'], 'no');
  156. $this->user->delete();
  157. }
  158. public function testElggUserIsAdmin() {
  159. // need to grab a real user with a guid and everything.
  160. $guid = $this->user->save();
  161. $this->assertTrue($this->user->makeAdmin());
  162. // this is testing the function, not the SQL.
  163. // that's been tested above.
  164. $this->assertTrue($this->user->isAdmin());
  165. $this->user->delete();
  166. }
  167. public function testElggUserIsNotAdmin() {
  168. // need to grab a real user with a guid and everything.
  169. $guid = $this->user->save();
  170. $this->assertTrue($this->user->removeAdmin());
  171. // this is testing the function, not the SQL.
  172. // that's been tested above.
  173. $this->assertFalse($this->user->isAdmin());
  174. $this->user->delete();
  175. }
  176. protected function fetchUser($guid) {
  177. global $CONFIG;
  178. return get_data_row("SELECT * FROM {$CONFIG->dbprefix}users_entity WHERE guid = '$guid'");
  179. }
  180. }
  181. class ElggUserTest extends ElggUser {
  182. public function expose_attributes() {
  183. return $this->attributes;
  184. }
  185. }