You are here

public function InstalledConfigurationTest::testInstalledConfiguration in Thunder 8.4

Same name and namespace in other branches
  1. 8.5 tests/src/Functional/InstalledConfigurationTest.php \Drupal\Tests\thunder\Functional\InstalledConfigurationTest::testInstalledConfiguration()
  2. 8.2 tests/src/Functional/InstalledConfigurationTest.php \Drupal\Tests\thunder\Functional\InstalledConfigurationTest::testInstalledConfiguration()
  3. 8.3 tests/src/Functional/InstalledConfigurationTest.php \Drupal\Tests\thunder\Functional\InstalledConfigurationTest::testInstalledConfiguration()
  4. 6.2.x tests/src/Functional/InstalledConfigurationTest.php \Drupal\Tests\thunder\Functional\InstalledConfigurationTest::testInstalledConfiguration()
  5. 6.0.x tests/src/Functional/InstalledConfigurationTest.php \Drupal\Tests\thunder\Functional\InstalledConfigurationTest::testInstalledConfiguration()
  6. 6.1.x tests/src/Functional/InstalledConfigurationTest.php \Drupal\Tests\thunder\Functional\InstalledConfigurationTest::testInstalledConfiguration()

Compare active configuration with configuration Yaml files.

File

tests/src/Functional/InstalledConfigurationTest.php, line 405

Class

InstalledConfigurationTest
Test for checking of configuration after install of thunder profile.

Namespace

Drupal\Tests\thunder\Functional

Code

public function testInstalledConfiguration() {
  $this
    ->setDefaultTheme($this->defaultTheme);

  /** @var \Drupal\config_update\ConfigReverter $configUpdate */
  $configUpdate = \Drupal::service('config_update.config_update');

  /** @var \Drupal\Core\Config\TypedConfigManager $typedConfigManager */
  $typedConfigManager = \Drupal::service('config.typed');
  $activeStorage = \Drupal::service('config.storage');
  $installStorage = \Drupal::service('config_update.extension_storage');

  /** @var \Drupal\Core\Config\ExtensionInstallStorage $optionalStorage */
  $optionalStorage = \Drupal::service('config_update.extension_optional_storage');

  // Get list of configurations (active, install and optional).
  $activeList = $activeStorage
    ->listAll();
  $installList = $installStorage
    ->listAll();
  $optionalList = $optionalStorage
    ->listAll();

  // Check that all required configurations are available.
  $installListDiff = array_diff($installList, $activeList);
  $this
    ->assertEquals([], $installListDiff, "All required configurations should be installed.");

  // Filter active list.
  $activeList = array_diff($activeList, static::$ignoreCoreConfigs);

  // Check that all active configuration are provided by Yaml files.
  $activeListDiff = array_diff($activeList, $installList, $optionalList);
  $this
    ->assertEquals([], $activeListDiff, "All active configurations should be defined in Yaml files.");

  /** @var \Drupal\config_update\ConfigDiffer $configDiffer */
  $configDiffer = \Drupal::service('config_update.config_diff');
  $differentConfigNames = [];
  $schemaCheckFail = [];
  foreach ($activeList as $activeConfigName) {

    // Skip incorrect configuration from contribution modules.
    if (in_array($activeConfigName, static::$ignoreConfigs)) {
      continue;
    }

    // Get configuration from file and active configuration.
    $activeConfig = $configUpdate
      ->getFromActive('', $activeConfigName);
    $fileConfig = $configUpdate
      ->getFromExtension('', $activeConfigName);

    // Validate fetched configuration against corresponding schema.
    if ($typedConfigManager
      ->hasConfigSchema($activeConfigName)) {

      // Validate active configuration.
      if ($this
        ->checkConfigSchema($typedConfigManager, $activeConfigName, $activeConfig) !== TRUE) {
        $schemaCheckFail['active'][] = $activeConfigName;
      }

      // Validate configuration from file.
      if ($this
        ->checkConfigSchema($typedConfigManager, $activeConfigName, $fileConfig) !== TRUE) {
        $schemaCheckFail['file'][] = $activeConfigName;
      }
    }
    else {
      $schemaCheckFail['no-schema'][] = $activeConfigName;
    }

    // Clean up configuration if it's required.
    list($activeConfig, $fileConfig) = $this
      ->cleanupConfigurations([
      $activeConfig,
      $fileConfig,
    ], $activeConfigName);

    // Check is active configuration same as in Yaml file.
    if (!$configDiffer
      ->same($fileConfig, $activeConfig)) {
      $differentConfigNames[] = $activeConfigName;
    }
  }

  // Output different configuration names and failed schema checks.
  if (!empty($differentConfigNames) || !empty($schemaCheckFail)) {
    $errorOutput = [
      'configuration-diff' => $differentConfigNames,
      'schema-check' => $schemaCheckFail,
    ];
    throw new \Exception('Configuration difference is found: ' . print_r($errorOutput, TRUE));
  }
}