You are here

public function ConfigOtherModuleTest::testInstallOtherModuleFirst in Drupal 10

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

Tests enabling the provider of the default configuration first.

File

core/modules/config/tests/src/Functional/ConfigOtherModuleTest.php, line 22

Class

ConfigOtherModuleTest
Tests default configuration provided by a module that does not own it.

Namespace

Drupal\Tests\config\Functional

Code

public function testInstallOtherModuleFirst() {
  $this
    ->installModule('config_other_module_config_test');

  // Check that the config entity doesn't exist before the config_test module
  // is enabled. We cannot use the entity system because the config_test
  // entity type does not exist.
  $config = $this
    ->config('config_test.dynamic.other_module_test');
  $this
    ->assertTrue($config
    ->isNew(), 'Default configuration for other modules is not installed if that module is not enabled.');

  // Install the module that provides the entity type. This installs the
  // default configuration.
  $this
    ->installModule('config_test');
  $this
    ->assertNotEmpty($this
    ->getStorage()
    ->load('other_module_test'), 'Default configuration has been installed.');

  // Uninstall the module that provides the entity type. This will remove the
  // default configuration.
  $this
    ->uninstallModule('config_test');
  $config = $this
    ->config('config_test.dynamic.other_module_test');
  $this
    ->assertTrue($config
    ->isNew(), 'Default configuration for other modules is removed when the config entity provider is disabled.');

  // Install the module that provides the entity type again. This installs the
  // default configuration.
  $this
    ->installModule('config_test');
  $other_module_config_entity = $this
    ->getStorage()
    ->load('other_module_test');
  $this
    ->assertNotEmpty($other_module_config_entity, "Default configuration has been recreated.");

  // Update the default configuration to test that the changes are preserved
  // if the module that provides the default configuration is uninstalled.
  $other_module_config_entity
    ->set('style', "The piano ain't got no wrong notes.");
  $other_module_config_entity
    ->save();

  // Uninstall the module that provides the default configuration.
  $this
    ->uninstallModule('config_other_module_config_test');
  $this
    ->assertNotEmpty($this
    ->getStorage()
    ->load('other_module_test'), 'Default configuration for other modules is not removed when the module that provides it is uninstalled.');

  // Default configuration provided by config_test should still exist.
  $this
    ->assertNotEmpty($this
    ->getStorage()
    ->load('dotted.default'), 'The configuration is not deleted.');

  // Re-enable module to test that pre-existing optional configuration does
  // not throw an error.
  $this
    ->installModule('config_other_module_config_test');
  $this
    ->assertTrue(\Drupal::moduleHandler()
    ->moduleExists('config_other_module_config_test'), 'The config_other_module_config_test module is installed.');

  // Ensure that optional configuration with unmet dependencies is only
  // installed once all the dependencies are met.
  $this
    ->assertNull($this
    ->getStorage()
    ->load('other_module_test_unmet'), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are not met is not created.');
  $this
    ->assertNull($this
    ->getStorage()
    ->load('other_module_test_optional_entity_unmet'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are not met is not created.');
  $this
    ->installModule('config_test_language');
  $this
    ->assertNull($this
    ->getStorage()
    ->load('other_module_test_optional_entity_unmet'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is not created.');
  $this
    ->installModule('config_install_dependency_test');
  $this
    ->assertNotEmpty($this
    ->getStorage()
    ->load('other_module_test_unmet'), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are met is now created.');

  // The following configuration entity's dependencies are now met. It is
  // indirectly dependent on the config_install_dependency_test module because
  // it has a dependency on the config_test.dynamic.dependency_for_unmet2
  // configuration provided by that module and, therefore, should be created.
  $this
    ->assertNotEmpty($this
    ->getStorage()
    ->load('other_module_test_optional_entity_unmet2'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet2 whose dependencies are met is now created.');

  // The following configuration entity's dependencies are now met even though
  // it has no direct dependency on the module. It is indirectly dependent on
  // the config_install_dependency_test module because it has a dependency on
  // the config_test.dynamic.other_module_test_unmet configuration that is
  // dependent on the config_install_dependency_test module and, therefore,
  // should be created.
  $entity = $this
    ->getStorage()
    ->load('other_module_test_optional_entity_unmet');
  $this
    ->assertNotEmpty($entity, 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is created.');
  $entity
    ->delete();

  // Install another module to ensure the configuration just deleted is not
  // recreated.
  $this
    ->installModule('config');
  $this
    ->assertNull($this
    ->getStorage()
    ->load('other_module_test_optional_entity_unmet'), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is not installed when an unrelated module is installed.');
}