You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
6.0 KiB
PHP
157 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace PHPMailer\PHPMailer {
|
|
class PHPMailer
|
|
{
|
|
public static array $instances = [];
|
|
public static bool $sendResult = true;
|
|
public static bool $throwOnSend = false;
|
|
|
|
public string $Host = '';
|
|
public bool $SMTPAuth = false;
|
|
public string $Username = '';
|
|
public string $Password = '';
|
|
public string $SMTPSecure = '';
|
|
public array $SMTPOptions = [];
|
|
public string $Port = '';
|
|
public int $Timeout = 0;
|
|
public int $Timelimit = 0;
|
|
public string $CharSet = '';
|
|
public string $Subject = '';
|
|
public string $Body = '';
|
|
public string $fromAddress = '';
|
|
public string $fromName = '';
|
|
public array $addresses = [];
|
|
public bool $smtpEnabled = false;
|
|
public bool $htmlEnabled = false;
|
|
|
|
public function __construct()
|
|
{
|
|
self::$instances[] = $this;
|
|
}
|
|
|
|
public function isSMTP(): void
|
|
{
|
|
$this->smtpEnabled = true;
|
|
}
|
|
|
|
public function setFrom($address, $name): void
|
|
{
|
|
$this->fromAddress = $address;
|
|
$this->fromName = $name;
|
|
}
|
|
|
|
public function addAddress($address): void
|
|
{
|
|
$this->addresses[] = $address;
|
|
}
|
|
|
|
public function isHTML($enabled): void
|
|
{
|
|
$this->htmlEnabled = $enabled;
|
|
}
|
|
|
|
public function addAttachment($file): void
|
|
{
|
|
throw new \RuntimeException('This test does not send attachments.');
|
|
}
|
|
|
|
public function send(): bool
|
|
{
|
|
if (self::$throwOnSend) {
|
|
throw new \RuntimeException('Mock SMTP transport failure');
|
|
}
|
|
return self::$sendResult;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace App\Services {
|
|
class SiteName
|
|
{
|
|
public static function defaultName(): string
|
|
{
|
|
return "Default Site\r\nInjected";
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
use App\Services\Mail\Smtp;
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
require dirname(__DIR__) . '/app/Services/Config.php';
|
|
require dirname(__DIR__) . '/app/Services/MailSenderIdentity.php';
|
|
require dirname(__DIR__) . '/app/Services/Mail/Base.php';
|
|
require dirname(__DIR__) . '/app/Services/Mail/Smtp.php';
|
|
|
|
function expectSmtpSingleConfig($condition, $message): void
|
|
{
|
|
if (!$condition) {
|
|
fwrite(STDERR, $message . PHP_EOL);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$System_Config = [
|
|
'smtp_host' => 'primary.example.com',
|
|
'smtp_username' => 'primary-user',
|
|
'smtp_port' => '587',
|
|
'smtp_sender' => 'sender@primary.example.com',
|
|
'smtp_name' => 'Primary sender',
|
|
'smtp_passsword' => 'primary-password',
|
|
'smtp_ssl' => 'true',
|
|
'gmail_smtp_host' => 'obsolete.example.com',
|
|
'gmail_smtp_username' => 'obsolete-user',
|
|
'gmail_smtp_port' => '465',
|
|
'gmail_smtp_sender' => 'sender@obsolete.example.com',
|
|
'gmail_smtp_name' => 'Obsolete sender',
|
|
'gmail_smtp_passsword' => 'obsolete-password',
|
|
'gmail_smtp_ssl' => 'false',
|
|
];
|
|
|
|
$smtp = new Smtp();
|
|
foreach (['member@example.com', 'member@gmaii.com', 'member@120.com'] as $address) {
|
|
$before = count(PHPMailer::$instances);
|
|
expectSmtpSingleConfig($smtp->send($address, 'Subject', 'Body', []) === true, 'Mock SMTP send failed for ' . $address);
|
|
expectSmtpSingleConfig(count(PHPMailer::$instances) === $before + 1, 'Expected one mailer for ' . $address);
|
|
$mail = PHPMailer::$instances[$before];
|
|
expectSmtpSingleConfig($mail->smtpEnabled && $mail->htmlEnabled, 'SMTP/HTML not enabled for ' . $address);
|
|
expectSmtpSingleConfig($mail->Host === 'primary.example.com', 'Wrong SMTP host for ' . $address);
|
|
expectSmtpSingleConfig($mail->Username === 'primary-user', 'Wrong SMTP username for ' . $address);
|
|
expectSmtpSingleConfig($mail->Password === 'primary-password', 'Wrong SMTP password for ' . $address);
|
|
expectSmtpSingleConfig($mail->Port === '587', 'Wrong SMTP port for ' . $address);
|
|
expectSmtpSingleConfig($mail->Timeout === 15 && $mail->Timelimit === 20,
|
|
'SMTP transport timeouts are not bounded for ' . $address);
|
|
expectSmtpSingleConfig($mail->SMTPSecure === 'tls', 'Wrong SMTP encryption for ' . $address);
|
|
expectSmtpSingleConfig($mail->fromAddress === 'sender@primary.example.com', 'Wrong sender address for ' . $address);
|
|
expectSmtpSingleConfig($mail->fromName === 'Default Site Injected', 'Wrong sender name for ' . $address);
|
|
expectSmtpSingleConfig($mail->addresses === [$address], 'Wrong recipient for ' . $address);
|
|
}
|
|
|
|
PHPMailer::$sendResult = false;
|
|
expectSmtpSingleConfig(
|
|
$smtp->send('failed@example.com', 'Subject', 'Body', []) === false,
|
|
'SMTP must return false when PHPMailer reports failure.'
|
|
);
|
|
|
|
PHPMailer::$throwOnSend = true;
|
|
$exceptionWasReported = false;
|
|
try {
|
|
$result = $smtp->send('exception@example.com', 'Subject', 'Body', []);
|
|
$exceptionWasReported = $result === false;
|
|
} catch (\RuntimeException $exception) {
|
|
$exceptionWasReported = $exception->getMessage() === 'Mock SMTP transport failure';
|
|
}
|
|
expectSmtpSingleConfig($exceptionWasReported, 'SMTP exception did not report a failed send.');
|
|
|
|
$source = file_get_contents(dirname(__DIR__) . '/app/Services/Mail/Smtp.php');
|
|
expectSmtpSingleConfig($source !== false && strpos($source, 'gmail_smtp_') === false, 'SMTP still reads obsolete gmail_smtp_* settings.');
|
|
expectSmtpSingleConfig(strpos($source, "Config::get('smtp_name')") === false, 'SMTP still uses smtp_name instead of the site default title.');
|
|
$example = file_get_contents(dirname(__DIR__) . '/config/.config.php.example');
|
|
expectSmtpSingleConfig(is_string($example) && strpos($example, "['smtp_name']") === false,
|
|
'The example configuration still advertises the retired smtp_name setting.');
|
|
|
|
echo 'SMTP single-configuration checks passed.' . PHP_EOL;
|
|
}
|