You are here

protected function ImportExportTestBase::assertImportedConfigEntity in Acquia Content Hub 8.2

Executes assertions on an imported configuration entity.

Parameters

\Drupal\Tests\acquia_contenthub\Kernel\Stubs\CdfExpectations $expectation: Expectation object.

string $config_name: Unique entity configuration name.

Throws

\Exception

1 call to ImportExportTestBase::assertImportedConfigEntity()
ImportExportTestBase::configEntityImportExport in tests/src/Kernel/ImportExportTestBase.php
Executes the set of import/export tests on a configuration entity.

File

tests/src/Kernel/ImportExportTestBase.php, line 287

Class

ImportExportTestBase
Base for testing exports and imports.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

protected function assertImportedConfigEntity(CdfExpectations $expectation, string $config_name) : void {
  if (!$expectation
    ->getLangcodes()) {

    // Perform language-agnostic assertions.
    foreach ($expectation
      ->getFieldNames() as $field_name) {
      $actual_value = \Drupal::config($config_name)
        ->get($field_name);
      $expected_value = $expectation
        ->getFieldValue($field_name);
      $this
        ->assertEquals($expected_value, $actual_value);
    }
    return;
  }

  /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
  $language_manager = $this->container
    ->get('language_manager');

  // Get the default language.
  $default_language = $language_manager
    ->getCurrentLanguage();
  foreach ($expectation
    ->getLangcodes() as $langcode) {
    $language = $language_manager
      ->getLanguage($langcode);
    $language_manager
      ->setConfigOverrideLanguage($language);
    foreach ($expectation
      ->getFieldNames() as $field_name) {
      if (FALSE !== strpos($field_name, ':')) {

        // Retrieve a second-level configuration value.
        [
          $field_name_level1,
          $field_name_level2,
        ] = explode(':', $field_name);
        $actual_value = \Drupal::config($config_name)
          ->get($field_name_level1);
        if (!isset($actual_value[$field_name_level2])) {
          throw new \Exception(sprintf("Failed to get actual value for '%s' field defined in the expectation file ('%s' configuration).", $field_name, $config_name));
        }
        $actual_value = $actual_value[$field_name_level2];
      }
      else {
        $actual_value = \Drupal::config($config_name)
          ->get($field_name);
      }
      $expected_value = $expectation
        ->getFieldValue($field_name, $langcode);
      $this
        ->assertEquals($expected_value, $actual_value);
    }
  }

  // Restore the default language.
  $language_manager
    ->setConfigOverrideLanguage($default_language);
}