OutOfOfficeEventDispatcherJobTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\unit\BackgroundJob;
  25. use OCA\DAV\BackgroundJob\OutOfOfficeEventDispatcherJob;
  26. use OCA\DAV\CalDAV\TimezoneService;
  27. use OCA\DAV\Db\Absence;
  28. use OCA\DAV\Db\AbsenceMapper;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\EventDispatcher\IEventDispatcher;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\User\Events\OutOfOfficeEndedEvent;
  34. use OCP\User\Events\OutOfOfficeStartedEvent;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Psr\Log\LoggerInterface;
  37. use Test\TestCase;
  38. class OutOfOfficeEventDispatcherJobTest extends TestCase {
  39. private OutOfOfficeEventDispatcherJob $job;
  40. /** @var MockObject|ITimeFactory */
  41. private $timeFactory;
  42. /** @var MockObject|AbsenceMapper */
  43. private $absenceMapper;
  44. /** @var MockObject|LoggerInterface */
  45. private $logger;
  46. /** @var MockObject|IEventDispatcher */
  47. private $eventDispatcher;
  48. /** @var MockObject|IUserManager */
  49. private $userManager;
  50. private MockObject|TimezoneService $timezoneService;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->timeFactory = $this->createMock(ITimeFactory::class);
  54. $this->absenceMapper = $this->createMock(AbsenceMapper::class);
  55. $this->logger = $this->createMock(LoggerInterface::class);
  56. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  57. $this->userManager = $this->createMock(IUserManager::class);
  58. $this->timezoneService = $this->createMock(TimezoneService::class);
  59. $this->job = new OutOfOfficeEventDispatcherJob(
  60. $this->timeFactory,
  61. $this->absenceMapper,
  62. $this->logger,
  63. $this->eventDispatcher,
  64. $this->userManager,
  65. $this->timezoneService,
  66. );
  67. }
  68. public function testDispatchStartEvent() {
  69. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  70. $absence = new Absence();
  71. $absence->setId(200);
  72. $absence->setUserId('user');
  73. $user = $this->createMock(IUser::class);
  74. $user->method('getUID')
  75. ->willReturn('user');
  76. $this->absenceMapper->expects(self::once())
  77. ->method('findById')
  78. ->with(1)
  79. ->willReturn($absence);
  80. $this->userManager->expects(self::once())
  81. ->method('get')
  82. ->with('user')
  83. ->willReturn($user);
  84. $this->eventDispatcher->expects(self::once())
  85. ->method('dispatchTyped')
  86. ->with(self::callback(static function ($event): bool {
  87. self::assertInstanceOf(OutOfOfficeStartedEvent::class, $event);
  88. return true;
  89. }));
  90. $this->job->run([
  91. 'id' => 1,
  92. 'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
  93. ]);
  94. }
  95. public function testDispatchStopEvent() {
  96. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  97. $absence = new Absence();
  98. $absence->setId(200);
  99. $absence->setUserId('user');
  100. $user = $this->createMock(IUser::class);
  101. $user->method('getUID')
  102. ->willReturn('user');
  103. $this->absenceMapper->expects(self::once())
  104. ->method('findById')
  105. ->with(1)
  106. ->willReturn($absence);
  107. $this->userManager->expects(self::once())
  108. ->method('get')
  109. ->with('user')
  110. ->willReturn($user);
  111. $this->eventDispatcher->expects(self::once())
  112. ->method('dispatchTyped')
  113. ->with(self::callback(static function ($event): bool {
  114. self::assertInstanceOf(OutOfOfficeEndedEvent::class, $event);
  115. return true;
  116. }));
  117. $this->job->run([
  118. 'id' => 1,
  119. 'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
  120. ]);
  121. }
  122. public function testDoesntDispatchUnknownEvent() {
  123. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  124. $absence = new Absence();
  125. $absence->setId(100);
  126. $absence->setUserId('user');
  127. $user = $this->createMock(IUser::class);
  128. $user->method('getUID')
  129. ->willReturn('user');
  130. $this->absenceMapper->expects(self::once())
  131. ->method('findById')
  132. ->with(1)
  133. ->willReturn($absence);
  134. $this->userManager->expects(self::once())
  135. ->method('get')
  136. ->with('user')
  137. ->willReturn($user);
  138. $this->eventDispatcher->expects(self::never())
  139. ->method('dispatchTyped');
  140. $this->logger->expects(self::once())
  141. ->method('error');
  142. $this->job->run([
  143. 'id' => 1,
  144. 'event' => 'foobar',
  145. ]);
  146. }
  147. }