Transform email header string to header array & replace it at various situations.
[friendica.git/.git] / tests / src / Util / Emailer / MailBuilderTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util\Emailer;
23
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;
33
34 /**
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
37  */
38 class MailBuilderTest extends MockedTest
39 {
40         use VFSTrait;
41
42         /** @var IConfig */
43         private $config;
44         /** @var L10n */
45         private $l10n;
46         /** @var BaseURL */
47         private $baseUrl;
48
49         /** @var string */
50         private $defaultHeaders;
51
52         public function setUp()
53         {
54                 parent::setUp();
55
56                 $this->setUpVfsDir();
57
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');
63
64                 $this->defaultHeaders = [];
65         }
66
67         public function assertEmail(IEmail $email, array $asserts)
68         {
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());
78         }
79
80         /**
81          * Test if the builder instance can get created
82          */
83         public function testBuilderInstance()
84         {
85                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
86
87                 $this->assertInstanceOf(MailBuilder::class, $builder);
88         }
89
90         /**
91          * Test if the builder can create full rendered emails
92          *
93          * @todo Create test once "Renderer" and "BBCode" are dynamic
94          */
95         public function testBuilderWithNonRawEmail()
96         {
97                 $this->markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
98         }
99
100         /**
101          * Test if the builder can create a "simple" raw mail
102          */
103         public function testBuilderWithRawEmail()
104         {
105                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
106
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])
112                         ->build(true);
113
114                 $this->assertEmail($testEmail, [
115                         'subject' => 'Subject',
116                         'html' => 'Html',
117                         'text' => 'text',
118                         'toAddress' => 'recipient@friendica.local',
119                         'fromName' => 'Sender',
120                         'fromAddress' => 'sender@friendica.local',
121                         'noReply' => 'no-reply@friendica.local',
122                         'uid' => 100,
123                         'headers' => $this->defaultHeaders,
124                 ]);
125         }
126
127         /**
128          * Test if the builder throws an exception in case no recipient
129          *
130          * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
131          * @expectedExceptionMessage Recipient address is missing.
132          */
133         public function testBuilderWithEmptyMail()
134         {
135                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
136
137                 $builder->build(true);
138         }
139
140         /**
141          * Test if the builder throws an exception in case no sender
142          *
143          * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
144          * @expectedExceptionMessage Sender address or name is missing.
145          */
146         public function testBuilderWithEmptySender()
147         {
148                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
149
150                 $builder
151                         ->withRecipient('test@friendica.local')
152                         ->build(true);
153         }
154
155         /**
156          * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
157          */
158         public function testBuilderWithoutMessage()
159         {
160                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
161
162                 $testEmail = $builder
163                         ->withRecipient('recipient@friendica.local')
164                         ->withSender('Sender', 'sender@friendica.local')
165                         ->build(true);
166
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,
173                 ]);
174         }
175
176         /**
177          * Test if the builder sets for the text the same as for
178          */
179         public function testBuilderWithJustPreamble()
180         {
181                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
182
183                 $testEmail = $builder
184                         ->withRecipient('recipient@friendica.local')
185                         ->withSender('Sender', 'sender@friendica.local')
186                         ->build(true);
187
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,
194                 ]);
195         }
196 }