xref: /webtrees/tests/app/Services/EmailServiceTest.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Contracts\UserInterface;
23use Fisharebest\Webtrees\Site;
24use Fisharebest\Webtrees\TestCase;
25
26use PHPUnit\Framework\Attributes\CoversClass;
27
28use PHPUnit\Framework\Attributes\DataProvider;
29
30use function function_exists;
31
32#[CoversClass(EmailService::class)]
33class EmailServiceTest extends TestCase
34{
35    protected static bool $uses_database = true;
36
37    public function testSend(): void
38    {
39        $email_service = new EmailService();
40
41        $user_from = $this->createMock(UserInterface::class);
42        $user_from->method('email')->willReturn('user.from@example.com');
43
44        $user_from = $this->createMock(UserInterface::class);
45        $user_from->method('email')->willReturn('user.from@example.com');
46
47        $user_to = $this->createMock(UserInterface::class);
48        $user_to->method('email')->willReturn('user.to@example.com');
49
50        $user_reply_to = $this->createMock(UserInterface::class);
51        $user_reply_to->method('email')->willReturn('user.replyto@example.com');
52
53        Site::setPreference('SMTP_ACTIVE', 'internal');
54
55        self::assertTrue($email_service->send($user_from, $user_to, $user_reply_to, 'Test No DKIM', 'Test Plain Message', '<p>Test Html Message</p>'));
56
57        Site::setPreference('DKIM_DOMAIN', 'example.com');
58        Site::setPreference('DKIM_SELECTOR', 'sel');
59        Site::setPreference('DKIM_KEY', '-----BEGIN RSA PRIVATE KEY-----
60MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
61wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
621s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
633tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
64pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
65GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
66AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
67L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
68X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
69U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
7037sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
71-----END RSA PRIVATE KEY-----');
72
73        self::assertTrue($email_service->send($user_from, $user_to, $user_reply_to, 'Test DKIM', 'Test Plain Message', '<p>Test Html Message</p>'));
74    }
75
76    /**
77     * Data provider for testing email validity
78     *
79     * @return array<array<bool|string>>
80     */
81    public static function emailProvider(): array
82    {
83        return [
84            // Valid emails
85            ['Abc@webtrees.com', true],
86            ['ABC@webtrees.com', true],
87            ['Abc.123@webtrees.com', true],
88            ['user+mailbox/tree=family@webtrees.com', true],
89            ['!#$%&\'*+-/=?^_`.{|}~@webtrees.com', true],
90            ['"Abc@def"@webtrees.com', true],
91            ['"John\ Doe"@webtrees.com', true],
92            ['"Joe.\\Smith"@webtrees.com', true],
93            ['généalogie@webtrees.com', true],
94            // Invalid
95            ['example@invalid.example.com', false],
96            ['example', false],
97            ['example@with space', false],
98            ['example@webtrees.', false],
99            ['example@webtr\ees.com', false],
100            ['example(comment)@example.com', false],
101            ["\x80\x81\x82@\x83\x84\x85.\x86\x87\x88", false],
102            ['user  name@example.com', false],
103            ['example.@example.com', false],
104            ['example(example]example@example.co.uk', false],
105            ['a@b.c+&%$.d', false],
106            ['a.b+&%$.c@d', false],
107            ['example@généalogie', false]
108        ];
109    }
110
111    #[DataProvider('emailProvider')]
112    public function testIsValidEmail(string $email, bool $is_valid): void
113    {
114        self::assertSame($is_valid, (new EmailService())->isValidEmail($email));
115    }
116
117    public function testMailSslOptions(): void
118    {
119        $options = (new EmailService())->mailSslOptions();
120        self::assertCount(3, $options);
121        self::assertArrayHasKey('ssl', $options);
122    }
123
124    public function testMailTransportOptions(): void
125    {
126        $options = (new EmailService())->mailTransportOptions();
127        self::assertCount(function_exists('proc_open') ? 2 : 1, $options);
128        self::assertArrayHasKey('external', $options);
129    }
130}
131