You are here

public function ConfigInstallWebTest::testIntegrationModuleReinstallation in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/config/tests/src/Functional/ConfigInstallWebTest.php \Drupal\Tests\config\Functional\ConfigInstallWebTest::testIntegrationModuleReinstallation()
  2. 10 core/modules/config/tests/src/Functional/ConfigInstallWebTest.php \Drupal\Tests\config\Functional\ConfigInstallWebTest::testIntegrationModuleReinstallation()

Tests module re-installation.

File

core/modules/config/tests/src/Functional/ConfigInstallWebTest.php, line 51

Class

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

Namespace

Drupal\Tests\config\Functional

Code

public 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([
    'config_test',
  ]);

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

  // Install the integration module.
  \Drupal::service('module_installer')
    ->install([
    'config_integration_test',
  ]);
  $this
    ->resetAll();

  // Verify that default module config exists.
  \Drupal::configFactory()
    ->reset($default_config);
  \Drupal::configFactory()
    ->reset($default_configuration_entity);
  $config_static = $this
    ->config($default_config);
  $this
    ->assertFalse($config_static
    ->isNew());
  $this
    ->assertSame('default setting', $config_static
    ->get('foo'));
  $config_entity = $this
    ->config($default_configuration_entity);
  $this
    ->assertFalse($config_entity
    ->isNew());
  $this
    ->assertSame('Default integration config label', $config_entity
    ->get('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([
    'config_integration_test',
  ]);

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

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

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

  // Delete the configuration entity so that the install will work.
  $config_entity
    ->delete();
  \Drupal::service('module_installer')
    ->install([
    '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
    ->assertFalse($config_static
    ->isNew());
  $this
    ->assertSame('default setting', $config_static
    ->get('foo'));

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