3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Network\HTTPException\InternalServerErrorException;
28 use Friendica\Object\EMail\IEmail;
29 use Friendica\Test\MockedTest;
30 use Friendica\Test\Util\SampleMailBuilder;
31 use Friendica\Test\Util\VFSTrait;
32 use Friendica\Util\EMailer\MailBuilder;
33 use Mockery\MockInterface;
34 use Psr\Log\NullLogger;
37 * This class tests the "MailBuilder" (@see MailBuilder )
38 * 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
40 class MailBuilderTest extends MockedTest
44 /** @var IConfig|MockInterface */
46 /** @var L10n|MockInterface */
48 /** @var BaseURL|MockInterface */
52 private $defaultHeaders;
54 protected function setUp(): void
60 $this->config = \Mockery::mock(IConfig::class);
61 $this->l10n = \Mockery::mock(L10n::class);
62 $this->baseUrl = \Mockery::mock(BaseURL::class);
63 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
64 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
66 $this->defaultHeaders = [];
69 public function assertEmail(IEmail $email, array $asserts)
71 self::assertEquals($asserts['subject'] ?? $email->getSubject(), $email->getSubject());
72 self::assertEquals($asserts['html'] ?? $email->getMessage(), $email->getMessage());
73 self::assertEquals($asserts['text'] ?? $email->getMessage(true), $email->getMessage(true));
74 self::assertEquals($asserts['toAddress'] ?? $email->getToAddress(), $email->getToAddress());
75 self::assertEquals($asserts['fromAddress'] ?? $email->getFromAddress(), $email->getFromAddress());
76 self::assertEquals($asserts['fromName'] ?? $email->getFromName(), $email->getFromName());
77 self::assertEquals($asserts['replyTo'] ?? $email->getReplyTo(), $email->getReplyTo());
78 self::assertEquals($asserts['uid'] ?? $email->getRecipientUid(), $email->getRecipientUid());
79 self::assertEquals($asserts['header'] ?? $email->getAdditionalMailHeader(), $email->getAdditionalMailHeader());
83 * Test if the builder instance can get created
85 public function testBuilderInstance()
87 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
89 self::assertInstanceOf(MailBuilder::class, $builder);
93 * Test if the builder can create full rendered emails
95 * @todo Create test once "Renderer" and "BBCode" are dynamic
96 * @doesNotPerformAssertions
98 public function testBuilderWithNonRawEmail()
100 static::markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
104 * Test if the builder can create a "simple" raw mail
106 public function testBuilderWithRawEmail()
108 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
110 $testEmail = $builder
111 ->withMessage('Subject', 'Html', 'text')
112 ->withRecipient('recipient@friendica.local')
113 ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
114 ->forUser(['uid' => 100])
117 self::assertEmail($testEmail, [
118 'subject' => 'Subject',
121 'toAddress' => 'recipient@friendica.local',
122 'fromName' => 'Sender',
123 'fromAddress' => 'sender@friendica.local',
124 'noReply' => 'no-reply@friendica.local',
126 'headers' => $this->defaultHeaders,
131 * Test if the builder throws an exception in case no recipient
134 public function testBuilderWithEmptyMail()
136 $this->expectException(InternalServerErrorException::class);
137 $this->expectExceptionMessage("Recipient address is missing.");
139 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
141 $builder->build(true);
145 * Test if the builder throws an exception in case no sender
147 public function testBuilderWithEmptySender()
149 $this->expectException(InternalServerErrorException::class);
150 $this->expectExceptionMessage("Sender address or name is missing.");
152 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
155 ->withRecipient('test@friendica.local')
160 * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
162 public function testBuilderWithoutMessage()
164 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
166 $testEmail = $builder
167 ->withRecipient('recipient@friendica.local')
168 ->withSender('Sender', 'sender@friendica.local')
171 self::assertEmail($testEmail, [
172 'toAddress' => 'recipient@friendica.local',
173 'fromName' => 'Sender',
174 'fromAddress' => 'sender@friendica.local',
175 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
176 'headers' => $this->defaultHeaders,
181 * Test if the builder sets for the text the same as for
183 public function testBuilderWithJustPreamble()
185 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
187 $testEmail = $builder
188 ->withRecipient('recipient@friendica.local')
189 ->withSender('Sender', 'sender@friendica.local')
192 self::assertEmail($testEmail, [
193 'toAddress' => 'recipient@friendica.local',
194 'fromName' => 'Sender',
195 'fromAddress' => 'sender@friendica.local',
196 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
197 'headers' => $this->defaultHeaders,