diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index 4968087..354d282 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -400,10 +400,12 @@ field_config_base: core.base_field_override.*.*.*: type: field_config_base + config_entity_type: base_field_override label: 'Base field bundle override' core.date_format.*: type: config_entity + config_entity_type: date_format label: 'Date format' mapping: id: diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index 90813e5..e4dbfae 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -2,6 +2,7 @@ core.entity_view_mode.*.*: type: config_entity + config_entity_type: entity_view_mode label: 'Entity view mode settings' mapping: id: @@ -19,6 +20,7 @@ core.entity_view_mode.*.*: core.entity_form_mode.*.*: type: config_entity + config_entity_type: entity_form_mode label: 'Entity form mode settings' mapping: id: @@ -37,6 +39,7 @@ core.entity_form_mode.*.*: # Overview configuration information for view mode or form mode displays. core.entity_view_display.*.*.*: type: config_entity + config_entity_type: entity_view_display label: 'Entity display' mapping: id: @@ -85,6 +88,7 @@ core.entity_view_display.*.*.*: # Overview configuration information for form mode displays. core.entity_form_display.*.*.*: type: config_entity + config_entity_type: entity_form_display label: 'Entity form display' mapping: id: diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index 56603c7..fd0bbf7 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -10,7 +10,6 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\Cache; use Drupal\Core\Config\ConfigException; -use Drupal\Core\Config\Schema\SchemaIncompleteException; use Drupal\Core\Entity\Entity; use Drupal\Core\Config\ConfigDuplicateUUIDException; use Drupal\Core\Entity\EntityStorageInterface; @@ -276,14 +275,6 @@ public function toArray() { $entity_type = $this->getEntityType(); $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) { @@ -304,15 +295,6 @@ public function toArray() { } /** - * Gets the typed config manager. - * - * @return \Drupal\Core\Config\TypedConfigManagerInterface - */ - protected function getTypedConfig() { - return \Drupal::service('config.typed'); - } - - /** * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php index f15eef5..7e8734d 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Config\Entity; use Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException; +use Drupal\Core\Config\Schema\SchemaIncompleteException; use Drupal\Core\Entity\EntityType; use Drupal\Core\Config\ConfigPrefixLengthException; use Drupal\Component\Utility\SafeMarkup; @@ -166,8 +167,8 @@ protected function checkStorageClass($class) { * {@inheritdoc} */ public function getPropertiesToExport() { - if (!empty($this->config_export)) { - if (empty($this->mergedConfigExport)) { + if (empty($this->mergedConfigExport)) { + if (!empty($this->config_export)) { // Always add default properties to be exported. $this->mergedConfigExport = [ 'uuid' => 'uuid', @@ -185,9 +186,24 @@ public function getPropertiesToExport() { } } } - return $this->mergedConfigExport; + else { + $definition = $this->getTypedConfig()->getDefinitionForEntityType($this->id()); + if (!isset($definition['mapping'])) { + throw new SchemaIncompleteException(SafeMarkup::format('Incomplete or missing schema for @entity_type_id', ['@entity_type_id' => $this->id()])); + } + $this->mergedConfigExport = array_combine(array_keys($definition['mapping']), array_keys($definition['mapping'])); + } } - return NULL; + return $this->mergedConfigExport; + } + + /** + * Gets the typed config manager. + * + * @return \Drupal\Core\Config\TypedConfigManagerInterface + */ + protected function getTypedConfig() { + return \Drupal::service('config.typed'); } } diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityTypeInterface.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityTypeInterface.php index 0f26ae0..586465a 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityTypeInterface.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityTypeInterface.php @@ -64,9 +64,8 @@ public function getConfigPrefix(); /** * Gets the config entity properties to export if declared on the annotation. * - * @return array|NULL - * The properties to export or NULL if they can not be determine from the - * config entity type annotation. + * @return array + * The properties to export or an empty array if they can not be determined. */ public function getPropertiesToExport(); diff --git a/core/lib/Drupal/Core/Config/TypedConfigManager.php b/core/lib/Drupal/Core/Config/TypedConfigManager.php index 42756d6..e31eab6 100644 --- a/core/lib/Drupal/Core/Config/TypedConfigManager.php +++ b/core/lib/Drupal/Core/Config/TypedConfigManager.php @@ -43,6 +43,15 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI protected $definitions; /** + * The array of plugin definitions that correspond to entity types. + * + * This array is keyed by entity type ID, not by plugin ID. + * + * @var array + */ + protected $entityTypeDefinitions = []; + + /** * Creates a new typed configuration manager. * * @param \Drupal\Core\Config\StorageInterface $configStorage @@ -159,8 +168,26 @@ public function getDefinition($base_plugin_id, $exception_on_invalid = TRUE, $is /** * {@inheritdoc} */ + public function getDefinitionForEntityType($entity_type_id) { + if (!isset($this->entityTypeDefinitions[$entity_type_id])) { + $id = 'undefined'; + foreach ($this->getDefinitions() as $definition_id => $definition) { + if (isset($definition['config_entity_type']) && $definition['config_entity_type'] === $entity_type_id) { + $id = $definition_id; + break; + } + } + $this->entityTypeDefinitions[$entity_type_id] = $this->getDefinition($id); + } + return $this->entityTypeDefinitions[$entity_type_id]; + } + + /** + * {@inheritdoc} + */ public function clearCachedDefinitions() { $this->schemaStorage->reset(); + $this->entityTypeDefinitions = []; parent::clearCachedDefinitions(); } diff --git a/core/lib/Drupal/Core/Config/TypedConfigManagerInterface.php b/core/lib/Drupal/Core/Config/TypedConfigManagerInterface.php index e9e562e..7aa1d33 100644 --- a/core/lib/Drupal/Core/Config/TypedConfigManagerInterface.php +++ b/core/lib/Drupal/Core/Config/TypedConfigManagerInterface.php @@ -122,4 +122,17 @@ public function hasConfigSchema($name); */ public function getDefinition($plugin_id, $exception_on_invalid = TRUE, $is_config_name = FALSE); + /** + * Gets the plugin definition for a specific entity type. + * + * @param string $entity_type_id + * The unique identifier of the entity type. + * + * @return array + * A plugin definition array. If the given entity type ID does not have + * typed configuration definition assigned, the definition of an undefined + * element type is returned. + */ + public function getDefinitionForEntityType($entity_type_id); + } diff --git a/core/modules/block/config/schema/block.schema.yml b/core/modules/block/config/schema/block.schema.yml index 16d79bc..619ca1a 100644 --- a/core/modules/block/config/schema/block.schema.yml +++ b/core/modules/block/config/schema/block.schema.yml @@ -2,6 +2,7 @@ block.block.*: type: config_entity + config_entity_type: block label: 'Block' mapping: id: diff --git a/core/modules/block_content/config/schema/block_content.schema.yml b/core/modules/block_content/config/schema/block_content.schema.yml index 2e7c338..b50cf97 100644 --- a/core/modules/block_content/config/schema/block_content.schema.yml +++ b/core/modules/block_content/config/schema/block_content.schema.yml @@ -2,6 +2,7 @@ block_content.type.*: type: config_entity + config_entity_type: block_content label: 'Custom block type settings' mapping: id: diff --git a/core/modules/comment/config/schema/comment.schema.yml b/core/modules/comment/config/schema/comment.schema.yml index d9f5a77..37b9445 100644 --- a/core/modules/comment/config/schema/comment.schema.yml +++ b/core/modules/comment/config/schema/comment.schema.yml @@ -37,6 +37,7 @@ action.configuration.comment_unpublish_action: comment.type.*: type: config_entity + config_entity_type: comment_type label: 'Comment type settings' mapping: id: diff --git a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml index 6cad91b..c00a3be 100644 --- a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml +++ b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml @@ -2,6 +2,7 @@ config_test_dynamic: type: config_entity + config_entity_type: config_test mapping: id: type: string @@ -35,6 +36,7 @@ config_test.dynamic.*.*: config_test.query.*: type: config_entity + config_entity_type: config_query_test mapping: id: type: string diff --git a/core/modules/contact/config/schema/contact.schema.yml b/core/modules/contact/config/schema/contact.schema.yml index 5a62e9d..52b17e3 100644 --- a/core/modules/contact/config/schema/contact.schema.yml +++ b/core/modules/contact/config/schema/contact.schema.yml @@ -2,6 +2,7 @@ contact.form.*: type: config_entity + config_entity_type: contact_form label: 'Contact form' mapping: id: diff --git a/core/modules/editor/config/schema/editor.schema.yml b/core/modules/editor/config/schema/editor.schema.yml index 80a01f9..7a19628 100644 --- a/core/modules/editor/config/schema/editor.schema.yml +++ b/core/modules/editor/config/schema/editor.schema.yml @@ -2,6 +2,7 @@ editor.editor.*: type: config_entity + config_entity_type: editor label: 'Text editor' mapping: format: diff --git a/core/modules/field/config/schema/field.schema.yml b/core/modules/field/config/schema/field.schema.yml index 55760a7..440abea 100644 --- a/core/modules/field/config/schema/field.schema.yml +++ b/core/modules/field/config/schema/field.schema.yml @@ -10,6 +10,7 @@ field.settings: field.storage.*.*: type: config_entity + config_entity_type: field_storage_config label: 'Field' mapping: id: @@ -53,4 +54,5 @@ field.storage.*.*: field.field.*.*.*: type: field_config_base + config_entity_type: field_config label: 'Field' diff --git a/core/modules/filter/config/schema/filter.schema.yml b/core/modules/filter/config/schema/filter.schema.yml index b45a475..8e184d2 100644 --- a/core/modules/filter/config/schema/filter.schema.yml +++ b/core/modules/filter/config/schema/filter.schema.yml @@ -13,6 +13,7 @@ filter.settings: filter.format.*: type: config_entity + config_entity_type: filter_format label: 'Text formats' mapping: name: diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index 4949fa3..d26fd12 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -2,6 +2,7 @@ image.style.*: type: config_entity + config_entity_type: image_style label: 'Image style' mapping: name: diff --git a/core/modules/language/config/schema/language.schema.yml b/core/modules/language/config/schema/language.schema.yml index 5e3f55b..767d47b 100644 --- a/core/modules/language/config/schema/language.schema.yml +++ b/core/modules/language/config/schema/language.schema.yml @@ -86,6 +86,7 @@ language.mappings: language.entity.*: type: config_entity + config_entity_type: configurable_language label: 'Language' mapping: id: @@ -106,6 +107,7 @@ language.entity.*: language.content_settings.*.*: type: config_entity + config_entity_type: language_content_settings label: 'Content Language Settings' mapping: id: diff --git a/core/modules/migrate/config/schema/migrate.schema.yml b/core/modules/migrate/config/schema/migrate.schema.yml index c48bdd1..4e39577 100644 --- a/core/modules/migrate/config/schema/migrate.schema.yml +++ b/core/modules/migrate/config/schema/migrate.schema.yml @@ -2,6 +2,7 @@ migrate.migration.*: type: config_entity + config_entity_type: migration label: 'Migration' mapping: id: diff --git a/core/modules/node/config/schema/node.schema.yml b/core/modules/node/config/schema/node.schema.yml index dbc8ac9..5fb8fdc 100644 --- a/core/modules/node/config/schema/node.schema.yml +++ b/core/modules/node/config/schema/node.schema.yml @@ -10,6 +10,7 @@ node.settings: node.type.*: type: config_entity + config_entity_type: node_type label: 'Content type' mapping: name: diff --git a/core/modules/rdf/config/schema/rdf.schema.yml b/core/modules/rdf/config/schema/rdf.schema.yml index a331382..27d8869 100644 --- a/core/modules/rdf/config/schema/rdf.schema.yml +++ b/core/modules/rdf/config/schema/rdf.schema.yml @@ -2,6 +2,7 @@ rdf.mapping.*.*: type: config_entity + config_entity_type: rdf_mapping label: 'RDF mapping' mapping: id: diff --git a/core/modules/responsive_image/config/schema/responsive_image.schema.yml b/core/modules/responsive_image/config/schema/responsive_image.schema.yml index f05a8e2..f4d9ec8 100644 --- a/core/modules/responsive_image/config/schema/responsive_image.schema.yml +++ b/core/modules/responsive_image/config/schema/responsive_image.schema.yml @@ -2,6 +2,7 @@ responsive_image.styles.*: type: config_entity + config_entity_type: responsive_image_style label: 'Responsive image style' mapping: id: diff --git a/core/modules/search/config/schema/search.schema.yml b/core/modules/search/config/schema/search.schema.yml index 2ed4c09..916186e 100644 --- a/core/modules/search/config/schema/search.schema.yml +++ b/core/modules/search/config/schema/search.schema.yml @@ -69,6 +69,7 @@ search.settings: search.page.*: type: config_entity + config_entity_type: search_page label: 'Search page' mapping: id: diff --git a/core/modules/shortcut/config/schema/shortcut.schema.yml b/core/modules/shortcut/config/schema/shortcut.schema.yml index 7d9420b..6d9b338 100644 --- a/core/modules/shortcut/config/schema/shortcut.schema.yml +++ b/core/modules/shortcut/config/schema/shortcut.schema.yml @@ -2,6 +2,7 @@ shortcut.set.*: type: config_entity + config_entity_type: shortcut_set label: 'Shortcut settings' mapping: id: diff --git a/core/modules/system/config/schema/system.schema.yml b/core/modules/system/config/schema/system.schema.yml index 7cde50d..5da1d6a 100644 --- a/core/modules/system/config/schema/system.schema.yml +++ b/core/modules/system/config/schema/system.schema.yml @@ -242,6 +242,7 @@ system.theme: system.menu.*: type: config_entity + config_entity_type: menu label: 'Menu' mapping: id: @@ -259,6 +260,7 @@ system.menu.*: system.action.*: type: config_entity + config_entity_type: action label: 'System action' mapping: id: diff --git a/core/modules/taxonomy/config/schema/taxonomy.schema.yml b/core/modules/taxonomy/config/schema/taxonomy.schema.yml index efa08e1..81d3ce6 100644 --- a/core/modules/taxonomy/config/schema/taxonomy.schema.yml +++ b/core/modules/taxonomy/config/schema/taxonomy.schema.yml @@ -16,6 +16,7 @@ taxonomy.settings: taxonomy.vocabulary.*: type: config_entity + config_entity_type: vocabulary label: 'Vocabulary' mapping: name: diff --git a/core/modules/tour/config/schema/tour.schema.yml b/core/modules/tour/config/schema/tour.schema.yml index caf7363..299d83e 100644 --- a/core/modules/tour/config/schema/tour.schema.yml +++ b/core/modules/tour/config/schema/tour.schema.yml @@ -2,6 +2,7 @@ tour.tour.*: type: config_entity + config_entity_type: tour label: 'Tour settings' mapping: id: diff --git a/core/modules/user/config/schema/user.schema.yml b/core/modules/user/config/schema/user.schema.yml index 627d8a6..6e57fb8 100644 --- a/core/modules/user/config/schema/user.schema.yml +++ b/core/modules/user/config/schema/user.schema.yml @@ -105,6 +105,7 @@ user.flood: user.role.*: type: config_entity + config_entity_type: role label: 'User role settings' mapping: id: diff --git a/core/modules/views/config/schema/views.schema.yml b/core/modules/views/config/schema/views.schema.yml index 7ea599d..97403ee 100644 --- a/core/modules/views/config/schema/views.schema.yml +++ b/core/modules/views/config/schema/views.schema.yml @@ -67,6 +67,7 @@ views.settings: views.view.*: type: config_entity + config_entity_type: view label: 'View' mapping: id: diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php index b654077..f120c68 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -82,13 +82,6 @@ class ConfigEntityBaseUnitTest extends UnitTestCase { protected $cacheTagsInvalidator; /** - * The mocked typed config manager. - * - * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $typedConfigManager; - - /** * {@inheritdoc} */ protected function setUp() { @@ -124,14 +117,11 @@ protected function setUp() { $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); - $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); - $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); $container->set('uuid', $this->uuid); $container->set('language_manager', $this->languageManager); $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); - $container->set('config.typed', $this->typedConfigManager); \Drupal::setContainer($container); $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', array($values, $this->entityTypeId)); @@ -463,8 +453,6 @@ public function testSort() { * @covers ::toArray */ public function testToArray() { - $this->typedConfigManager->expects($this->never()) - ->method('getDefinition'); $this->entityType->expects($this->any()) ->method('getPropertiesToExport') ->willReturn(['id' => 'configId', 'dependencies' => 'dependencies']); @@ -485,8 +473,6 @@ public function testToArrayIdKey() { ->method('get') ->with('dependencies') ->willReturn([]); - $this->typedConfigManager->expects($this->never()) - ->method('getDefinition'); $this->entityType->expects($this->any()) ->method('getPropertiesToExport') ->willReturn(['id' => 'configId', 'dependencies' => 'dependencies']); @@ -500,33 +486,6 @@ public function testToArrayIdKey() { } /** - * @covers ::toArray - */ - public function testToArraySchemaFallback() { - $this->typedConfigManager->expects($this->once()) - ->method('getDefinition') - ->will($this->returnValue(array('mapping' => array('id' => '', 'dependencies' => '')))); - $this->entityType->expects($this->any()) - ->method('getPropertiesToExport') - ->willReturn([]); - $properties = $this->entity->toArray(); - $this->assertInternalType('array', $properties); - $this->assertEquals(array('id' => $this->entity->id(), 'dependencies' => array()), $properties); - } - - /** - * @covers ::toArray - * - * @expectedException \Drupal\Core\Config\Schema\SchemaIncompleteException - */ - public function testToArrayFallback() { - $this->entityType->expects($this->any()) - ->method('getPropertiesToExport') - ->willReturn([]); - $this->entity->toArray(); - } - - /** * @covers ::getThirdPartySetting * @covers ::setThirdPartySetting * @covers ::getThirdPartySettings diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php index c4413eb..8707671 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php @@ -90,13 +90,6 @@ class ConfigEntityStorageTest extends UnitTestCase { protected $cacheTagsInvalidator; /** - * The mocked typed config manager. - * - * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $typedConfigManager; - - /** * The configuration manager. * * @var \Drupal\Core\Config\ConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject @@ -132,6 +125,13 @@ protected function setUp() { $this->entityType->expects($this->any()) ->method('getListCacheTags') ->willReturn(array('test_entity_type_list')); + $this->entityType->expects($this->any()) + ->method('getPropertiesToExport') + ->willReturn([ + 'id' => 'id', + 'uuid' => 'uuid', + 'dependencies' => 'dependencies', + ]); $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); @@ -163,16 +163,10 @@ protected function setUp() { $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); - $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); - $this->typedConfigManager->expects($this->any()) - ->method('getDefinition') - ->will($this->returnValue(array('mapping' => array('id' => '', 'uuid' => '', 'dependencies' => '')))); - $this->configManager = $this->getMock('Drupal\Core\Config\ConfigManagerInterface'); $container = new ContainerBuilder(); $container->set('entity.manager', $this->entityManager); - $container->set('config.typed', $this->typedConfigManager); $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); $container->set('config.manager', $this->configManager); $container->set('language_manager', $this->languageManager); diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php index 524fea0..1aea2f7 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Config\Entity; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; use Drupal\Core\Config\Entity\ConfigEntityType; use Drupal\Component\Utility\SafeMarkup; @@ -149,11 +150,6 @@ public function testGetPropertiesToExport($definition, $expected) { public function providerGetPropertiesToExport() { $data = []; $data[] = [ - [], - NULL, - ]; - - $data[] = [ [ 'config_export' => [ 'id', @@ -187,4 +183,56 @@ public function providerGetPropertiesToExport() { return $data; } + /** + * @covers ::getPropertiesToExport + */ + public function testGetPropertiesToExportFromSchema() { + $typed_config_manager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); + $typed_config_manager->expects($this->once()) + ->method('getDefinitionForEntityType') + ->with('example_config_entity_type') + ->willReturn(['mapping' => ['id' => '', 'dependencies' => '']]); + $container = new ContainerBuilder(); + $container->set('config.typed', $typed_config_manager); + \Drupal::setContainer($container); + + $expected = [ + 'id' => 'id', + 'dependencies' => 'dependencies', + ]; + $entity_type = $this->setUpConfigEntityType([]); + + $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); + } + + /** + * @covers ::getPropertiesToExport + * + * @expectedException \Drupal\Core\Config\Schema\SchemaIncompleteException + * @expectedExceptionMessage Incomplete or missing schema for example_config_entity_type + */ + public function testGetPropertiesToExportFromMissingSchema() { + $typed_config_manager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); + $typed_config_manager->expects($this->once()) + ->method('getDefinitionForEntityType') + ->willReturn([]); + $container = new ContainerBuilder(); + $container->set('config.typed', $typed_config_manager); + \Drupal::setContainer($container); + + $expected = []; + $entity_type = $this->setUpConfigEntityType([]); + $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); + } + } diff --git a/core/tests/Drupal/Tests/Core/Config/TypedConfigManagerTest.php b/core/tests/Drupal/Tests/Core/Config/TypedConfigManagerTest.php new file mode 100644 index 0000000..5432e2a --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/TypedConfigManagerTest.php @@ -0,0 +1,82 @@ + [ + 'label' => 'Undefined', + 'class' => '\Drupal\Core\Config\Schema\Undefined', + ], + 'core.valid.*' => [ + 'label' => 'Valid', + 'config_entity_type' => 'valid', + ], + 'core.invalid.*' => [ + 'label' => 'invalid', + ], + ]; + + $manager = $this->getMockBuilder('\Drupal\Core\Config\TypedConfigManager') + ->disableOriginalConstructor() + ->setMethods(['getDefinitions']) + ->getMock(); + + // Once for the initial loop and once from within getDefinition(). + $manager->expects($this->exactly(2)) + ->method('getDefinitions') + ->willReturn($definitions); + + $definition = $manager->getDefinitionForEntityType($entity_type_id); + $this->assertSame($expected, $definition); + + // Ensure this doesn't loop over getDefinitions() twice. + $definition = $manager->getDefinitionForEntityType($entity_type_id); + $this->assertSame($expected, $definition); + } + + /** + * Provides test data for testGetDefinitionForEntityType(). + */ + public function providerTestGetDefinitionForEntityType() { + $data = []; + $data[] = [ + 'valid', + [ + 'label' => 'Valid', + 'config_entity_type' => 'valid', + 'definition_class' => '\Drupal\Core\TypedData\DataDefinition', + 'type' => 'core.valid.*', + ], + ]; + $data[] = [ + 'invalid', + [ + 'label' => 'Undefined', + 'class' => '\Drupal\Core\Config\Schema\Undefined', + 'definition_class' => '\Drupal\Core\TypedData\DataDefinition', + 'type' => 'undefined', + ], + ]; + return $data; + } + +}