function MailerDsnConfigValidationTest::testMailerSchemeValidation

File

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

Class

MailerDsnConfigValidationTest
Tests validation of mailer dsn config.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testMailerSchemeValidation() : void {
    $config = $this->config('system.mail');
    $this->assertFalse($config->isNew());
    $data = $config->get();
    // If the scheme is NULL, it should be an error.
    $data['mailer_dsn']['scheme'] = NULL;
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.scheme', $violations[0]->getPropertyPath());
    $this->assertSame('This value should not be null.', (string) $violations[0]->getMessage());
    // If the scheme is blank, it should be an error.
    $data['mailer_dsn']['scheme'] = '';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.scheme', $violations[0]->getPropertyPath());
    $this->assertSame('The mailer DSN must contain a scheme.', (string) $violations[0]->getMessage());
    // If the scheme doesn't start with a letter, it should be an error.
    $data['mailer_dsn']['scheme'] = '-unexpected-first-character';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.scheme', $violations[0]->getPropertyPath());
    $this->assertSame('The mailer DSN scheme must start with a letter followed by zero or more letters, numbers, plus (+), minus (-) or periods (.)', (string) $violations[0]->getMessage());
    // If the scheme contains unexpected characters, it should be an error.
    $data['mailer_dsn']['scheme'] = 'unexpected_underscore';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('mailer_dsn.scheme', $violations[0]->getPropertyPath());
    $this->assertSame('The mailer DSN scheme must start with a letter followed by zero or more letters, numbers, plus (+), minus (-) or periods (.)', (string) $violations[0]->getMessage());
    // If the scheme is valid, it should be accepted.
    $data['mailer_dsn']['scheme'] = 'smtp';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(0, $violations);
    // If the scheme is valid, it should be accepted.
    $data['mailer_dsn']['scheme'] = 'sendmail+smtp';
    $violations = $this->configManager
        ->createFromNameAndData($config->getName(), $data)
        ->validate();
    $this->assertCount(0, $violations);
    // If the scheme is valid, it should be accepted.
    $data['mailer_dsn']['scheme'] = 'drupal.test-capture';
    $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.