]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Emailer/MailBuilderTest.php
File test
[friendica.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(): void
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          * @doesNotPerformAssertions
97          */
98         public function testBuilderWithNonRawEmail()
99         {
100                 static::markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
101         }
102
103         /**
104          * Test if the builder can create a "simple" raw mail
105          */
106         public function testBuilderWithRawEmail()
107         {
108                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
109
110                 $testEmail = $builder
111                         ->withMessage('Subject', 'Html', 'text')
112                         ->withRecipient('recipient@friendica.local')
113                         ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
114                         ->forUser(['uid' => 100])
115                         ->build(true);
116
117                 self::assertEmail($testEmail, [
118                         'subject' => 'Subject',
119                         'html' => 'Html',
120                         'text' => 'text',
121                         'toAddress' => 'recipient@friendica.local',
122                         'fromName' => 'Sender',
123                         'fromAddress' => 'sender@friendica.local',
124                         'noReply' => 'no-reply@friendica.local',
125                         'uid' => 100,
126                         'headers' => $this->defaultHeaders,
127                 ]);
128         }
129
130         /**
131          * Test if the builder throws an exception in case no recipient
132          *
133          */
134         public function testBuilderWithEmptyMail()
135         {
136                 $this->expectException(InternalServerErrorException::class);
137                 $this->expectExceptionMessage("Recipient address is missing.");
138
139                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
140
141                 $builder->build(true);
142         }
143
144         /**
145          * Test if the builder throws an exception in case no sender
146          */
147         public function testBuilderWithEmptySender()
148         {
149                 $this->expectException(InternalServerErrorException::class);
150                 $this->expectExceptionMessage("Sender address or name is missing.");
151
152                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
153
154                 $builder
155                         ->withRecipient('test@friendica.local')
156                         ->build(true);
157         }
158
159         /**
160          * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
161          */
162         public function testBuilderWithoutMessage()
163         {
164                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
165
166                 $testEmail = $builder
167                         ->withRecipient('recipient@friendica.local')
168                         ->withSender('Sender', 'sender@friendica.local')
169                         ->build(true);
170
171                 self::assertEmail($testEmail, [
172                         'toAddress' => 'recipient@friendica.local',
173                         'fromName' => 'Sender',
174                         'fromAddress' => 'sender@friendica.local',
175                         'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
176                         'headers' => $this->defaultHeaders,
177                 ]);
178         }
179
180         /**
181          * Test if the builder sets for the text the same as for
182          */
183         public function testBuilderWithJustPreamble()
184         {
185                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
186
187                 $testEmail = $builder
188                         ->withRecipient('recipient@friendica.local')
189                         ->withSender('Sender', 'sender@friendica.local')
190                         ->build(true);
191
192                 self::assertEmail($testEmail, [
193                         'toAddress' => 'recipient@friendica.local',
194                         'fromName' => 'Sender',
195                         'fromAddress' => 'sender@friendica.local',
196                         'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
197                         'headers' => $this->defaultHeaders,
198                 ]);
199         }
200 }