View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\simpletest\WebTestBase;
class ConfigEntityImportTest extends WebTestBase {
public static $modules = array(
'action',
'block',
'filter',
'image',
'search',
'search_extra_type',
);
protected function setUp() {
parent::setUp();
$this
->copyConfig($this->container
->get('config.storage'), $this->container
->get('config.storage.sync'));
}
public function testConfigUpdateImport() {
$this
->doActionUpdate();
$this
->doBlockUpdate();
$this
->doFilterFormatUpdate();
$this
->doImageStyleUpdate();
$this
->doSearchPageUpdate();
}
protected function doActionUpdate() {
$name = 'system.action.apple';
$entity = entity_create('action', array(
'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
->drupalPlaceBlock('system_powered_by_block', array(
'id' => 'apple',
'label' => 'Red Delicious',
));
$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 = entity_load('filter_format', 'plain_text');
$plugin_collection = $entity
->getPluginCollections()['filters'];
$filters = $entity
->get('filters');
$this
->assertIdentical(72, $filters['filter_url']['settings']['filter_url_length']);
$filters['filter_url']['settings']['filter_url_length'] = 100;
$entity
->set('filters', $filters);
$entity
->save();
$this
->assertIdentical($filters, $entity
->get('filters'));
$this
->assertIdentical($filters, $plugin_collection
->getConfiguration());
$filters['filter_url']['settings']['filter_url_length'] = -100;
$entity
->getPluginCollections()['filters']
->setConfiguration($filters);
$entity
->save();
$this
->assertIdentical($filters, $entity
->get('filters'));
$this
->assertIdentical($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
->assertIdentical(100, $effects[$effect_id]['data']['height']);
$effects[$effect_id]['data']['height'] = 50;
$entity
->set('effects', $effects);
$entity
->save();
$this
->assertIdentical($effects, $entity
->get('effects'));
$this
->assertIdentical($effects, $plugin_collection
->getConfiguration());
$effects[$effect_id]['data']['height'] = -50;
$entity
->getPluginCollections()['effects']
->setConfiguration($effects);
$entity
->save();
$this
->assertIdentical($effects, $entity
->get('effects'));
$this
->assertIdentical($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 = entity_create('search_page', array(
'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 checkSinglePluginConfigSync(EntityWithPluginCollectionInterface $entity, $config_key, $setting_key, $expected) {
$plugin_collection = $entity
->getPluginCollections()[$config_key];
$settings = $entity
->get($config_key);
$this
->assertIdentical($expected, $settings[$setting_key]);
$settings[$setting_key] = $this
->randomString();
$entity
->set($config_key, $settings);
$entity
->save();
$this
->assertIdentical($settings, $entity
->get($config_key));
$this
->assertIdentical($settings, $plugin_collection
->getConfiguration());
$settings[$setting_key] = $this
->randomString();
$plugin_collection
->setConfiguration($settings);
$entity
->save();
$this
->assertIdentical($settings, $entity
->get($config_key));
$this
->assertIdentical($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
->assertIdentical($config
->get(), $original_data);
$this
->configImporter()
->import();
$this->container
->get('config.factory')
->reset($name);
$config = $this
->config($name);
$this
->assertIdentical($config
->get(), $custom_data);
}
}