diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index 285d087..56603c7 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -272,17 +272,19 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) */ public function toArray() { $properties = array(); + /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */ $entity_type = $this->getEntityType(); - $config_name = $entity_type->getConfigPrefix() . '.' . $this->id(); $properties_to_export = $entity_type->getPropertiesToExport(); if (empty($properties_to_export)) { + $config_name = $entity_type->getConfigPrefix() . '.' . $this->id(); $definition = $this->getTypedConfig()->getDefinition($config_name); if (!isset($definition['mapping'])) { throw new SchemaIncompleteException(SafeMarkup::format('Incomplete or missing schema for @config_name', array('@config_name' => $config_name))); } $properties_to_export = array_combine(array_keys($definition['mapping']), array_keys($definition['mapping'])); } + $id_key = $entity_type->getKey('id'); foreach ($properties_to_export as $property_name => $export_name) { // Special handling for IDs so that computed compound IDs work. @@ -294,6 +296,7 @@ public function toArray() { $properties[$export_name] = $this->get($property_name); } } + if (empty($this->third_party_settings)) { unset($properties['third_party_settings']); } diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php index 618690d..f15eef5 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php @@ -74,7 +74,6 @@ public function __construct($definition) { $this->handlers += array( 'storage' => 'Drupal\Core\Config\Entity\ConfigEntityStorage', ); - } /** @@ -175,7 +174,7 @@ public function getPropertiesToExport() { 'langcode' => 'langcode', 'status' => 'status', 'dependencies' => 'dependencies', - 'third_party_settings' => 'third_party_settings' + 'third_party_settings' => 'third_party_settings', ]; foreach ($this->config_export as $property => $name) { if (is_numeric($property)) { diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php index c71a7e5..5e11828 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -476,6 +476,31 @@ public function testToArray() { /** * @covers ::toArray */ + public function testToArrayIdKey() { + $entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', [[], $this->entityTypeId], '', TRUE, TRUE, TRUE, ['id', 'get']); + $entity->expects($this->exactly(3)) + ->method('id') + ->willReturn($this->id); + $entity->expects($this->once()) + ->method('get') + ->willReturn([]); + $this->typedConfigManager->expects($this->never()) + ->method('getDefinition'); + $this->entityType->expects($this->any()) + ->method('getPropertiesToExport') + ->willReturn(['id' => 'configId', 'dependencies' => 'dependencies']); + $this->entityType->expects($this->once()) + ->method('getKey') + ->with('id') + ->willReturn('id'); + $properties = $entity->toArray(); + $this->assertInternalType('array', $properties); + $this->assertEquals(['configId' => $entity->id(), 'dependencies' => []], $properties); + } + + /** + * @covers ::toArray + */ public function testToArraySchemaFallback() { $this->typedConfigManager->expects($this->once()) ->method('getDefinition') diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php index b427dfc..524fea0 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php @@ -131,4 +131,60 @@ public function providerTestGetConfigPrefix() { ); } + /** + * @covers ::getPropertiesToExport + * + * @dataProvider providerGetPropertiesToExport + */ + public function testGetPropertiesToExport($definition, $expected) { + $entity_type = $this->setUpConfigEntityType($definition); + $properties_to_export = $entity_type->getPropertiesToExport(); + $this->assertSame($expected, $properties_to_export); + + // Ensure the method is idempotent. + $properties_to_export = $entity_type->getPropertiesToExport(); + $this->assertSame($expected, $properties_to_export); + } + + public function providerGetPropertiesToExport() { + $data = []; + $data[] = [ + [], + NULL, + ]; + + $data[] = [ + [ + 'config_export' => [ + 'id', + 'custom_property' => 'customProperty', + ], + ], + [ + 'uuid' => 'uuid', + 'langcode' => 'langcode', + 'status' => 'status', + 'dependencies' => 'dependencies', + 'third_party_settings' => 'third_party_settings', + 'id' => 'id', + 'custom_property' => 'customProperty', + ], + ]; + + $data[] = [ + [ + 'config_export' => [ + 'id', + ], + 'mergedConfigExport' => [ + 'random_key' => 'random_key', + ], + ], + [ + 'random_key' => 'random_key', + ], + ]; + return $data; + } + }