summaryrefslogtreecommitdiff
path: root/tests/app/Services/EmailServiceTest.php
blob: 0130b60b838d055958544c467105a451af6d546d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2025 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Services;

use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

use function function_exists;

#[CoversClass(EmailService::class)]
class EmailServiceTest extends TestCase
{
    protected static bool $uses_database = true;

    public function testSend(): void
    {
        $email_service = new EmailService();

        $user_from = $this->createMock(UserInterface::class);
        $user_from->method('email')->willReturn('user.from@example.com');

        $user_from = $this->createMock(UserInterface::class);
        $user_from->method('email')->willReturn('user.from@example.com');

        $user_to = $this->createMock(UserInterface::class);
        $user_to->method('email')->willReturn('user.to@example.com');

        $user_reply_to = $this->createMock(UserInterface::class);
        $user_reply_to->method('email')->willReturn('user.replyto@example.com');

        Site::setPreference('SMTP_ACTIVE', 'internal');

        self::assertTrue($email_service->send($user_from, $user_to, $user_reply_to, 'Test No DKIM', 'Test Plain Message', '<p>Test Html Message</p>'));

        Site::setPreference('DKIM_DOMAIN', 'example.com');
        Site::setPreference('DKIM_SELECTOR', 'sel');
        Site::setPreference('DKIM_KEY', '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
-----END RSA PRIVATE KEY-----');

        self::assertTrue($email_service->send($user_from, $user_to, $user_reply_to, 'Test DKIM', 'Test Plain Message', '<p>Test Html Message</p>'));
    }

    /**
     * Data provider for testing email validity
     *
     * @return array<array<bool|string>>
     */
    public static function emailProvider(): array
    {
        return [
            // Valid emails
            ['Abc@webtrees.com', true],
            ['ABC@webtrees.com', true],
            ['Abc.123@webtrees.com', true],
            ['user+mailbox/tree=family@webtrees.com', true],
            ['!#$%&\'*+-/=?^_`.{|}~@webtrees.com', true],
            ['"Abc@def"@webtrees.com', true],
            ['"John\ Doe"@webtrees.com', true],
            ['"Joe.\\Smith"@webtrees.com', true],
            ['généalogie@webtrees.com', true],
            // Invalid
            ['example@invalid.example.com', false],
            ['example', false],
            ['example@with space', false],
            ['example@webtrees.', false],
            ['example@webtr\ees.com', false],
            ['example(comment)@example.com', false],
            ["\x80\x81\x82@\x83\x84\x85.\x86\x87\x88", false],
            ['user  name@example.com', false],
            ['example.@example.com', false],
            ['example(example]example@example.co.uk', false],
            ['a@b.c+&%$.d', false],
            ['a.b+&%$.c@d', false],
            ['example@généalogie', false]
        ];
    }

    #[DataProvider('emailProvider')]
    public function testIsValidEmail(string $email, bool $is_valid): void
    {
        self::assertSame($is_valid, (new EmailService())->isValidEmail($email));
    }

    public function testMailSslOptions(): void
    {
        $options = (new EmailService())->mailSslOptions();
        self::assertCount(3, $options);
        self::assertArrayHasKey('ssl', $options);
    }

    public function testMailTransportOptions(): void
    {
        $options = (new EmailService())->mailTransportOptions();
        self::assertCount(function_exists('proc_open') ? 2 : 1, $options);
        self::assertArrayHasKey('external', $options);
    }
}