]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/EMailerTest.php
spelling: forums
[friendica.git] / tests / src / Util / EMailerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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;
23
24 use Friendica\App\BaseURL;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\L10n;
27 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
28 use Friendica\Object\EMail\IEmail;
29 use Friendica\Test\MockedTest;
30 use Friendica\Test\Util\EmailerSpy;
31 use Friendica\Test\Util\HookMockTrait;
32 use Friendica\Test\Util\SampleMailBuilder;
33 use Friendica\Test\Util\VFSTrait;
34 use Mockery\MockInterface;
35 use Psr\Log\NullLogger;
36
37 /**
38  * Annotation necessary because of Hook calls
39  *
40  * @runTestsInSeparateProcesses
41  * @preserveGlobalState disabled
42  */
43 class EMailerTest extends MockedTest
44 {
45         use VFSTrait;
46         use HookMockTrait;
47
48         /** @var IManageConfigValues|MockInterface */
49         private $config;
50         /** @var IManagePersonalConfigValues|MockInterface */
51         private $pConfig;
52         /** @var L10n|MockInterface */
53         private $l10n;
54         /** @var BaseURL|MockInterface */
55         private $baseUrl;
56
57         protected function setUp(): void
58         {
59                 parent::setUp();
60
61                 $this->setUpVfsDir();
62
63                 $this->config  = \Mockery::mock(IManageConfigValues::class);
64                 $this->config->shouldReceive('get')->withArgs(['config', 'sender_email'])->andReturn('test@friendica.local')->once();
65                 $this->config->shouldReceive('get')->withArgs(['config', 'sitename', 'Friendica Social Network'])->andReturn('Friendica Social Network')->once();
66                 $this->config->shouldReceive('get')->withArgs(['system', 'sendmail_params', true])->andReturn(true);
67
68                 $this->pConfig = \Mockery::mock(IManagePersonalConfigValues::class);
69                 $this->l10n    = \Mockery::mock(L10n::class);
70                 $this->baseUrl = \Mockery::mock(BaseURL::class);
71                 $this->baseUrl->shouldReceive('getHost')->andReturn('friendica.local');
72                 $this->baseUrl->shouldReceive('__toString')->andReturn('http://friendica.local');
73         }
74
75         protected function tearDown(): void
76         {
77                 EmailerSpy::$MAIL_DATA = [];
78
79                 parent::tearDown();
80         }
81
82         public function testEmail()
83         {
84                 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
85
86                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
87
88                 $testEmail = $builder
89                         ->withRecipient('recipient@friendica.local')
90                         ->withMessage('Test Subject', "Test Message<b>Bold</b>", 'Test Text')
91                         ->withSender('Sender', 'sender@friendica.local')
92                         ->forUser(['uid' => 1])
93                         ->addHeader('Message-ID', 'first Id')
94                         ->build(true);
95
96                 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
97
98                 self::assertTrue($emailer->send($testEmail));
99
100                 self::assertStringContainsString("X-Friendica-Host: friendica.local", EmailerSpy::$MAIL_DATA['headers']);
101                 self::assertStringContainsString("X-Friendica-Platform: Friendica", EmailerSpy::$MAIL_DATA['headers']);
102                 self::assertStringContainsString("List-ID: <notification.friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
103                 self::assertStringContainsString("List-Archive: <http://friendica.local/notifications/system>", EmailerSpy::$MAIL_DATA['headers']);
104                 self::assertStringContainsString("Reply-To: Sender <sender@friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
105                 self::assertStringContainsString("MIME-Version: 1.0", EmailerSpy::$MAIL_DATA['headers']);
106                 // Base64 "Test Text"
107                 self::assertStringContainsString(chunk_split(base64_encode('Test Text')), EmailerSpy::$MAIL_DATA['body']);
108                 // Base64 "Test Message<b>Bold</b>"
109                 self::assertStringContainsString(chunk_split(base64_encode("Test Message<b>Bold</b>")), EmailerSpy::$MAIL_DATA['body']);
110                 self::assertEquals("Test Subject", EmailerSpy::$MAIL_DATA['subject']);
111                 self::assertEquals("recipient@friendica.local", EmailerSpy::$MAIL_DATA['to']);
112                 self::assertEquals("-f sender@friendica.local", EmailerSpy::$MAIL_DATA['parameters']);
113         }
114
115         public function testTwoMessageIds()
116         {
117                 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
118
119                 /** @var IEmail $preparedEmail */
120                 $preparedEmail = null;
121                 /** @var IEmail $sentEMail */
122                 $sentEMail = null;
123
124                 $this->mockHookCallAll('emailer_send_prepare', $preparedEmail);
125                 $this->mockHookCallAll('emailer_send', $sentEMail);
126
127                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
128
129                 $testEmail = $builder
130                         ->withRecipient('recipient@friendica.local')
131                         ->withMessage('Test Subject', "Test Message<b>Bold</b>", 'Test Text')
132                         ->withSender('Sender', 'sender@friendica.loca')
133                         ->forUser(['uid' => 1])
134                         ->addHeader('Message-ID', 'first Id')
135                         ->addHeader('Message-Id', 'second Id')
136                         ->build(true);
137
138                 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
139
140                 // even in case there are two message ids, send the mail anyway
141                 self::assertTrue($emailer->send($testEmail));
142
143                 // check case sensitive key problem
144                 self::assertArrayHasKey('Message-ID', $testEmail->getAdditionalMailHeader());
145                 self::assertArrayHasKey('Message-Id', $testEmail->getAdditionalMailHeader());
146         }
147 }