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