xref: /webtrees/tests/app/Services/EmailServiceTest.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\Site;
23use Fisharebest\Webtrees\TestCase;
24use Fisharebest\Webtrees\Contracts\UserInterface;
25
26/**
27 * Test harness for the class EmailService
28 *
29 * @covers \Fisharebest\Webtrees\Services\EmailService
30 */
31class EmailServiceTest extends TestCase
32{
33    protected static bool $uses_database = true;
34
35    /**
36     * @covers \Fisharebest\Webtrees\Services\EmailService::send
37     * @covers \Fisharebest\Webtrees\Services\EmailService::message
38     * @covers \Fisharebest\Webtrees\Services\EmailService::transport
39     */
40    public function testSend(): void
41    {
42        $email_service = new EmailService();
43
44        $user_from = $this->createMock(UserInterface::class);
45        $user_from->method('email')->willReturn('user.from@example.com');
46
47        $user_from = $this->createMock(UserInterface::class);
48        $user_from->method('email')->willReturn('user.from@example.com');
49
50        $user_to = $this->createMock(UserInterface::class);
51        $user_to->method('email')->willReturn('user.to@example.com');
52
53        $user_reply_to = $this->createMock(UserInterface::class);
54        $user_reply_to->method('email')->willReturn('user.replyto@example.com');
55
56        Site::setPreference('SMTP_ACTIVE', 'internal');
57
58        self::assertTrue($email_service->send($user_from, $user_to, $user_reply_to, 'Test No DKIM', 'Test Plain Message', '<p>Test Html Message</p>'));
59
60        Site::setPreference('DKIM_DOMAIN', 'example.com');
61        Site::setPreference('DKIM_SELECTOR', 'sel');
62        Site::setPreference('DKIM_KEY', '-----BEGIN RSA PRIVATE KEY-----
63MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
64wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
651s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
663tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
67pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
68GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
69AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
70L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
71X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
72U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
7337sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
74-----END RSA PRIVATE KEY-----');
75
76        self::assertTrue($email_service->send($user_from, $user_to, $user_reply_to, 'Test DKIM', 'Test Plain Message', '<p>Test Html Message</p>'));
77    }
78
79    /**
80     * Data provider for testing email validity
81     *
82     * @return array<array<bool|string>>
83     */
84    public function emailProvider(): array
85    {
86        return [
87            // Valid emails
88            ['Abc@webtrees.com', true],
89            ['ABC@webtrees.com', true],
90            ['Abc.123@webtrees.com', true],
91            ['user+mailbox/tree=family@webtrees.com', true],
92            ['!#$%&\'*+-/=?^_`.{|}~@webtrees.com', true],
93            ['"Abc@def"@webtrees.com', true],
94            ['"John\ Doe"@webtrees.com', true],
95            ['"Joe.\\Smith"@webtrees.com', true],
96            ['généalogie@webtrees.com', true],
97            // Invalid
98            ['example@invalid.example.com', false],
99            ['example', false],
100            ['example@with space', false],
101            ['example@webtrees.', false],
102            ['example@webtr\ees.com', false],
103            ['example(comment)@example.com', false],
104            ["\x80\x81\x82@\x83\x84\x85.\x86\x87\x88", false],
105            ['user  name@example.com', false],
106            ['example.@example.com', false],
107            ['example(example]example@example.co.uk', false],
108            ['a@b.c+&%$.d', false],
109            ['a.b+&%$.c@d', false],
110            ['example@généalogie', false]
111        ];
112    }
113
114    /**
115     * @dataProvider emailProvider
116     *
117     * @covers \Fisharebest\Webtrees\Services\EmailService::isValidEmail
118     *
119     * @param string $email
120     * @param bool $is_valid
121     */
122    public function testIsValidEmail(string $email, bool $is_valid): void
123    {
124        self::assertSame($is_valid, (new EmailService())->isValidEmail($email));
125    }
126
127    /**
128     * @covers \Fisharebest\Webtrees\Services\EmailService::mailSslOptions
129     */
130    public function testMailSslOptions(): void
131    {
132        $options = (new EmailService())->mailSslOptions();
133        self::assertCount(3, $options);
134        self::assertArrayHasKey('ssl', $options);
135    }
136
137    /**
138     * @covers \Fisharebest\Webtrees\Services\EmailService::mailTransportOptions
139     */
140    public function testMailTransportOptions(): void
141    {
142        $options = (new EmailService())->mailTransportOptions();
143        self::assertCount(function_exists('proc_open') ? 2 : 1, $options);
144        self::assertArrayHasKey('external', $options);
145    }
146}
147