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