diff --git a/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php b/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php index ffe6930fdb4a..f444696f9f3b 100644 --- a/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php +++ b/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php @@ -41,21 +41,12 @@ * @license http://sabre.io/license/ Modified BSD License */ class IMipPlugin extends SabreIMipPlugin { - /** @var IMailer */ - private $mailer; - - /** @var ILogger */ - private $logger; - - /** @var IRequest */ - private $request; + private IMailer $mailer; + private ILogger $logger; + private IRequest $request; /** * Creates the email handler. - * - * @param IMailer $mailer - * @param ILogger $logger - * @param IRequest $request */ public function __construct(IMailer $mailer, ILogger $logger, IRequest $request) { parent::__construct(''); @@ -123,7 +114,7 @@ public function schedule(ITip\Message $iTipMessage) { ->setFrom([$sender => $senderName]) ->setTo([$recipient => $recipientName]) ->setSubject($subject) - ->setBody($iTipMessage->message->serialize(), $contentType); + ->attach($iTipMessage->message->serialize(), "event.ics", $contentType); try { $failed = $this->mailer->send($message); $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip'; diff --git a/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php b/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php index ae617ad581af..85540fe29656 100644 --- a/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php +++ b/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php @@ -22,124 +22,88 @@ namespace OCA\DAV\Tests\unit\CalDAV\Schedule; +use Exception; use OC\Mail\Mailer; use OCA\DAV\CalDAV\Schedule\IMipPlugin; use OCP\ILogger; use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\VObject\Component\VCalendar; use Sabre\VObject\ITip\Message; +use Symfony\Component\Mime\Email; use Test\TestCase; use OC\Log; class IMipPluginTest extends TestCase { - public function testDelivery() { - $mailMessage = new \OC\Mail\Message(new \Swift_Message()); - /** @var Mailer | \PHPUnit\Framework\MockObject\MockObject $mailer */ - $mailer = $this->createMock(Mailer::class); - $mailer->method('createMessage')->willReturn($mailMessage); - $mailer->expects($this->once())->method('send'); - /** @var ILogger | \PHPUnit\Framework\MockObject\MockObject $logger */ - $logger = $this->createMock(Log::class); - /** @var IRequest| \PHPUnit\Framework\MockObject\MockObject $request */ + private \OC\Mail\Message $mailMessage; + /** + * @var Mailer|MockObject + */ + private $mailer; + private IMipPlugin $plugin; + /** @var ILogger|MockObject */ + private $logger; + + protected function setUp(): void { + parent::setUp(); + + $this->mailMessage = new \OC\Mail\Message(new Email()); + $this->mailer = $this->createMock(Mailer::class); + $this->mailer->method('createMessage')->willReturn($this->mailMessage); + + $this->logger = $this->createMock(Log::class); + /** @var IRequest| MockObject $request */ $request = $this->createMock(IRequest::class); - $plugin = new IMipPlugin($mailer, $logger, $request); - $message = new Message(); - $message->method = 'REQUEST'; - $message->message = new VCalendar(); - $message->message->add('VEVENT', [ - 'UID' => $message->uid, - 'SEQUENCE' => $message->sequence, - 'SUMMARY' => 'Fellowship meeting', - ]); - $message->sender = 'mailto:gandalf@wiz.ard'; - $message->recipient = 'mailto:frodo@hobb.it'; + $this->plugin = new IMipPlugin($this->mailer, $this->logger, $request); + } + + public function testDelivery(): void { + $this->mailer->expects($this->once())->method('send'); - $plugin->schedule($message); + $message = $this->buildIMIPMessage('REQUEST'); + + $this->plugin->schedule($message); $this->assertEquals('1.1', $message->getScheduleStatus()); - $this->assertEquals('Fellowship meeting', $mailMessage->getSubject()); - $this->assertEquals(['frodo@hobb.it' => null], $mailMessage->getTo()); - $this->assertEquals(['gandalf@wiz.ard' => null], $mailMessage->getReplyTo()); - $this->assertEquals('text/calendar; charset=UTF-8; method=REQUEST', $mailMessage->getMessage()->getContentType()); + $this->assertEquals('Fellowship meeting', $this->mailMessage->getSubject()); + $this->assertEquals(['frodo@hobb.it' => null], $this->mailMessage->getTo()); + $this->assertEquals(['gandalf@wiz.ard' => null], $this->mailMessage->getReplyTo()); + $this->assertStringContainsString('text/calendar; charset=UTF-8; method=REQUEST', $this->mailMessage->getMessage()->getBody()->bodyToString()); } - public function testFailedDeliveryWithException() { - $mailMessage = new \OC\Mail\Message(new \Swift_Message()); - /** @var Mailer | \PHPUnit\Framework\MockObject\MockObject $mailer */ - $mailer = $this->createMock(Mailer::class); - $mailer->method('createMessage')->willReturn($mailMessage); - $mailer->method('send')->willThrowException(new \Exception()); - /** @var ILogger | \PHPUnit\Framework\MockObject\MockObject $logger */ - $logger = $this->createMock(Log::class); - /** @var IRequest| \PHPUnit\Framework\MockObject\MockObject $request */ - $request = $this->createMock(IRequest::class); + public function testFailedDeliveryWithException(): void { + $ex = new Exception(); + $this->mailer->method('send')->willThrowException($ex); + $this->logger->expects(self::once())->method('logException')->with($ex, ['app' => 'dav']); - $plugin = new IMipPlugin($mailer, $logger, $request); - $message = new Message(); - $message->method = 'REQUEST'; - $message->message = new VCalendar(); - $message->message->add('VEVENT', [ - 'UID' => $message->uid, - 'SEQUENCE' => $message->sequence, - 'SUMMARY' => 'Fellowship meeting', - ]); - $message->sender = 'mailto:gandalf@wiz.ard'; - $message->recipient = 'mailto:frodo@hobb.it'; + $message = $this->buildIMIPMessage('REQUEST'); - $plugin->schedule($message); - $this->assertEquals('5.0', $message->getScheduleStatus()); - $this->assertEquals('Fellowship meeting', $mailMessage->getSubject()); - $this->assertEquals(['frodo@hobb.it' => null], $mailMessage->getTo()); - $this->assertEquals(['gandalf@wiz.ard' => null], $mailMessage->getReplyTo()); - $this->assertEquals('text/calendar; charset=UTF-8; method=REQUEST', $mailMessage->getMessage()->getContentType()); + $this->plugin->schedule($message); + $this->assertIMipState($message, '5.0', 'REQUEST', 'Fellowship meeting'); } - public function testFailedDelivery() { - $mailMessage = new \OC\Mail\Message(new \Swift_Message()); - /** @var Mailer | \PHPUnit\Framework\MockObject\MockObject $mailer */ - $mailer = $this->createMock(Mailer::class); - $mailer->method('createMessage')->willReturn($mailMessage); - $mailer->method('send')->willReturn(['foo@example.net']); - /** @var ILogger | \PHPUnit\Framework\MockObject\MockObject $logger */ - $logger = $this->createMock(Log::class); - $logger->expects(self::once())->method('error')->with('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => 'foo@example.net']); - /** @var IRequest| \PHPUnit\Framework\MockObject\MockObject $request */ - $request = $this->createMock(IRequest::class); + public function testFailedDelivery(): void { + $this->mailer->method('send')->willReturn(['foo@example.net']); + $this->logger->expects(self::once())->method('error')->with('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => 'foo@example.net']); - $plugin = new IMipPlugin($mailer, $logger, $request); - $message = new Message(); - $message->method = 'REQUEST'; - $message->message = new VCalendar(); - $message->message->add('VEVENT', [ - 'UID' => $message->uid, - 'SEQUENCE' => $message->sequence, - 'SUMMARY' => 'Fellowship meeting', - ]); - $message->sender = 'mailto:gandalf@wiz.ard'; - $message->recipient = 'mailto:frodo@hobb.it'; + $message = $this->buildIMIPMessage('REQUEST'); - $plugin->schedule($message); - $this->assertEquals('5.0', $message->getScheduleStatus()); - $this->assertEquals('Fellowship meeting', $mailMessage->getSubject()); - $this->assertEquals(['frodo@hobb.it' => null], $mailMessage->getTo()); - $this->assertEquals(['gandalf@wiz.ard' => null], $mailMessage->getReplyTo()); - $this->assertEquals('text/calendar; charset=UTF-8; method=REQUEST', $mailMessage->getMessage()->getContentType()); + $this->plugin->schedule($message); + $this->assertIMipState($message, '5.0', 'REQUEST', 'Fellowship meeting'); } - public function testDeliveryOfCancel() { - $mailMessage = new \OC\Mail\Message(new \Swift_Message()); - /** @var Mailer | \PHPUnit\Framework\MockObject\MockObject $mailer */ - $mailer = $this->createMock(Mailer::class); - $mailer->method('createMessage')->willReturn($mailMessage); - $mailer->expects($this->once())->method('send'); - /** @var ILogger | \PHPUnit\Framework\MockObject\MockObject $logger */ - $logger = $this->createMock(Log::class); - /** @var IRequest| \PHPUnit\Framework\MockObject\MockObject $request */ - $request = $this->createMock(IRequest::class); + public function testDeliveryOfCancel(): void { + $this->mailer->expects($this->once())->method('send'); - $plugin = new IMipPlugin($mailer, $logger, $request); + $message = $this->buildIMIPMessage('CANCEL'); + + $this->plugin->schedule($message); + $this->assertIMipState($message, '1.1', 'CANCEL', 'Cancelled: Fellowship meeting'); + } + + private function buildIMIPMessage(string $method): Message { $message = new Message(); - $message->method = 'CANCEL'; + $message->method = $method; $message->message = new VCalendar(); $message->message->add('VEVENT', [ 'UID' => $message->uid, @@ -148,13 +112,14 @@ public function testDeliveryOfCancel() { ]); $message->sender = 'mailto:gandalf@wiz.ard'; $message->recipient = 'mailto:frodo@hobb.it'; + return $message; + } - $plugin->schedule($message); - $this->assertEquals('1.1', $message->getScheduleStatus()); - $this->assertEquals('Cancelled: Fellowship meeting', $mailMessage->getSubject()); - $this->assertEquals(['frodo@hobb.it' => null], $mailMessage->getTo()); - $this->assertEquals(['gandalf@wiz.ard' => null], $mailMessage->getReplyTo()); - $this->assertEquals('text/calendar; charset=UTF-8; method=CANCEL', $mailMessage->getMessage()->getContentType()); - $this->assertEquals('CANCELLED', $message->message->VEVENT->STATUS->getValue()); + private function assertIMipState(Message $message, string $scheduleStatus, string $method, string $mailSubject): void { + $this->assertEquals($scheduleStatus, $message->getScheduleStatus()); + $this->assertEquals($mailSubject, $this->mailMessage->getSubject()); + $this->assertEquals(['frodo@hobb.it' => null], $this->mailMessage->getTo()); + $this->assertEquals(['gandalf@wiz.ard' => null], $this->mailMessage->getReplyTo()); + $this->assertStringContainsString("text/calendar; charset=UTF-8; method=$method", $this->mailMessage->getMessage()->getBody()->bodyToString()); } } diff --git a/lib/private/Mail/Message.php b/lib/private/Mail/Message.php index 1b8598594b50..d716531b6e10 100644 --- a/lib/private/Mail/Message.php +++ b/lib/private/Mail/Message.php @@ -22,51 +22,41 @@ namespace OC\Mail; +use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; /** - * Class Message provides a wrapper around SwiftMail + * Class Message provides a wrapper around Symfony\Component\Mime\Email * * @package OC\Mail */ class Message { private Email $message; + /** + * @var Address[] + */ + private array $from; + private array $replyTo; + private array $to; + private array $cc; + private array $bcc; public function __construct(Email $swiftMessage) { $this->message = $swiftMessage; } /** - * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains - * FIXME: Remove this once SwiftMailer supports IDN - * * @param array $addresses Array of mail addresses, key will get converted - * @return array Converted addresses if `idn_to_ascii` exists + * @return Address[] Converted addresses if `idn_to_ascii` exists */ protected function convertAddresses(array $addresses): array { - if (!\function_exists('idn_to_ascii')) { - return $addresses; - } - $convertedAddresses = []; foreach ($addresses as $email => $readableName) { - if (!\is_numeric($email)) { - [$name, $domain] = \explode('@', $email, 2); - if (\defined('INTL_IDNA_VARIANT_UTS46')) { - $domain = \idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); - } else { - $domain = \idn_to_ascii($domain); - } - $convertedAddresses[$name.'@'.$domain] = $readableName; + if (\is_numeric($email)) { + $convertedAddresses[] = new Address($readableName); } else { - [$name, $domain] = \explode('@', $readableName, 2); - if (\defined('INTL_IDNA_VARIANT_UTS46')) { - $domain = \idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46); - } else { - $domain = \idn_to_ascii($domain); - } - $convertedAddresses[$email] = $name.'@'.$domain; + $convertedAddresses[] = new Address($email, $readableName ?? ''); } } @@ -81,9 +71,9 @@ protected function convertAddresses(array $addresses): array { * @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name') */ public function setFrom(array $addresses): Message { - $addresses = $this->convertAddresses($addresses); + $this->message->from(...$this->convertAddresses($addresses)); - $this->message->from(...$addresses); + $this->from = $addresses; return $this; } @@ -93,16 +83,16 @@ public function setFrom(array $addresses): Message { * @return array */ public function getFrom(): array { - return $this->message->getFrom(); + return $this->from; } /** * Set the Reply-To address of this message */ public function setReplyTo(array $addresses): Message { - $addresses = $this->convertAddresses($addresses); + $this->message->replyTo(...$this->convertAddresses($addresses)); - $this->message->replyTo(...$addresses); + $this->replyTo = $addresses; return $this; } @@ -110,18 +100,18 @@ public function setReplyTo(array $addresses): Message { * Returns the Reply-To address of this message */ public function getReplyTo(): array { - return $this->message->getReplyTo(); + return $this->replyTo; } /** - * Set the to addresses of this message. + * Set the to-addresses of this message. * * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name') */ public function setTo(array $recipients): Message { - $recipients = $this->convertAddresses($recipients); + $this->message->to(...$this->convertAddresses($recipients)); - $this->message->to(...$recipients); + $this->to = $recipients; return $this; } @@ -131,7 +121,7 @@ public function setTo(array $recipients): Message { * @return array */ public function getTo(): array { - return $this->message->getTo(); + return $this->to; } /** @@ -140,9 +130,9 @@ public function getTo(): array { * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name') */ public function setCc(array $recipients): Message { - $recipients = $this->convertAddresses($recipients); + $this->message->cc(...$this->convertAddresses($recipients)); - $this->message->cc(...$recipients); + $this->cc = $recipients; return $this; } @@ -150,7 +140,7 @@ public function setCc(array $recipients): Message { * Get the cc address of this message. */ public function getCc(): array { - return $this->message->getCc(); + return $this->cc; } /** @@ -159,9 +149,9 @@ public function getCc(): array { * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name') */ public function setBcc(array $recipients): Message { - $recipients = $this->convertAddresses($recipients); + $this->message->bcc(...$this->convertAddresses($recipients)); - $this->message->bcc(...$recipients); + $this->bcc = $recipients; return $this; } @@ -169,7 +159,7 @@ public function setBcc(array $recipients): Message { * Get the Bcc address of this message. */ public function getBcc(): array { - return $this->message->getBcc(); + return $this->bcc; } /** @@ -181,7 +171,7 @@ public function setSubject(string $subject): Message { } /** - * Get the from subject of this message. + * Get the subject of this message. */ public function getSubject(): string { return $this->message->getSubject(); @@ -228,4 +218,9 @@ public function setBody(string $body, string $contentType): Message { return $this; } + + public function attach($body, string $name = null, string $contentType = null): self { + $this->message->attach($body, $name, $contentType); + return $this; + } } diff --git a/tests/Settings/Controller/MailSettingsControllerTest.php b/tests/Settings/Controller/MailSettingsControllerTest.php index c37206aa97fd..98fd00d75801 100644 --- a/tests/Settings/Controller/MailSettingsControllerTest.php +++ b/tests/Settings/Controller/MailSettingsControllerTest.php @@ -11,46 +11,52 @@ namespace Tests\Settings\Controller; use OC\Settings\Application; +use OCP\AppFramework\IAppContainer; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; +use OC\User\User; +use OCP\IConfig; +use OCP\IL10N; +use OC\User\Session; +use OC\Mail\Mailer; /** * @package Tests\Settings\Controller */ -class MailSettingsControllerTest extends \Test\TestCase { - private $container; +class MailSettingsControllerTest extends TestCase { + private IAppContainer $container; protected function setUp(): void { parent::setUp(); $app = new Application(); $this->container = $app->getContainer(); - $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') + $this->container['Config'] = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor()->getMock(); - $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') + $this->container['L10N'] = $this->getMockBuilder(IL10N::class) ->disableOriginalConstructor()->getMock(); $this->container['AppName'] = 'settings'; - $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') + $this->container['UserSession'] = $this->getMockBuilder(Session::class) ->disableOriginalConstructor()->getMock(); - $this->container['MailMessage'] = $this->getMockBuilder('\OCP\Mail\IMessage') - ->disableOriginalConstructor()->getMock(); - $this->container['Mailer'] = $this->getMockBuilder('\OC\Mail\Mailer') + $this->container['Mailer'] = $this->getMockBuilder(Mailer::class) ->setMethods(['send', 'validateMailAddress']) ->disableOriginalConstructor()->getMock(); - $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults') + $this->container['Defaults'] = $this->getMockBuilder(\OC_Defaults::class) ->disableOriginalConstructor()->getMock(); $this->container['DefaultMailAddress'] = 'no-reply@owncloud.com'; } - public function testSetInvalidMail() { + public function testSetInvalidMail(): void { $this->container['L10N'] ->expects($this->exactly(2)) ->method('t') - ->will($this->returnValue('Invalid email address')); + ->willReturn('Invalid email address'); $this->container['Mailer'] ->expects($this->exactly(2)) ->method('validateMailAddress') ->with('@@owncloud.com') - ->will($this->returnValue(false)); + ->willReturn(false); // With authentication $response = $this->container['MailSettingsController']->setMailSettings( @@ -77,54 +83,26 @@ public function testSetInvalidMail() { 0, '25' ); - $expectedResponse = ['data' => ['message' =>'Invalid email address'], 'status' => 'error']; + $this->assertSame($expectedResponse, $response); } - public function testSetMailSettings() { + public function testSetMailSettings(): void { $this->container['L10N'] ->expects($this->exactly(2)) ->method('t') - ->will($this->returnValue('Saved')); - - /** - * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 - */ - /* - $this->container['Config'] - ->expects($this->exactly(15)) - ->method('setSystemValue') - ->withConsecutive( - array($this->equalTo('mail_domain'), $this->equalTo('owncloud.com')), - array($this->equalTo('mail_from_address'), $this->equalTo('demo')), - array($this->equalTo('mail_smtpmode'), $this->equalTo('smtp')), - array($this->equalTo('mail_smtpsecure'), $this->equalTo('ssl')), - array($this->equalTo('mail_smtphost'), $this->equalTo('mx.owncloud.com')), - array($this->equalTo('mail_smtpauthtype'), $this->equalTo('NTLM')), - array($this->equalTo('mail_smtpauth'), $this->equalTo(1)), - array($this->equalTo('mail_smtpport'), $this->equalTo('25')), - array($this->equalTo('mail_domain'), $this->equalTo('owncloud.com')), - array($this->equalTo('mail_from_address'), $this->equalTo('demo@owncloud.com')), - array($this->equalTo('mail_smtpmode'), $this->equalTo('smtp')), - array($this->equalTo('mail_smtpsecure'), $this->equalTo('ssl')), - array($this->equalTo('mail_smtphost'), $this->equalTo('mx.owncloud.com')), - array($this->equalTo('mail_smtpauthtype'), $this->equalTo('NTLM')), - array($this->equalTo('mail_smtpport'), $this->equalTo('25')) - ); - */ + ->willReturn('Saved'); $this->container['Mailer'] ->expects($this->exactly(2)) ->method('validateMailAddress') ->with('demo@owncloud.com') - ->will($this->returnValue(true)); + ->willReturn(true); - /** @var \PHPUnit\Framework\MockObject\MockObject $config */ + /** @var MockObject $config */ $config = $this->container['Config']; $config->expects($this->exactly(2)) - ->method('setSystemValues'); - /** - * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1 + ->method('setSystemValues') ->withConsecutive( [[ 'mail_domain' => 'owncloud.com', @@ -132,7 +110,6 @@ public function testSetMailSettings() { 'mail_smtpmode' => 'smtp', 'mail_smtpsecure' => 'ssl', 'mail_smtphost' => 'mx.owncloud.com', - 'mail_smtpauthtype' => 'NTLM', 'mail_smtpauth' => 1, 'mail_smtpport' => '25', ]], @@ -142,14 +119,12 @@ public function testSetMailSettings() { 'mail_smtpmode' => 'smtp', 'mail_smtpsecure' => 'ssl', 'mail_smtphost' => 'mx.owncloud.com', - 'mail_smtpauthtype' => 'NTLM', 'mail_smtpauth' => null, - 'mail_smtpport' => '25', + 'mail_smtpport' => '587', 'mail_smtpname' => null, - 'mail_smtppassword' => null, + 'mail_smtppassword' => null ]] ); - */ // With authentication $response = $this->container['MailSettingsController']->setMailSettings( @@ -158,7 +133,6 @@ public function testSetMailSettings() { 'smtp', 'ssl', 'mx.owncloud.com', - 'NTLM', 1, '25' ); @@ -172,19 +146,17 @@ public function testSetMailSettings() { 'smtp', 'ssl', 'mx.owncloud.com', - 'NTLM', 0, - '25' + '587' ); - $expectedResponse = ['data' => ['message' =>'Saved'], 'status' => 'success']; $this->assertSame($expectedResponse, $response); } - public function testStoreCredentials() { + public function testStoreCredentials(): void { $this->container['L10N'] ->expects($this->once()) ->method('t') - ->will($this->returnValue('Saved')); + ->willReturn('Saved'); $this->container['Config'] ->expects($this->once()) @@ -200,47 +172,43 @@ public function testStoreCredentials() { $this->assertSame($expectedResponse, $response); } - public function testSendTestMail() { - $user = $this->getMockBuilder('\OC\User\User') + public function testSendTestMail(): void { + $user = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); - $user->expects($this->any()) + $user ->method('getUID') - ->will($this->returnValue('Werner')); - $user->expects($this->any()) + ->willReturn('Werner'); + $user ->method('getDisplayName') - ->will($this->returnValue('Werner Brösel')); + ->willReturn('Werner Brösel'); $this->container['L10N'] - ->expects($this->any()) ->method('t') - ->will( - $this->returnValueMap( - [ - ['You need to set your user email before being able to send test emails.', [], - 'You need to set your user email before being able to send test emails.'], - ['A problem occurred while sending the e-mail. Please revisit your settings.', [], - 'A problem occurred while sending the e-mail. Please revisit your settings.'], - ['Email sent', [], 'Email sent'], - ['test email settings', [], 'test email settings'], - ['If you received this email, the settings seem to be correct.', [], - 'If you received this email, the settings seem to be correct.'] - ] - ) + ->willReturnMap( + [ + ['You need to set your user email before being able to send test emails.', [], + 'You need to set your user email before being able to send test emails.'], + ['A problem occurred while sending the e-mail. Please revisit your settings.', [], + 'A problem occurred while sending the e-mail. Please revisit your settings.'], + ['Email sent', [], 'Email sent'], + ['test email settings', [], 'test email settings'], + ['If you received this email, the settings seem to be correct.', [], + 'If you received this email, the settings seem to be correct.'] + ] ); $this->container['UserSession'] - ->expects($this->any()) ->method('getUser') - ->will($this->returnValue($user)); + ->willReturn($user); // Ensure that it fails when no mail address has been specified $response = $this->container['MailSettingsController']->sendTestMail(); $expectedResponse = ['data' => ['message' =>'You need to set your user email before being able to send test emails.'], 'status' => 'error']; $this->assertSame($expectedResponse, $response); - $user->expects($this->any()) + $user ->method('getEMailAddress') - ->will($this->returnValue('mail@example.invalid')); + ->willReturn('mail@example.invalid'); $response = $this->container['MailSettingsController']->sendTestMail(); $expectedResponse = ['data' => ['message' =>'Email sent'], 'status' => 'success']; $this->assertSame($expectedResponse, $response); diff --git a/tests/Settings/Controller/UsersControllerTest.php b/tests/Settings/Controller/UsersControllerTest.php index ebe2b64b1462..0ce11dd3bc0b 100644 --- a/tests/Settings/Controller/UsersControllerTest.php +++ b/tests/Settings/Controller/UsersControllerTest.php @@ -3530,6 +3530,10 @@ public function testSetPassword(): void { ->method('send') ->with($message) ->willReturn([]); + $l10n->method('t') + ->willReturnCallback(function ($text, $parameters = []) { + return \vsprintf($text, $parameters); + }); $result = $usersController->setPassword('fooBaZ1', 'foo', '123'); $this->assertEquals(new Http\JSONResponse(['status' => 'success']), $result); diff --git a/tests/lib/Mail/MessageTest.php b/tests/lib/Mail/MessageTest.php index 3830b4511936..e12cb1c3c8c7 100644 --- a/tests/lib/Mail/MessageTest.php +++ b/tests/lib/Mail/MessageTest.php @@ -9,6 +9,7 @@ namespace Test\Mail; use OC\Mail\Message; +use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; use Test\TestCase; @@ -17,159 +18,60 @@ class MessageTest extends TestCase { private $symfonyMail; private Message $message; - public function mailAddressProvider(): array { - return [ - [['lukas@owncloud.com' => 'Lukas Reschke'], ['lukas@owncloud.com' => 'Lukas Reschke']], - [['lukas@owncloud.com' => 'Lukas Reschke', 'lukas@öwnclöüd.com', 'lukäs@owncloud.örg' => 'Lükäs Réschke'], - ['lukas@owncloud.com' => 'Lukas Reschke', 'lukas@xn--wncld-iuae2c.com', 'lukäs@owncloud.xn--rg-eka' => 'Lükäs Réschke']], - [['lukas@öwnclöüd.com'], ['lukas@xn--wncld-iuae2c.com']] - ]; - } - public function setUp(): void { parent::setUp(); - $this->symfonyMail = $this->getMockBuilder(Email::class) - ->disableOriginalConstructor()->getMock(); - + $this->symfonyMail = new Email(); $this->message = new Message($this->symfonyMail); } - /** - * @requires function idn_to_ascii - * @dataProvider mailAddressProvider - */ - public function testConvertAddresses(array $unconverted, array $expected): void { - $this->assertSame($expected, self::invokePrivate($this->message, 'convertAddresses', [$unconverted])); - } - - public function testSetFrom(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('from') - ->with('lukas@owncloud.com'); + public function testFrom(): void { $this->message->setFrom(['lukas@owncloud.com']); - } - - public function testGetFrom(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('getFrom') - ->willReturn(['lukas@owncloud.com']); - $this->assertSame(['lukas@owncloud.com'], $this->message->getFrom()); + $this->assertEquals([new Address('lukas@owncloud.com')], $this->symfonyMail->getFrom()); } - public function testSetReplyTo(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('replyTo') - ->with('lukas@owncloud.com'); - $this->message->setReplyTo(['lukas@owncloud.com']); - } - - public function testGetReplyTo(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('getReplyTo') - ->willReturn(['lukas@owncloud.com']); - - $this->assertSame(['lukas@owncloud.com'], $this->message->getReplyTo()); - } - - public function testSetTo(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('to') - ->with('lukas@owncloud.com'); + public function testTo(): void { $this->message->setTo(['lukas@owncloud.com']); - } - - public function testGetTo(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('getTo') - ->willReturn(['lukas@owncloud.com']); - $this->assertSame(['lukas@owncloud.com'], $this->message->getTo()); + $this->assertEquals([new Address('lukas@owncloud.com')], $this->symfonyMail->getTo()); } - public function testSetCc(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('cc') - ->with('lukas@owncloud.com'); - $this->message->setCc(['lukas@owncloud.com']); + public function testReplyTo(): void { + $this->message->setReplyTo(['lukas@owncloud.com']); + $this->assertSame(['lukas@owncloud.com'], $this->message->getReplyTo()); + $this->assertEquals([new Address('lukas@owncloud.com')], $this->symfonyMail->getReplyTo()); } - public function testGetCc(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('getCc') - ->willReturn(['lukas@owncloud.com']); - + public function testCC(): void { + $this->message->setCc(['lukas@owncloud.com']); $this->assertSame(['lukas@owncloud.com'], $this->message->getCc()); + $this->assertEquals([new Address('lukas@owncloud.com')], $this->symfonyMail->getCc()); } - public function testSetBcc(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('bcc') - ->with('lukas@owncloud.com'); + public function testBCC(): void { $this->message->setBcc(['lukas@owncloud.com']); - } - - public function testGetBcc(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('getBcc') - ->willReturn(['lukas@owncloud.com']); - $this->assertSame(['lukas@owncloud.com'], $this->message->getBcc()); + $this->assertEquals([new Address('lukas@owncloud.com')], $this->symfonyMail->getBcc()); } - - public function testSetSubject(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('subject') - ->with('Fancy Subject'); - + public function testSubject(): void { $this->message->setSubject('Fancy Subject'); - } - - public function testGetSubject(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('getSubject') - ->willReturn('Fancy Subject'); - $this->assertSame('Fancy Subject', $this->message->getSubject()); + $this->assertEquals('Fancy Subject', $this->symfonyMail->getSubject()); } public function testSetPlainBody(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('text') - ->with('Fancy Body'); - $this->message->setPlainBody('Fancy Body'); + self::assertEquals('Fancy Body', $this->symfonyMail->getTextBody()); } public function testGetPlainBody(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('getTextBody') - ->willReturn('Fancy Body'); - + $this->symfonyMail->text('Fancy Body'); $this->assertSame('Fancy Body', $this->message->getPlainBody()); } public function testSetHtmlBody(): void { - $this->symfonyMail - ->expects($this->once()) - ->method('html') - ->with('Fancy Body', 'utf-8'); - $this->message->setHtmlBody('Fancy Body'); + self::assertEquals('Fancy Body', $this->symfonyMail->getHtmlBody()); } }