summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorAlex Pott2019-01-22 10:21:20 (GMT)
committerAlex Pott2019-01-22 10:21:27 (GMT)
commitda56eaa0ab405ac579a595948b0404e187f7bfad (patch)
tree28e520d51c7364e82b7c35dfacc38103206d3ca9 /core
parent1aa356a198d7af1a777cda28144e78ecbdee09d3 (diff)
Issue #3026043 by Berdir: ConfigEntityBase::__sleep() serializes plugin instances if they were not previously initialized
(cherry picked from commit 5fbbbde83dece039818555cded8a55e4c7bd456e)
Diffstat (limited to 'core')
-rw-r--r--core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php5
-rw-r--r--core/modules/filter/tests/src/Kernel/FilterAPITest.php12
-rw-r--r--core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php20
3 files changed, 33 insertions, 4 deletions
diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
index 4efcade..5c30435 100644
--- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
+++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
@@ -348,8 +348,11 @@ abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface
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,
diff --git a/core/modules/filter/tests/src/Kernel/FilterAPITest.php b/core/modules/filter/tests/src/Kernel/FilterAPITest.php
index a26005d..8ce4fa9 100644
--- a/core/modules/filter/tests/src/Kernel/FilterAPITest.php
+++ b/core/modules/filter/tests/src/Kernel/FilterAPITest.php
@@ -509,4 +509,16 @@ class FilterAPITest extends EntityKernelTestBase {
$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);
+ }
+
}
diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php
index ebb41d6..0160f69 100644
--- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php
+++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php
@@ -322,6 +322,12 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
$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 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
$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];
}