2015-02-12 07:53:27 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-02-12 07:53:27 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-02-12 07:53:27 -05:00
|
|
|
*/
|
|
|
|
|
|
2016-05-19 02:50:14 -04:00
|
|
|
namespace Test\Mail;
|
2015-02-12 07:53:27 -05:00
|
|
|
|
|
|
|
|
use OC\Mail\Message;
|
2023-01-03 09:03:40 -05:00
|
|
|
use OCP\Mail\Headers\AutoSubmitted;
|
2018-04-16 09:55:39 -04:00
|
|
|
use OCP\Mail\IEMailTemplate;
|
2023-11-23 04:22:34 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-12-20 05:35:06 -05:00
|
|
|
use Symfony\Component\Mime\Address;
|
|
|
|
|
use Symfony\Component\Mime\Email;
|
|
|
|
|
use Symfony\Component\Mime\Exception\RfcComplianceException;
|
|
|
|
|
use Symfony\Component\Mime\Header\HeaderInterface;
|
|
|
|
|
use Symfony\Component\Mime\Header\Headers;
|
2016-05-19 02:50:14 -04:00
|
|
|
use Test\TestCase;
|
2015-02-12 07:53:27 -05:00
|
|
|
|
|
|
|
|
class MessageTest extends TestCase {
|
2021-12-20 05:35:06 -05:00
|
|
|
/** @var Email */
|
|
|
|
|
private $symfonyEmail;
|
2015-02-12 07:53:27 -05:00
|
|
|
/** @var Message */
|
|
|
|
|
private $message;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2025-05-13 02:48:58 -04:00
|
|
|
public static function mailAddressProvider(): array {
|
2020-03-26 04:30:18 -04:00
|
|
|
return [
|
2021-12-20 05:35:06 -05:00
|
|
|
[
|
|
|
|
|
['lukas@owncloud.com' => 'Lukas Reschke'],
|
|
|
|
|
[new Address('lukas@owncloud.com', 'Lukas Reschke')]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
'lukas@owncloud.com' => 'Lukas Reschke',
|
|
|
|
|
'lukas@öwnclöüd.com',
|
|
|
|
|
'lukäs@owncloud.örg' => 'Lükäs Réschke'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
new Address('lukas@owncloud.com', 'Lukas Reschke'),
|
|
|
|
|
new Address('lukas@öwnclöüd.com'),
|
|
|
|
|
new Address('lukäs@owncloud.örg', 'Lükäs Réschke')
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
['lukas@öwnclöüd.com'],
|
|
|
|
|
[new Address('lukas@öwnclöüd.com')]
|
|
|
|
|
],
|
2020-03-26 04:30:18 -04:00
|
|
|
];
|
2019-03-15 21:29:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getMailAddressProvider() {
|
2020-03-26 04:30:18 -04:00
|
|
|
return [
|
2021-12-20 05:35:06 -05:00
|
|
|
[[], []],
|
2020-03-26 04:30:18 -04:00
|
|
|
[['lukas@owncloud.com' => 'Lukas Reschke'], ['lukas@owncloud.com' => 'Lukas Reschke']],
|
|
|
|
|
];
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2015-02-12 07:53:27 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2025-05-13 02:48:58 -04:00
|
|
|
$this->symfonyEmail = $this->createMock(Email::class);
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->message = new Message($this->symfonyEmail, false);
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-10-16 05:02:10 -04:00
|
|
|
*
|
|
|
|
|
* @param string $unconverted
|
|
|
|
|
* @param string $expected
|
2015-02-12 07:53:27 -05:00
|
|
|
*/
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('mailAddressProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testConvertAddresses($unconverted, $expected): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->assertEquals($expected, self::invokePrivate($this->message, 'convertAddresses', [$unconverted]));
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
public function testSetRecipients(): void {
|
|
|
|
|
$this->message = $this->message->setFrom(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']);
|
|
|
|
|
$this->message = $this->message->setTo(['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]);
|
|
|
|
|
$this->message = $this->message->setReplyTo(['penny@stardewvalley-library.co.edu' => 'Penny']);
|
|
|
|
|
$this->message = $this->message->setCc(['gunther@stardewvalley-library.co.edu' => 'Gunther']);
|
|
|
|
|
$this->message = $this->message->setBcc(['pam@stardewvalley-bus.com' => 'Pam']);
|
2019-03-15 21:29:03 -04:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('from')
|
2024-10-04 10:13:28 -04:00
|
|
|
->with(new Address('pierres-general-store@stardewvalley.com', 'Pierres General Store'));
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('to')
|
2024-10-04 10:13:28 -04:00
|
|
|
->with(new Address('lewis-tent@stardewvalley.com', "Lewis' Tent Life"));
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('replyTo')
|
2024-10-04 10:13:28 -04:00
|
|
|
->with(new Address('penny@stardewvalley-library.co.edu', 'Penny'));
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('cc')
|
2024-10-04 10:13:28 -04:00
|
|
|
->with(new Address('gunther@stardewvalley-library.co.edu', 'Gunther'));
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
2015-02-12 07:53:27 -05:00
|
|
|
->expects($this->once())
|
2021-12-20 05:35:06 -05:00
|
|
|
->method('bcc')
|
2024-10-04 10:13:28 -04:00
|
|
|
->with(new Address('pam@stardewvalley-bus.com', 'Pam'));
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->message->setRecipients();
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetTo(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store'];
|
2015-04-10 11:21:52 -04:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$message = $this->message->setTo(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']);
|
2015-04-10 11:21:52 -04:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->assertEquals($expected, $message->getTo());
|
2015-04-10 11:21:52 -04:00
|
|
|
}
|
2021-12-20 05:35:06 -05:00
|
|
|
public function testSetRecipientsException(): void {
|
|
|
|
|
$message = $this->message->setTo(['lewis-tent@~~~~.com' => "Lewis' Tent Life"]);
|
2015-04-10 11:21:52 -04:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
2015-02-12 07:53:27 -05:00
|
|
|
->expects($this->once())
|
2021-12-20 05:35:06 -05:00
|
|
|
->method('to')
|
|
|
|
|
->willThrowException(new RfcComplianceException());
|
2022-10-24 10:45:24 -04:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->expectException(RfcComplianceException::class);
|
|
|
|
|
$message->setRecipients();
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
public function testSetRecipientsEmptyValues(): void {
|
|
|
|
|
$message = $this->message->setTo([]);
|
|
|
|
|
|
|
|
|
|
$this->symfonyEmail
|
2015-02-12 07:53:27 -05:00
|
|
|
->expects($this->once())
|
2021-12-20 05:35:06 -05:00
|
|
|
->method('to');
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$message->setRecipients();
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetGetFrom(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$expected = ['pierres-general-store@stardewvalley.com' => 'Pierres General Store'];
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$message = $this->message->setFrom(['pierres-general-store@stardewvalley.com' => 'Pierres General Store']);
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->assertEquals($expected, $message->getFrom());
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetGetTo(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$expected = ['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"];
|
|
|
|
|
|
|
|
|
|
$message = $this->message->setTo(['lewis-tent@stardewvalley.com' => "Lewis' Tent Life"]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $message->getTo());
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetGetReplyTo(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$expected = ['penny@stardewvalley-library.co.edu' => 'Penny'];
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$message = $this->message->setReplyTo(['penny@stardewvalley-library.co.edu' => 'Penny']);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $message->getReplyTo());
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetGetCC(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$expected = ['gunther@stardewvalley-library.co.edu' => 'Gunther'];
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$message = $this->message->setCc(['gunther@stardewvalley-library.co.edu' => 'Gunther']);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $message->getCc());
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetGetBCC(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$expected = ['pam@stardewvalley-bus.com' => 'Pam'];
|
|
|
|
|
|
|
|
|
|
$message = $this->message->setBcc(['pam@stardewvalley-bus.com' => 'Pam']);
|
2015-02-12 07:53:27 -05:00
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->assertEquals($expected, $message->getBcc());
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetPlainBody(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
2015-02-12 07:53:27 -05:00
|
|
|
->expects($this->once())
|
2021-12-20 05:35:06 -05:00
|
|
|
->method('text')
|
2015-02-12 07:53:27 -05:00
|
|
|
->with('Fancy Body');
|
|
|
|
|
|
|
|
|
|
$this->message->setPlainBody('Fancy Body');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetPlainBody(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
2015-02-12 07:53:27 -05:00
|
|
|
->expects($this->once())
|
2021-12-20 05:35:06 -05:00
|
|
|
->method('getTextBody')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('Fancy Body');
|
2015-02-12 07:53:27 -05:00
|
|
|
|
|
|
|
|
$this->assertSame('Fancy Body', $this->message->getPlainBody());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetHtmlBody(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->symfonyEmail
|
2015-02-12 07:53:27 -05:00
|
|
|
->expects($this->once())
|
2021-12-20 05:35:06 -05:00
|
|
|
->method('html')
|
|
|
|
|
->with('<blink>Fancy Body</blink>', 'utf-8');
|
2015-02-12 07:53:27 -05:00
|
|
|
|
|
|
|
|
$this->message->setHtmlBody('<blink>Fancy Body</blink>');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testPlainTextRenderOption(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
/** @var MockObject|Email $symfonyEmail */
|
|
|
|
|
$symfonyEmail = $this->getMockBuilder(Email::class)
|
2018-04-16 09:55:39 -04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2021-12-20 05:35:06 -05:00
|
|
|
/** @var MockObject|IEMailTemplate $template */
|
|
|
|
|
$template = $this->getMockBuilder(IEMailTemplate::class)
|
2018-04-16 09:55:39 -04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$message = new Message($symfonyEmail, true);
|
2018-04-16 09:55:39 -04:00
|
|
|
|
|
|
|
|
$template
|
|
|
|
|
->expects($this->never())
|
|
|
|
|
->method('renderHTML');
|
|
|
|
|
$template
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('renderText');
|
|
|
|
|
$template
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('renderSubject');
|
|
|
|
|
|
|
|
|
|
$message->useTemplate($template);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testBothRenderingOptions(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
/** @var MockObject|Email $symfonyEmail */
|
|
|
|
|
$symfonyEmail = $this->getMockBuilder(Email::class)
|
2018-04-16 09:55:39 -04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2021-12-20 05:35:06 -05:00
|
|
|
/** @var MockObject|IEMailTemplate $template */
|
|
|
|
|
$template = $this->getMockBuilder(IEMailTemplate::class)
|
2018-04-16 09:55:39 -04:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
2021-12-20 05:35:06 -05:00
|
|
|
$message = new Message($symfonyEmail, false);
|
2018-04-16 09:55:39 -04:00
|
|
|
|
|
|
|
|
$template
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('renderHTML');
|
|
|
|
|
$template
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('renderText');
|
|
|
|
|
$template
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('renderSubject');
|
|
|
|
|
|
|
|
|
|
$message->useTemplate($template);
|
|
|
|
|
}
|
2022-12-23 06:46:15 -05:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetAutoSubmitted1(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$headers = new Headers($this->createMock(HeaderInterface::class));
|
2024-08-23 09:10:27 -04:00
|
|
|
$headers->addTextHeader(AutoSubmitted::HEADER, 'yes');
|
2021-12-20 05:35:06 -05:00
|
|
|
$symfonyEmail = $this->createMock(Email::class);
|
|
|
|
|
|
|
|
|
|
$symfonyEmail->method('getHeaders')
|
|
|
|
|
->willReturn($headers);
|
|
|
|
|
|
|
|
|
|
$message = new Message($symfonyEmail, false);
|
2023-01-03 09:03:40 -05:00
|
|
|
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->assertNotSame('no', $message->getAutoSubmitted());
|
2022-12-23 06:46:15 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetAutoSubmitted2(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$headers = new Headers($this->createMock(HeaderInterface::class));
|
|
|
|
|
$headers->addTextHeader(AutoSubmitted::HEADER, 'no');
|
|
|
|
|
$symfonyEmail = $this->createMock(Email::class);
|
|
|
|
|
|
|
|
|
|
$symfonyEmail->method('getHeaders')
|
|
|
|
|
->willReturn($headers);
|
|
|
|
|
|
|
|
|
|
$message = new Message($symfonyEmail, false);
|
2023-01-03 09:03:40 -05:00
|
|
|
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
|
2021-12-20 05:35:06 -05:00
|
|
|
$this->assertSame('auto-generated', $message->getAutoSubmitted());
|
2022-12-23 06:46:15 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetAutoSubmitted(): void {
|
2021-12-20 05:35:06 -05:00
|
|
|
$headers = new Headers($this->createMock(HeaderInterface::class));
|
|
|
|
|
$headers->addTextHeader(AutoSubmitted::HEADER, 'no');
|
|
|
|
|
$symfonyEmail = $this->createMock(Email::class);
|
|
|
|
|
|
|
|
|
|
$symfonyEmail->method('getHeaders')
|
|
|
|
|
->willReturn($headers);
|
|
|
|
|
|
|
|
|
|
$message = new Message($symfonyEmail, false);
|
2024-08-23 09:10:27 -04:00
|
|
|
$this->assertSame('no', $message->getAutoSubmitted());
|
2022-12-23 06:46:15 -05:00
|
|
|
}
|
2015-02-12 07:53:27 -05:00
|
|
|
}
|