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()
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
97 public function testBuilderWithNonRawEmail()
99 static::markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
103 * Test if the builder can create a "simple" raw mail
105 public function testBuilderWithRawEmail()
107 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
109 $testEmail = $builder
110 ->withMessage('Subject', 'Html', 'text')
111 ->withRecipient('recipient@friendica.local')
112 ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
113 ->forUser(['uid' => 100])
116 self::assertEmail($testEmail, [
117 'subject' => 'Subject',
120 'toAddress' => 'recipient@friendica.local',
121 'fromName' => 'Sender',
122 'fromAddress' => 'sender@friendica.local',
123 'noReply' => 'no-reply@friendica.local',
125 'headers' => $this->defaultHeaders,
130 * Test if the builder throws an exception in case no recipient
133 public function testBuilderWithEmptyMail()
135 $this->expectException(InternalServerErrorException::class);
136 $this->expectExceptionMessage("Recipient address is missing.");
138 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
140 $builder->build(true);
144 * Test if the builder throws an exception in case no sender
146 public function testBuilderWithEmptySender()
148 $this->expectException(InternalServerErrorException::class);
149 $this->expectExceptionMessage("Sender address or name is missing.");
151 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
154 ->withRecipient('test@friendica.local')
159 * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
161 public function testBuilderWithoutMessage()
163 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
165 $testEmail = $builder
166 ->withRecipient('recipient@friendica.local')
167 ->withSender('Sender', 'sender@friendica.local')
170 self::assertEmail($testEmail, [
171 'toAddress' => 'recipient@friendica.local',
172 'fromName' => 'Sender',
173 'fromAddress' => 'sender@friendica.local',
174 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
175 'headers' => $this->defaultHeaders,
180 * Test if the builder sets for the text the same as for
182 public function testBuilderWithJustPreamble()
184 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
186 $testEmail = $builder
187 ->withRecipient('recipient@friendica.local')
188 ->withSender('Sender', 'sender@friendica.local')
191 self::assertEmail($testEmail, [
192 'toAddress' => 'recipient@friendica.local',
193 'fromName' => 'Sender',
194 'fromAddress' => 'sender@friendica.local',
195 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
196 'headers' => $this->defaultHeaders,