ElggUserTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. unset($this->user);
  27. }
  28. /**
  29. * Called after each test object.
  30. */
  31. public function __destruct() {
  32. // all code should go above here
  33. parent::__destruct();
  34. }
  35. public function testElggUserConstructor() {
  36. $attributes = array();
  37. $attributes['guid'] = null;
  38. $attributes['type'] = 'user';
  39. $attributes['subtype'] = null;
  40. $attributes['owner_guid'] = elgg_get_logged_in_user_guid();
  41. $attributes['container_guid'] = elgg_get_logged_in_user_guid();
  42. $attributes['site_guid'] = null;
  43. $attributes['access_id'] = ACCESS_PRIVATE;
  44. $attributes['time_created'] = null;
  45. $attributes['time_updated'] = null;
  46. $attributes['last_action'] = null;
  47. $attributes['enabled'] = 'yes';
  48. $attributes['name'] = null;
  49. $attributes['username'] = null;
  50. $attributes['password'] = null;
  51. $attributes['salt'] = null;
  52. $attributes['password_hash'] = null;
  53. $attributes['email'] = null;
  54. $attributes['language'] = null;
  55. $attributes['banned'] = 'no';
  56. $attributes['admin'] = 'no';
  57. $attributes['prev_last_action'] = null;
  58. $attributes['last_login'] = null;
  59. $attributes['prev_last_login'] = null;
  60. ksort($attributes);
  61. $entity_attributes = $this->user->expose_attributes();
  62. ksort($entity_attributes);
  63. $this->assertIdentical($entity_attributes, $attributes);
  64. }
  65. public function testElggUserLoad() {
  66. // new object
  67. $object = new \ElggObject();
  68. $this->AssertEqual($object->getGUID(), 0);
  69. $guid = $object->save();
  70. $this->AssertNotEqual($guid, 0);
  71. // fail on wrong type
  72. $this->assertFalse(get_user($guid));
  73. // clean up
  74. $object->delete();
  75. }
  76. public function testElggUserConstructorWithGarbage() {
  77. try {
  78. $error = new \ElggUserTest(array('invalid'));
  79. $this->assertTrue(false);
  80. } catch (Exception $e) {
  81. $this->assertIsA($e, 'InvalidParameterException');
  82. }
  83. }
  84. public function testElggUserConstructorByDbRow() {
  85. $row = $this->fetchUser(elgg_get_logged_in_user_guid());
  86. $user = new \ElggUser($row);
  87. $this->assertIdenticalEntities($user, $_SESSION['user']);
  88. }
  89. public function testElggUserSave() {
  90. // new object
  91. $this->AssertEqual($this->user->getGUID(), 0);
  92. $guid = $this->user->save();
  93. $this->AssertNotEqual($guid, 0);
  94. // clean up
  95. $this->user->delete();
  96. }
  97. public function testElggUserDelete() {
  98. $guid = $this->user->save();
  99. // delete object
  100. $this->assertIdentical(true, $this->user->delete());
  101. // check GUID not in database
  102. $this->assertFalse($this->fetchUser($guid));
  103. }
  104. public function testElggUserNameCache() {
  105. // issue https://github.com/elgg/elgg/issues/1305
  106. // very unlikely a user would have this username
  107. $name = (string)time();
  108. $this->user->username = $name;
  109. $guid = $this->user->save();
  110. $user = get_user_by_username($name);
  111. $user->delete();
  112. $user = get_user_by_username($name);
  113. $this->assertFalse($user);
  114. }
  115. public function testGetUserByUsernameAcceptsUrlEncoded() {
  116. $username = (string)time();
  117. $this->user->username = $username;
  118. $guid = $this->user->save();
  119. // percent encode first letter
  120. $first_letter = $username[0];
  121. $first_letter = str_pad('%' . dechex(ord($first_letter)), 2, '0', STR_PAD_LEFT);
  122. $username = $first_letter . substr($username, 1);
  123. $user = get_user_by_username($username);
  124. $this->assertTrue((bool) $user);
  125. $this->assertEqual($guid, $user->guid);
  126. $this->user->delete();
  127. }
  128. public function testElggUserMakeAdmin() {
  129. global $CONFIG;
  130. // need to save user to have a guid
  131. $guid = $this->user->save();
  132. $this->assertTrue($this->user->makeAdmin());
  133. $row = _elgg_services()->db->getDataRow("
  134. SELECT admin FROM {$CONFIG->dbprefix}users_entity WHERE guid = $guid
  135. ");
  136. $this->assertEqual($row->admin, 'yes');
  137. $this->user->delete();
  138. }
  139. public function testElggUserRemoveAdmin() {
  140. global $CONFIG;
  141. // need to save user to have a guid
  142. $guid = $this->user->save();
  143. $this->assertTrue($this->user->removeAdmin());
  144. $row = _elgg_services()->db->getDataRow("
  145. SELECT admin FROM {$CONFIG->dbprefix}users_entity WHERE guid = $guid
  146. ");
  147. $this->assertEqual($row->admin, 'no');
  148. $this->user->delete();
  149. }
  150. public function testElggUserIsAdmin() {
  151. // need to grab a real user with a guid and everything.
  152. $guid = $this->user->save();
  153. $this->assertTrue($this->user->makeAdmin());
  154. // this is testing the function, not the SQL.
  155. // that's been tested above.
  156. $this->assertTrue($this->user->isAdmin());
  157. $this->user->delete();
  158. }
  159. public function testElggUserIsNotAdmin() {
  160. // need to grab a real user with a guid and everything.
  161. $guid = $this->user->save();
  162. $this->assertTrue($this->user->removeAdmin());
  163. // this is testing the function, not the SQL.
  164. // that's been tested above.
  165. $this->assertFalse($this->user->isAdmin());
  166. $this->user->delete();
  167. }
  168. protected function fetchUser($guid) {
  169. global $CONFIG;
  170. return get_data_row("SELECT * FROM {$CONFIG->dbprefix}users_entity WHERE guid = '$guid'");
  171. }
  172. }
  173. class ElggUserTest extends \ElggUser {
  174. public function expose_attributes() {
  175. return $this->attributes;
  176. }
  177. }