aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/Listener/CalendarContactInteractionListenerTest.php
blob: dc3dce8a62fc7c7765a04c8c22198b5a6c1577cc (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\DAV\Tests\unit\Listener;

use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\Events\CalendarShareUpdatedEvent;
use OCA\DAV\Listener\CalendarContactInteractionListener;
use OCP\Calendar\Events\CalendarObjectCreatedEvent;
use OCP\Contacts\Events\ContactInteractedWithEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class CalendarContactInteractionListenerTest extends TestCase {
	private IEventDispatcher&MockObject $eventDispatcher;
	private IUserSession&MockObject $userSession;
	private Principal&MockObject $principalConnector;
	private LoggerInterface&MockObject $logger;
	private IMailer&MockObject $mailer;
	private CalendarContactInteractionListener $listener;

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

		$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
		$this->userSession = $this->createMock(IUserSession::class);
		$this->principalConnector = $this->createMock(Principal::class);
		$this->mailer = $this->createMock(IMailer::class);
		$this->logger = $this->createMock(LoggerInterface::class);

		$this->listener = new CalendarContactInteractionListener(
			$this->eventDispatcher,
			$this->userSession,
			$this->principalConnector,
			$this->mailer,
			$this->logger
		);
	}

	public function testParseUnrelated(): void {
		$event = new Event();
		$this->eventDispatcher->expects(self::never())->method('dispatchTyped');

		$this->listener->handle($event);
	}

	public function testHandleWithoutAnythingInteresting(): void {
		$event = new CalendarShareUpdatedEvent(123, [], [], [], []);
		$user = $this->createMock(IUser::class);
		$this->userSession->expects(self::once())->method('getUser')->willReturn($user);
		$this->eventDispatcher->expects(self::never())->method('dispatchTyped');

		$this->listener->handle($event);
	}

	public function testParseInvalidData(): void {
		$event = new CalendarObjectCreatedEvent(123, [], [], ['calendardata' => 'BEGIN:FOO']);
		$user = $this->createMock(IUser::class);
		$this->userSession->expects(self::once())->method('getUser')->willReturn($user);
		$this->eventDispatcher->expects(self::never())->method('dispatchTyped');
		$this->logger->expects(self::once())->method('warning');

		$this->listener->handle($event);
	}

	public function testParseCalendarEventWithInvalidEmail(): void {
		$event = new CalendarObjectCreatedEvent(123, [], [], ['calendardata' => <<<EVENT
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//IDN nextcloud.com//Calendar app 2.1.3//EN
BEGIN:VTIMEZONE
TZID:Europe/Vienna
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20210202T091151Z
DTSTAMP:20210203T130231Z
LAST-MODIFIED:20210203T130231Z
SEQUENCE:9
UID:b74a0c8e-93b0-447f-aed5-b679b19e874a
DTSTART;TZID=Europe/Vienna:20210202T103000
DTEND;TZID=Europe/Vienna:20210202T133000
SUMMARY:tes
ORGANIZER;CN=admin:mailto:christoph.wurst@nextcloud.com
ATTENDEE;CN=somethingbutnotanemail;CUTYPE=INDIVIDUAL;PARTSTAT=NEEDS-ACTION;
 ROLE=REQ-PARTICIPANT;RSVP=FALSE:mailto:somethingbutnotanemail
DESCRIPTION:test
END:VEVENT
END:VCALENDAR
EVENT]);
		$user = $this->createMock(IUser::class);
		$this->userSession->expects(self::once())->method('getUser')->willReturn($user);
		$this->eventDispatcher->expects(self::never())->method('dispatchTyped');
		$this->logger->expects(self::never())->method('warning');

		$this->listener->handle($event);
	}

	public function testParseCalendarEvent(): void {
		$event = new CalendarObjectCreatedEvent(123, [], [], ['calendardata' => <<<EVENT
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//IDN nextcloud.com//Calendar app 2.1.3//EN
BEGIN:VTIMEZONE
TZID:Europe/Vienna
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20210202T091151Z
DTSTAMP:20210203T130231Z
LAST-MODIFIED:20210203T130231Z
SEQUENCE:9
UID:b74a0c8e-93b0-447f-aed5-b679b19e874a
DTSTART;TZID=Europe/Vienna:20210202T103000
DTEND;TZID=Europe/Vienna:20210202T133000
SUMMARY:tes
ORGANIZER;CN=admin:mailto:christoph.wurst@nextcloud.com
ATTENDEE;CN=user@domain.tld;CUTYPE=INDIVIDUAL;PARTSTAT=NEEDS-ACTION;
 ROLE=REQ-PARTICIPANT;RSVP=FALSE:mailto:user@domain.tld
DESCRIPTION:test
END:VEVENT
END:VCALENDAR
EVENT]);
		$user = $this->createMock(IUser::class);
		$this->userSession->expects(self::once())->method('getUser')->willReturn($user);
		$this->mailer->expects(self::once())->method('validateMailAddress')->willReturn(true);
		$this->eventDispatcher->expects(self::once())
			->method('dispatchTyped')
			->with(self::equalTo((new ContactInteractedWithEvent($user))->setEmail('user@domain.tld')));
		$this->logger->expects(self::never())->method('warning');

		$this->listener->handle($event);
	}
}