You are here

public function ConfigWithTranslationTest::testConfigImport in Config Ignore 8.3

Tests config import.

File

tests/src/Functional/ConfigWithTranslationTest.php, line 147

Class

ConfigWithTranslationTest
Tests config_ignore with translated configurations.

Namespace

Drupal\Tests\config_ignore\Functional

Code

public function testConfigImport() {

  // Add the config_ignore.settings changes in the sync store. Remember that
  // the ignore patterns were added only in the active store, in ::setUp(),
  // but were never exported in sync. Otherwise the values will be reverted,
  // later, in the first config import.
  // @see self::setUp()
  $this
    ->setConfigSyncValue('config_ignore.settings', 'ignored_config_entities', [
    'user.role.anonymous',
    'user.role.authenticated:weight',
    'user.role.authenticated:is_admin',
  ]);

  // Change configurations in the sync store.
  $this
    ->setConfigSyncValue('user.settings', 'anonymous', 'Visitor');
  $this
    ->setConfigSyncValue('user.role.anonymous', 'label', 'Visitor');
  $this
    ->setConfigSyncValue('user.role.authenticated', 'label', 'Authenticated');
  $this
    ->setConfigSyncValue('user.role.authenticated', 'weight', 2);
  $this
    ->setConfigSyncValue('user.role.authenticated', 'is_admin', TRUE);

  // Change translations of user.role.anonymous and user.role.authenticated.
  $this
    ->setConfigSyncValue('user.role.anonymous', 'label', 'Vizitator', 'ro');
  $this
    ->setConfigSyncValue('user.role.authenticated', 'label', 'Logat', 'ro');

  // Check that user.settings was changed in the sync store.
  $this
    ->assertExportedValue('user.settings', 'anonymous', 'Visitor');

  // Check that main user.role.anonymous.yml was changed in the sync store.
  $this
    ->assertExportedValue('user.role.anonymous', 'label', 'Visitor');

  // Check that the translated override was changed in the sync store.
  $this
    ->assertExportedValue('user.role.anonymous', 'label', 'Vizitator', 'ro');
  $this
    ->assertExportedValue('user.role.authenticated', 'label', 'Authenticated');
  $this
    ->assertExportedValue('user.role.authenticated', 'weight', 2);
  $this
    ->assertExportedValue('user.role.authenticated', 'is_admin', TRUE);

  // Import changes.
  $this
    ->drush('config:import', [], [
    'yes' => NULL,
  ]);

  // As the tests are running in the same request we manually clear the static
  // cache of the config objects.
  \Drupal::configFactory()
    ->reset();

  // Check that user.settings has been overridden by import.
  $this
    ->assertSame('Visitor', $this
    ->config('user.settings')
    ->get('anonymous'));

  // Check that user.role.anonymous has been preserved.
  $this
    ->assertSame('Anonymous user', $this
    ->config('user.role.anonymous')
    ->get('label'));

  // Check that user.role.authenticated has been overridden by import.
  $this
    ->assertSame('Authenticated', $this
    ->config('user.role.authenticated')
    ->get('label'));
  $this
    ->assertEquals(1, $this
    ->config('user.role.authenticated')
    ->get('weight'));
  $this
    ->assertFalse($this
    ->config('user.role.authenticated')
    ->get('is_admin'));

  // Check that the user.role.anonymous translation has been also preserved.
  $language_manager = \Drupal::languageManager();
  $original_language = $language_manager
    ->getConfigOverrideLanguage();

  /** @var \Drupal\language\Config\LanguageConfigOverride $translated */
  $translated = $language_manager
    ->getLanguageConfigOverride('ro', 'user.role.anonymous');
  $this
    ->assertSame('Utilizator anonim', $translated
    ->get('label'));
  $translated = $language_manager
    ->getLanguageConfigOverride('ro', 'user.role.authenticated');
  $this
    ->assertSame('Logat', $translated
    ->get('label'));
  $language_manager
    ->setConfigOverrideLanguage($original_language);

  /** @var \Drupal\Core\Config\StorageInterface $active_storage */
  $active_storage = \Drupal::service('config.storage');

  // Remove the config in order to test again when the destination is missed.
  $active_storage
    ->delete('user.role.authenticated');

  // Re-import changes.
  $this
    ->drush('config:import', [], [
    'yes' => NULL,
  ]);
  \Drupal::configFactory()
    ->reset('user.role.authenticated');
  $this
    ->assertSame('Authenticated', $this
    ->config('user.role.authenticated')
    ->get('label'));
  $this
    ->assertEquals(1, $this
    ->config('user.role.authenticated')
    ->get('weight'));
  $this
    ->assertFalse((bool) $this
    ->config('user.role.authenticated')
    ->get('is_admin'));
}