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