function MailerDsnConfigValidationTest::testMailerHostValidation

File

core/tests/Drupal/KernelTests/Core/Config/MailerDsnConfigValidationTest.php, line 93

Class

MailerDsnConfigValidationTest
Tests validation of mailer dsn config.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testMailerHostValidation() : void {
    $config = $this->config('system.mail');
    $this->assertFalse($config->isNew());
    $data = $config->get();
    // If the host is NULL, it should be an error.
    $data['mailer_dsn']['host'] = NULL;
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.host', $violations[0]->getPropertyPath());
    $this->assertSame('This value should not be null.', (string) $violations[0]->getMessage());
    // If the host is blank, it should be an error.
    $data['mailer_dsn']['host'] = '';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.host', $violations[0]->getPropertyPath());
    $this->assertSame('The mailer DSN must contain a host (use "default" by default).', (string) $violations[0]->getMessage());
    // If the host contains a newline, it should be an error.
    $data['mailer_dsn']['host'] = "host\nwith\nnewline";
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.host', $violations[0]->getPropertyPath());
    $this->assertSame('The mailer DSN host should conform to RFC 3986 URI host component.', (string) $violations[0]->getMessage());
    // If the host contains unexpected characters, it should be an error.
    $data['mailer_dsn']['host'] = "host\rwith\tcontrol-chars";
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.host', $violations[0]->getPropertyPath());
    $this->assertSame('The mailer DSN host should conform to RFC 3986 URI host component.', (string) $violations[0]->getMessage());
    // If the host is valid, it should be accepted.
    $data['mailer_dsn']['host'] = 'default';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(0, $violations);
    // If the host is valid, it should be accepted.
    $data['mailer_dsn']['host'] = 'mail.example.com';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(0, $violations);
    // If the host is valid, it should be accepted.
    $data['mailer_dsn']['host'] = '127.0.0.1';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(0, $violations);
    // If the host is valid, it should be accepted.
    $data['mailer_dsn']['host'] = '[::1]';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(0, $violations);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.