3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Util\Emailer;
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Config\IConfig;
26 use Friendica\Core\L10n;
27 use Friendica\Object\EMail\IEmail;
28 use Friendica\Test\MockedTest;
29 use Friendica\Test\Util\SampleMailBuilder;
30 use Friendica\Test\Util\VFSTrait;
31 use Friendica\Util\EMailer\MailBuilder;
32 use Mockery\MockInterface;
33 use Psr\Log\NullLogger;
36 * This class tests the "MailBuilder" (@see MailBuilder )
37 * Since it's an abstract class and every extended class of it has dependencies, we use a "SampleMailBuilder" (@see SampleMailBuilder ) to make this class work
39 class MailBuilderTest extends MockedTest
43 /** @var IConfig|MockInterface */
45 /** @var L10n|MockInterface */
47 /** @var BaseURL|MockInterface */
51 private $defaultHeaders;
53 public function setUp()
59 $this->config = \Mockery::mock(IConfig::class);
60 $this->l10n = \Mockery::mock(L10n::class);
61 $this->baseUrl = \Mockery::mock(BaseURL::class);
62 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
63 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
65 $this->defaultHeaders = [];
68 public function assertEmail(IEmail $email, array $asserts)
70 self::assertEquals($asserts['subject'] ?? $email->getSubject(), $email->getSubject());
71 self::assertEquals($asserts['html'] ?? $email->getMessage(), $email->getMessage());
72 self::assertEquals($asserts['text'] ?? $email->getMessage(true), $email->getMessage(true));
73 self::assertEquals($asserts['toAddress'] ?? $email->getToAddress(), $email->getToAddress());
74 self::assertEquals($asserts['fromAddress'] ?? $email->getFromAddress(), $email->getFromAddress());
75 self::assertEquals($asserts['fromName'] ?? $email->getFromName(), $email->getFromName());
76 self::assertEquals($asserts['replyTo'] ?? $email->getReplyTo(), $email->getReplyTo());
77 self::assertEquals($asserts['uid'] ?? $email->getRecipientUid(), $email->getRecipientUid());
78 self::assertEquals($asserts['header'] ?? $email->getAdditionalMailHeader(), $email->getAdditionalMailHeader());
82 * Test if the builder instance can get created
84 public function testBuilderInstance()
86 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
88 self::assertInstanceOf(MailBuilder::class, $builder);
92 * Test if the builder can create full rendered emails
94 * @todo Create test once "Renderer" and "BBCode" are dynamic
96 public function testBuilderWithNonRawEmail()
98 $this->markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
102 * Test if the builder can create a "simple" raw mail
104 public function testBuilderWithRawEmail()
106 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
108 $testEmail = $builder
109 ->withMessage('Subject', 'Html', 'text')
110 ->withRecipient('recipient@friendica.local')
111 ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
112 ->forUser(['uid' => 100])
115 self::assertEmail($testEmail, [
116 'subject' => 'Subject',
119 'toAddress' => 'recipient@friendica.local',
120 'fromName' => 'Sender',
121 'fromAddress' => 'sender@friendica.local',
122 'noReply' => 'no-reply@friendica.local',
124 'headers' => $this->defaultHeaders,
129 * Test if the builder throws an exception in case no recipient
131 * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
132 * @expectedExceptionMessage Recipient address is missing.
134 public function testBuilderWithEmptyMail()
136 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
138 $builder->build(true);
142 * Test if the builder throws an exception in case no sender
144 * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
145 * @expectedExceptionMessage Sender address or name is missing.
147 public function testBuilderWithEmptySender()
149 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
152 ->withRecipient('test@friendica.local')
157 * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
159 public function testBuilderWithoutMessage()
161 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
163 $testEmail = $builder
164 ->withRecipient('recipient@friendica.local')
165 ->withSender('Sender', 'sender@friendica.local')
168 self::assertEmail($testEmail, [
169 'toAddress' => 'recipient@friendica.local',
170 'fromName' => 'Sender',
171 'fromAddress' => 'sender@friendica.local',
172 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
173 'headers' => $this->defaultHeaders,
178 * Test if the builder sets for the text the same as for
180 public function testBuilderWithJustPreamble()
182 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
184 $testEmail = $builder
185 ->withRecipient('recipient@friendica.local')
186 ->withSender('Sender', 'sender@friendica.local')
189 self::assertEmail($testEmail, [
190 'toAddress' => 'recipient@friendica.local',
191 'fromName' => 'Sender',
192 'fromAddress' => 'sender@friendica.local',
193 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
194 'headers' => $this->defaultHeaders,