function ExtensionAvailableConstraintValidatorTest::testValidationModule

Tests the ExtensionAvailable constraint validator.

Attributes

#[DataProvider('dataProvider')]

File

core/tests/Drupal/KernelTests/Core/Extension/ExtensionAvailableConstraintValidatorTest.php, line 63

Class

ExtensionAvailableConstraintValidatorTest
Tests the ExtensionAvailable constraint validator.

Namespace

Drupal\KernelTests\Core\Extension

Code

public function testValidationModule(array $new_config, ?array $initial_config = [], array $expected_violations = [], bool $testing_environment = TRUE) : void {
  if ($testing_environment === FALSE) {
    $reflection = new \ReflectionClass(ExtensionAvailableConstraintValidator::class);
    $reflection->setStaticPropertyValue('inTestEnvironment', FALSE);
  }
  /** @var \Drupal\Core\Config\TypedConfigManager $typed_config */
  $typed_config = $this->container
    ->get('config.typed');
  $config = $this->container
    ->get('config.factory')
    ->getEditable('core.extension');
  foreach ($initial_config as $key => $value) {
    if ($value === '__UNSET__') {
      $config->clear($key);
    }
    else {
      $config->set($key, $value);
    }
    $config->save();
    if ($key === 'profile') {
      // The installation profile is provided by a container parameter. Saving
      // the configuration doesn't automatically trigger invalidation
      $this->container
        ->get('kernel')
        ->rebuildContainer();
    }
  }
  $result = $typed_config->createFromNameAndData('core.extension', $new_config);
  $violations = $result->validate();
  $violationMessages = [];
  foreach ($violations as $violation) {
    $violationMessages[] = (string) $violation->getMessage();
  }
  $this->assertEquals($expected_violations, $violationMessages);
  $expected = count($expected_violations);
  $this->assertCount($expected, $violations, 'Expected violations count matches actual violations count.');
}

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