3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Util;
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;
38 * Annotation necessary because of Hook calls
40 * @runTestsInSeparateProcesses
41 * @preserveGlobalState disabled
43 class EMailerTest extends MockedTest
48 /** @var IManageConfigValues|MockInterface */
50 /** @var IManagePersonalConfigValues|MockInterface */
52 /** @var L10n|MockInterface */
54 /** @var BaseURL|MockInterface */
57 protected function setUp(): void
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);
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('getHostname')->andReturn('friendica.local');
72 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
75 protected function tearDown(): void
77 EmailerSpy::$MAIL_DATA = [];
82 public function testEmail()
84 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
86 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
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')
96 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
98 self::assertTrue($emailer->send($testEmail));
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']);
115 public function testTwoMessageIds()
117 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
119 /** @var IEmail $preparedEmail */
120 $preparedEmail = null;
121 /** @var IEmail $sentEMail */
124 $this->mockHookCallAll('emailer_send_prepare', $preparedEmail);
125 $this->mockHookCallAll('emailer_send', $sentEMail);
127 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
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')
138 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
140 // even in case there are two message ids, send the mail anyway
141 self::assertTrue($emailer->send($testEmail));
143 // check case sensitive key problem
144 self::assertArrayHasKey('Message-ID', $testEmail->getAdditionalMailHeader());
145 self::assertArrayHasKey('Message-Id', $testEmail->getAdditionalMailHeader());