From 53068d078fbaff85de001d8051348da8cbbec314 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Fri, 28 Feb 2014 10:36:02 +0000 Subject: [PATCH] Issue #2191903 by damiankloip: Provide a ConfigEntityNormalizer. --- .../Normalizer/ConfigEntityNormalizer.php | 29 ++++++++++ .../serialization/serialization.services.yml | 5 ++ .../Tests/Normalizer/ConfigNormalizerTest.php | 53 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 core/modules/serialization/lib/Drupal/serialization/Normalizer/ConfigEntityNormalizer.php create mode 100644 core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/ConfigNormalizerTest.php diff --git a/core/modules/serialization/lib/Drupal/serialization/Normalizer/ConfigEntityNormalizer.php b/core/modules/serialization/lib/Drupal/serialization/Normalizer/ConfigEntityNormalizer.php new file mode 100644 index 000000000000..0879860eee2f --- /dev/null +++ b/core/modules/serialization/lib/Drupal/serialization/Normalizer/ConfigEntityNormalizer.php @@ -0,0 +1,29 @@ +<?php + +/** + * @file + * Contains \Drupal\serialization\Normalizer\ConfigEntityNormalizer. + */ + +namespace Drupal\serialization\Normalizer; + +/** + * Normalizes/denormalizes Drupal config entity objects into an array structure. + */ +class ConfigEntityNormalizer extends EntityNormalizer { + + /** + * The interface or class that this Normalizer supports. + * + * @var array + */ + protected $supportedInterfaceOrClass = array('Drupal\Core\Config\Entity\ConfigEntityInterface'); + + /** + * {@inheritdoc} + */ + public function normalize($object, $format = NULL, array $context = array()) { + return $object->getExportProperties(); + } + +} diff --git a/core/modules/serialization/serialization.services.yml b/core/modules/serialization/serialization.services.yml index a293a6bb4209..bf852fe386de 100644 --- a/core/modules/serialization/serialization.services.yml +++ b/core/modules/serialization/serialization.services.yml @@ -2,6 +2,11 @@ services: serializer: class: Symfony\Component\Serializer\Serializer arguments: [{ }, { }] + serializer.normalizer.config_entity: + class: Drupal\serialization\Normalizer\ConfigEntityNormalizer + tags: + - { name: normalizer } + arguments: ['@entity.manager'] serializer.normalizer.entity: class: Drupal\serialization\Normalizer\EntityNormalizer tags: diff --git a/core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/ConfigNormalizerTest.php b/core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/ConfigNormalizerTest.php new file mode 100644 index 000000000000..1d90156d703a --- /dev/null +++ b/core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/ConfigNormalizerTest.php @@ -0,0 +1,53 @@ +<?php + +/** + * @file + * Contains \Drupal\serialization\Tests\Normalizer\ConfigNormalizerTest. + */ + +namespace Drupal\serialization\Tests\Normalizer; + +use Drupal\serialization\Normalizer\ConfigEntityNormalizer; +use Drupal\Tests\UnitTestCase; + +/** + * Tests the ConfigEntityNormalizer class. + * + * @group Serialization + * + * @coversDefaultClass \Drupal\serialization\Normalizer\ConfigEntityNormalizer + */ +class ConfigEntityNormalizerTest extends UnitTestCase { + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'ConfigEntityNormalizer', + 'description' => 'Tests the ConfigEntityNormalizer class.', + 'group' => 'Serialization', + ); + } + + /** + * Tests the normalize() method. + * + * @covers ::normalize + */ + public function testNormalize() { + $test_export_properties = array('test' => 'test'); + + $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); + $normalizer = new ConfigEntityNormalizer($entity_manager); + + $config_entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface'); + $config_entity->expects($this->once()) + ->method('getExportProperties') + ->will($this->returnValue($test_export_properties)); + + $this->assertSame($test_export_properties, $normalizer->normalize($config_entity)); + } + +} + -- GitLab