just log double message IDs, don't discard the email
[friendica.git/.git] / tests / src / Util / EMailerTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util;
4
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;
17
18 /**
19  * Annotation necessary because of Hook calls
20  *
21  * @runTestsInSeparateProcesses
22  * @preserveGlobalState false
23  */
24 class EMailerTest extends MockedTest
25 {
26         use VFSTrait;
27         use HookMockTrait;
28
29         /** @var IConfig|MockInterface */
30         private $config;
31         /** @var IPConfig|MockInterface */
32         private $pConfig;
33         /** @var L10n|MockInterface */
34         private $l10n;
35         /** @var BaseURL|MockInterface */
36         private $baseUrl;
37
38         /** @var string */
39         private $defaultHeaders;
40
41         protected function setUp()
42         {
43                 parent::setUp();
44
45                 $this->setUpVfsDir();
46
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);
51
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');
57
58                 $this->defaultHeaders = [];
59         }
60
61         protected function tearDown()
62         {
63                 EmailerSpy::$MAIL_DATA = [];
64
65                 parent::tearDown();
66         }
67
68         public function testEmail()
69         {
70                 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
71
72                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
73
74                 $testEmail = $builder
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')
80                         ->build(true);
81
82                 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
83
84                 $this->assertTrue($emailer->send($testEmail));
85
86                 $this->assertContains("X-Friendica-Host : friendica.local", EmailerSpy::$MAIL_DATA['headers']);
87                 $this->assertContains("X-Friendica-Platform : Friendica", EmailerSpy::$MAIL_DATA['headers']);
88                 $this->assertContains("List-ID : <notification.friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
89                 $this->assertContains("List-Archive : <http://friendica.local/notifications/system>", EmailerSpy::$MAIL_DATA['headers']);
90                 $this->assertContains("Reply-To: Sender <sender@friendica.local>", EmailerSpy::$MAIL_DATA['headers']);
91                 $this->assertContains("MIME-Version: 1.0", EmailerSpy::$MAIL_DATA['headers']);
92                 // Base64 "Test Text"
93                 $this->assertContains(chunk_split(base64_encode('Test Text')), EmailerSpy::$MAIL_DATA['body']);
94                 // Base64 "Test Message<b>Bold</b>"
95                 $this->assertContains(chunk_split(base64_encode("Test Message<b>Bold</b>")), EmailerSpy::$MAIL_DATA['body']);
96                 $this->assertEquals("Test Subject", EmailerSpy::$MAIL_DATA['subject']);
97                 $this->assertEquals("recipient@friendica.local", EmailerSpy::$MAIL_DATA['to']);
98                 $this->assertEquals("-f sender@friendica.local", EmailerSpy::$MAIL_DATA['parameters']);
99         }
100
101         public function testTwoMessageIds()
102         {
103                 $this->pConfig->shouldReceive('get')->withArgs(['1', 'system', 'email_textonly'])->andReturn(false)->once();
104
105                 /** @var IEmail $preparedEmail */
106                 $preparedEmail = null;
107                 /** @var IEmail $sentEMail */
108                 $sentEMail = null;
109
110                 $this->mockHookCallAll('emailer_send_prepare', $preparedEmail);
111                 $this->mockHookCallAll('emailer_send', $sentEMail);
112
113                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
114
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')
122                         ->build(true);
123
124                 $emailer = new EmailerSpy($this->config, $this->pConfig, $this->baseUrl, new NullLogger(), $this->l10n);
125
126                 // even in case there are two message ids, send the mail anyway
127                 $this->assertTrue($emailer->send($testEmail));
128
129                 // check case sensitive key problem
130                 $this->assertArrayHasKey('Message-ID', $testEmail->getAdditionalMailHeader());
131                 $this->assertArrayHasKey('Message-Id', $testEmail->getAdditionalMailHeader());
132         }
133 }