diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index 56603c7..547e415 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -24,7 +24,7 @@ * * @ingroup entity_api */ -abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface { +abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface, \Serializable { use PluginDependencyTrait { addDependency as addDependencyTrait; @@ -612,4 +612,43 @@ public function save() { return $return; } + /** + * {@inheritdoc} + */ + public function serialize() { + if ($this->getEntityType()->get('config_export')) { + $data = [ + 'entity_type_id' => $this->getEntityTypeId(), + 'is_new' => $this->isNew(), + 'values' => $this->toArray(), + ]; + return serialize(['__smart_serialize' => $data]); + } + else { + return serialize(get_object_vars($this)); + } + } + + /** + * {@inheritdoc} + */ + public function unserialize($serialized) { + $unserialized = unserialize($serialized); + // PHPUnit uses unserialize() on a made up string as a trick when bypassing + // the contructor on mock object creation. Make sure we only run on objects + // that have been serialized through our serialize() method. + if (is_array($unserialized)) { + if (isset($unserialized['__smart_serialize'])) { + $data = $unserialized['__smart_serialize']; + $this->__construct($data['values'], $data['entity_type_id']); + $this->enforceIsNew($data['is_new']); + } + else { + foreach ($unserialized as $name => $value) { + $this->$name = $value; + } + } + } + } + } diff --git a/core/lib/Drupal/Core/Field/FieldConfigBase.php b/core/lib/Drupal/Core/Field/FieldConfigBase.php index fa34ca3..8731359 100644 --- a/core/lib/Drupal/Core/Field/FieldConfigBase.php +++ b/core/lib/Drupal/Core/Field/FieldConfigBase.php @@ -419,21 +419,6 @@ public function getDefaultValue(FieldableEntityInterface $entity) { } /** - * Implements the magic __sleep() method. - * - * Using the Serialize interface and serialize() / unserialize() methods - * breaks entity forms in PHP 5.4. - * @todo Investigate in https://drupal.org/node/2074253. - */ - public function __sleep() { - // Only serialize necessary properties, excluding those that can be - // recalculated. - $properties = get_object_vars($this); - unset($properties['fieldStorage'], $properties['itemDefinition'], $properties['bundleRenameAllowed'], $properties['original']); - return array_keys($properties); - } - - /** * {@inheritdoc} */ public static function createFromItemType($item_type) { diff --git a/core/modules/field/src/Entity/FieldStorageConfig.php b/core/modules/field/src/Entity/FieldStorageConfig.php index 97db63f..ace5384 100644 --- a/core/modules/field/src/Entity/FieldStorageConfig.php +++ b/core/modules/field/src/Entity/FieldStorageConfig.php @@ -697,21 +697,6 @@ public function hasData() { } /** - * Implements the magic __sleep() method. - * - * Using the Serialize interface and serialize() / unserialize() methods - * breaks entity forms in PHP 5.4. - * @todo Investigate in https://drupal.org/node/2074253. - */ - public function __sleep() { - // Only serialize necessary properties, excluding those that can be - // recalculated. - $properties = get_object_vars($this); - unset($properties['schema'], $properties['propertyDefinitions'], $properties['original']); - return array_keys($properties); - } - - /** * {@inheritdoc} */ public function getConstraints() { diff --git a/core/modules/tour/tests/src/Unit/Entity/TourTest.php b/core/modules/tour/tests/src/Unit/Entity/TourTest.php index a6625c8..93526e9 100644 --- a/core/modules/tour/tests/src/Unit/Entity/TourTest.php +++ b/core/modules/tour/tests/src/Unit/Entity/TourTest.php @@ -6,6 +6,7 @@ namespace Drupal\Tests\tour\Unit\Entity; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; /** @@ -31,8 +32,17 @@ class TourTest extends UnitTestCase { * @dataProvider routeProvider */ public function testHasMatchingRoute($routes, $route_name, $route_params, $result) { - $tour = $this->getMockBuilder('\Drupal\tour\Entity\Tour') + // Tour::__construct() pulls the plugin.manager.tour.tip service from the + // container. Mock it and and place it there. + $tip_manager = $this->getMockBuilder('Drupal\tour\TipPluginManager') ->disableOriginalConstructor() + ->getMock(); + $container = new ContainerBuilder(); + $container->set('plugin.manager.tour.tip', $tip_manager); + \Drupal::setContainer($container); + + $tour = $this->getMockBuilder('\Drupal\tour\Entity\Tour') + ->setConstructorArgs(array(array(), 'tour')) ->setMethods(array('getRoutes')) ->getMock();