StatusServiceIntegrationTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Tests\Integration\BackgroundJob;
  8. use OCA\UserStatus\Service\StatusService;
  9. use OCP\AppFramework\Db\DoesNotExistException;
  10. use OCP\IDBConnection;
  11. use OCP\Server;
  12. use OCP\UserStatus\IUserStatus;
  13. use Test\TestCase;
  14. use function sleep;
  15. use function time;
  16. /**
  17. * @group DB
  18. */
  19. class StatusServiceIntegrationTest extends TestCase {
  20. private StatusService $service;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->service = Server::get(StatusService::class);
  24. $db = Server::get(IDBConnection::class);
  25. $qb = $db->getQueryBuilder();
  26. $qb->delete('user_status')->executeStatement();
  27. }
  28. public function testNoStatusYet(): void {
  29. $this->expectException(DoesNotExistException::class);
  30. $this->service->findByUserId('test123');
  31. }
  32. public function testCustomStatusMessageTimestamp(): void {
  33. $this->service->setCustomMessage(
  34. 'test123',
  35. '🍕',
  36. 'Lunch',
  37. null,
  38. );
  39. $status = $this->service->findByUserId('test123');
  40. self::assertSame('Lunch', $status->getCustomMessage());
  41. self::assertGreaterThanOrEqual(time(), $status->getStatusMessageTimestamp());
  42. }
  43. public function testOnlineStatusKeepsMessageTimestamp(): void {
  44. $this->service->setStatus(
  45. 'test123',
  46. IUserStatus::OFFLINE,
  47. time() + 1000,
  48. false,
  49. );
  50. $this->service->setCustomMessage(
  51. 'test123',
  52. '🍕',
  53. 'Lunch',
  54. null,
  55. );
  56. $timeAfterInsert = time();
  57. sleep(1);
  58. $this->service->setStatus(
  59. 'test123',
  60. IUserStatus::ONLINE,
  61. time() + 2000,
  62. false,
  63. );
  64. $status = $this->service->findByUserId('test123');
  65. self::assertSame('Lunch', $status->getCustomMessage());
  66. self::assertLessThanOrEqual($timeAfterInsert, $status->getStatusMessageTimestamp());
  67. }
  68. public function testCreateRestoreBackupAutomatically(): void {
  69. $this->service->setStatus(
  70. 'test123',
  71. IUserStatus::ONLINE,
  72. null,
  73. false,
  74. );
  75. $this->service->setUserStatus(
  76. 'test123',
  77. IUserStatus::DND,
  78. 'meeting',
  79. true,
  80. );
  81. self::assertSame(
  82. 'meeting',
  83. $this->service->findByUserId('test123')->getMessageId(),
  84. );
  85. $this->service->revertUserStatus(
  86. 'test123',
  87. 'meeting',
  88. );
  89. self::assertSame(
  90. IUserStatus::ONLINE,
  91. $this->service->findByUserId('test123')->getStatus(),
  92. );
  93. }
  94. public function testCallOverwritesMeetingStatus(): void {
  95. $this->service->setStatus(
  96. 'test123',
  97. IUserStatus::ONLINE,
  98. null,
  99. false,
  100. );
  101. $this->service->setUserStatus(
  102. 'test123',
  103. IUserStatus::AWAY,
  104. IUserStatus::MESSAGE_CALENDAR_BUSY,
  105. true,
  106. );
  107. self::assertSame(
  108. 'meeting',
  109. $this->service->findByUserId('test123')->getMessageId(),
  110. );
  111. $this->service->setUserStatus(
  112. 'test123',
  113. IUserStatus::AWAY,
  114. IUserStatus::MESSAGE_CALL,
  115. true,
  116. );
  117. self::assertSame(
  118. IUserStatus::AWAY,
  119. $this->service->findByUserId('test123')->getStatus(),
  120. );
  121. self::assertSame(
  122. IUserStatus::MESSAGE_CALL,
  123. $this->service->findByUserId('test123')->getMessageId(),
  124. );
  125. }
  126. public function testOtherAutomationsDoNotOverwriteEachOther(): void {
  127. $this->service->setStatus(
  128. 'test123',
  129. IUserStatus::ONLINE,
  130. null,
  131. false,
  132. );
  133. $this->service->setUserStatus(
  134. 'test123',
  135. IUserStatus::AWAY,
  136. IUserStatus::MESSAGE_CALENDAR_BUSY,
  137. true,
  138. );
  139. self::assertSame(
  140. 'meeting',
  141. $this->service->findByUserId('test123')->getMessageId(),
  142. );
  143. $nostatus = $this->service->setUserStatus(
  144. 'test123',
  145. IUserStatus::AWAY,
  146. IUserStatus::MESSAGE_AVAILABILITY,
  147. true,
  148. );
  149. self::assertNull($nostatus);
  150. self::assertSame(
  151. IUserStatus::MESSAGE_CALENDAR_BUSY,
  152. $this->service->findByUserId('test123')->getMessageId(),
  153. );
  154. }
  155. public function testCi(): void {
  156. // TODO: remove if CI turns red
  157. self::assertTrue(false);
  158. }
  159. }