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 Psr\Log\NullLogger;
35 * This class tests the "MailBuilder" (@see MailBuilder )
36 * 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
38 class MailBuilderTest extends MockedTest
50 private $defaultHeaders;
52 public function setUp()
58 $this->config = \Mockery::mock(IConfig::class);
59 $this->l10n = \Mockery::mock(L10n::class);
60 $this->baseUrl = \Mockery::mock(BaseURL::class);
61 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
62 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
64 $this->defaultHeaders = "";
67 public function assertEmail(IEmail $email, array $asserts)
69 $this->assertEquals($asserts['subject'] ?? $email->getSubject(), $email->getSubject());
70 $this->assertEquals($asserts['html'] ?? $email->getMessage(), $email->getMessage());
71 $this->assertEquals($asserts['text'] ?? $email->getMessage(true), $email->getMessage(true));
72 $this->assertEquals($asserts['toAddress'] ?? $email->getToAddress(), $email->getToAddress());
73 $this->assertEquals($asserts['fromAddress'] ?? $email->getFromAddress(), $email->getFromAddress());
74 $this->assertEquals($asserts['fromName'] ?? $email->getFromName(), $email->getFromName());
75 $this->assertEquals($asserts['replyTo'] ?? $email->getReplyTo(), $email->getReplyTo());
76 $this->assertEquals($asserts['uid'] ?? $email->getRecipientUid(), $email->getRecipientUid());
77 $this->assertEquals($asserts['header'] ?? $email->getAdditionalMailHeader(), $email->getAdditionalMailHeader());
81 * Test if the builder instance can get created
83 public function testBuilderInstance()
85 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
87 $this->assertInstanceOf(MailBuilder::class, $builder);
91 * Test if the builder can create full rendered emails
93 * @todo Create test once "Renderer" and "BBCode" are dynamic
95 public function testBuilderWithNonRawEmail()
97 $this->markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
101 * Test if the builder can create a "simple" raw mail
103 public function testBuilderWithRawEmail()
105 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
107 $testEmail = $builder
108 ->withMessage('Subject', 'Html', 'text')
109 ->withRecipient('recipient@friendica.local')
110 ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
111 ->forUser(['uid' => 100])
114 $this->assertEmail($testEmail, [
115 'subject' => 'Subject',
118 'toAddress' => 'recipient@friendica.local',
119 'fromName' => 'Sender',
120 'fromAddress' => 'sender@friendica.local',
121 'noReply' => 'no-reply@friendica.local',
123 'headers' => $this->defaultHeaders,
128 * Test if the builder throws an exception in case no recipient
130 * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
131 * @expectedExceptionMessage Recipient address is missing.
133 public function testBuilderWithEmptyMail()
135 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
137 $builder->build(true);
141 * Test if the builder throws an exception in case no sender
143 * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
144 * @expectedExceptionMessage Sender address or name is missing.
146 public function testBuilderWithEmptySender()
148 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
151 ->withRecipient('test@friendica.local')
156 * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
158 public function testBuilderWithoutMessage()
160 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
162 $testEmail = $builder
163 ->withRecipient('recipient@friendica.local')
164 ->withSender('Sender', 'sender@friendica.local')
167 $this->assertEmail($testEmail, [
168 'toAddress' => 'recipient@friendica.local',
169 'fromName' => 'Sender',
170 'fromAddress' => 'sender@friendica.local',
171 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
172 'headers' => $this->defaultHeaders,
177 * Test if the builder sets for the text the same as for
179 public function testBuilderWithJustPreamble()
181 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
183 $testEmail = $builder
184 ->withRecipient('recipient@friendica.local')
185 ->withSender('Sender', 'sender@friendica.local')
188 $this->assertEmail($testEmail, [
189 'toAddress' => 'recipient@friendica.local',
190 'fromName' => 'Sender',
191 'fromAddress' => 'sender@friendica.local',
192 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
193 'headers' => $this->defaultHeaders,