3 namespace Friendica\Test\src\Util\Emailer;
5 use Friendica\App\BaseURL;
6 use Friendica\Core\Config\IConfig;
7 use Friendica\Core\L10n;
8 use Friendica\Object\EMail\IEmail;
9 use Friendica\Test\MockedTest;
10 use Friendica\Test\Util\SampleMailBuilder;
11 use Friendica\Test\Util\VFSTrait;
12 use Friendica\Util\EMailer\MailBuilder;
13 use Psr\Log\NullLogger;
16 * This class tests the "MailBuilder" (@see MailBuilder )
17 * 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
19 class MailBuilderTest extends MockedTest
31 private $defaultHeaders;
33 public function setUp()
39 $this->config = \Mockery::mock(IConfig::class);
40 $this->l10n = \Mockery::mock(L10n::class);
41 $this->baseUrl = \Mockery::mock(BaseURL::class);
42 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
43 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
45 $this->defaultHeaders = "";
48 public function assertEmail(IEmail $email, array $asserts)
50 $this->assertEquals($asserts['subject'] ?? $email->getSubject(), $email->getSubject());
51 $this->assertEquals($asserts['html'] ?? $email->getMessage(), $email->getMessage());
52 $this->assertEquals($asserts['text'] ?? $email->getMessage(true), $email->getMessage(true));
53 $this->assertEquals($asserts['toAddress'] ?? $email->getToAddress(), $email->getToAddress());
54 $this->assertEquals($asserts['fromAddress'] ?? $email->getFromAddress(), $email->getFromAddress());
55 $this->assertEquals($asserts['fromName'] ?? $email->getFromName(), $email->getFromName());
56 $this->assertEquals($asserts['replyTo'] ?? $email->getReplyTo(), $email->getReplyTo());
57 $this->assertEquals($asserts['uid'] ?? $email->getRecipientUid(), $email->getRecipientUid());
58 $this->assertEquals($asserts['header'] ?? $email->getAdditionalMailHeader(), $email->getAdditionalMailHeader());
62 * Test if the builder instance can get created
64 public function testBuilderInstance()
66 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
68 $this->assertInstanceOf(MailBuilder::class, $builder);
72 * Test if the builder can create full rendered emails
74 * @todo Create test once "Renderer" and "BBCode" are dynamic
76 public function testBuilderWithNonRawEmail()
78 $this->markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
82 * Test if the builder can create a "simple" raw mail
84 public function testBuilderWithRawEmail()
86 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
89 ->withMessage('Subject', 'Html', 'text')
90 ->withRecipient('recipient@friendica.local')
91 ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
92 ->forUser(['uid' => 100])
95 $this->assertEmail($testEmail, [
96 'subject' => 'Subject',
99 'toAddress' => 'recipient@friendica.local',
100 'fromName' => 'Sender',
101 'fromAddress' => 'sender@friendica.local',
102 'noReply' => 'no-reply@friendica.local',
104 'headers' => $this->defaultHeaders,
109 * Test if the builder throws an exception in case no recipient
111 * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
112 * @expectedExceptionMessage Recipient address is missing.
114 public function testBuilderWithEmptyMail()
116 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
118 $builder->build(true);
122 * Test if the builder throws an exception in case no sender
124 * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
125 * @expectedExceptionMessage Sender address or name is missing.
127 public function testBuilderWithEmptySender()
129 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
132 ->withRecipient('test@friendica.local')
137 * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
139 public function testBuilderWithoutMessage()
141 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
143 $testEmail = $builder
144 ->withRecipient('recipient@friendica.local')
145 ->withSender('Sender', 'sender@friendica.local')
148 $this->assertEmail($testEmail, [
149 'toAddress' => 'recipient@friendica.local',
150 'fromName' => 'Sender',
151 'fromAddress' => 'sender@friendica.local',
152 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
153 'headers' => $this->defaultHeaders,
158 * Test if the builder sets for the text the same as for
160 public function testBuilderWithJustPreamble()
162 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
164 $testEmail = $builder
165 ->withRecipient('recipient@friendica.local')
166 ->withSender('Sender', 'sender@friendica.local')
169 $this->assertEmail($testEmail, [
170 'toAddress' => 'recipient@friendica.local',
171 'fromName' => 'Sender',
172 'fromAddress' => 'sender@friendica.local',
173 'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
174 'headers' => $this->defaultHeaders,