View source
<?php
namespace Drupal\Tests\system\Kernel\Entity;
use Drupal\Component\Uuid\Php;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\filter\Entity\FilterFormat;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search\Entity\SearchPage;
use Drupal\system\Entity\Action;
use Drupal\Tests\block\Traits\BlockCreationTrait;
class ConfigEntityImportTest extends KernelTestBase {
use BlockCreationTrait;
protected static $modules = [
'action',
'block',
'config_test',
'filter',
'image',
'search',
'search_extra_type',
'system',
];
public function testConfigUpdateImport() {
$this
->installConfig([
'action',
'block',
'filter',
'image',
]);
$this->container
->get('theme_installer')
->install([
'bartik',
]);
$config_storage = $this->container
->get('config.storage');
$config_storage
->write('system.site', [
'uuid' => (new Php())
->generate(),
]);
$this
->copyConfig($config_storage, $this->container
->get('config.storage.sync'));
$this
->doActionUpdate();
$this
->doBlockUpdate();
$this
->doFilterFormatUpdate();
$this
->doImageStyleUpdate();
$this
->doSearchPageUpdate();
$this
->doThirdPartySettingsUpdate();
}
protected function doActionUpdate() {
$name = 'system.action.apple';
$entity = Action::create([
'id' => 'apple',
'plugin' => 'action_message_action',
]);
$entity
->save();
$this
->checkSinglePluginConfigSync($entity, 'configuration', 'message', '');
$custom_data = $original_data = $this->container
->get('config.storage')
->read($name);
$custom_data['configuration']['message'] = 'Granny Smith';
$this
->assertConfigUpdateImport($name, $original_data, $custom_data);
}
protected function doBlockUpdate() {
$name = 'block.block.apple';
$block = $this
->placeBlock('system_powered_by_block', [
'id' => 'apple',
'label' => 'Red Delicious',
'theme' => 'bartik',
]);
$this
->checkSinglePluginConfigSync($block, 'settings', 'label', 'Red Delicious');
$custom_data = $original_data = $this->container
->get('config.storage')
->read($name);
$custom_data['settings']['label'] = 'Granny Smith';
$this
->assertConfigUpdateImport($name, $original_data, $custom_data);
}
protected function doFilterFormatUpdate() {
$name = 'filter.format.plain_text';
$entity = FilterFormat::load('plain_text');
$plugin_collection = $entity
->getPluginCollections()['filters'];
$filters = $entity
->get('filters');
$this
->assertSame(72, $filters['filter_url']['settings']['filter_url_length']);
$filters['filter_url']['settings']['filter_url_length'] = 100;
$entity
->set('filters', $filters);
$entity
->save();
$this
->assertSame($filters, $entity
->get('filters'));
$this
->assertSame($filters, $plugin_collection
->getConfiguration());
$filters['filter_url']['settings']['filter_url_length'] = -100;
$entity
->getPluginCollections()['filters']
->setConfiguration($filters);
$entity
->save();
$this
->assertSame($filters, $entity
->get('filters'));
$this
->assertSame($filters, $plugin_collection
->getConfiguration());
$custom_data = $original_data = $this->container
->get('config.storage')
->read($name);
$custom_data['filters']['filter_url']['settings']['filter_url_length'] = 100;
$this
->assertConfigUpdateImport($name, $original_data, $custom_data);
}
protected function doImageStyleUpdate() {
$name = 'image.style.thumbnail';
$entity = ImageStyle::load('thumbnail');
$plugin_collection = $entity
->getPluginCollections()['effects'];
$effects = $entity
->get('effects');
$effect_id = key($effects);
$this
->assertSame(100, $effects[$effect_id]['data']['height']);
$effects[$effect_id]['data']['height'] = 50;
$entity
->set('effects', $effects);
$entity
->save();
$this
->assertSame($effects, $entity
->get('effects'));
$this
->assertSame($effects, $plugin_collection
->getConfiguration());
$effects[$effect_id]['data']['height'] = -50;
$entity
->getPluginCollections()['effects']
->setConfiguration($effects);
$entity
->save();
$this
->assertSame($effects, $entity
->get('effects'));
$this
->assertSame($effects, $plugin_collection
->getConfiguration());
$custom_data = $original_data = $this->container
->get('config.storage')
->read($name);
$effect_name = key($original_data['effects']);
$custom_data['effects'][$effect_name]['data']['upscale'] = FALSE;
$this
->assertConfigUpdateImport($name, $original_data, $custom_data);
}
protected function doSearchPageUpdate() {
$name = 'search.page.apple';
$entity = SearchPage::create([
'id' => 'apple',
'plugin' => 'search_extra_type_search',
]);
$entity
->save();
$this
->checkSinglePluginConfigSync($entity, 'configuration', 'boost', 'bi');
$custom_data = $original_data = $this->container
->get('config.storage')
->read($name);
$custom_data['configuration']['boost'] = 'asdf';
$this
->assertConfigUpdateImport($name, $original_data, $custom_data);
}
protected function doThirdPartySettingsUpdate() {
$name = 'system.action.third_party_settings_test';
$entity = Action::create([
'id' => 'third_party_settings_test',
'plugin' => 'action_message_action',
]);
$entity
->save();
$this
->assertSame([], $entity
->getThirdPartyProviders());
$no_third_part_setting_config = $this->container
->get('config.storage')
->read($name);
$entity
->setThirdPartySetting('config_test', 'integer', 1);
$entity
->save();
$this
->assertSame(1, $entity
->getThirdPartySetting('config_test', 'integer'));
$has_third_part_setting_config = $this->container
->get('config.storage')
->read($name);
$this
->assertConfigUpdateImport($name, $has_third_part_setting_config, $no_third_part_setting_config);
}
protected function checkSinglePluginConfigSync(EntityWithPluginCollectionInterface $entity, $config_key, $setting_key, $expected) {
$plugin_collection = $entity
->getPluginCollections()[$config_key];
$settings = $entity
->get($config_key);
$this
->assertSame($expected, $settings[$setting_key]);
$settings[$setting_key] = $this
->randomString();
$entity
->set($config_key, $settings);
$entity
->save();
$this
->assertSame($settings, $entity
->get($config_key));
$this
->assertSame($settings, $plugin_collection
->getConfiguration());
$settings[$setting_key] = $this
->randomString();
$plugin_collection
->setConfiguration($settings);
$entity
->save();
$this
->assertSame($settings, $entity
->get($config_key));
$this
->assertSame($settings, $plugin_collection
->getConfiguration());
}
public function assertConfigUpdateImport($name, $original_data, $custom_data) {
$this->container
->get('config.storage.sync')
->write($name, $custom_data);
$config = $this
->config($name);
$this
->assertSame($config
->get(), $original_data);
$this
->configImporter()
->import();
$this->container
->get('config.factory')
->reset($name);
$config = $this
->config($name);
$this
->assertSame($config
->get(), $custom_data);
}
}