UserStatusMapperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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->setStatusMessageTimestamp(5000);
  191. $userStatus2->setIsUserDefined(true);
  192. $userStatus2->setCustomIcon('💩');
  193. $userStatus2->setCustomMessage('Do not disturb');
  194. $userStatus2->setClearAt(50000);
  195. $userStatus3 = new UserStatus();
  196. $userStatus3->setUserId('user2');
  197. $userStatus3->setStatus('away');
  198. $userStatus3->setStatusTimestamp(6000);
  199. $userStatus3->setStatusMessageTimestamp(6000);
  200. $userStatus3->setIsUserDefined(false);
  201. $userStatus3->setCustomIcon('🏝');
  202. $userStatus3->setCustomMessage('On vacation');
  203. $userStatus3->setClearAt(60000);
  204. $this->mapper->insert($userStatus1);
  205. $this->mapper->insert($userStatus2);
  206. $this->mapper->insert($userStatus3);
  207. }
  208. public function dataCreateBackupStatus(): array {
  209. return [
  210. [false, false, false],
  211. [true, false, true],
  212. [false, true, false],
  213. [true, true, false],
  214. ];
  215. }
  216. /**
  217. * @dataProvider dataCreateBackupStatus
  218. * @param bool $hasStatus
  219. * @param bool $hasBackup
  220. * @param bool $backupCreated
  221. */
  222. public function testCreateBackupStatus(bool $hasStatus, bool $hasBackup, bool $backupCreated): void {
  223. if ($hasStatus) {
  224. $userStatus1 = new UserStatus();
  225. $userStatus1->setUserId('user1');
  226. $userStatus1->setStatus('online');
  227. $userStatus1->setStatusTimestamp(5000);
  228. $userStatus1->setIsUserDefined(true);
  229. $userStatus1->setIsBackup(false);
  230. $userStatus1->setCustomIcon('🚀');
  231. $userStatus1->setCustomMessage('Current');
  232. $userStatus1->setClearAt(50000);
  233. $this->mapper->insert($userStatus1);
  234. }
  235. if ($hasBackup) {
  236. $userStatus1 = new UserStatus();
  237. $userStatus1->setUserId('_user1');
  238. $userStatus1->setStatus('online');
  239. $userStatus1->setStatusTimestamp(5000);
  240. $userStatus1->setIsUserDefined(true);
  241. $userStatus1->setIsBackup(true);
  242. $userStatus1->setCustomIcon('🚀');
  243. $userStatus1->setCustomMessage('Backup');
  244. $userStatus1->setClearAt(50000);
  245. $this->mapper->insert($userStatus1);
  246. }
  247. if ($hasStatus && $hasBackup) {
  248. $this->expectException(Exception::class);
  249. }
  250. self::assertSame($backupCreated, $this->mapper->createBackupStatus('user1'));
  251. if ($backupCreated) {
  252. $user1Status = $this->mapper->findByUserId('user1', true);
  253. $this->assertEquals('_user1', $user1Status->getUserId());
  254. $this->assertEquals(true, $user1Status->getIsBackup());
  255. $this->assertEquals('Current', $user1Status->getCustomMessage());
  256. } elseif ($hasBackup) {
  257. $user1Status = $this->mapper->findByUserId('user1', true);
  258. $this->assertEquals('_user1', $user1Status->getUserId());
  259. $this->assertEquals(true, $user1Status->getIsBackup());
  260. $this->assertEquals('Backup', $user1Status->getCustomMessage());
  261. }
  262. }
  263. public function testRestoreBackupStatuses(): void {
  264. $userStatus1 = new UserStatus();
  265. $userStatus1->setUserId('_user1');
  266. $userStatus1->setStatus('online');
  267. $userStatus1->setStatusTimestamp(5000);
  268. $userStatus1->setIsUserDefined(true);
  269. $userStatus1->setIsBackup(true);
  270. $userStatus1->setCustomIcon('🚀');
  271. $userStatus1->setCustomMessage('Releasing');
  272. $userStatus1->setClearAt(50000);
  273. $userStatus1 = $this->mapper->insert($userStatus1);
  274. $userStatus2 = new UserStatus();
  275. $userStatus2->setUserId('_user2');
  276. $userStatus2->setStatus('away');
  277. $userStatus2->setStatusTimestamp(5000);
  278. $userStatus2->setIsUserDefined(true);
  279. $userStatus2->setIsBackup(true);
  280. $userStatus2->setCustomIcon('💩');
  281. $userStatus2->setCustomMessage('Do not disturb');
  282. $userStatus2->setClearAt(50000);
  283. $userStatus2 = $this->mapper->insert($userStatus2);
  284. $userStatus3 = new UserStatus();
  285. $userStatus3->setUserId('_user3');
  286. $userStatus3->setStatus('away');
  287. $userStatus3->setStatusTimestamp(5000);
  288. $userStatus3->setIsUserDefined(true);
  289. $userStatus3->setIsBackup(true);
  290. $userStatus3->setCustomIcon('🏝️');
  291. $userStatus3->setCustomMessage('Vacationing');
  292. $userStatus3->setClearAt(50000);
  293. $this->mapper->insert($userStatus3);
  294. $this->mapper->restoreBackupStatuses([$userStatus1->getId(), $userStatus2->getId()]);
  295. $user1Status = $this->mapper->findByUserId('user1', false);
  296. $this->assertEquals('user1', $user1Status->getUserId());
  297. $this->assertEquals(false, $user1Status->getIsBackup());
  298. $this->assertEquals('Releasing', $user1Status->getCustomMessage());
  299. $user2Status = $this->mapper->findByUserId('user2', false);
  300. $this->assertEquals('user2', $user2Status->getUserId());
  301. $this->assertEquals(false, $user2Status->getIsBackup());
  302. $this->assertEquals('Do not disturb', $user2Status->getCustomMessage());
  303. $user3Status = $this->mapper->findByUserId('user3', true);
  304. $this->assertEquals('_user3', $user3Status->getUserId());
  305. $this->assertEquals(true, $user3Status->getIsBackup());
  306. $this->assertEquals('Vacationing', $user3Status->getCustomMessage());
  307. }
  308. }