3 namespace Drupal\Tests\system\Functional\Entity;
5 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\image\Entity\ImageStyle;
8 use Drupal\search\Entity\SearchPage;
9 use Drupal\Tests\BrowserTestBase;
10 use Drupal\system\Entity\Action;
13 * Tests ConfigEntity importing.
17 class ConfigEntityImportTest extends BrowserTestBase {
24 public static $modules = ['action', 'block', 'filter', 'image', 'search', 'search_extra_type', 'config_test'];
29 protected function setUp() {
32 $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
36 * Runs test methods for each module within a single test run.
38 public function testConfigUpdateImport() {
39 $this->doActionUpdate();
40 $this->doBlockUpdate();
41 $this->doFilterFormatUpdate();
42 $this->doImageStyleUpdate();
43 $this->doSearchPageUpdate();
44 $this->doThirdPartySettingsUpdate();
48 * Tests updating a action during import.
50 protected function doActionUpdate() {
51 // Create a test action with a known label.
52 $name = 'system.action.apple';
53 $entity = Action::create([
55 'plugin' => 'action_message_action',
59 $this->checkSinglePluginConfigSync($entity, 'configuration', 'message', '');
61 // Read the existing data, and prepare an altered version in sync.
62 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
63 $custom_data['configuration']['message'] = 'Granny Smith';
64 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
69 * Tests updating a block during import.
71 protected function doBlockUpdate() {
72 // Create a test block with a known label.
73 $name = 'block.block.apple';
74 $block = $this->drupalPlaceBlock('system_powered_by_block', [
76 'label' => 'Red Delicious',
79 $this->checkSinglePluginConfigSync($block, 'settings', 'label', 'Red Delicious');
81 // Read the existing data, and prepare an altered version in sync.
82 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
83 $custom_data['settings']['label'] = 'Granny Smith';
84 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
88 * Tests updating a filter format during import.
90 protected function doFilterFormatUpdate() {
91 // Create a test filter format with a known label.
92 $name = 'filter.format.plain_text';
94 /** @var $entity \Drupal\filter\Entity\FilterFormat */
95 $entity = FilterFormat::load('plain_text');
96 $plugin_collection = $entity->getPluginCollections()['filters'];
98 $filters = $entity->get('filters');
99 $this->assertIdentical(72, $filters['filter_url']['settings']['filter_url_length']);
101 $filters['filter_url']['settings']['filter_url_length'] = 100;
102 $entity->set('filters', $filters);
104 $this->assertIdentical($filters, $entity->get('filters'));
105 $this->assertIdentical($filters, $plugin_collection->getConfiguration());
107 $filters['filter_url']['settings']['filter_url_length'] = -100;
108 $entity->getPluginCollections()['filters']->setConfiguration($filters);
110 $this->assertIdentical($filters, $entity->get('filters'));
111 $this->assertIdentical($filters, $plugin_collection->getConfiguration());
113 // Read the existing data, and prepare an altered version in sync.
114 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
115 $custom_data['filters']['filter_url']['settings']['filter_url_length'] = 100;
116 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
120 * Tests updating an image style during import.
122 protected function doImageStyleUpdate() {
123 // Create a test image style with a known label.
124 $name = 'image.style.thumbnail';
126 /** @var $entity \Drupal\image\Entity\ImageStyle */
127 $entity = ImageStyle::load('thumbnail');
128 $plugin_collection = $entity->getPluginCollections()['effects'];
130 $effects = $entity->get('effects');
131 $effect_id = key($effects);
132 $this->assertIdentical(100, $effects[$effect_id]['data']['height']);
134 $effects[$effect_id]['data']['height'] = 50;
135 $entity->set('effects', $effects);
137 // Ensure the entity and plugin have the correct configuration.
138 $this->assertIdentical($effects, $entity->get('effects'));
139 $this->assertIdentical($effects, $plugin_collection->getConfiguration());
141 $effects[$effect_id]['data']['height'] = -50;
142 $entity->getPluginCollections()['effects']->setConfiguration($effects);
144 // Ensure the entity and plugin have the correct configuration.
145 $this->assertIdentical($effects, $entity->get('effects'));
146 $this->assertIdentical($effects, $plugin_collection->getConfiguration());
148 // Read the existing data, and prepare an altered version in sync.
149 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
150 $effect_name = key($original_data['effects']);
152 $custom_data['effects'][$effect_name]['data']['upscale'] = FALSE;
153 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
157 * Tests updating a search page during import.
159 protected function doSearchPageUpdate() {
160 // Create a test search page with a known label.
161 $name = 'search.page.apple';
162 $entity = SearchPage::create([
164 'plugin' => 'search_extra_type_search',
168 $this->checkSinglePluginConfigSync($entity, 'configuration', 'boost', 'bi');
170 // Read the existing data, and prepare an altered version in sync.
171 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
172 $custom_data['configuration']['boost'] = 'asdf';
173 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
177 * Tests updating of third party settings.
179 protected function doThirdPartySettingsUpdate() {
180 // Create a test action with a known label.
181 $name = 'system.action.third_party_settings_test';
183 /** @var \Drupal\config_test\Entity\ConfigTest $entity */
184 $entity = Action::create([
185 'id' => 'third_party_settings_test',
186 'plugin' => 'action_message_action',
190 $this->assertIdentical([], $entity->getThirdPartyProviders());
191 // Get a copy of the configuration before the third party setting is added.
192 $no_third_part_setting_config = $this->container->get('config.storage')->read($name);
194 // Add a third party setting.
195 $entity->setThirdPartySetting('config_test', 'integer', 1);
197 $this->assertIdentical(1, $entity->getThirdPartySetting('config_test', 'integer'));
198 $has_third_part_setting_config = $this->container->get('config.storage')->read($name);
200 // Ensure configuration imports can completely remove third party settings.
201 $this->assertConfigUpdateImport($name, $has_third_part_setting_config, $no_third_part_setting_config);
205 * Tests that a single set of plugin config stays in sync.
207 * @param \Drupal\Core\Entity\EntityWithPluginCollectionInterface $entity
209 * @param string $config_key
210 * Where the plugin config is stored.
211 * @param string $setting_key
212 * The setting within the plugin config to change.
213 * @param mixed $expected
214 * The expected default value of the plugin config setting.
216 protected function checkSinglePluginConfigSync(EntityWithPluginCollectionInterface $entity, $config_key, $setting_key, $expected) {
217 $plugin_collection = $entity->getPluginCollections()[$config_key];
218 $settings = $entity->get($config_key);
220 // Ensure the default config exists.
221 $this->assertIdentical($expected, $settings[$setting_key]);
223 // Change the plugin config by setting it on the entity.
224 $settings[$setting_key] = $this->randomString();
225 $entity->set($config_key, $settings);
227 $this->assertIdentical($settings, $entity->get($config_key));
228 $this->assertIdentical($settings, $plugin_collection->getConfiguration());
230 // Change the plugin config by setting it on the plugin.
231 $settings[$setting_key] = $this->randomString();
232 $plugin_collection->setConfiguration($settings);
234 $this->assertIdentical($settings, $entity->get($config_key));
235 $this->assertIdentical($settings, $plugin_collection->getConfiguration());
239 * Asserts that config entities are updated during import.
241 * @param string $name
242 * The name of the config object.
243 * @param array $original_data
244 * The original data stored in the config object.
245 * @param array $custom_data
246 * The new data to store in the config object.
248 public function assertConfigUpdateImport($name, $original_data, $custom_data) {
249 $this->container->get('config.storage.sync')->write($name, $custom_data);
251 // Verify the active configuration still returns the default values.
252 $config = $this->config($name);
253 $this->assertIdentical($config->get(), $original_data);
256 $this->configImporter()->import();
258 // Verify the values were updated.
259 $this->container->get('config.factory')->reset($name);
260 $config = $this->config($name);
261 $this->assertIdentical($config->get(), $custom_data);