UserLiveStatusListenerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  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\Listener;
  26. use OCA\UserStatus\Db\UserStatus;
  27. use OCA\UserStatus\Db\UserStatusMapper;
  28. use OCA\UserStatus\Listener\UserDeletedListener;
  29. use OCA\UserStatus\Listener\UserLiveStatusListener;
  30. use OCA\UserStatus\Service\StatusService;
  31. use OCP\AppFramework\Db\DoesNotExistException;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\EventDispatcher\GenericEvent;
  34. use OCP\IUser;
  35. use OCP\User\Events\UserLiveStatusEvent;
  36. use Test\TestCase;
  37. class UserLiveStatusListenerTest extends TestCase {
  38. /** @var UserStatusMapper|\PHPUnit\Framework\MockObject\MockObject */
  39. private $mapper;
  40. /** @var StatusService|\PHPUnit\Framework\MockObject\MockObject */
  41. private $statusService;
  42. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  43. private $timeFactory;
  44. /** @var UserDeletedListener */
  45. private $listener;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->mapper = $this->createMock(UserStatusMapper::class);
  49. $this->statusService = $this->createMock(StatusService::class);
  50. $this->timeFactory = $this->createMock(ITimeFactory::class);
  51. $this->listener = new UserLiveStatusListener($this->mapper, $this->statusService, $this->timeFactory);
  52. }
  53. /**
  54. * @param string $userId
  55. * @param string $previousStatus
  56. * @param int $previousTimestamp
  57. * @param bool $previousIsUserDefined
  58. * @param string $eventStatus
  59. * @param int $eventTimestamp
  60. * @param bool $expectExisting
  61. * @param bool $expectUpdate
  62. *
  63. * @dataProvider handleEventWithCorrectEventDataProvider
  64. */
  65. public function testHandleWithCorrectEvent(string $userId,
  66. string $previousStatus,
  67. int $previousTimestamp,
  68. bool $previousIsUserDefined,
  69. string $eventStatus,
  70. int $eventTimestamp,
  71. bool $expectExisting,
  72. bool $expectUpdate): void {
  73. $userStatus = new UserStatus();
  74. if ($expectExisting) {
  75. $userStatus->setId(42);
  76. $userStatus->setUserId($userId);
  77. $userStatus->setStatus($previousStatus);
  78. $userStatus->setStatusTimestamp($previousTimestamp);
  79. $userStatus->setIsUserDefined($previousIsUserDefined);
  80. $this->statusService->expects($this->once())
  81. ->method('findByUserId')
  82. ->with($userId)
  83. ->willReturn($userStatus);
  84. } else {
  85. $this->statusService->expects($this->once())
  86. ->method('findByUserId')
  87. ->with($userId)
  88. ->willThrowException(new DoesNotExistException(''));
  89. }
  90. $user = $this->createMock(IUser::class);
  91. $user->method('getUID')->willReturn($userId);
  92. $event = new UserLiveStatusEvent($user, $eventStatus, $eventTimestamp);
  93. $this->timeFactory->expects($this->atMost(1))
  94. ->method('getTime')
  95. ->willReturn(5000);
  96. if ($expectUpdate) {
  97. if ($expectExisting) {
  98. $this->mapper->expects($this->never())
  99. ->method('insert');
  100. $this->mapper->expects($this->once())
  101. ->method('update')
  102. ->with($this->callback(function ($userStatus) use ($eventStatus, $eventTimestamp) {
  103. $this->assertEquals($eventStatus, $userStatus->getStatus());
  104. $this->assertEquals($eventTimestamp, $userStatus->getStatusTimestamp());
  105. $this->assertFalse($userStatus->getIsUserDefined());
  106. return true;
  107. }));
  108. } else {
  109. $this->mapper->expects($this->once())
  110. ->method('insert')
  111. ->with($this->callback(function ($userStatus) use ($eventStatus, $eventTimestamp) {
  112. $this->assertEquals($eventStatus, $userStatus->getStatus());
  113. $this->assertEquals($eventTimestamp, $userStatus->getStatusTimestamp());
  114. $this->assertFalse($userStatus->getIsUserDefined());
  115. return true;
  116. }));
  117. $this->mapper->expects($this->never())
  118. ->method('update');
  119. }
  120. $this->listener->handle($event);
  121. } else {
  122. $this->mapper->expects($this->never())
  123. ->method('insert');
  124. $this->mapper->expects($this->never())
  125. ->method('update');
  126. $this->listener->handle($event);
  127. }
  128. }
  129. public function handleEventWithCorrectEventDataProvider(): array {
  130. return [
  131. ['john.doe', 'offline', 0, false, 'online', 5000, true, true],
  132. ['john.doe', 'offline', 0, false, 'online', 5000, false, true],
  133. ['john.doe', 'online', 5000, false, 'online', 5000, true, false],
  134. ['john.doe', 'online', 5000, false, 'online', 5000, false, true],
  135. ['john.doe', 'away', 5000, false, 'online', 5000, true, true],
  136. ['john.doe', 'online', 5000, false, 'away', 5000, true, false],
  137. ['john.doe', 'away', 5000, true, 'online', 5000, true, false],
  138. ['john.doe', 'online', 5000, true, 'away', 5000, true, false],
  139. ];
  140. }
  141. public function testHandleWithWrongEvent(): void {
  142. $this->mapper->expects($this->never())
  143. ->method('insertOrUpdate');
  144. $event = new GenericEvent();
  145. $this->listener->handle($event);
  146. }
  147. }