function RecipeMultipleModulesConfigStorageTest::testSimilarModuleNameIsolation

Same name and namespace in other branches
  1. main core/tests/Drupal/Tests/Core/Recipe/RecipeMultipleModulesConfigStorageTest.php \Drupal\Tests\Core\Recipe\RecipeMultipleModulesConfigStorageTest::testSimilarModuleNameIsolation()

Tests that modules with similar name prefixes are correctly isolated.

The 'system' and 'system_test' modules share the string prefix "system" but must be treated as entirely separate modules. Configuration is routed by the part of the config name before the first dot, so 'system.site' belongs to the 'system' module and 'system_test.settings' belongs to the 'system_test' module.

File

core/tests/Drupal/Tests/Core/Recipe/RecipeMultipleModulesConfigStorageTest.php, line 141

Class

RecipeMultipleModulesConfigStorageTest
Tests RecipeMultipleModulesConfigStorage.

Namespace

Drupal\Tests\Core\Recipe

Code

public function testSimilarModuleNameIsolation() : void {
  $storage = RecipeMultipleModulesConfigStorage::createFromModuleList([
    'system',
    'system_test',
    'user',
  ], $this->extensionList);
  // Each module's config is read from its own storage.
  $this->assertSame([
    'name' => 'Site A',
  ], $storage->read('system.site'));
  $this->assertSame([
    'verbose' => TRUE,
  ], $storage->read('system_test.settings'));
  $this->assertSame([
    'register' => 'visitors',
  ], $storage->read('user.settings'));
  // exists() correctly distinguishes between the two modules.
  $this->assertTrue($storage->exists('system.site'));
  $this->assertTrue($storage->exists('system_test.settings'));
  $this->assertFalse($storage->exists('system_test.nonexistent'));
  $this->assertFalse($storage->exists('system.nonexistent'));
  // listAll() with a dot-terminated prefix only returns config from the
  // matching module — 'system.' must not include 'system_test.' config.
  $this->assertSame([
    'system.site',
  ], $storage->listAll('system.'));
  $this->assertSame([
    'system_test.settings',
  ], $storage->listAll('system_test.'));
  // listAll() without a trailing dot filters by string prefix. 'system'
  // matches both 'system.site' and 'system_test.settings'.
  $result = $storage->listAll('system');
  $this->assertContains('system.site', $result);
  $this->assertContains('system_test.settings', $result);
  $this->assertNotContains('user.settings', $result);
  // listAll() with no prefix returns all config sorted.
  $this->assertSame([
    'system.site',
    'system_test.settings',
    'user.settings',
  ], $storage->listAll());
  // readMultiple() routes each name to the correct module.
  $result = $storage->readMultiple([
    'system.site',
    'system_test.settings',
  ]);
  $this->assertSame([
    'name' => 'Site A',
  ], $result['system.site']);
  $this->assertSame([
    'verbose' => TRUE,
  ], $result['system_test.settings']);
  // Without system_test in the module list, its config is inaccessible.
  $storage = RecipeMultipleModulesConfigStorage::createFromModuleList([
    'system',
    'user',
  ], $this->extensionList);
  $this->assertFalse($storage->exists('system_test.settings'));
  $this->assertFalse($storage->read('system_test.settings'));
  $this->assertSame([], $storage->listAll('system_test.'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.