]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Emailer/MailBuilderTest.php
Fix wrong `$this->assert...()` with `self::assert...()
[friendica.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 Mockery\MockInterface;
33 use Psr\Log\NullLogger;
34
35 /**
36  * This class tests the "MailBuilder" (@see MailBuilder )
37  * 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  */
39 class MailBuilderTest extends MockedTest
40 {
41         use VFSTrait;
42
43         /** @var IConfig|MockInterface */
44         private $config;
45         /** @var L10n|MockInterface */
46         private $l10n;
47         /** @var BaseURL|MockInterface */
48         private $baseUrl;
49
50         /** @var string */
51         private $defaultHeaders;
52
53         public function setUp()
54         {
55                 parent::setUp();
56
57                 $this->setUpVfsDir();
58
59                 $this->config  = \Mockery::mock(IConfig::class);
60                 $this->l10n    = \Mockery::mock(L10n::class);
61                 $this->baseUrl = \Mockery::mock(BaseURL::class);
62                 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
63                 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
64
65                 $this->defaultHeaders = [];
66         }
67
68         public function assertEmail(IEmail $email, array $asserts)
69         {
70                 self::assertEquals($asserts['subject'] ?? $email->getSubject(), $email->getSubject());
71                 self::assertEquals($asserts['html'] ?? $email->getMessage(), $email->getMessage());
72                 self::assertEquals($asserts['text'] ?? $email->getMessage(true), $email->getMessage(true));
73                 self::assertEquals($asserts['toAddress'] ?? $email->getToAddress(), $email->getToAddress());
74                 self::assertEquals($asserts['fromAddress'] ?? $email->getFromAddress(), $email->getFromAddress());
75                 self::assertEquals($asserts['fromName'] ?? $email->getFromName(), $email->getFromName());
76                 self::assertEquals($asserts['replyTo'] ?? $email->getReplyTo(), $email->getReplyTo());
77                 self::assertEquals($asserts['uid'] ?? $email->getRecipientUid(), $email->getRecipientUid());
78                 self::assertEquals($asserts['header'] ?? $email->getAdditionalMailHeader(), $email->getAdditionalMailHeader());
79         }
80
81         /**
82          * Test if the builder instance can get created
83          */
84         public function testBuilderInstance()
85         {
86                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
87
88                 self::assertInstanceOf(MailBuilder::class, $builder);
89         }
90
91         /**
92          * Test if the builder can create full rendered emails
93          *
94          * @todo Create test once "Renderer" and "BBCode" are dynamic
95          */
96         public function testBuilderWithNonRawEmail()
97         {
98                 $this->markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
99         }
100
101         /**
102          * Test if the builder can create a "simple" raw mail
103          */
104         public function testBuilderWithRawEmail()
105         {
106                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
107
108                 $testEmail = $builder
109                         ->withMessage('Subject', 'Html', 'text')
110                         ->withRecipient('recipient@friendica.local')
111                         ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
112                         ->forUser(['uid' => 100])
113                         ->build(true);
114
115                 self::assertEmail($testEmail, [
116                         'subject' => 'Subject',
117                         'html' => 'Html',
118                         'text' => 'text',
119                         'toAddress' => 'recipient@friendica.local',
120                         'fromName' => 'Sender',
121                         'fromAddress' => 'sender@friendica.local',
122                         'noReply' => 'no-reply@friendica.local',
123                         'uid' => 100,
124                         'headers' => $this->defaultHeaders,
125                 ]);
126         }
127
128         /**
129          * Test if the builder throws an exception in case no recipient
130          *
131          * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
132          * @expectedExceptionMessage Recipient address is missing.
133          */
134         public function testBuilderWithEmptyMail()
135         {
136                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
137
138                 $builder->build(true);
139         }
140
141         /**
142          * Test if the builder throws an exception in case no sender
143          *
144          * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
145          * @expectedExceptionMessage Sender address or name is missing.
146          */
147         public function testBuilderWithEmptySender()
148         {
149                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
150
151                 $builder
152                         ->withRecipient('test@friendica.local')
153                         ->build(true);
154         }
155
156         /**
157          * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
158          */
159         public function testBuilderWithoutMessage()
160         {
161                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
162
163                 $testEmail = $builder
164                         ->withRecipient('recipient@friendica.local')
165                         ->withSender('Sender', 'sender@friendica.local')
166                         ->build(true);
167
168                 self::assertEmail($testEmail, [
169                         'toAddress' => 'recipient@friendica.local',
170                         'fromName' => 'Sender',
171                         'fromAddress' => 'sender@friendica.local',
172                         'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
173                         'headers' => $this->defaultHeaders,
174                 ]);
175         }
176
177         /**
178          * Test if the builder sets for the text the same as for
179          */
180         public function testBuilderWithJustPreamble()
181         {
182                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
183
184                 $testEmail = $builder
185                         ->withRecipient('recipient@friendica.local')
186                         ->withSender('Sender', 'sender@friendica.local')
187                         ->build(true);
188
189                 self::assertEmail($testEmail, [
190                         'toAddress' => 'recipient@friendica.local',
191                         'fromName' => 'Sender',
192                         'fromAddress' => 'sender@friendica.local',
193                         'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
194                         'headers' => $this->defaultHeaders,
195                 ]);
196         }
197 }