Unverified Commit 5fbbbde8 authored by alexpott's avatar alexpott

Issue #3026043 by Berdir: ConfigEntityBase::__sleep() serializes plugin...

Issue #3026043 by Berdir: ConfigEntityBase::__sleep() serializes plugin instances if they were not previously initialized
parent 67ed5a5a
......@@ -326,8 +326,11 @@ public function preSave(EntityStorageInterface $storage) {
public function __sleep() {
$keys_to_unset = [];
if ($this instanceof EntityWithPluginCollectionInterface) {
// Get the plugin collections first, so that the properties are
// initialized in $vars and can be found later.
$plugin_collections = $this->getPluginCollections();
$vars = get_object_vars($this);
foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) {
foreach ($plugin_collections as $plugin_config_key => $plugin_collection) {
// Save any changes to the plugin configuration to the entity.
$this->set($plugin_config_key, $plugin_collection->getConfiguration());
// If the plugin collections are stored as properties on the entity,
......
......@@ -509,4 +509,16 @@ public function testDependencyRemoval() {
$this->assertFalse(isset($filters['filter_test_restrict_tags_and_attributes']), 'The filter plugin filter_test_restrict_tags_and_attributes is not configured by the filtered_html filter format.');
}
/**
* Tests that format entities are serialized without their plugin collection.
*/
public function testSleep() {
$filter_format = FilterFormat::load('filtered_html');
$this->assertNull($filter_format->get('filterCollection'));
$vars = $filter_format->__sleep();
$this->assertContains('filters', $vars);
$this->assertNotContains('filterCollection', $vars);
}
}
......@@ -322,6 +322,12 @@ public function testSleepWithPluginCollections() {
$plugin_manager = $this->prophesize(PluginManagerInterface::class);
$plugin_manager->createInstance($instance_id, ['id' => $instance_id])->willReturn($instance);
// Also set up a container with the plugin manager so that we can assert
// that the plugin manager itself is also not serialized.
$container = new ContainerBuilder();
$container->set('plugin.manager.foo', $plugin_manager);
\Drupal::setContainer($container);
$entity_values = ['the_plugin_collection_config' => [$instance_id => ['foo' => 'original_value']]];
$entity = new TestConfigEntityWithPluginCollections($entity_values, $this->entityTypeId);
$entity->setPluginManager($plugin_manager->reveal());
......@@ -334,8 +340,11 @@ public function testSleepWithPluginCollections() {
$expected_plugin_config = [$instance_id => ['foo' => 'original_value']];
$this->assertSame($expected_plugin_config, $entity->get('the_plugin_collection_config'));
// Ensure the plugin collection is not stored.
$this->assertNotContains('pluginCollection', $entity->__sleep());
// Ensure the plugin collection and manager is not stored.
$vars = $entity->__sleep();
$this->assertNotContains('pluginCollection', $vars);
$this->assertNotContains('pluginManager', $vars);
$this->assertSame(['pluginManager' => 'plugin.manager.foo'], $entity->get('_serviceIds'));
$expected_plugin_config = [$instance_id => ['foo' => 'new_value']];
// Ensure the updated values are stored in the entity.
......@@ -603,14 +612,19 @@ class TestConfigEntityWithPluginCollections extends ConfigEntityBaseWithPluginCo
protected $pluginCollection;
protected $pluginManager;
public function setPluginManager(PluginManagerInterface $plugin_manager) {
$this->pluginCollection = new DefaultLazyPluginCollection($plugin_manager, ['the_instance_id' => ['id' => 'the_instance_id']]);
$this->pluginManager = $plugin_manager;
}
/**
* {@inheritdoc}
*/
public function getPluginCollections() {
if (!$this->pluginCollection) {
$this->pluginCollection = new DefaultLazyPluginCollection($this->pluginManager, ['the_instance_id' => ['id' => 'the_instance_id']]);
}
return ['the_plugin_collection_config' => $this->pluginCollection];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment