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'];
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();
46 * Tests updating a action during import.
48 protected function doActionUpdate() {
49 // Create a test action with a known label.
50 $name = 'system.action.apple';
51 $entity = Action::create([
53 'plugin' => 'action_message_action',
57 $this->checkSinglePluginConfigSync($entity, 'configuration', 'message', '');
59 // Read the existing data, and prepare an altered version in sync.
60 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
61 $custom_data['configuration']['message'] = 'Granny Smith';
62 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
67 * Tests updating a block during import.
69 protected function doBlockUpdate() {
70 // Create a test block with a known label.
71 $name = 'block.block.apple';
72 $block = $this->drupalPlaceBlock('system_powered_by_block', [
74 'label' => 'Red Delicious',
77 $this->checkSinglePluginConfigSync($block, 'settings', 'label', 'Red Delicious');
79 // Read the existing data, and prepare an altered version in sync.
80 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
81 $custom_data['settings']['label'] = 'Granny Smith';
82 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
86 * Tests updating a filter format during import.
88 protected function doFilterFormatUpdate() {
89 // Create a test filter format with a known label.
90 $name = 'filter.format.plain_text';
92 /** @var $entity \Drupal\filter\Entity\FilterFormat */
93 $entity = FilterFormat::load('plain_text');
94 $plugin_collection = $entity->getPluginCollections()['filters'];
96 $filters = $entity->get('filters');
97 $this->assertIdentical(72, $filters['filter_url']['settings']['filter_url_length']);
99 $filters['filter_url']['settings']['filter_url_length'] = 100;
100 $entity->set('filters', $filters);
102 $this->assertIdentical($filters, $entity->get('filters'));
103 $this->assertIdentical($filters, $plugin_collection->getConfiguration());
105 $filters['filter_url']['settings']['filter_url_length'] = -100;
106 $entity->getPluginCollections()['filters']->setConfiguration($filters);
108 $this->assertIdentical($filters, $entity->get('filters'));
109 $this->assertIdentical($filters, $plugin_collection->getConfiguration());
111 // Read the existing data, and prepare an altered version in sync.
112 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
113 $custom_data['filters']['filter_url']['settings']['filter_url_length'] = 100;
114 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
118 * Tests updating an image style during import.
120 protected function doImageStyleUpdate() {
121 // Create a test image style with a known label.
122 $name = 'image.style.thumbnail';
124 /** @var $entity \Drupal\image\Entity\ImageStyle */
125 $entity = ImageStyle::load('thumbnail');
126 $plugin_collection = $entity->getPluginCollections()['effects'];
128 $effects = $entity->get('effects');
129 $effect_id = key($effects);
130 $this->assertIdentical(100, $effects[$effect_id]['data']['height']);
132 $effects[$effect_id]['data']['height'] = 50;
133 $entity->set('effects', $effects);
135 // Ensure the entity and plugin have the correct configuration.
136 $this->assertIdentical($effects, $entity->get('effects'));
137 $this->assertIdentical($effects, $plugin_collection->getConfiguration());
139 $effects[$effect_id]['data']['height'] = -50;
140 $entity->getPluginCollections()['effects']->setConfiguration($effects);
142 // Ensure the entity and plugin have the correct configuration.
143 $this->assertIdentical($effects, $entity->get('effects'));
144 $this->assertIdentical($effects, $plugin_collection->getConfiguration());
146 // Read the existing data, and prepare an altered version in sync.
147 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
148 $effect_name = key($original_data['effects']);
150 $custom_data['effects'][$effect_name]['data']['upscale'] = FALSE;
151 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
155 * Tests updating a search page during import.
157 protected function doSearchPageUpdate() {
158 // Create a test search page with a known label.
159 $name = 'search.page.apple';
160 $entity = SearchPage::create([
162 'plugin' => 'search_extra_type_search',
166 $this->checkSinglePluginConfigSync($entity, 'configuration', 'boost', 'bi');
168 // Read the existing data, and prepare an altered version in sync.
169 $custom_data = $original_data = $this->container->get('config.storage')->read($name);
170 $custom_data['configuration']['boost'] = 'asdf';
171 $this->assertConfigUpdateImport($name, $original_data, $custom_data);
175 * Tests that a single set of plugin config stays in sync.
177 * @param \Drupal\Core\Entity\EntityWithPluginCollectionInterface $entity
179 * @param string $config_key
180 * Where the plugin config is stored.
181 * @param string $setting_key
182 * The setting within the plugin config to change.
183 * @param mixed $expected
184 * The expected default value of the plugin config setting.
186 protected function checkSinglePluginConfigSync(EntityWithPluginCollectionInterface $entity, $config_key, $setting_key, $expected) {
187 $plugin_collection = $entity->getPluginCollections()[$config_key];
188 $settings = $entity->get($config_key);
190 // Ensure the default config exists.
191 $this->assertIdentical($expected, $settings[$setting_key]);
193 // Change the plugin config by setting it on the entity.
194 $settings[$setting_key] = $this->randomString();
195 $entity->set($config_key, $settings);
197 $this->assertIdentical($settings, $entity->get($config_key));
198 $this->assertIdentical($settings, $plugin_collection->getConfiguration());
200 // Change the plugin config by setting it on the plugin.
201 $settings[$setting_key] = $this->randomString();
202 $plugin_collection->setConfiguration($settings);
204 $this->assertIdentical($settings, $entity->get($config_key));
205 $this->assertIdentical($settings, $plugin_collection->getConfiguration());
209 * Asserts that config entities are updated during import.
211 * @param string $name
212 * The name of the config object.
213 * @param array $original_data
214 * The original data stored in the config object.
215 * @param array $custom_data
216 * The new data to store in the config object.
218 public function assertConfigUpdateImport($name, $original_data, $custom_data) {
219 $this->container->get('config.storage.sync')->write($name, $custom_data);
221 // Verify the active configuration still returns the default values.
222 $config = $this->config($name);
223 $this->assertIdentical($config->get(), $original_data);
226 $this->configImporter()->import();
228 // Verify the values were updated.
229 $this->container->get('config.factory')->reset($name);
230 $config = $this->config($name);
231 $this->assertIdentical($config->get(), $custom_data);