|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\language\Kernel; |
| 4 | + |
| 5 | +use Drupal\Core\Config\ConfigImporter; |
| 6 | +use Drupal\Core\Config\StorageComparer; |
| 7 | +use Drupal\KernelTests\KernelTestBase; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests importing of config with language overrides. |
| 11 | + * |
| 12 | + * @group language |
| 13 | + */ |
| 14 | +class OverriddenConfigImportTest extends KernelTestBase { |
| 15 | + |
| 16 | + /** |
| 17 | + * Config Importer object used for testing. |
| 18 | + * |
| 19 | + * @var \Drupal\Core\Config\ConfigImporter |
| 20 | + */ |
| 21 | + protected $configImporter; |
| 22 | + |
| 23 | + /** |
| 24 | + * {@inheritdoc} |
| 25 | + */ |
| 26 | + protected static $modules = ['system', 'language']; |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + */ |
| 31 | + protected function setUp() { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->installConfig(['system']); |
| 35 | + $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync')); |
| 36 | + |
| 37 | + // Set up the ConfigImporter object for testing. |
| 38 | + $storage_comparer = new StorageComparer( |
| 39 | + $this->container->get('config.storage.sync'), |
| 40 | + $this->container->get('config.storage'), |
| 41 | + $this->container->get('config.manager') |
| 42 | + ); |
| 43 | + $this->configImporter = new ConfigImporter( |
| 44 | + $storage_comparer->createChangelist(), |
| 45 | + $this->container->get('event_dispatcher'), |
| 46 | + $this->container->get('config.manager'), |
| 47 | + $this->container->get('lock'), |
| 48 | + $this->container->get('config.typed'), |
| 49 | + $this->container->get('module_handler'), |
| 50 | + $this->container->get('module_installer'), |
| 51 | + $this->container->get('theme_handler'), |
| 52 | + $this->container->get('string_translation') |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Tests importing overridden config alongside config in the default language. |
| 58 | + */ |
| 59 | + public function testConfigImportUpdates() { |
| 60 | + $storage = $this->container->get('config.storage'); |
| 61 | + $sync = $this->container->get('config.storage.sync'); |
| 62 | + /** @var \Drupal\language\ConfigurableLanguageManagerInterface $language_manager */ |
| 63 | + $language_manager = $this->container->get('language_manager'); |
| 64 | + |
| 65 | + // Make a change to the site configuration in the default collection. |
| 66 | + $data = $storage->read('system.site'); |
| 67 | + $data['name'] = 'English site name'; |
| 68 | + $sync->write('system.site', $data); |
| 69 | + |
| 70 | + // Also make a change to the same config object, but using a language |
| 71 | + // override. |
| 72 | + /* @var \Drupal\Core\Config\StorageInterface $overridden_sync */ |
| 73 | + $overridden_sync = $sync->createCollection('language.fr'); |
| 74 | + $overridden_sync->write('system.site', ['name' => 'French site name']); |
| 75 | + |
| 76 | + // Before we start the import, the change to the site name should not be |
| 77 | + // present. This action also primes the cache in the config factory so that |
| 78 | + // we can test whether the cached data is correctly updated. |
| 79 | + $config = $this->config('system.site'); |
| 80 | + $this->assertNotEquals('English site name', $config->getRawData()['name']); |
| 81 | + |
| 82 | + // Before the import is started the site name should not yet be overridden. |
| 83 | + $this->assertFalse($config->hasOverrides()); |
| 84 | + $override = $language_manager->getLanguageConfigOverride('fr', 'system.site'); |
| 85 | + $this->assertTrue($override->isNew()); |
| 86 | + |
| 87 | + // Start the import of the new configuration. |
| 88 | + $this->configImporter->reset()->import(); |
| 89 | + |
| 90 | + // Verify the new site name in the default language. |
| 91 | + $config = $this->config('system.site')->getRawData(); |
| 92 | + $this->assertEquals('English site name', $config['name']); |
| 93 | + |
| 94 | + // Verify the overridden site name. |
| 95 | + $override = $language_manager->getLanguageConfigOverride('fr', 'system.site'); |
| 96 | + $this->assertEquals('French site name', $override->get('name')); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments