|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 7 | + * |
| 8 | + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\DAV\Tests\Unit\Listener; |
| 27 | + |
| 28 | +use OCA\DAV\Connector\Sabre\Principal; |
| 29 | +use OCA\DAV\Events\CalendarObjectCreatedEvent; |
| 30 | +use OCA\DAV\Events\CalendarShareUpdatedEvent; |
| 31 | +use OCA\DAV\Listener\CalendarContactInteractionListener; |
| 32 | +use OCP\Contacts\Events\ContactInteractedWithEvent; |
| 33 | +use OCP\EventDispatcher\Event; |
| 34 | +use OCP\EventDispatcher\IEventDispatcher; |
| 35 | +use OCP\IUser; |
| 36 | +use OCP\IUserSession; |
| 37 | +use OCP\Mail\IMailer; |
| 38 | +use PHPUnit\Framework\MockObject\MockObject; |
| 39 | +use Psr\Log\LoggerInterface; |
| 40 | +use Test\TestCase; |
| 41 | + |
| 42 | +class CalendarContactInteractionListenerTest extends TestCase { |
| 43 | + |
| 44 | + /** @var IEventDispatcher|MockObject */ |
| 45 | + private $eventDispatcher; |
| 46 | + |
| 47 | + /** @var IUserSession|MockObject */ |
| 48 | + private $userSession; |
| 49 | + |
| 50 | + /** @var Principal|MockObject */ |
| 51 | + private $principalConnector; |
| 52 | + |
| 53 | + /** @var LoggerInterface|MockObject */ |
| 54 | + private $logger; |
| 55 | + |
| 56 | + /** @var IMailer|MockObject */ |
| 57 | + private $mailer; |
| 58 | + |
| 59 | + /** @var CalendarContactInteractionListener */ |
| 60 | + private $listener; |
| 61 | + |
| 62 | + protected function setUp(): void { |
| 63 | + parent::setUp(); |
| 64 | + |
| 65 | + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); |
| 66 | + $this->userSession = $this->createMock(IUserSession::class); |
| 67 | + $this->principalConnector = $this->createMock(Principal::class); |
| 68 | + $this->mailer = $this->createMock(IMailer::class); |
| 69 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 70 | + |
| 71 | + $this->listener = new CalendarContactInteractionListener( |
| 72 | + $this->eventDispatcher, |
| 73 | + $this->userSession, |
| 74 | + $this->principalConnector, |
| 75 | + $this->mailer, |
| 76 | + $this->logger |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function testParseUnrelated(): void { |
| 81 | + $event = new Event(); |
| 82 | + $this->eventDispatcher->expects(self::never())->method('dispatchTyped'); |
| 83 | + |
| 84 | + $this->listener->handle($event); |
| 85 | + } |
| 86 | + |
| 87 | + public function testHandleWithoutAnythingInteresting(): void { |
| 88 | + $event = new CalendarShareUpdatedEvent(123, [], [], [], []); |
| 89 | + $user = $this->createMock(IUser::class); |
| 90 | + $this->userSession->expects(self::once())->method('getUser')->willReturn($user); |
| 91 | + $this->eventDispatcher->expects(self::never())->method('dispatchTyped'); |
| 92 | + |
| 93 | + $this->listener->handle($event); |
| 94 | + } |
| 95 | + |
| 96 | + public function testParseInvalidData(): void { |
| 97 | + $event = new CalendarObjectCreatedEvent(123, [], [], ['calendardata' => 'BEGIN:FOO']); |
| 98 | + $user = $this->createMock(IUser::class); |
| 99 | + $this->userSession->expects(self::once())->method('getUser')->willReturn($user); |
| 100 | + $this->eventDispatcher->expects(self::never())->method('dispatchTyped'); |
| 101 | + $this->logger->expects(self::once())->method('warning'); |
| 102 | + |
| 103 | + $this->listener->handle($event); |
| 104 | + } |
| 105 | + |
| 106 | + public function testParseCalendarEventWithInvalidEmail(): void { |
| 107 | + $event = new CalendarObjectCreatedEvent(123, [], [], ['calendardata' => <<<EVENT |
| 108 | +BEGIN:VCALENDAR |
| 109 | +VERSION:2.0 |
| 110 | +CALSCALE:GREGORIAN |
| 111 | +PRODID:-//IDN nextcloud.com//Calendar app 2.1.3//EN |
| 112 | +BEGIN:VTIMEZONE |
| 113 | +TZID:Europe/Vienna |
| 114 | +BEGIN:DAYLIGHT |
| 115 | +TZOFFSETFROM:+0100 |
| 116 | +TZOFFSETTO:+0200 |
| 117 | +TZNAME:CEST |
| 118 | +DTSTART:19700329T020000 |
| 119 | +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU |
| 120 | +END:DAYLIGHT |
| 121 | +BEGIN:STANDARD |
| 122 | +TZOFFSETFROM:+0200 |
| 123 | +TZOFFSETTO:+0100 |
| 124 | +TZNAME:CET |
| 125 | +DTSTART:19701025T030000 |
| 126 | +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU |
| 127 | +END:STANDARD |
| 128 | +END:VTIMEZONE |
| 129 | +BEGIN:VEVENT |
| 130 | +CREATED:20210202T091151Z |
| 131 | +DTSTAMP:20210203T130231Z |
| 132 | +LAST-MODIFIED:20210203T130231Z |
| 133 | +SEQUENCE:9 |
| 134 | +UID:b74a0c8e-93b0-447f-aed5-b679b19e874a |
| 135 | +DTSTART;TZID=Europe/Vienna:20210202T103000 |
| 136 | +DTEND;TZID=Europe/Vienna:20210202T133000 |
| 137 | +SUMMARY:tes |
| 138 | +ORGANIZER;CN=admin:mailto:christoph.wurst@nextcloud.com |
| 139 | +ATTENDEE;CN=somethingbutnotanemail;CUTYPE=INDIVIDUAL;PARTSTAT=NEEDS-ACTION; |
| 140 | + ROLE=REQ-PARTICIPANT;RSVP=FALSE:mailto:somethingbutnotanemail |
| 141 | +DESCRIPTION:test |
| 142 | +END:VEVENT |
| 143 | +END:VCALENDAR |
| 144 | +EVENT]); |
| 145 | + $user = $this->createMock(IUser::class); |
| 146 | + $this->userSession->expects(self::once())->method('getUser')->willReturn($user); |
| 147 | + $this->eventDispatcher->expects(self::never())->method('dispatchTyped'); |
| 148 | + $this->logger->expects(self::never())->method('warning'); |
| 149 | + |
| 150 | + $this->listener->handle($event); |
| 151 | + } |
| 152 | + |
| 153 | + public function testParseCalendarEvent(): void { |
| 154 | + $event = new CalendarObjectCreatedEvent(123, [], [], ['calendardata' => <<<EVENT |
| 155 | +BEGIN:VCALENDAR |
| 156 | +VERSION:2.0 |
| 157 | +CALSCALE:GREGORIAN |
| 158 | +PRODID:-//IDN nextcloud.com//Calendar app 2.1.3//EN |
| 159 | +BEGIN:VTIMEZONE |
| 160 | +TZID:Europe/Vienna |
| 161 | +BEGIN:DAYLIGHT |
| 162 | +TZOFFSETFROM:+0100 |
| 163 | +TZOFFSETTO:+0200 |
| 164 | +TZNAME:CEST |
| 165 | +DTSTART:19700329T020000 |
| 166 | +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU |
| 167 | +END:DAYLIGHT |
| 168 | +BEGIN:STANDARD |
| 169 | +TZOFFSETFROM:+0200 |
| 170 | +TZOFFSETTO:+0100 |
| 171 | +TZNAME:CET |
| 172 | +DTSTART:19701025T030000 |
| 173 | +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU |
| 174 | +END:STANDARD |
| 175 | +END:VTIMEZONE |
| 176 | +BEGIN:VEVENT |
| 177 | +CREATED:20210202T091151Z |
| 178 | +DTSTAMP:20210203T130231Z |
| 179 | +LAST-MODIFIED:20210203T130231Z |
| 180 | +SEQUENCE:9 |
| 181 | +UID:b74a0c8e-93b0-447f-aed5-b679b19e874a |
| 182 | +DTSTART;TZID=Europe/Vienna:20210202T103000 |
| 183 | +DTEND;TZID=Europe/Vienna:20210202T133000 |
| 184 | +SUMMARY:tes |
| 185 | +ORGANIZER;CN=admin:mailto:christoph.wurst@nextcloud.com |
| 186 | +ATTENDEE;CN=user@domain.tld;CUTYPE=INDIVIDUAL;PARTSTAT=NEEDS-ACTION; |
| 187 | + ROLE=REQ-PARTICIPANT;RSVP=FALSE:mailto:user@domain.tld |
| 188 | +DESCRIPTION:test |
| 189 | +END:VEVENT |
| 190 | +END:VCALENDAR |
| 191 | +EVENT]); |
| 192 | + $user = $this->createMock(IUser::class); |
| 193 | + $this->userSession->expects(self::once())->method('getUser')->willReturn($user); |
| 194 | + $this->mailer->expects(self::once())->method('validateMailAddress')->willReturn(true); |
| 195 | + $this->eventDispatcher->expects(self::once()) |
| 196 | + ->method('dispatchTyped') |
| 197 | + ->with(self::equalTo((new ContactInteractedWithEvent($user))->setEmail('user@domain.tld'))); |
| 198 | + $this->logger->expects(self::never())->method('warning'); |
| 199 | + |
| 200 | + $this->listener->handle($event); |
| 201 | + } |
| 202 | +} |
0 commit comments