You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserLiveStatusListenerTest.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Tests\Listener;
  24. use OCA\UserStatus\Db\UserStatus;
  25. use OCA\UserStatus\Db\UserStatusMapper;
  26. use OCA\UserStatus\Listener\UserDeletedListener;
  27. use OCA\UserStatus\Listener\UserLiveStatusListener;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\EventDispatcher\GenericEvent;
  31. use OCP\IUser;
  32. use OCP\User\Events\UserLiveStatusEvent;
  33. use Test\TestCase;
  34. class UserLiveStatusListenerTest extends TestCase {
  35. /** @var UserStatusMapper|\PHPUnit\Framework\MockObject\MockObject */
  36. private $mapper;
  37. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  38. private $timeFactory;
  39. /** @var UserDeletedListener */
  40. private $listener;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->mapper = $this->createMock(UserStatusMapper::class);
  44. $this->timeFactory = $this->createMock(ITimeFactory::class);
  45. $this->listener = new UserLiveStatusListener($this->mapper, $this->timeFactory);
  46. }
  47. /**
  48. * @param string $userId
  49. * @param string $previousStatus
  50. * @param int $previousTimestamp
  51. * @param bool $previousIsUserDefined
  52. * @param string $eventStatus
  53. * @param int $eventTimestamp
  54. * @param bool $expectExisting
  55. * @param bool $expectUpdate
  56. *
  57. * @dataProvider handleEventWithCorrectEventDataProvider
  58. */
  59. public function testHandleWithCorrectEvent(string $userId,
  60. string $previousStatus,
  61. int $previousTimestamp,
  62. bool $previousIsUserDefined,
  63. string $eventStatus,
  64. int $eventTimestamp,
  65. bool $expectExisting,
  66. bool $expectUpdate): void {
  67. $userStatus = new UserStatus();
  68. if ($expectExisting) {
  69. $userStatus->setId(42);
  70. $userStatus->setUserId($userId);
  71. $userStatus->setStatus($previousStatus);
  72. $userStatus->setStatusTimestamp($previousTimestamp);
  73. $userStatus->setIsUserDefined($previousIsUserDefined);
  74. $this->mapper->expects($this->once())
  75. ->method('findByUserId')
  76. ->with($userId)
  77. ->willReturn($userStatus);
  78. } else {
  79. $this->mapper->expects($this->once())
  80. ->method('findByUserId')
  81. ->with($userId)
  82. ->willThrowException(new DoesNotExistException(''));
  83. }
  84. $user = $this->createMock(IUser::class);
  85. $user->method('getUID')->willReturn($userId);
  86. $event = new UserLiveStatusEvent($user, $eventStatus, $eventTimestamp);
  87. $this->timeFactory->expects($this->once())
  88. ->method('getTime')
  89. ->willReturn(5000);
  90. if ($expectUpdate) {
  91. if ($expectExisting) {
  92. $this->mapper->expects($this->never())
  93. ->method('insert');
  94. $this->mapper->expects($this->once())
  95. ->method('update')
  96. ->with($this->callback(function ($userStatus) use ($eventStatus, $eventTimestamp) {
  97. $this->assertEquals($eventStatus, $userStatus->getStatus());
  98. $this->assertEquals($eventTimestamp, $userStatus->getStatusTimestamp());
  99. $this->assertFalse($userStatus->getIsUserDefined());
  100. return true;
  101. }));
  102. } else {
  103. $this->mapper->expects($this->once())
  104. ->method('insert')
  105. ->with($this->callback(function ($userStatus) use ($eventStatus, $eventTimestamp) {
  106. $this->assertEquals($eventStatus, $userStatus->getStatus());
  107. $this->assertEquals($eventTimestamp, $userStatus->getStatusTimestamp());
  108. $this->assertFalse($userStatus->getIsUserDefined());
  109. return true;
  110. }));
  111. $this->mapper->expects($this->never())
  112. ->method('update');
  113. }
  114. $this->listener->handle($event);
  115. } else {
  116. $this->mapper->expects($this->never())
  117. ->method('insert');
  118. $this->mapper->expects($this->never())
  119. ->method('update');
  120. $this->listener->handle($event);
  121. }
  122. }
  123. public function handleEventWithCorrectEventDataProvider(): array {
  124. return [
  125. ['john.doe', 'offline', 0, false, 'online', 5000, true, true],
  126. ['john.doe', 'offline', 0, false, 'online', 5000, false, true],
  127. ['john.doe', 'online', 5000, false, 'online', 5000, true, false],
  128. ['john.doe', 'online', 5000, false, 'online', 5000, false, true],
  129. ['john.doe', 'away', 5000, false, 'online', 5000, true, true],
  130. ['john.doe', 'online', 5000, false, 'away', 5000, true, false],
  131. ];
  132. }
  133. public function testHandleWithWrongEvent(): void {
  134. $this->mapper->expects($this->never())
  135. ->method('insertOrUpdate');
  136. $event = new GenericEvent();
  137. $this->listener->handle($event);
  138. }
  139. }