Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / system / tests / src / Functional / Entity / ConfigEntityImportTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Entity;
4
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;
11
12 /**
13  * Tests ConfigEntity importing.
14  *
15  * @group Entity
16  */
17 class ConfigEntityImportTest extends BrowserTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['action', 'block', 'filter', 'image', 'search', 'search_extra_type'];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync'));
33   }
34
35   /**
36    * Runs test methods for each module within a single test run.
37    */
38   public function testConfigUpdateImport() {
39     $this->doActionUpdate();
40     $this->doBlockUpdate();
41     $this->doFilterFormatUpdate();
42     $this->doImageStyleUpdate();
43     $this->doSearchPageUpdate();
44   }
45   /**
46    * Tests updating a action during import.
47    */
48   protected function doActionUpdate() {
49     // Create a test action with a known label.
50     $name = 'system.action.apple';
51     $entity = Action::create([
52       'id' => 'apple',
53       'plugin' => 'action_message_action',
54     ]);
55     $entity->save();
56
57     $this->checkSinglePluginConfigSync($entity, 'configuration', 'message', '');
58
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);
63
64   }
65
66   /**
67    * Tests updating a block during import.
68    */
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', [
73       'id' => 'apple',
74       'label' => 'Red Delicious',
75     ]);
76
77     $this->checkSinglePluginConfigSync($block, 'settings', 'label', 'Red Delicious');
78
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);
83   }
84
85   /**
86    * Tests updating a filter format during import.
87    */
88   protected function doFilterFormatUpdate() {
89     // Create a test filter format with a known label.
90     $name = 'filter.format.plain_text';
91
92     /** @var $entity \Drupal\filter\Entity\FilterFormat */
93     $entity = FilterFormat::load('plain_text');
94     $plugin_collection = $entity->getPluginCollections()['filters'];
95
96     $filters = $entity->get('filters');
97     $this->assertIdentical(72, $filters['filter_url']['settings']['filter_url_length']);
98
99     $filters['filter_url']['settings']['filter_url_length'] = 100;
100     $entity->set('filters', $filters);
101     $entity->save();
102     $this->assertIdentical($filters, $entity->get('filters'));
103     $this->assertIdentical($filters, $plugin_collection->getConfiguration());
104
105     $filters['filter_url']['settings']['filter_url_length'] = -100;
106     $entity->getPluginCollections()['filters']->setConfiguration($filters);
107     $entity->save();
108     $this->assertIdentical($filters, $entity->get('filters'));
109     $this->assertIdentical($filters, $plugin_collection->getConfiguration());
110
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);
115   }
116
117   /**
118    * Tests updating an image style during import.
119    */
120   protected function doImageStyleUpdate() {
121     // Create a test image style with a known label.
122     $name = 'image.style.thumbnail';
123
124     /** @var $entity \Drupal\image\Entity\ImageStyle */
125     $entity = ImageStyle::load('thumbnail');
126     $plugin_collection = $entity->getPluginCollections()['effects'];
127
128     $effects = $entity->get('effects');
129     $effect_id = key($effects);
130     $this->assertIdentical(100, $effects[$effect_id]['data']['height']);
131
132     $effects[$effect_id]['data']['height'] = 50;
133     $entity->set('effects', $effects);
134     $entity->save();
135     // Ensure the entity and plugin have the correct configuration.
136     $this->assertIdentical($effects, $entity->get('effects'));
137     $this->assertIdentical($effects, $plugin_collection->getConfiguration());
138
139     $effects[$effect_id]['data']['height'] = -50;
140     $entity->getPluginCollections()['effects']->setConfiguration($effects);
141     $entity->save();
142     // Ensure the entity and plugin have the correct configuration.
143     $this->assertIdentical($effects, $entity->get('effects'));
144     $this->assertIdentical($effects, $plugin_collection->getConfiguration());
145
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']);
149
150     $custom_data['effects'][$effect_name]['data']['upscale'] = FALSE;
151     $this->assertConfigUpdateImport($name, $original_data, $custom_data);
152   }
153
154   /**
155    * Tests updating a search page during import.
156    */
157   protected function doSearchPageUpdate() {
158     // Create a test search page with a known label.
159     $name = 'search.page.apple';
160     $entity = SearchPage::create([
161       'id' => 'apple',
162       'plugin' => 'search_extra_type_search',
163     ]);
164     $entity->save();
165
166     $this->checkSinglePluginConfigSync($entity, 'configuration', 'boost', 'bi');
167
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);
172   }
173
174   /**
175    * Tests that a single set of plugin config stays in sync.
176    *
177    * @param \Drupal\Core\Entity\EntityWithPluginCollectionInterface $entity
178    *   The 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.
185    */
186   protected function checkSinglePluginConfigSync(EntityWithPluginCollectionInterface $entity, $config_key, $setting_key, $expected) {
187     $plugin_collection = $entity->getPluginCollections()[$config_key];
188     $settings = $entity->get($config_key);
189
190     // Ensure the default config exists.
191     $this->assertIdentical($expected, $settings[$setting_key]);
192
193     // Change the plugin config by setting it on the entity.
194     $settings[$setting_key] = $this->randomString();
195     $entity->set($config_key, $settings);
196     $entity->save();
197     $this->assertIdentical($settings, $entity->get($config_key));
198     $this->assertIdentical($settings, $plugin_collection->getConfiguration());
199
200     // Change the plugin config by setting it on the plugin.
201     $settings[$setting_key] = $this->randomString();
202     $plugin_collection->setConfiguration($settings);
203     $entity->save();
204     $this->assertIdentical($settings, $entity->get($config_key));
205     $this->assertIdentical($settings, $plugin_collection->getConfiguration());
206   }
207
208   /**
209    * Asserts that config entities are updated during import.
210    *
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.
217    */
218   public function assertConfigUpdateImport($name, $original_data, $custom_data) {
219     $this->container->get('config.storage.sync')->write($name, $custom_data);
220
221     // Verify the active configuration still returns the default values.
222     $config = $this->config($name);
223     $this->assertIdentical($config->get(), $original_data);
224
225     // Import.
226     $this->configImporter()->import();
227
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);
232   }
233
234 }