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 */
39 private $defaultHeaders;
41 protected function setUp()
47 $this->config = \Mockery::mock(IConfig::class);
48 $this->config->shouldReceive('get')->withArgs(['config', 'sender_email'])->andReturn('test@friendica.local')->once();
49 $this->config->shouldReceive('get')->withArgs(['config', 'sitename', 'Friendica Social Network'])->andReturn('Friendica Social Network')->once();
50 $this->config->shouldReceive('get')->withArgs(['system', 'sendmail_params', true])->andReturn(true);
52 $this->pConfig = \Mockery::mock(IPConfig::class);
53 $this->l10n = \Mockery::mock(L10n::class);
54 $this->baseUrl = \Mockery::mock(BaseURL::class);
55 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
56 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
58 $this->defaultHeaders = [];
61 protected function tearDown()
63 EmailerSpy::$MAIL_DATA = [];
68 public function testEmail()
70 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
72 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
75 ->withRecipient('recipient@friendica.local')
76 ->withMessage('Test Subject', "Test Message<b>Bold</b>", 'Test Text')
77 ->withSender('Sender', 'sender@friendica.local')
78 ->forUser(['uid' => 1])
79 ->addHeader('Message-ID', 'first Id')
82 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
84 self::assertTrue($emailer->send($testEmail));
86 self::assertContains("X-Friendica-Host: friendica.local", EmailerSpy::$MAIL_DATA['headers']);
87 self::assertContains("X-Friendica-Platform: Friendica", EmailerSpy::$MAIL_DATA['headers']);
88 self::assertContains("List-ID: <notification.friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
89 self::assertContains("List-Archive: <http://friendica.local/notifications/system>", EmailerSpy::$MAIL_DATA['headers']);
90 self::assertContains("Reply-To: Sender <sender@friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
91 self::assertContains("MIME-Version: 1.0", EmailerSpy::$MAIL_DATA['headers']);
93 self::assertContains(chunk_split(base64_encode('Test Text')), EmailerSpy::$MAIL_DATA['body']);
94 // Base64 "Test Message<b>Bold</b>"
95 self::assertContains(chunk_split(base64_encode("Test Message<b>Bold</b>")), EmailerSpy::$MAIL_DATA['body']);
96 self::assertEquals("Test Subject", EmailerSpy::$MAIL_DATA['subject']);
97 self::assertEquals("recipient@friendica.local", EmailerSpy::$MAIL_DATA['to']);
98 self::assertEquals("-f sender@friendica.local", EmailerSpy::$MAIL_DATA['parameters']);
101 public function testTwoMessageIds()
103 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
105 /** @var IEmail $preparedEmail */
106 $preparedEmail = null;
107 /** @var IEmail $sentEMail */
110 $this->mockHookCallAll('emailer_send_prepare', $preparedEmail);
111 $this->mockHookCallAll('emailer_send', $sentEMail);
113 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
115 $testEmail = $builder
116 ->withRecipient('recipient@friendica.local')
117 ->withMessage('Test Subject', "Test Message<b>Bold</b>", 'Test Text')
118 ->withSender('Sender', 'sender@friendica.loca')
119 ->forUser(['uid' => 1])
120 ->addHeader('Message-ID', 'first Id')
121 ->addHeader('Message-Id', 'second Id')
124 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
126 // even in case there are two message ids, send the mail anyway
127 self::assertTrue($emailer->send($testEmail));
129 // check case sensitive key problem
130 self::assertArrayHasKey('Message-ID', $testEmail->getAdditionalMailHeader());
131 self::assertArrayHasKey('Message-Id', $testEmail->getAdditionalMailHeader());