View source
<?php
namespace Drupal\Tests\smtp\Kernel\ConnectionTester;
use Drupal\KernelTests\KernelTestBase;
use Drupal\smtp\ConnectionTester\ConnectionTester;
use PHPMailer\PHPMailer\Exception as PHPMailerException;
use PHPMailer\PHPMailer\PHPMailer;
class ConnectionTesterTest extends KernelTestBase {
public static $modules = [
'smtp',
];
public function testHookRequirements(string $message, bool $smtp_on, bool $result, string $exception, array $expected) {
$smtp_settings = \Drupal::configFactory()
->getEditable('smtp.settings');
$smtp_settings
->set('smtp_on', $smtp_on);
$smtp_settings
->save();
$object = \Drupal::service('smtp.connection_tester');
$object
->setMailer($this
->getMockMailer($result, $exception));
$object
->testConnection();
$output = $object
->hookRequirements('runtime');
if ($output != $expected) {
print_r([
'message' => $message,
'output' => $output,
'expected' => $expected,
]);
}
$this
->assertTrue($output == $expected, $message);
}
public function providerHookRequirements() {
return [
[
'message' => 'SMTP on, working.',
'$smtp_on' => TRUE,
'result' => TRUE,
'exception' => '',
'expected' => [
'smtp_connection' => [
'title' => 'SMTP connection',
'value' => 'SMTP module is enabled, turned on, and connection is valid.',
'severity' => ConnectionTester::REQUIREMENT_OK,
],
],
],
[
'message' => 'SMTP on, result FALSE.',
'$smtp_on' => TRUE,
'result' => FALSE,
'exception' => '',
'expected' => [
'smtp_connection' => [
'title' => 'SMTP connection',
'value' => 'SMTP module is enabled, turned on, but SmtpConnect() returned FALSE.',
'severity' => ConnectionTester::REQUIREMENT_ERROR,
],
],
],
[
'message' => 'SMTP on, PHPMailerException.',
'$smtp_on' => TRUE,
'result' => FALSE,
'exception' => PHPMailerException::class,
'expected' => [
'smtp_connection' => [
'title' => 'SMTP connection',
'value' => 'SMTP module is enabled, turned on, but SmtpConnect() threw exception EXCEPTION MESSAGE',
'severity' => ConnectionTester::REQUIREMENT_ERROR,
],
],
],
[
'message' => 'SMTP on, Exception.',
'$smtp_on' => TRUE,
'result' => FALSE,
'exception' => \Exception::class,
'expected' => [
'smtp_connection' => [
'title' => 'SMTP connection',
'value' => 'SMTP module is enabled, turned on, but SmtpConnect() threw an unexpected exception',
'severity' => ConnectionTester::REQUIREMENT_ERROR,
],
],
],
[
'message' => 'SMTP off.',
'$smtp_on' => FALSE,
'result' => FALSE,
'exception' => '',
'expected' => [
'smtp_connection' => [
'title' => 'SMTP connection',
'value' => 'SMTP module is enabled but turned off.',
'severity' => ConnectionTester::REQUIREMENT_OK,
],
],
],
];
}
private function getMockMailer($result, $exception) {
$class = new class($result, $exception) extends PHPMailer {
public function __construct($result, $exception) {
$this->result = $result;
$this->exception = $exception;
}
public function smtpConnect($options = NULL) {
if ($this->exception) {
$class = $this->exception;
throw new $class('EXCEPTION MESSAGE');
}
return $this->result;
}
};
return $class;
}
}