]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/Emailer/MailBuilderTest.php
Merge pull request #8237 from annando/a11y-2
[friendica.git] / tests / src / Util / Emailer / MailBuilderTest.php
1 <?php
2
3 namespace Friendica\Test\src\Util\Emailer;
4
5 use Friendica\App\BaseURL;
6 use Friendica\Core\Config\IConfig;
7 use Friendica\Core\L10n;
8 use Friendica\Object\EMail\IEmail;
9 use Friendica\Test\MockedTest;
10 use Friendica\Test\Util\SampleMailBuilder;
11 use Friendica\Test\Util\VFSTrait;
12 use Friendica\Util\EMailer\MailBuilder;
13 use Psr\Log\NullLogger;
14
15 /**
16  * This class tests the "MailBuilder" (@see MailBuilder )
17  * Since it's an abstract class and every extended class of it has dependencies, we use a "SampleMailBuilder" (@see SampleMailBuilder ) to make this class work
18  */
19 class MailBuilderTest extends MockedTest
20 {
21         use VFSTrait;
22
23         /** @var IConfig */
24         private $config;
25         /** @var L10n */
26         private $l10n;
27         /** @var BaseURL */
28         private $baseUrl;
29
30         /** @var string */
31         private $defaultHeaders;
32
33         public function setUp()
34         {
35                 parent::setUp();
36
37                 $this->setUpVfsDir();
38
39                 $this->config  = \Mockery::mock(IConfig::class);
40                 $this->l10n    = \Mockery::mock(L10n::class);
41                 $this->baseUrl = \Mockery::mock(BaseURL::class);
42                 $this->baseUrl->shouldReceive('getHostname')->andReturn('friendica.local');
43                 $this->baseUrl->shouldReceive('get')->andReturn('http://friendica.local');
44
45                 $this->defaultHeaders = "";
46         }
47
48         public function assertEmail(IEmail $email, array $asserts)
49         {
50                 $this->assertEquals($asserts['subject'] ?? $email->getSubject(), $email->getSubject());
51                 $this->assertEquals($asserts['html'] ?? $email->getMessage(), $email->getMessage());
52                 $this->assertEquals($asserts['text'] ?? $email->getMessage(true), $email->getMessage(true));
53                 $this->assertEquals($asserts['toAddress'] ?? $email->getToAddress(), $email->getToAddress());
54                 $this->assertEquals($asserts['fromAddress'] ?? $email->getFromAddress(), $email->getFromAddress());
55                 $this->assertEquals($asserts['fromName'] ?? $email->getFromName(), $email->getFromName());
56                 $this->assertEquals($asserts['replyTo'] ?? $email->getReplyTo(), $email->getReplyTo());
57                 $this->assertEquals($asserts['uid'] ?? $email->getRecipientUid(), $email->getRecipientUid());
58                 $this->assertEquals($asserts['header'] ?? $email->getAdditionalMailHeader(), $email->getAdditionalMailHeader());
59         }
60
61         /**
62          * Test if the builder instance can get created
63          */
64         public function testBuilderInstance()
65         {
66                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
67
68                 $this->assertInstanceOf(MailBuilder::class, $builder);
69         }
70
71         /**
72          * Test if the builder can create full rendered emails
73          *
74          * @todo Create test once "Renderer" and "BBCode" are dynamic
75          */
76         public function testBuilderWithNonRawEmail()
77         {
78                 $this->markTestIncomplete('Cannot easily mock Renderer and BBCode, so skipping tests wit them');
79         }
80
81         /**
82          * Test if the builder can create a "simple" raw mail
83          */
84         public function testBuilderWithRawEmail()
85         {
86                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
87
88                 $testEmail = $builder
89                         ->withMessage('Subject', 'Html', 'text')
90                         ->withRecipient('recipient@friendica.local')
91                         ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
92                         ->forUser(['uid' => 100])
93                         ->build(true);
94
95                 $this->assertEmail($testEmail, [
96                         'subject' => 'Subject',
97                         'html' => 'Html',
98                         'text' => 'text',
99                         'toAddress' => 'recipient@friendica.local',
100                         'fromName' => 'Sender',
101                         'fromAddress' => 'sender@friendica.local',
102                         'noReply' => 'no-reply@friendica.local',
103                         'uid' => 100,
104                         'headers' => $this->defaultHeaders,
105                 ]);
106         }
107
108         /**
109          * Test if the builder throws an exception in case no recipient
110          *
111          * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
112          * @expectedExceptionMessage Recipient address is missing.
113          */
114         public function testBuilderWithEmptyMail()
115         {
116                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
117
118                 $builder->build(true);
119         }
120
121         /**
122          * Test if the builder throws an exception in case no sender
123          *
124          * @expectedException \Friendica\Network\HTTPException\InternalServerErrorException
125          * @expectedExceptionMessage Sender address or name is missing.
126          */
127         public function testBuilderWithEmptySender()
128         {
129                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
130
131                 $builder
132                         ->withRecipient('test@friendica.local')
133                         ->build(true);
134         }
135
136         /**
137          * Test if the builder is capable of creating "empty" mails if needed (not the decision of the builder if so ..)
138          */
139         public function testBuilderWithoutMessage()
140         {
141                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
142
143                 $testEmail = $builder
144                         ->withRecipient('recipient@friendica.local')
145                         ->withSender('Sender', 'sender@friendica.local')
146                         ->build(true);
147
148                 $this->assertEmail($testEmail, [
149                         'toAddress' => 'recipient@friendica.local',
150                         'fromName' => 'Sender',
151                         'fromAddress' => 'sender@friendica.local',
152                         'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set
153                         'headers' => $this->defaultHeaders,
154                 ]);
155         }
156
157         /**
158          * Test if the builder sets for the text the same as for
159          */
160         public function testBuilderWithJustPreamble()
161         {
162                 $builder = new SampleMailBuilder($this->l10n, $this->baseUrl, $this->config, new NullLogger());
163
164                 $testEmail = $builder
165                         ->withRecipient('recipient@friendica.local')
166                         ->withSender('Sender', 'sender@friendica.local')
167                         ->build(true);
168
169                 $this->assertEmail($testEmail, [
170                         'toAddress' => 'recipient@friendica.local',
171                         'fromName' => 'Sender',
172                         'fromAddress' => 'sender@friendica.local',
173                         'noReply' => 'sender@friendica.local', // no-reply is set same as address in case it's not set,
174                         'headers' => $this->defaultHeaders,
175                 ]);
176         }
177 }