You are here

function ConfigInstallWebTest::testIntegrationModuleReinstallation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config/src/Tests/ConfigInstallWebTest.php \Drupal\config\Tests\ConfigInstallWebTest::testIntegrationModuleReinstallation()

Tests module re-installation.

File

core/modules/config/src/Tests/ConfigInstallWebTest.php, line 44
Contains \Drupal\config\Tests\ConfigInstallWebTest.

Class

ConfigInstallWebTest
Tests installation and removal of configuration objects in install, disable and uninstall functionality.

Namespace

Drupal\config\Tests

Code

function testIntegrationModuleReinstallation() {
  $default_config = 'config_integration_test.settings';
  $default_configuration_entity = 'config_test.dynamic.config_integration_test';

  // Install the config_test module we're integrating with.
  \Drupal::service('module_installer')
    ->install(array(
    'config_test',
  ));

  // Verify the configuration does not exist prior to installation.
  $config_static = $this
    ->config($default_config);
  $this
    ->assertIdentical($config_static
    ->isNew(), TRUE);
  $config_entity = $this
    ->config($default_configuration_entity);
  $this
    ->assertIdentical($config_entity
    ->isNew(), TRUE);

  // Install the integration module.
  \Drupal::service('module_installer')
    ->install(array(
    'config_integration_test',
  ));

  // Verify that default module config exists.
  \Drupal::configFactory()
    ->reset($default_config);
  \Drupal::configFactory()
    ->reset($default_configuration_entity);
  $config_static = $this
    ->config($default_config);
  $this
    ->assertIdentical($config_static
    ->isNew(), FALSE);
  $this
    ->assertIdentical($config_static
    ->get('foo'), 'default setting');
  $config_entity = $this
    ->config($default_configuration_entity);
  $this
    ->assertIdentical($config_entity
    ->isNew(), FALSE);
  $this
    ->assertIdentical($config_entity
    ->get('label'), 'Default integration config label');

  // Customize both configuration objects.
  $config_static
    ->set('foo', 'customized setting')
    ->save();
  $config_entity
    ->set('label', 'Customized integration config label')
    ->save();

  // @todo FIXME: Setting config keys WITHOUT SAVING retains the changed config
  //   object in memory. Every new call to $this->config() MUST revert in-memory changes
  //   that haven't been saved!
  //   In other words: This test passes even without this reset, but it shouldn't.
  $this->container
    ->get('config.factory')
    ->reset();

  // Disable and uninstall the integration module.
  $this->container
    ->get('module_installer')
    ->uninstall(array(
    'config_integration_test',
  ));

  // Verify the integration module's config was uninstalled.
  $config_static = $this
    ->config($default_config);
  $this
    ->assertIdentical($config_static
    ->isNew(), TRUE);

  // Verify the integration config still exists.
  $config_entity = $this
    ->config($default_configuration_entity);
  $this
    ->assertIdentical($config_entity
    ->isNew(), FALSE);
  $this
    ->assertIdentical($config_entity
    ->get('label'), 'Customized integration config label');

  // Reinstall the integration module.
  try {
    \Drupal::service('module_installer')
      ->install(array(
      'config_integration_test',
    ));
    $this
      ->fail('Expected PreExistingConfigException not thrown.');
  } catch (PreExistingConfigException $e) {
    $this
      ->assertEqual($e
      ->getExtension(), 'config_integration_test');
    $this
      ->assertEqual($e
      ->getConfigObjects(), [
      StorageInterface::DEFAULT_COLLECTION => [
        'config_test.dynamic.config_integration_test',
      ],
    ]);
    $this
      ->assertEqual($e
      ->getMessage(), 'Configuration objects (config_test.dynamic.config_integration_test) provided by config_integration_test already exist in active configuration');
  }

  // Delete the configuration entity so that the install will work.
  $config_entity
    ->delete();
  \Drupal::service('module_installer')
    ->install(array(
    'config_integration_test',
  ));

  // Verify the integration module's config was re-installed.
  \Drupal::configFactory()
    ->reset($default_config);
  \Drupal::configFactory()
    ->reset($default_configuration_entity);
  $config_static = $this
    ->config($default_config);
  $this
    ->assertIdentical($config_static
    ->isNew(), FALSE);
  $this
    ->assertIdentical($config_static
    ->get('foo'), 'default setting');

  // Verify the integration config is using the default.
  $config_entity = \Drupal::config($default_configuration_entity);
  $this
    ->assertIdentical($config_entity
    ->isNew(), FALSE);
  $this
    ->assertIdentical($config_entity
    ->get('label'), 'Default integration config label');
}