Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test(imip): ensure charset is set for the text/calendar attachment
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Jul 6, 2025
commit 4dee17868f937c93a50a1242e47e406a11d335fe
193 changes: 193 additions & 0 deletions apps/dav/tests/unit/CalDAV/Schedule/IMipPluginCharsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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:[email protected]');
$event->add($organizer);
$attendee = new CalAddress($calendar, 'ATTENDEE', 'mailto:[email protected]', ['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:[email protected]';
$this->itipMessage->senderName = 'Luigi';
$this->itipMessage->recipient = 'mailto:' . '[email protected]';
}

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());
}
}
Loading