Update copyright
[friendica.git/.git] / tests / src / Util / Emailer / MailBuilderTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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\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;
35
36 /**
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
39  */
40 class MailBuilderTest extends MockedTest
41 {
42         use VFSTrait;
43
44         /** @var IConfig|MockInterface */
45         private $config;
46         /** @var L10n|MockInterface */
47         private $l10n;
48         /** @var BaseURL|MockInterface */
49         private $baseUrl;
50
51         /** @var string */
52         private $defaultHeaders;
53
54         protected function setUp()
55         {
56                 parent::setUp();
57
58                 $this->setUpVfsDir();
59
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');
65
66                 $this->defaultHeaders = [];
67         }
68
69         public function assertEmail(IEmail $email, array $asserts)
70         {
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());
80         }
81
82         /**
83          * Test if the builder instance can get created
84          */
85         public function testBuilderInstance()
86         {
87                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
88
89                 self::assertInstanceOf(MailBuilder::class, $builder);
90         }
91
92         /**
93          * Test if the builder can create full rendered emails
94          *
95          * @todo Create test once "Renderer" and "BBCode" are dynamic
96          */
97         public function testBuilderWithNonRawEmail()
98         {
99                 static::markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
100         }
101
102         /**
103          * Test if the builder can create a "simple" raw mail
104          */
105         public function testBuilderWithRawEmail()
106         {
107                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
108
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])
114                         ->build(true);
115
116                 self::assertEmail($testEmail, [
117                         'subject' => 'Subject',
118                         'html' => 'Html',
119                         'text' => 'text',
120                         'toAddress' => 'recipient@friendica.local',
121                         'fromName' => 'Sender',
122                         'fromAddress' => 'sender@friendica.local',
123                         'noReply' => 'no-reply@friendica.local',
124                         'uid' => 100,
125                         'headers' => $this->defaultHeaders,
126                 ]);
127         }
128
129         /**
130          * Test if the builder throws an exception in case no recipient
131          *
132          */
133         public function testBuilderWithEmptyMail()
134         {
135                 $this->expectException(InternalServerErrorException::class);
136                 $this->expectExceptionMessage("Recipient address is missing.");
137
138                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
139
140                 $builder->build(true);
141         }
142
143         /**
144          * Test if the builder throws an exception in case no sender
145          */
146         public function testBuilderWithEmptySender()
147         {
148                 $this->expectException(InternalServerErrorException::class);
149                 $this->expectExceptionMessage("Sender address or name is missing.");
150
151                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
152
153                 $builder
154                         ->withRecipient('test@friendica.local')
155                         ->build(true);
156         }
157
158         /**
159          * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
160          */
161         public function testBuilderWithoutMessage()
162         {
163                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
164
165                 $testEmail = $builder
166                         ->withRecipient('recipient@friendica.local')
167                         ->withSender('Sender', 'sender@friendica.local')
168                         ->build(true);
169
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,
176                 ]);
177         }
178
179         /**
180          * Test if the builder sets for the text the same as for
181          */
182         public function testBuilderWithJustPreamble()
183         {
184                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
185
186                 $testEmail = $builder
187                         ->withRecipient('recipient@friendica.local')
188                         ->withSender('Sender', 'sender@friendica.local')
189                         ->build(true);
190
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,
197                 ]);
198         }
199 }