UserTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace tests;
  3. class UserTest extends Base
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. public function testCreate()
  10. {
  11. $username = Utils::randomUserName();
  12. $password = Utils::randomPassword();
  13. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  14. $this->assertArrayHasKey('uuid', $this->user->get($username));
  15. }
  16. public function testBatchCreate()
  17. {
  18. $randomUsername = Utils::randomUserName();
  19. $randomUsername1 = Utils::randomUserName();
  20. $randomPassword = Utils::randomPassword();
  21. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $randomUsername, 'password' => $randomPassword), array('username' => $randomUsername1, 'password' => $randomPassword))));
  22. $this->assertArrayHasKey('uuid', $this->user->get($randomUsername));
  23. $this->assertArrayHasKey('uuid', $this->user->get($randomUsername1));
  24. }
  25. public function testUserLifeCycles()
  26. {
  27. $username = Utils::randomUserName();
  28. $password = Utils::randomPassword();
  29. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  30. $this->assertArrayHasKey('uuid', $this->user->get($username));
  31. $this->assertTrue($this->user->delete($username));
  32. $result = $this->user->get($username);
  33. $this->assertEquals(404, $result['code']);
  34. }
  35. public function testUserForceLogout()
  36. {
  37. $username = Utils::randomUserName();
  38. $password = Utils::randomPassword();
  39. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  40. $this->assertIsBool($this->user->forceLogoutAllDevices($username));
  41. $this->assertTrue($this->user->delete($username));
  42. }
  43. public function testUserUpdatePassword()
  44. {
  45. $username = Utils::randomUserName();
  46. $password = Utils::randomPassword();
  47. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  48. $this->assertIsBool($this->user->updateUserPassword($username, 'password'));
  49. $this->assertTrue($this->user->delete($username));
  50. }
  51. public function testUserListUsers()
  52. {
  53. $username = Utils::randomUserName();
  54. $password = Utils::randomPassword();
  55. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  56. $data = $this->user->listUsers(1);
  57. $this->assertArrayHasKey('data', $data);
  58. $this->assertEquals(1, count($data['data']));
  59. $this->assertArrayHasKey('uuid', $data['data'][0]);
  60. $this->assertTrue($this->user->delete($username));
  61. }
  62. public function testUserContactLifeCycles()
  63. {
  64. $username = Utils::randomUserName();
  65. $password = Utils::randomPassword();
  66. $contactUsername = Utils::randomUserName();
  67. $contactPassword = Utils::randomPassword();
  68. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $username, 'password' => $password), array('username' => $contactUsername, 'password' => $contactPassword))));
  69. $this->assertIsBool($this->contact->add($username, $contactUsername));
  70. $data = $this->contact->get($username);
  71. $this->assertArrayNotHasKey('code', $data);
  72. $this->assertEquals(1, count($data));
  73. $this->assertEquals($data[0], $contactUsername);
  74. $this->assertIsBool($this->contact->remove($username, $contactUsername));
  75. $data = $this->contact->get($username);
  76. $this->assertArrayNotHasKey('code', $data);
  77. $this->assertEquals(0, count($data));
  78. $this->assertTrue($this->user->delete($username));
  79. $this->assertTrue($this->user->delete($contactUsername));
  80. }
  81. public function testUserGetUsersBlockedFromSendMsg()
  82. {
  83. $username = Utils::randomUserName();
  84. $password = Utils::randomPassword();
  85. $randomUsernameCodeJack = Utils::randomUserName();
  86. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $username, 'password' => $password), array('username' => $randomUsernameCodeJack, 'password' => $password))));
  87. $this->assertTrue($this->block->blockUserSendMsgToUser($username, array($randomUsernameCodeJack)));
  88. $data = $this->block->getUsersBlockedFromSendMsgToUser($username);
  89. $this->assertArrayNotHasKey('code', $data);
  90. $this->assertEquals(1, count($data));
  91. $this->assertEquals($data[0], $randomUsernameCodeJack);
  92. $this->assertTrue($this->user->delete($username));
  93. $this->assertTrue($this->user->delete($randomUsernameCodeJack));
  94. }
  95. public function testUserBlockUserSendMsg()
  96. {
  97. $username = Utils::randomUserName();
  98. $password = Utils::randomPassword();
  99. $randomUsernameCodeJack = Utils::randomUserName();
  100. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $username, 'password' => $password), array('username' => $randomUsernameCodeJack, 'password' => $password))));
  101. $this->assertIsBool($this->contact->add($username, $randomUsernameCodeJack));
  102. $data = $this->contact->get($username);
  103. $this->assertArrayNotHasKey('code', $data);
  104. $this->assertEquals(1, count($data));
  105. $this->assertEquals($data[0], $randomUsernameCodeJack);
  106. $this->assertTrue($this->block->blockUserSendMsgToUser($username, array($randomUsernameCodeJack)));
  107. $data = $this->block->getUsersBlockedFromSendMsgToUser($username);
  108. $this->assertArrayNotHasKey('code', $data);
  109. $this->assertEquals(1, count($data));
  110. $this->assertEquals($data[0], $randomUsernameCodeJack);
  111. $data = $this->contact->get($username);
  112. $this->assertArrayNotHasKey('code', $data);
  113. $this->assertEquals(1, count($data));
  114. $this->assertEquals($data[0], $randomUsernameCodeJack);
  115. $this->assertTrue($this->user->delete($username));
  116. $this->assertTrue($this->user->delete($randomUsernameCodeJack));
  117. }
  118. public function testUserUnblockUserSendMsg()
  119. {
  120. $username = Utils::randomUserName();
  121. $password = Utils::randomPassword();
  122. $randomUsernameCodeJack = Utils::randomUserName();
  123. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $username, 'password' => $password), array('username' => $randomUsernameCodeJack, 'password' => $password))));
  124. $this->assertIsBool($this->contact->add($username, $randomUsernameCodeJack));
  125. $data = $this->contact->get($username);
  126. $this->assertArrayNotHasKey('code', $data);
  127. $this->assertEquals(1, count($data));
  128. $this->assertEquals($data[0], $randomUsernameCodeJack);
  129. $this->assertTrue($this->block->blockUserSendMsgToUser($username, array($randomUsernameCodeJack)));
  130. $data = $this->block->getUsersBlockedFromSendMsgToUser($username);
  131. $this->assertArrayNotHasKey('code', $data);
  132. $this->assertEquals(1, count($data));
  133. $this->assertEquals($data[0], $randomUsernameCodeJack);
  134. $this->assertTrue($this->block->unblockUserSendMsgToUser($username, $randomUsernameCodeJack));
  135. $data = $this->block->getUsersBlockedFromSendMsgToUser($username);
  136. $this->assertArrayNotHasKey('code', $data);
  137. $this->assertEquals(0, count($data));
  138. $data = $this->contact->get($username);
  139. $this->assertArrayNotHasKey('code', $data);
  140. $this->assertEquals(1, count($data));
  141. $this->assertEquals($data[0], $randomUsernameCodeJack);
  142. $this->assertTrue($this->user->delete($username));
  143. $this->assertTrue($this->user->delete($randomUsernameCodeJack));
  144. }
  145. public function testUserCountMissedMessages()
  146. {
  147. $username = Utils::randomUserName();
  148. $password = Utils::randomPassword();
  149. $randomUsernameCodeJack = Utils::randomUserName();
  150. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $username, 'password' => $password), array('username' => $randomUsernameCodeJack, 'password' => $password))));
  151. $this->assertArrayNotHasKey('code', $this->message->text('users', array($randomUsernameCodeJack), array('msg' => 'CountMissedMessages'), $username));
  152. $data = $this->message->countMissedMessages($randomUsernameCodeJack);
  153. $this->assertIsInt($data);
  154. $this->assertEquals(1, $data);
  155. $this->assertTrue($this->user->delete($username));
  156. $this->assertTrue($this->user->delete($randomUsernameCodeJack));
  157. }
  158. public function testUserBlockLogin()
  159. {
  160. $username = Utils::randomUserName();
  161. $password = Utils::randomPassword();
  162. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  163. $this->assertTrue($this->block->blockUserLogin($username));
  164. $result = $this->user->get($username);
  165. $this->assertFalse($result['activated']);
  166. $this->assertTrue($this->block->unblockUserLogin($username));
  167. $result = $this->user->get($username);
  168. $this->assertTrue($result['activated']);
  169. $this->assertTrue($this->user->delete($username));
  170. }
  171. public function testUserOnlineStatus()
  172. {
  173. $username = Utils::randomUserName();
  174. $password = Utils::randomPassword();
  175. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  176. $this->assertIsBool($this->user->isUserOnline($username));
  177. $this->assertTrue($this->user->delete($username));
  178. }
  179. public function testUsersOnlineStatus()
  180. {
  181. $username = Utils::randomUserName();
  182. $password = Utils::randomPassword();
  183. $randomUsernameCodeJack = Utils::randomUserName();
  184. $this->assertArrayNotHasKey('code', $this->user->create(array(array('username' => $username, 'password' => $password), array('username' => $randomUsernameCodeJack, 'password' => $password))));
  185. $this->assertArrayNotHasKey('code', $this->user->isUsersOnline(array($username, $randomUsernameCodeJack)));
  186. $this->assertTrue($this->user->delete($username));
  187. $this->assertTrue($this->user->delete($randomUsernameCodeJack));
  188. }
  189. public function testGetUserToken()
  190. {
  191. $username = Utils::randomUserName();
  192. $password = Utils::randomPassword();
  193. $this->assertArrayNotHasKey('code', $this->user->create(array('username' => $username, 'password' => $password)));
  194. $this->assertArrayHasKey('access_token', $this->auth->getUserToken($username, $password));
  195. $this->assertTrue($this->user->delete($username));
  196. }
  197. }