UserStatusMapperTest.php 12 KB

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