3 namespace Friendica\Test\src\Util;
5 use Friendica\App\BaseURL;
6 use Friendica\Core\Config\IConfig;
7 use Friendica\Core\L10n;
8 use Friendica\Core\PConfig\IPConfig;
9 use Friendica\Object\EMail\IEmail;
10 use Friendica\Test\MockedTest;
11 use Friendica\Test\Util\EmailerSpy;
12 use Friendica\Test\Util\HookMockTrait;
13 use Friendica\Test\Util\SampleMailBuilder;
14 use Friendica\Test\Util\VFSTrait;
15 use Mockery\MockInterface;
16 use Psr\Log\NullLogger;
19 * Annotation necessary because of Hook calls
21 * @runTestsInSeparateProcesses
22 * @preserveGlobalState disabled
24 class EMailerTest extends MockedTest
29 /** @var IConfig|MockInterface */
31 /** @var IPConfig|MockInterface */
33 /** @var L10n|MockInterface */
35 /** @var BaseURL|MockInterface */
38 protected function setUp()
44 $this->config = \Mockery::mock(IConfig::class);
45 $this->config->shouldReceive('get')->withArgs(['config', 'sender_email'])->andReturn('test@friendica.local')->once();
46 $this->config->shouldReceive('get')->withArgs(['config', 'sitename', 'Friendica Social Network'])->andReturn('Friendica Social Network')->once();
47 $this->config->shouldReceive('get')->withArgs(['system', 'sendmail_params', true])->andReturn(true);
49 $this->pConfig = \Mockery::mock(IPConfig::class);
50 $this->l10n = \Mockery::mock(L10n::class);
51 $this->baseUrl = \Mockery::mock(BaseURL::class);
52 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
53 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
56 protected function tearDown()
58 EmailerSpy::$MAIL_DATA = [];
63 public function testEmail()
65 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
67 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
70 ->withRecipient('recipient@friendica.local')
71 ->withMessage('Test Subject', "Test Message<b>Bold</b>", 'Test Text')
72 ->withSender('Sender', 'sender@friendica.local')
73 ->forUser(['uid' => 1])
74 ->addHeader('Message-ID', 'first Id')
77 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
79 self::assertTrue($emailer->send($testEmail));
81 self::assertContains("X-Friendica-Host: friendica.local", EmailerSpy::$MAIL_DATA['headers']);
82 self::assertContains("X-Friendica-Platform: Friendica", EmailerSpy::$MAIL_DATA['headers']);
83 self::assertContains("List-ID: <notification.friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
84 self::assertContains("List-Archive: <http://friendica.local/notifications/system>", EmailerSpy::$MAIL_DATA['headers']);
85 self::assertContains("Reply-To: Sender <sender@friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
86 self::assertContains("MIME-Version: 1.0", EmailerSpy::$MAIL_DATA['headers']);
88 self::assertContains(chunk_split(base64_encode('Test Text')), EmailerSpy::$MAIL_DATA['body']);
89 // Base64 "Test Message<b>Bold</b>"
90 self::assertContains(chunk_split(base64_encode("Test Message<b>Bold</b>")), EmailerSpy::$MAIL_DATA['body']);
91 self::assertEquals("Test Subject", EmailerSpy::$MAIL_DATA['subject']);
92 self::assertEquals("recipient@friendica.local", EmailerSpy::$MAIL_DATA['to']);
93 self::assertEquals("-f sender@friendica.local", EmailerSpy::$MAIL_DATA['parameters']);
96 public function testTwoMessageIds()
98 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
100 /** @var IEmail $preparedEmail */
101 $preparedEmail = null;
102 /** @var IEmail $sentEMail */
105 $this->mockHookCallAll('emailer_send_prepare', $preparedEmail);
106 $this->mockHookCallAll('emailer_send', $sentEMail);
108 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
110 $testEmail = $builder
111 ->withRecipient('recipient@friendica.local')
112 ->withMessage('Test Subject', "Test Message<b>Bold</b>", 'Test Text')
113 ->withSender('Sender', 'sender@friendica.loca')
114 ->forUser(['uid' => 1])
115 ->addHeader('Message-ID', 'first Id')
116 ->addHeader('Message-Id', 'second Id')
119 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
121 // even in case there are two message ids, send the mail anyway
122 self::assertTrue($emailer->send($testEmail));
124 // check case sensitive key problem
125 self::assertArrayHasKey('Message-ID', $testEmail->getAdditionalMailHeader());
126 self::assertArrayHasKey('Message-Id', $testEmail->getAdditionalMailHeader());