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