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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
use OC\L10N\L10N;
use OC\URLGenerator;
use OCA\DAV\CalDAV\EventComparisonService;
use OCA\DAV\CalDAV\Schedule\IMipPlugin;
use OCA\DAV\CalDAV\Schedule\IMipService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Defaults;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Mail\IMessage;
use OCP\Mail\Provider\IManager;
use OCP\Mail\Provider\IMessageSend;
use OCP\Mail\Provider\IService;
use OCP\Mail\Provider\Message as MailProviderMessage;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\ITip\Message;
use Sabre\VObject\Property\ICalendar\CalAddress;
use Symfony\Component\Mime\Email;
use Test\TestCase;
class IMipPluginCharsetTest extends TestCase {
// Dependencies
private Defaults&MockObject $defaults;
private IAppConfig&MockObject $appConfig;
private IConfig&MockObject $config;
private IDBConnection&MockObject $db;
private IFactory $l10nFactory;
private IManager&MockObject $mailManager;
private IMailer&MockObject $mailer;
private ISecureRandom&MockObject $random;
private ITimeFactory&MockObject $timeFactory;
private IUrlGenerator&MockObject $urlGenerator;
private IUserSession&MockObject $userSession;
private LoggerInterface $logger;
// Services
private EventComparisonService $eventComparisonService;
private IMipPlugin $imipPlugin;
private IMipService $imipService;
// ITip Message
private Message $itipMessage;
protected function setUp(): void {
// Used by IMipService and IMipPlugin
$today = new \DateTime('2025-06-15 14:30');
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->timeFactory->method('getTime')
->willReturn($today->getTimestamp());
$this->timeFactory->method('getDateTime')
->willReturn($today);
// IMipService
$this->urlGenerator = $this->createMock(URLGenerator::class);
$this->config = $this->createMock(IConfig::class);
$this->db = $this->createMock(IDBConnection::class);
$this->random = $this->createMock(ISecureRandom::class);
$l10n = $this->createMock(L10N::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->l10nFactory->method('findGenericLanguage')
->willReturn('en');
$this->l10nFactory->method('findLocale')
->willReturn('en_US');
$this->l10nFactory->method('get')
->willReturn($l10n);
$this->imipService = new IMipService(
$this->urlGenerator,
$this->config,
$this->db,
$this->random,
$this->l10nFactory,
$this->timeFactory,
);
// EventComparisonService
$this->eventComparisonService = new EventComparisonService();
// IMipPlugin
$this->appConfig = $this->createMock(IAppConfig::class);
$message = new \OC\Mail\Message(new Email(), false);
$this->mailer = $this->createMock(IMailer::class);
$this->mailer->method('createMessage')
->willReturn($message);
$this->mailer->method('validateMailAddress')
->willReturn(true);
$this->logger = new NullLogger();
$this->defaults = $this->createMock(Defaults::class);
$this->defaults->method('getName')
->willReturn('Instance Name 123');
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('luigi');
$this->userSession = $this->createMock(IUserSession::class);
$this->userSession->method('getUser')
->willReturn($user);
$this->mailManager = $this->createMock(IManager::class);
$this->imipPlugin = new IMipPlugin(
$this->appConfig,
$this->mailer,
$this->logger,
$this->timeFactory,
$this->defaults,
$this->userSession,
$this->imipService,
$this->eventComparisonService,
$this->mailManager,
);
// ITipMessage
$calendar = new VCalendar();
$event = new VEvent($calendar, 'VEVENT');
$event->UID = 'uid-1234';
$event->SEQUENCE = 1;
$event->SUMMARY = 'Lunch';
$event->DTSTART = new \DateTime('2025-06-20 12:30:00');
$organizer = new CalAddress($calendar, 'ORGANIZER', 'mailto:luigi@example.org');
$event->add($organizer);
$attendee = new CalAddress($calendar, 'ATTENDEE', 'mailto:jose@example.org', ['RSVP' => 'TRUE', 'CN' => 'José']);
$event->add($attendee);
$calendar->add($event);
$this->itipMessage = new Message();
$this->itipMessage->method = 'REQUEST';
$this->itipMessage->message = $calendar;
$this->itipMessage->sender = 'mailto:luigi@example.org';
$this->itipMessage->senderName = 'Luigi';
$this->itipMessage->recipient = 'mailto:' . 'jose@example.org';
}
public function testCharsetMailer(): void {
// Arrange
$symfonyEmail = null;
$this->mailer->expects(self::once())
->method('send')
->willReturnCallback(function (IMessage $message) use (&$symfonyEmail): array {
if ($message instanceof \OC\Mail\Message) {
$symfonyEmail = $message->getSymfonyEmail();
}
return [];
});
// Act
$this->imipPlugin->schedule($this->itipMessage);
// Assert
$this->assertNotNull($symfonyEmail);
$body = $symfonyEmail->getBody()->toString();
$this->assertStringContainsString('Content-Type: text/calendar; method=REQUEST; charset="utf-8"; name=event.ics', $body);
}
public function testCharsetMailProvider(): void {
// Arrange
$this->appConfig->method('getValueBool')
->with('core', 'mail_providers_enabled', true)
->willReturn(true);
$mailMessage = new MailProviderMessage();
$mailService = $this->createStubForIntersectionOfInterfaces([IService::class, IMessageSend::class]);
$mailService->method('initiateMessage')
->willReturn($mailMessage);
$mailService->expects(self::once())
->method('sendMessage');
$this->mailManager->method('findServiceByAddress')
->willReturn($mailService);
// Act
$this->imipPlugin->schedule($this->itipMessage);
// Assert
$attachments = $mailMessage->getAttachments();
$this->assertCount(1, $attachments);
$this->assertStringContainsString('text/calendar; method=REQUEST; charset="utf-8"; name=event.ics', $attachments[0]->getType());
}
}
|