UserStatusMapperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Tests\Db;
  8. use OCA\UserStatus\Db\UserStatus;
  9. use OCA\UserStatus\Db\UserStatusMapper;
  10. use OCP\AppFramework\Db\DoesNotExistException;
  11. use OCP\DB\Exception;
  12. use Test\TestCase;
  13. class UserStatusMapperTest extends TestCase {
  14. /** @var UserStatusMapper */
  15. private $mapper;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. // make sure that DB is empty
  19. $qb = self::$realDatabase->getQueryBuilder();
  20. $qb->delete('user_status')->execute();
  21. $this->mapper = new UserStatusMapper(self::$realDatabase);
  22. }
  23. public function testGetTableName(): void {
  24. $this->assertEquals('user_status', $this->mapper->getTableName());
  25. }
  26. public function testGetFindAll(): void {
  27. $this->insertSampleStatuses();
  28. $allResults = $this->mapper->findAll();
  29. $this->assertCount(3, $allResults);
  30. $limitedResults = $this->mapper->findAll(2);
  31. $this->assertCount(2, $limitedResults);
  32. $this->assertEquals('admin', $limitedResults[0]->getUserId());
  33. $this->assertEquals('user1', $limitedResults[1]->getUserId());
  34. $offsetResults = $this->mapper->findAll(null, 2);
  35. $this->assertCount(1, $offsetResults);
  36. $this->assertEquals('user2', $offsetResults[0]->getUserId());
  37. }
  38. public function testFindAllRecent(): void {
  39. $this->insertSampleStatuses();
  40. $allResults = $this->mapper->findAllRecent(2, 0);
  41. $this->assertCount(2, $allResults);
  42. $this->assertEquals('user2', $allResults[0]->getUserId());
  43. $this->assertEquals('user1', $allResults[1]->getUserId());
  44. }
  45. public function testGetFind(): void {
  46. $this->insertSampleStatuses();
  47. $adminStatus = $this->mapper->findByUserId('admin');
  48. $this->assertEquals('admin', $adminStatus->getUserId());
  49. $this->assertEquals('offline', $adminStatus->getStatus());
  50. $this->assertEquals(0, $adminStatus->getStatusTimestamp());
  51. $this->assertEquals(false, $adminStatus->getIsUserDefined());
  52. $this->assertEquals(null, $adminStatus->getCustomIcon());
  53. $this->assertEquals(null, $adminStatus->getCustomMessage());
  54. $this->assertEquals(null, $adminStatus->getClearAt());
  55. $user1Status = $this->mapper->findByUserId('user1');
  56. $this->assertEquals('user1', $user1Status->getUserId());
  57. $this->assertEquals('dnd', $user1Status->getStatus());
  58. $this->assertEquals(5000, $user1Status->getStatusTimestamp());
  59. $this->assertEquals(true, $user1Status->getIsUserDefined());
  60. $this->assertEquals('💩', $user1Status->getCustomIcon());
  61. $this->assertEquals('Do not disturb', $user1Status->getCustomMessage());
  62. $this->assertEquals(50000, $user1Status->getClearAt());
  63. $user2Status = $this->mapper->findByUserId('user2');
  64. $this->assertEquals('user2', $user2Status->getUserId());
  65. $this->assertEquals('away', $user2Status->getStatus());
  66. $this->assertEquals(6000, $user2Status->getStatusTimestamp());
  67. $this->assertEquals(false, $user2Status->getIsUserDefined());
  68. $this->assertEquals('🏝', $user2Status->getCustomIcon());
  69. $this->assertEquals('On vacation', $user2Status->getCustomMessage());
  70. $this->assertEquals(60000, $user2Status->getClearAt());
  71. }
  72. public function testFindByUserIds(): void {
  73. $this->insertSampleStatuses();
  74. $statuses = $this->mapper->findByUserIds(['admin', 'user2']);
  75. $this->assertCount(2, $statuses);
  76. $adminStatus = $statuses[0];
  77. $this->assertEquals('admin', $adminStatus->getUserId());
  78. $this->assertEquals('offline', $adminStatus->getStatus());
  79. $this->assertEquals(0, $adminStatus->getStatusTimestamp());
  80. $this->assertEquals(false, $adminStatus->getIsUserDefined());
  81. $this->assertEquals(null, $adminStatus->getCustomIcon());
  82. $this->assertEquals(null, $adminStatus->getCustomMessage());
  83. $this->assertEquals(null, $adminStatus->getClearAt());
  84. $user2Status = $statuses[1];
  85. $this->assertEquals('user2', $user2Status->getUserId());
  86. $this->assertEquals('away', $user2Status->getStatus());
  87. $this->assertEquals(6000, $user2Status->getStatusTimestamp());
  88. $this->assertEquals(false, $user2Status->getIsUserDefined());
  89. $this->assertEquals('🏝', $user2Status->getCustomIcon());
  90. $this->assertEquals('On vacation', $user2Status->getCustomMessage());
  91. $this->assertEquals(60000, $user2Status->getClearAt());
  92. }
  93. public function testUserIdUnique(): void {
  94. // Test that inserting a second status for a user is throwing an exception
  95. $userStatus1 = new UserStatus();
  96. $userStatus1->setUserId('admin');
  97. $userStatus1->setStatus('dnd');
  98. $userStatus1->setStatusTimestamp(5000);
  99. $userStatus1->setIsUserDefined(true);
  100. $this->mapper->insert($userStatus1);
  101. $userStatus2 = new UserStatus();
  102. $userStatus2->setUserId('admin');
  103. $userStatus2->setStatus('away');
  104. $userStatus2->setStatusTimestamp(6000);
  105. $userStatus2->setIsUserDefined(false);
  106. $this->expectException(Exception::class);
  107. $this->mapper->insert($userStatus2);
  108. }
  109. /**
  110. * @param string $status
  111. * @param bool $isUserDefined
  112. * @param int $timestamp
  113. * @param bool $expectsClean
  114. *
  115. * @dataProvider clearStatusesOlderThanDataProvider
  116. */
  117. public function testClearStatusesOlderThan(string $status, bool $isUserDefined, int $timestamp, bool $expectsClean): void {
  118. $oldStatus = UserStatus::fromParams([
  119. 'userId' => 'john.doe',
  120. 'status' => $status,
  121. 'isUserDefined' => $isUserDefined,
  122. 'statusTimestamp' => $timestamp,
  123. ]);
  124. $this->mapper->insert($oldStatus);
  125. $this->mapper->clearStatusesOlderThan(5000, 8000);
  126. $updatedStatus = $this->mapper->findAll()[0];
  127. if ($expectsClean) {
  128. $this->assertEquals('offline', $updatedStatus->getStatus());
  129. $this->assertFalse($updatedStatus->getIsUserDefined());
  130. $this->assertEquals(8000, $updatedStatus->getStatusTimestamp());
  131. } else {
  132. $this->assertEquals($status, $updatedStatus->getStatus());
  133. $this->assertEquals($isUserDefined, $updatedStatus->getIsUserDefined());
  134. $this->assertEquals($timestamp, $updatedStatus->getStatusTimestamp());
  135. }
  136. }
  137. public function clearStatusesOlderThanDataProvider(): array {
  138. return [
  139. ['offline', false, 6000, false],
  140. ['online', true, 6000, false],
  141. ['online', true, 4000, true],
  142. ['online', false, 6000, false],
  143. ['online', false, 4000, true],
  144. ['away', true, 6000, false],
  145. ['away', true, 4000, false],
  146. ['away', false, 6000, false],
  147. ['away', false, 4000, true],
  148. ['dnd', true, 6000, false],
  149. ['dnd', true, 4000, false],
  150. ['invisible', true, 6000, false],
  151. ['invisible', true, 4000, false],
  152. ];
  153. }
  154. public function testClearOlderThanClearAt(): void {
  155. $this->insertSampleStatuses();
  156. $this->mapper->clearOlderThanClearAt(55000);
  157. $allStatuses = $this->mapper->findAll();
  158. $this->assertCount(2, $allStatuses);
  159. $this->expectException(DoesNotExistException::class);
  160. $this->mapper->findByUserId('user1');
  161. }
  162. private function insertSampleStatuses(): void {
  163. $userStatus1 = new UserStatus();
  164. $userStatus1->setUserId('admin');
  165. $userStatus1->setStatus('offline');
  166. $userStatus1->setStatusTimestamp(0);
  167. $userStatus1->setIsUserDefined(false);
  168. $userStatus2 = new UserStatus();
  169. $userStatus2->setUserId('user1');
  170. $userStatus2->setStatus('dnd');
  171. $userStatus2->setStatusTimestamp(5000);
  172. $userStatus2->setStatusMessageTimestamp(5000);
  173. $userStatus2->setIsUserDefined(true);
  174. $userStatus2->setCustomIcon('💩');
  175. $userStatus2->setCustomMessage('Do not disturb');
  176. $userStatus2->setClearAt(50000);
  177. $userStatus3 = new UserStatus();
  178. $userStatus3->setUserId('user2');
  179. $userStatus3->setStatus('away');
  180. $userStatus3->setStatusTimestamp(6000);
  181. $userStatus3->setStatusMessageTimestamp(6000);
  182. $userStatus3->setIsUserDefined(false);
  183. $userStatus3->setCustomIcon('🏝');
  184. $userStatus3->setCustomMessage('On vacation');
  185. $userStatus3->setClearAt(60000);
  186. $this->mapper->insert($userStatus1);
  187. $this->mapper->insert($userStatus2);
  188. $this->mapper->insert($userStatus3);
  189. }
  190. public function dataCreateBackupStatus(): array {
  191. return [
  192. [false, false, false],
  193. [true, false, true],
  194. [false, true, false],
  195. [true, true, false],
  196. ];
  197. }
  198. /**
  199. * @dataProvider dataCreateBackupStatus
  200. * @param bool $hasStatus
  201. * @param bool $hasBackup
  202. * @param bool $backupCreated
  203. */
  204. public function testCreateBackupStatus(bool $hasStatus, bool $hasBackup, bool $backupCreated): void {
  205. if ($hasStatus) {
  206. $userStatus1 = new UserStatus();
  207. $userStatus1->setUserId('user1');
  208. $userStatus1->setStatus('online');
  209. $userStatus1->setStatusTimestamp(5000);
  210. $userStatus1->setIsUserDefined(true);
  211. $userStatus1->setIsBackup(false);
  212. $userStatus1->setCustomIcon('🚀');
  213. $userStatus1->setCustomMessage('Current');
  214. $userStatus1->setClearAt(50000);
  215. $this->mapper->insert($userStatus1);
  216. }
  217. if ($hasBackup) {
  218. $userStatus1 = new UserStatus();
  219. $userStatus1->setUserId('_user1');
  220. $userStatus1->setStatus('online');
  221. $userStatus1->setStatusTimestamp(5000);
  222. $userStatus1->setIsUserDefined(true);
  223. $userStatus1->setIsBackup(true);
  224. $userStatus1->setCustomIcon('🚀');
  225. $userStatus1->setCustomMessage('Backup');
  226. $userStatus1->setClearAt(50000);
  227. $this->mapper->insert($userStatus1);
  228. }
  229. if ($hasStatus && $hasBackup) {
  230. $this->expectException(Exception::class);
  231. }
  232. self::assertSame($backupCreated, $this->mapper->createBackupStatus('user1'));
  233. if ($backupCreated) {
  234. $user1Status = $this->mapper->findByUserId('user1', true);
  235. $this->assertEquals('_user1', $user1Status->getUserId());
  236. $this->assertEquals(true, $user1Status->getIsBackup());
  237. $this->assertEquals('Current', $user1Status->getCustomMessage());
  238. } elseif ($hasBackup) {
  239. $user1Status = $this->mapper->findByUserId('user1', true);
  240. $this->assertEquals('_user1', $user1Status->getUserId());
  241. $this->assertEquals(true, $user1Status->getIsBackup());
  242. $this->assertEquals('Backup', $user1Status->getCustomMessage());
  243. }
  244. }
  245. public function testRestoreBackupStatuses(): void {
  246. $userStatus1 = new UserStatus();
  247. $userStatus1->setUserId('_user1');
  248. $userStatus1->setStatus('online');
  249. $userStatus1->setStatusTimestamp(5000);
  250. $userStatus1->setIsUserDefined(true);
  251. $userStatus1->setIsBackup(true);
  252. $userStatus1->setCustomIcon('🚀');
  253. $userStatus1->setCustomMessage('Releasing');
  254. $userStatus1->setClearAt(50000);
  255. $userStatus1 = $this->mapper->insert($userStatus1);
  256. $userStatus2 = new UserStatus();
  257. $userStatus2->setUserId('_user2');
  258. $userStatus2->setStatus('away');
  259. $userStatus2->setStatusTimestamp(5000);
  260. $userStatus2->setIsUserDefined(true);
  261. $userStatus2->setIsBackup(true);
  262. $userStatus2->setCustomIcon('💩');
  263. $userStatus2->setCustomMessage('Do not disturb');
  264. $userStatus2->setClearAt(50000);
  265. $userStatus2 = $this->mapper->insert($userStatus2);
  266. $userStatus3 = new UserStatus();
  267. $userStatus3->setUserId('_user3');
  268. $userStatus3->setStatus('away');
  269. $userStatus3->setStatusTimestamp(5000);
  270. $userStatus3->setIsUserDefined(true);
  271. $userStatus3->setIsBackup(true);
  272. $userStatus3->setCustomIcon('🏝️');
  273. $userStatus3->setCustomMessage('Vacationing');
  274. $userStatus3->setClearAt(50000);
  275. $this->mapper->insert($userStatus3);
  276. $this->mapper->restoreBackupStatuses([$userStatus1->getId(), $userStatus2->getId()]);
  277. $user1Status = $this->mapper->findByUserId('user1', false);
  278. $this->assertEquals('user1', $user1Status->getUserId());
  279. $this->assertEquals(false, $user1Status->getIsBackup());
  280. $this->assertEquals('Releasing', $user1Status->getCustomMessage());
  281. $user2Status = $this->mapper->findByUserId('user2', false);
  282. $this->assertEquals('user2', $user2Status->getUserId());
  283. $this->assertEquals(false, $user2Status->getIsBackup());
  284. $this->assertEquals('Do not disturb', $user2Status->getCustomMessage());
  285. $user3Status = $this->mapper->findByUserId('user3', true);
  286. $this->assertEquals('_user3', $user3Status->getUserId());
  287. $this->assertEquals(true, $user3Status->getIsBackup());
  288. $this->assertEquals('Vacationing', $user3Status->getCustomMessage());
  289. }
  290. }