aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/BackgroundJob/OutOfOfficeEventDispatcherJobTest.php
blob: 6135fd00fdcf3573101b1c91715952a5ed9eaa5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\DAV\Tests\unit\BackgroundJob;

use OCA\DAV\BackgroundJob\OutOfOfficeEventDispatcherJob;
use OCA\DAV\CalDAV\TimezoneService;
use OCA\DAV\Db\Absence;
use OCA\DAV\Db\AbsenceMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use OCP\IUserManager;
use OCP\User\Events\OutOfOfficeEndedEvent;
use OCP\User\Events\OutOfOfficeStartedEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class OutOfOfficeEventDispatcherJobTest extends TestCase {
	private OutOfOfficeEventDispatcherJob $job;
	private ITimeFactory&MockObject $timeFactory;
	private AbsenceMapper&MockObject $absenceMapper;
	private LoggerInterface&MockObject $logger;
	private IEventDispatcher&MockObject $eventDispatcher;
	private IUserManager&MockObject $userManager;
	private MockObject|TimezoneService $timezoneService;

	protected function setUp(): void {
		parent::setUp();

		$this->timeFactory = $this->createMock(ITimeFactory::class);
		$this->absenceMapper = $this->createMock(AbsenceMapper::class);
		$this->logger = $this->createMock(LoggerInterface::class);
		$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
		$this->userManager = $this->createMock(IUserManager::class);
		$this->timezoneService = $this->createMock(TimezoneService::class);

		$this->job = new OutOfOfficeEventDispatcherJob(
			$this->timeFactory,
			$this->absenceMapper,
			$this->logger,
			$this->eventDispatcher,
			$this->userManager,
			$this->timezoneService,
		);
	}

	public function testDispatchStartEvent(): void {
		$this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');

		$absence = new Absence();
		$absence->setId(200);
		$absence->setUserId('user');

		$user = $this->createMock(IUser::class);
		$user->method('getUID')
			->willReturn('user');

		$this->absenceMapper->expects(self::once())
			->method('findById')
			->with(1)
			->willReturn($absence);
		$this->userManager->expects(self::once())
			->method('get')
			->with('user')
			->willReturn($user);
		$this->eventDispatcher->expects(self::once())
			->method('dispatchTyped')
			->with(self::callback(static function ($event): bool {
				self::assertInstanceOf(OutOfOfficeStartedEvent::class, $event);
				return true;
			}));

		$this->job->run([
			'id' => 1,
			'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
		]);
	}

	public function testDispatchStopEvent(): void {
		$this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');

		$absence = new Absence();
		$absence->setId(200);
		$absence->setUserId('user');

		$user = $this->createMock(IUser::class);
		$user->method('getUID')
			->willReturn('user');

		$this->absenceMapper->expects(self::once())
			->method('findById')
			->with(1)
			->willReturn($absence);
		$this->userManager->expects(self::once())
			->method('get')
			->with('user')
			->willReturn($user);
		$this->eventDispatcher->expects(self::once())
			->method('dispatchTyped')
			->with(self::callback(static function ($event): bool {
				self::assertInstanceOf(OutOfOfficeEndedEvent::class, $event);
				return true;
			}));

		$this->job->run([
			'id' => 1,
			'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
		]);
	}

	public function testDoesntDispatchUnknownEvent(): void {
		$this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');

		$absence = new Absence();
		$absence->setId(100);
		$absence->setUserId('user');

		$user = $this->createMock(IUser::class);
		$user->method('getUID')
			->willReturn('user');

		$this->absenceMapper->expects(self::once())
			->method('findById')
			->with(1)
			->willReturn($absence);
		$this->userManager->expects(self::once())
			->method('get')
			->with('user')
			->willReturn($user);
		$this->eventDispatcher->expects(self::never())
			->method('dispatchTyped');
		$this->logger->expects(self::once())
			->method('error');

		$this->job->run([
			'id' => 1,
			'event' => 'foobar',
		]);
	}
}