Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.48% covered (success)
98.48%
519 / 527
86.96% covered (warning)
86.96%
20 / 23
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_messenger_method_email_test
98.48% covered (success)
98.48%
519 / 527
86.96% covered (warning)
86.96%
20 / 23
30
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
73 / 73
100.00% covered (success)
100.00%
1 / 1
1
 test_miscellaneous
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_set_dsn_from_config
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 test_set_dns
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 test_set_transport
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
3
 test_init
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 test_get_mailer
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_set_addresses
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 test_to
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 test_cc
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 test_bcc
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 test_reply_to
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 test_from
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 test_subject
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 test_anti_abuse_headers
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 test_set_mail_priority
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 test_process_queue_not_enabled
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 test_process_queue
100.00% covered (success)
100.00%
64 / 64
100.00% covered (success)
100.00%
1 / 1
4
 test_send_break
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
 email_template_data
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 test_send_no_queue
98.04% covered (success)
98.04%
50 / 51
0.00% covered (danger)
0.00%
0 / 1
1
 test_send_exception
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
1
 test_send_queue
96.67% covered (success)
96.67%
58 / 60
0.00% covered (danger)
0.00%
0 / 1
1
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14use phpbb\config\config;
15use phpbb\language\language;
16use phpbb\language\language_file_loader;
17use phpbb\messenger\method\email;
18use phpbb\messenger\queue;
19use phpbb\path_helper;
20use phpbb\symfony_request;
21use phpbb\template\assets_bag;
22use Symfony\Component\Mime\RawMessage;
23
24class phpbb_messenger_method_email_test extends \phpbb_test_case
25{
26    protected $assets_bag;
27    protected $cache_path;
28    protected config $config;
29    protected $dispatcher;
30    protected $extension_manager;
31    protected email $method_email;
32    protected $language;
33    protected $log;
34    protected $path_helper;
35    protected queue $queue;
36    protected $request;
37    protected $twig_extensions_collection;
38    protected $twig_lexer;
39    protected $user;
40    protected $filesystem;
41    protected $symfony_request;
42
43    public function setUp(): void
44    {
45        global $config, $request, $symfony_request, $user, $phpbb_root_path, $phpEx;
46
47        $this->assets_bag = new assets_bag();
48        $this->cache_path = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/twig';
49        $this->config = new config([
50            'force_server_vars' => false,
51        ]);
52        $config = $this->config;
53        $this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher')
54            ->disableOriginalConstructor()
55            ->getMock();
56        $this->filesystem = new \phpbb\filesystem\filesystem();
57        $this->language = new language(new language_file_loader($phpbb_root_path, $phpEx));
58        $this->queue = $this->createMock(queue::class);
59        $this->request = new phpbb_mock_request();
60        $request = $this->request;
61        $this->symfony_request = new symfony_request(new phpbb_mock_request());
62        $symfony_request = $this->symfony_request;
63        $this->user = new \phpbb\user($this->language, '\phpbb\datetime');
64        $user = $this->user;
65        $user->page['root_script_path'] = 'phpbb/';
66        $this->user->host = 'yourdomain.com';
67        $this->path_helper = new path_helper(
68            $this->symfony_request,
69            $this->request,
70            $phpbb_root_path,
71            $phpEx
72        );
73        $phpbb_container = new phpbb_mock_container_builder;
74        $this->twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container);
75        $twig = new \phpbb\template\twig\environment(
76            $this->assets_bag,
77            $this->config,
78            $this->filesystem,
79            $this->path_helper,
80            $this->cache_path,
81            null,
82            new \phpbb\template\twig\loader(''),
83            $this->dispatcher,
84            array(
85                'cache'            => false,
86                'debug'            => false,
87                'auto_reload'    => true,
88                'autoescape'    => false,
89            )
90        );
91        $this->twig_lexer = new \phpbb\template\twig\lexer($twig);
92        $this->extension_manager = new phpbb_mock_extension_manager(
93            __DIR__ . '/',
94            array(
95                'vendor2/foo' => array(
96                    'ext_name' => 'vendor2/foo',
97                    'ext_active' => '1',
98                    'ext_path' => 'ext/vendor2/foo/',
99                ),
100            )
101        );
102        $this->log = $this->createMock(\phpbb\log\log_interface::class);
103
104        $this->method_email = new email(
105            $this->assets_bag,
106            $this->config,
107            $this->dispatcher,
108            $this->language,
109            $this->queue,
110            $this->path_helper,
111            $this->request,
112            $this->twig_extensions_collection,
113            $this->twig_lexer,
114            $this->user,
115            $phpbb_root_path,
116            $this->cache_path,
117            $this->extension_manager,
118            $this->log
119        );
120    }
121
122    public function test_miscellaneous(): void
123    {
124        $this->assertEquals('email', $this->method_email->get_queue_object_name());
125        $this->assertFalse($this->method_email->is_enabled());
126        $this->config->offsetSet('email_enable', true);
127        $this->assertTrue($this->method_email->is_enabled());
128    }
129
130    public function test_set_dsn_from_config()
131    {
132        $config_values = [
133            'smtp_delivery'        => true,
134            'smtp_host'            => 'smtp.example.com',
135            'smtp_username'        => 'user',
136            'smtp_password'        => 'pass',
137            'smtp_port'            => 587,
138        ];
139        foreach ($config_values as $key => $value)
140        {
141            $this->config->set($key, $value);
142        }
143
144        $this->method_email->set_dsn();
145        $this->assertEquals('smtp://user:pass@smtp.example.com:587', $this->method_email->get_dsn());
146
147        $this->config->set('smtp_host', '');
148        $this->method_email->set_dsn();
149        $this->assertEquals('null://null', $this->method_email->get_dsn());
150    }
151
152    public function test_set_dns()
153    {
154        $this->assertEquals('', $this->method_email->get_dsn());
155        $this->method_email->set_dsn('');
156        $this->assertEquals('sendmail://default', $this->method_email->get_dsn());
157
158        $this->method_email->set_dsn('smtp://user:pass1@smtp.example.com:587');
159        $this->assertEquals('smtp://user:pass1@smtp.example.com:587', $this->method_email->get_dsn());
160    }
161
162    public function test_set_transport()
163    {
164        $this->assertEmpty($this->method_email->get_dsn());
165
166        $config_values = [
167            'smtp_delivery'                => true,
168            'smtp_host'                    => 'smtp.example.com',
169            'smtp_username'                => 'user',
170            'smtp_password'                => 'pass',
171            'smtp_port'                    => 587,
172            'smtp_verify_peer'            => true,
173            'smtp_verify_peer_name'        => true,
174            'smtp_allow_self_signed'    => false,
175        ];
176        foreach ($config_values as $key => $value)
177        {
178            $this->config->set($key, $value);
179        }
180
181        $this->method_email->set_transport();
182
183        // set_dsn() should have been called in set_transport()
184        $this->assertEquals('smtp://user:pass@smtp.example.com:587', $this->method_email->get_dsn());
185
186        $transport = $this->method_email->get_transport();
187        $this->assertInstanceOf('\Symfony\Component\Mailer\Transport\Smtp\SmtpTransport', $transport);
188        if (method_exists($transport->getStream(), 'getStreamOptions'))
189        {
190            $this->assertEquals([
191                'verify_peer'        => true,
192                'verify_peer_name'    => true,
193                'allow_self_signed'    => false,
194            ], $transport->getStream()->getStreamOptions()['ssl'] ?? null);
195        }
196    }
197
198    public function test_init()
199    {
200        $this->config->set('email_package_size', 100);
201        $email_reflection = new \ReflectionClass($this->method_email);
202        $email_property = $email_reflection->getProperty('email');
203
204        $use_queue_property = $email_reflection->getProperty('use_queue');
205        $this->assertFalse($use_queue_property->getValue($this->method_email));
206
207        $this->method_email->init();
208        /** @var \Symfony\Component\Mime\Email $email */
209        $email = $email_property->getValue($this->method_email);
210        $this->assertNotNull($email);
211
212        $this->assertTrue($use_queue_property->getValue($this->method_email));
213        $this->assertEmpty($email->getTo());
214
215        $this->method_email->to('foo@bar.com');
216        $this->assertNotEmpty($email->getTo());
217
218        $this->method_email->init();
219
220        $email = $email_property->getValue($this->method_email);
221        $this->assertNotNull($email);
222        $this->assertEmpty($email->getTo());
223    }
224
225    public function test_get_mailer()
226    {
227        $email_reflection = new \ReflectionClass($this->method_email);
228        $this->method_email->init();
229        $this->method_email->set_transport();
230        $mailer_method = $email_reflection->getMethod('get_mailer');
231
232        $mailer = $mailer_method->invoke($this->method_email);
233        $this->assertInstanceOf(\Symfony\Component\Mailer\Mailer::class, $mailer);
234    }
235
236    public function test_set_addresses()
237    {
238        $email_reflection = new \ReflectionClass($this->method_email);
239        $email_property = $email_reflection->getProperty('email');
240        $this->method_email->init();
241        /** @var \Symfony\Component\Mime\Email $email */
242        $email = $email_property->getValue($this->method_email);
243        $this->assertNotNull($email);
244
245        $this->method_email->set_addresses([]);
246        $this->assertEmpty($email->getTo());
247
248        $this->method_email->set_addresses(['user_email' => 'foo@bar.com']);
249        $this->assertNotEmpty($email->getTo());
250        $this->assertEquals('foo@bar.com', $email->getTo()[0]->getAddress());
251        $this->assertEmpty($email->getTo()[0]->getName());
252
253        $this->method_email->set_addresses(['user_email' => 'bar@foo.com', 'username' => 'Bar Foo']);
254
255        $this->assertEquals('bar@foo.com', $email->getTo()[1]->getAddress());
256        $this->assertEquals('Bar Foo', $email->getTo()[1]->getName());
257    }
258
259    public function test_to()
260    {
261        $email_reflection = new \ReflectionClass($this->method_email);
262        $email_property = $email_reflection->getProperty('email');
263        $this->method_email->init();
264        /** @var \Symfony\Component\Mime\Email $email */
265        $email = $email_property->getValue($this->method_email);
266        $this->assertNotNull($email);
267
268        // Empty address
269        $this->assertEmpty($email->getTo());
270        $this->method_email->to('');
271        $this->assertEmpty($email->getTo());
272
273        // Valid address
274        $this->method_email->to('foo@bar.com');
275        $this->assertNotEmpty($email->getTo());
276        $this->assertEquals('foo@bar.com', $email->getTo()[0]->getAddress());
277        $this->assertEmpty($email->getTo()[0]->getName());
278
279        // Valid address with name
280        $this->method_email->to('bar@foo.com', 'Bar Foo');
281        $this->assertEquals('bar@foo.com', $email->getTo()[1]->getAddress());
282        $this->assertEquals('Bar Foo', $email->getTo()[1]->getName());
283    }
284
285    public function test_cc()
286    {
287        $email_reflection = new \ReflectionClass($this->method_email);
288        $email_property = $email_reflection->getProperty('email');
289        $this->method_email->init();
290        /** @var \Symfony\Component\Mime\Email $email */
291        $email = $email_property->getValue($this->method_email);
292        $this->assertNotNull($email);
293
294        // Empty address
295        $this->assertEmpty($email->getCc());
296        $this->method_email->cc('');
297        $this->assertEmpty($email->getCc());
298
299        // Valid address
300        $this->method_email->cc('foo@bar.com');
301        $this->assertNotEmpty($email->getCc());
302        $this->assertEquals('foo@bar.com', $email->getCc()[0]->getAddress());
303        $this->assertEmpty($email->getCc()[0]->getName());
304
305        // Valid address with name
306        $this->method_email->cc('bar@foo.com', 'Bar Foo');
307        $this->assertEquals('bar@foo.com', $email->getCc()[1]->getAddress());
308        $this->assertEquals('Bar Foo', $email->getCc()[1]->getName());
309    }
310
311    public function test_bcc()
312    {
313        $email_reflection = new \ReflectionClass($this->method_email);
314        $email_property = $email_reflection->getProperty('email');
315        $this->method_email->init();
316        /** @var \Symfony\Component\Mime\Email $email */
317        $email = $email_property->getValue($this->method_email);
318        $this->assertNotNull($email);
319
320        // Empty address
321        $this->assertEmpty($email->getBcc());
322        $this->method_email->bcc('');
323        $this->assertEmpty($email->getBcc());
324
325        // Valid address
326        $this->method_email->bcc('foo@bar.com');
327        $this->assertNotEmpty($email->getBcc());
328        $this->assertEquals('foo@bar.com', $email->getBcc()[0]->getAddress());
329        $this->assertEmpty($email->getBcc()[0]->getName());
330
331        // Valid address with name
332        $this->method_email->bcc('bar@foo.com', 'Bar Foo');
333        $this->assertEquals('bar@foo.com', $email->getBcc()[1]->getAddress());
334        $this->assertEquals('Bar Foo', $email->getBcc()[1]->getName());
335    }
336
337    public function test_reply_to()
338    {
339        $email_reflection = new \ReflectionClass($this->method_email);
340        $email_property = $email_reflection->getProperty('email');
341        $this->method_email->init();
342        /** @var \Symfony\Component\Mime\Email $email */
343        $email = $email_property->getValue($this->method_email);
344        $this->assertNotNull($email);
345
346        // Empty address
347        $this->assertEmpty($email->getReplyTo());
348        $this->method_email->reply_to('');
349        $this->assertEmpty($email->getReplyTo());
350
351        // Valid address
352        $this->method_email->reply_to('foo@bar.com');
353        $this->assertNotEmpty($email->getReplyTo());
354        $this->assertEquals('foo@bar.com', $email->getReplyTo()[0]->getAddress());
355        $this->assertEmpty($email->getReplyTo()[0]->getName());
356
357        // Valid address with name
358        $this->method_email->reply_to('bar@foo.com', 'Bar Foo');
359        $this->assertEquals('bar@foo.com', $email->getReplyTo()[1]->getAddress());
360        $this->assertEquals('Bar Foo', $email->getReplyTo()[1]->getName());
361    }
362
363    public function test_from()
364    {
365        $email_reflection = new \ReflectionClass($this->method_email);
366        $email_property = $email_reflection->getProperty('email');
367        $this->method_email->init();
368        /** @var \Symfony\Component\Mime\Email $email */
369        $email = $email_property->getValue($this->method_email);
370        $this->assertNotNull($email);
371
372        // Empty address
373        $this->assertEmpty($email->getFrom());
374        $this->method_email->from('');
375        $this->assertEmpty($email->getFrom());
376
377        // Valid address
378        $this->method_email->from('foo@bar.com');
379        $this->assertNotEmpty($email->getFrom());
380        $this->assertEquals('foo@bar.com', $email->getFrom()[0]->getAddress());
381        $this->assertEmpty($email->getFrom()[0]->getName());
382
383        // Valid address with name
384        $this->method_email->from('bar@foo.com', 'Bar Foo');
385        $this->assertEquals('bar@foo.com', $email->getFrom()[1]->getAddress());
386        $this->assertEquals('Bar Foo', $email->getFrom()[1]->getName());
387    }
388
389    public function test_subject()
390    {
391        $email_reflection = new \ReflectionClass($this->method_email);
392        $email_property = $email_reflection->getProperty('email');
393        $this->method_email->init();
394        /** @var \Symfony\Component\Mime\Email $email */
395        $email = $email_property->getValue($this->method_email);
396        $this->assertNotNull($email);
397
398        // Empty subject
399        $this->assertEmpty($email->getSubject());
400        $this->method_email->subject('');
401        $this->assertEmpty($email->getSubject());
402
403        // Test subject
404        $this->method_email->subject('Test email');
405        $this->assertNotEmpty($email->getSubject());
406        $this->assertEquals('Test email', $email->getSubject());
407
408        // Overwrite subject
409        $this->method_email->subject('Reply to test email');
410        $this->assertNotEmpty($email->getSubject());
411        $this->assertEquals('Reply to test email', $email->getSubject());
412    }
413
414    public function test_anti_abuse_headers()
415    {
416        $email_reflection = new \ReflectionClass($this->method_email);
417        $headers_property = $email_reflection->getProperty('headers');
418        $this->method_email->init();
419
420        /** @var \Symfony\Component\Mime\Header\Headers $headers */
421        $headers = $headers_property->getValue($this->method_email);
422
423        $this->config->set('server_name', 'yourdomain.com');
424        $this->user->data['user_id'] = 2;
425        $this->user->data['username'] = 'admin';
426        $this->user->ip = '127.0.0.1';
427
428        $this->assertEmpty($headers->toArray());
429        $this->method_email->anti_abuse_headers($this->config, $this->user);
430
431        $this->assertEquals(
432            [
433                'X-AntiAbuse: Board servername - yourdomain.com',
434                'X-AntiAbuse: User_id - 2',
435                'X-AntiAbuse: Username - admin',
436                'X-AntiAbuse: User IP - 127.0.0.1',
437            ],
438            $headers->toArray()
439        );
440    }
441
442    public function test_set_mail_priority()
443    {
444        $email_reflection = new \ReflectionClass($this->method_email);
445        $email_property = $email_reflection->getProperty('email');
446        $this->method_email->init();
447        /** @var \Symfony\Component\Mime\Email $email */
448        $email = $email_property->getValue($this->method_email);
449        $this->assertNotNull($email);
450
451        // Default priority
452        $this->assertEquals(\Symfony\Component\Mime\Email::PRIORITY_NORMAL, $email->getPriority());
453
454        // Highest priority
455        $this->method_email->set_mail_priority(\Symfony\Component\Mime\Email::PRIORITY_HIGHEST);
456        $this->assertEquals(\Symfony\Component\Mime\Email::PRIORITY_HIGHEST, $email->getPriority());
457    }
458
459    public function test_process_queue_not_enabled()
460    {
461        $this->method_email->init();
462
463        $queue_data = [
464            'email'    => [
465                'data'    => [
466                    'message_one',
467                    'message_two',
468                ]
469            ]
470        ];
471
472        // Process queue will remove emails if email method is not enabled
473        $this->method_email->process_queue($queue_data);
474        $this->assertEmpty($queue_data);
475    }
476
477    public function test_process_queue()
478    {
479        global $phpbb_root_path;
480
481        $this->config->set('email_enable', true);
482        $this->user->data['user_id'] = 2;
483        $this->user->session_id = 'abcdef';
484
485        $this->method_email = $this->getMockBuilder($this->method_email::class)
486            ->setConstructorArgs([$this->assets_bag,
487                $this->config,
488                $this->dispatcher,
489                $this->language,
490                $this->queue,
491                $this->path_helper,
492                $this->request,
493                $this->twig_extensions_collection,
494                $this->twig_lexer,
495                $this->user,
496                $phpbb_root_path,
497                $this->cache_path,
498                $this->extension_manager,
499                $this->log
500            ])
501            ->onlyMethods(['get_mailer'])
502            ->getMock();
503
504        $mailer_mock = $this->getMockBuilder(\Symfony\Component\Mailer\MailerInterface::class)
505            ->disableOriginalConstructor()
506            ->onlyMethods(['send'])
507            ->getMock();
508        $sent_emails = 0;
509        $mailer_mock->method('send')
510            ->willReturnCallback(function(RawMessage $mail) use(&$sent_emails) {
511                if ($mail->toString() === 'throw_exception')
512                {
513                    throw new \Symfony\Component\Mailer\Exception\TransportException('exception');
514                }
515
516                $sent_emails++;
517            });
518        $this->method_email->method('get_mailer')->willReturn($mailer_mock);
519
520        $this->method_email->init();
521        $errors = [];
522        $this->log->method('add')
523            ->willReturnCallback(function($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = []) use (&$errors) {
524                $errors[] = $additional_data[0];
525            });
526
527        $this->dispatcher
528            ->expects($this->atLeastOnce())
529            ->method('trigger_event')
530            ->willReturnCallback(function($event_name, $value_array) {
531                if ($event_name === 'core.notification_message_process' && $value_array['email']->toString() == 'message_three')
532                {
533                    $value_array['break'] = true;
534                }
535
536                return $value_array;
537            });
538
539        $queue_data = [
540            'email'    => [
541                'data'    => [
542                    ['email' => new RawMessage('message_one')],
543                    ['email' => new RawMessage('message_two')],
544                    ['email' => new RawMessage('message_three')],
545                    ['email' => new RawMessage('throw_exception')],
546                    ['email' => new RawMessage('message_four')],
547                ]
548            ]
549        ];
550
551        $this->assertEmpty($errors);
552        $this->method_email->process_queue($queue_data);
553        $this->assertEmpty($queue_data);
554        $this->assertEquals(3, $sent_emails);
555
556        $this->assertEquals(['<strong>EMAIL</strong><br><em></em><br><br><br>'], $errors);
557    }
558
559    public function test_send_break()
560    {
561        $this->dispatcher
562            ->expects($this->atLeastOnce())
563            ->method('trigger_event')
564            ->willReturnCallback(function($event_name, $value_array) {
565                if ($event_name !== 'core.notification_message_email')
566                {
567                    return $value_array;
568                }
569
570                $value_array['break'] = true;
571                return $value_array;
572            });
573
574        $this->config->set('board_email', 'admin@yourdomain.com');
575
576        $this->method_email->init();
577        $this->method_email->to('foo@bar.com');
578        $this->method_email->subject('Test email');
579        $this->method_email->template('test', 'en');
580        $this->method_email->assign_block_vars('foo', ['bar' => 'baz']);
581
582        $this->method_email->send();
583    }
584
585    public static function email_template_data(): array
586    {
587        return [
588            ['test'],
589            ['admin_send_email'],
590            ['topic_notify'],
591        ];
592    }
593
594    /**
595     * @dataProvider email_template_data
596     */
597    public function test_send_no_queue($email_template)
598    {
599        global $phpbb_root_path;
600
601        $this->config->set('email_enable', true);
602        $this->user->data['user_id'] = 2;
603        $this->user->session_id = 'abcdef';
604
605        $this->method_email = $this->getMockBuilder($this->method_email::class)
606            ->setConstructorArgs([$this->assets_bag,
607                $this->config,
608                $this->dispatcher,
609                $this->language,
610                $this->queue,
611                $this->path_helper,
612                $this->request,
613                $this->twig_extensions_collection,
614                $this->twig_lexer,
615                $this->user,
616                $phpbb_root_path,
617                $this->cache_path,
618                $this->extension_manager,
619                $this->log
620            ])
621            ->onlyMethods(['get_mailer'])
622            ->getMock();
623
624        $mailer_mock = $this->getMockBuilder(\Symfony\Component\Mailer\MailerInterface::class)
625            ->disableOriginalConstructor()
626            ->onlyMethods(['send'])
627            ->getMock();
628        $sent_emails = 0;
629        $mailer_mock->method('send')
630            ->willReturnCallback(function(RawMessage $mail) use(&$sent_emails) {
631                $sent_emails++;
632            });
633        $this->method_email->method('get_mailer')->willReturn($mailer_mock);
634
635        $this->config->set('board_email', 'admin@yourdomain.com');
636
637        $this->method_email->init();
638        $errors = [];
639        $this->log->method('add')
640            ->willReturnCallback(function($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = []) use (&$errors) {
641                $errors[] = $additional_data[0];
642            });
643
644        $this->dispatcher
645            ->expects($this->atLeastOnce())
646            ->method('trigger_event')
647            ->willReturnCallback(function($event_name, $value_array) {
648                return $value_array;
649            });
650
651        $this->method_email->to('foo@bar.com');
652        $this->method_email->subject('Test email');
653        $this->method_email->template($email_template, 'en');
654
655        $this->assertTrue($this->method_email->send());
656        $this->assertEquals(1, $sent_emails);
657
658        $this->assertEmpty($errors);
659    }
660
661    public function test_send_exception()
662    {
663        global $phpbb_root_path;
664
665        $this->config->set('email_enable', true);
666        $this->user->data['user_id'] = 2;
667        $this->user->session_id = 'abcdef';
668
669        $this->method_email = $this->getMockBuilder($this->method_email::class)
670            ->setConstructorArgs([$this->assets_bag,
671                $this->config,
672                $this->dispatcher,
673                $this->language,
674                $this->queue,
675                $this->path_helper,
676                $this->request,
677                $this->twig_extensions_collection,
678                $this->twig_lexer,
679                $this->user,
680                $phpbb_root_path,
681                $this->cache_path,
682                $this->extension_manager,
683                $this->log
684            ])
685            ->onlyMethods(['get_mailer'])
686            ->getMock();
687
688        $mailer_mock = $this->getMockBuilder(\Symfony\Component\Mailer\MailerInterface::class)
689            ->disableOriginalConstructor()
690            ->onlyMethods(['send'])
691            ->getMock();
692        $mailer_mock->method('send')
693            ->willReturnCallback(function(RawMessage $mail) use(&$sent_emails) {
694                throw new \Symfony\Component\Mailer\Exception\TransportException('exception');
695            });
696        $this->method_email->method('get_mailer')->willReturn($mailer_mock);
697
698        $this->config->set('board_email', 'admin@yourdomain.com');
699
700        $this->method_email->init();
701        $errors = [];
702        $this->log->method('add')
703            ->willReturnCallback(function($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = []) use (&$errors) {
704                $errors[] = $additional_data[0];
705            });
706
707        $this->dispatcher
708            ->expects($this->atLeastOnce())
709            ->method('trigger_event')
710            ->willReturnCallback(function($event_name, $value_array) {
711                return $value_array;
712            });
713
714        $this->method_email->to('foo@bar.com');
715        $this->method_email->subject('Test email');
716        $this->method_email->template('test', 'en');
717
718        $this->assertFalse($this->method_email->send());
719
720        $this->assertEquals(['<strong>EMAIL</strong><br><em></em><br><br><br>'], $errors);
721    }
722
723    public function test_send_queue()
724    {
725        global $phpbb_root_path;
726
727        $this->config->set('email_enable', true);
728        $this->config->set('email_package_size', 100);
729        $this->user->data['user_id'] = 2;
730        $this->user->session_id = 'abcdef';
731
732        $this->method_email = $this->getMockBuilder($this->method_email::class)
733            ->setConstructorArgs([$this->assets_bag,
734                $this->config,
735                $this->dispatcher,
736                $this->language,
737                $this->queue,
738                $this->path_helper,
739                $this->request,
740                $this->twig_extensions_collection,
741                $this->twig_lexer,
742                $this->user,
743                $phpbb_root_path,
744                $this->cache_path,
745                $this->extension_manager,
746                $this->log
747            ])
748            ->onlyMethods(['get_mailer'])
749            ->getMock();
750
751        $mailer_mock = $this->getMockBuilder(\Symfony\Component\Mailer\MailerInterface::class)
752            ->disableOriginalConstructor()
753            ->onlyMethods(['send'])
754            ->getMock();
755        $mailer_mock->method('send')
756            ->willReturnCallback(function(RawMessage $mail) use(&$sent_emails) {
757                throw new \Symfony\Component\Mailer\Exception\TransportException('exception');
758            });
759        $this->method_email->method('get_mailer')->willReturn($mailer_mock);
760
761        $this->config->set('board_email', 'admin@yourdomain.com');
762
763        $this->method_email->init();
764        $errors = [];
765        $this->log->method('add')
766            ->willReturnCallback(function($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = []) use (&$errors) {
767                $errors[] = $additional_data[0];
768            });
769
770        $this->dispatcher
771            ->expects($this->atLeastOnce())
772            ->method('trigger_event')
773            ->willReturnCallback(function($event_name, $value_array) {
774                return $value_array;
775            });
776
777        // Mock queue methods
778        $this->queue->method('init')
779            ->willReturnCallback(function(string $object, int $package_size) {
780                $this->assertEquals('email', $object);
781                $this->assertEquals($this->config['email_package_size'], $package_size);
782            });
783        $this->queue->method('put')
784            ->willReturnCallback(function(string $object, array $message_data) {
785                $this->assertEquals('email', $object);
786                $this->assertStringContainsString('phpBB is correctly configured to send emails', $message_data['email']->getSubject());
787            });
788
789        $this->method_email->to('foo@bar.com');
790        $this->method_email->subject('Test email');
791        $this->method_email->template('test', 'en');
792
793        $this->assertTrue($this->method_email->send());
794
795        $this->assertEmpty($errors);
796    }
797}