Commit 0c33183a authored by catch's avatar catch

Issue #2256679 by alexpott, tim.plunkett: Fixed Use config schema to determine...

Issue #2256679 by alexpott, tim.plunkett: Fixed Use config schema to determine which config entity properties to export regardless of whether they are public or protected.
parent 7d1c02a7
...@@ -235,15 +235,9 @@ config_dependencies: ...@@ -235,15 +235,9 @@ config_dependencies:
config_entity: config_entity:
type: mapping type: mapping
mapping: mapping:
id:
type: string
label: 'ID'
uuid: uuid:
type: string type: string
label: 'UUID' label: 'UUID'
label:
type: label
label: 'Label'
langcode: langcode:
type: string type: string
label: 'Default language' label: 'Default language'
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\Entity;
use Drupal\Core\Config\ConfigDuplicateUUIDException; use Drupal\Core\Config\ConfigDuplicateUUIDException;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
...@@ -240,19 +241,35 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) ...@@ -240,19 +241,35 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b)
* {@inheritdoc} * {@inheritdoc}
*/ */
public function toArray() { public function toArray() {
// Configuration objects do not have a schema. Extract all key names from
// class properties.
$class_info = new \ReflectionClass($this);
$properties = array(); $properties = array();
foreach ($class_info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { $config_name = $this->getEntityType()->getConfigPrefix() . '.' . $this->id();
$name = $property->getName(); $definition = $this->getTypedConfig()->getDefinition($config_name);
$properties[$name] = $this->get($name); if (!isset($definition['mapping'])) {
throw new SchemaIncompleteException(String::format('Incomplete or missing schema for @config_name', array('@config_name' => $config_name)));
}
$id_key = $this->getEntityType()->getKey('id');
foreach (array_keys($definition['mapping']) as $name) {
// Special handling for IDs so that computed compound IDs work.
// @see \Drupal\entity\EntityDisplayBase::id()
if ($name == $id_key) {
$properties[$name] = $this->id();
}
else {
$properties[$name] = $this->get($name);
}
} }
// Add protected dependencies property.
$properties['dependencies'] = $this->dependencies;
return $properties; return $properties;
} }
/**
* Gets the typed config manager.
*
* @return \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected function getTypedConfig() {
return \Drupal::service('config.typed');
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -4,6 +4,9 @@ block.block.*: ...@@ -4,6 +4,9 @@ block.block.*:
type: config_entity type: config_entity
label: 'Block' label: 'Block'
mapping: mapping:
id:
type: string
label: 'ID'
theme: theme:
type: string type: string
label: 'Theme' label: 'Theme'
......
...@@ -33,8 +33,7 @@ ...@@ -33,8 +33,7 @@
* admin_permission = "administer blocks", * admin_permission = "administer blocks",
* fieldable = FALSE, * fieldable = FALSE,
* entity_keys = { * entity_keys = {
* "id" = "id", * "id" = "id"
* "label" = "label"
* }, * },
* links = { * links = {
* "delete-form" = "block.admin_block_delete", * "delete-form" = "block.admin_block_delete",
...@@ -134,25 +133,6 @@ public function label() { ...@@ -134,25 +133,6 @@ public function label() {
} }
} }
/**
* {@inheritdoc}
*/
public function toArray() {
$properties = parent::toArray();
$names = array(
'theme',
'region',
'weight',
'plugin',
'settings',
'visibility',
);
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}
/** /**
* Sorts active blocks by weight; sorts inactive blocks by name. * Sorts active blocks by weight; sorts inactive blocks by name.
*/ */
......
...@@ -90,13 +90,15 @@ protected function createTests() { ...@@ -90,13 +90,15 @@ protected function createTests() {
// Ensure that default values are filled in. // Ensure that default values are filled in.
$expected_properties = array( $expected_properties = array(
'id' => 'test_block',
'weight' => NULL,
'status' => TRUE,
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id, 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id,
'status' => TRUE,
'dependencies' => array('module' => array('block_test'), 'theme' => array('stark')), 'dependencies' => array('module' => array('block_test'), 'theme' => array('stark')),
'id' => 'test_block',
'theme' => 'stark', 'theme' => 'stark',
'region' => '-1', 'region' => '-1',
'weight' => NULL,
'provider' => NULL,
'visibility' => NULL,
'plugin' => 'test_html', 'plugin' => 'test_html',
'settings' => array( 'settings' => array(
'id' => 'test_html', 'id' => 'test_html',
...@@ -108,8 +110,8 @@ protected function createTests() { ...@@ -108,8 +110,8 @@ protected function createTests() {
'contexts' => array(), 'contexts' => array(),
), ),
), ),
'visibility' => NULL,
); );
$this->assertIdentical($actual_properties, $expected_properties); $this->assertIdentical($actual_properties, $expected_properties);
$this->assertTrue($entity->getPlugin() instanceof TestHtmlBlock, 'The entity has an instance of the correct block plugin.'); $this->assertTrue($entity->getPlugin() instanceof TestHtmlBlock, 'The entity has an instance of the correct block plugin.');
......
...@@ -4,6 +4,12 @@ block_content.type.*: ...@@ -4,6 +4,12 @@ block_content.type.*:
type: config_entity type: config_entity
label: 'Custom block type settings' label: 'Custom block type settings'
mapping: mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
revision: revision:
type: integer type: integer
label: 'Create new revision' label: 'Create new revision'
......
...@@ -3,6 +3,12 @@ breakpoint.breakpoint.*.*.*: ...@@ -3,6 +3,12 @@ breakpoint.breakpoint.*.*.*:
type: config_entity type: config_entity
label: 'Defines the Breakpoint entity' label: 'Defines the Breakpoint entity'
mapping: mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
name: name:
type: string type: string
label: 'Machine name' label: 'Machine name'
...@@ -29,6 +35,12 @@ breakpoint.breakpoint_group.*.*.*: ...@@ -29,6 +35,12 @@ breakpoint.breakpoint_group.*.*.*:
type: config_entity type: config_entity
label: 'Breakpoint group settings' label: 'Breakpoint group settings'
mapping: mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
name: name:
type: string type: string
label: 'Machine name' label: 'Machine name'
......
...@@ -211,29 +211,6 @@ public function getBreakpointById($id) { ...@@ -211,29 +211,6 @@ public function getBreakpointById($id) {
return FALSE; return FALSE;
} }
/**
* {@inheritdoc}
*/
public function toArray() {
$names = array(
'id',
'uuid',
'name',
'label',
'breakpoint_ids',
'source',
'sourceType',
'status',
'langcode',
'dependencies',
);
$properties = array();
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -120,7 +120,7 @@ function comment_uri(CommentInterface $comment) { ...@@ -120,7 +120,7 @@ function comment_uri(CommentInterface $comment) {
function comment_entity_extra_field_info() { function comment_entity_extra_field_info() {
$return = array(); $return = array();
foreach (CommentType::loadMultiple() as $comment_type) { foreach (CommentType::loadMultiple() as $comment_type) {
$return['comment'][$comment_type->id] = array( $return['comment'][$comment_type->id()] = array(
'form' => array( 'form' => array(
'author' => array( 'author' => array(
'label' => t('Author'), 'label' => t('Author'),
......
...@@ -47,6 +47,12 @@ comment.type.*: ...@@ -47,6 +47,12 @@ comment.type.*:
type: config_entity type: config_entity
label: 'Comment type settings' label: 'Comment type settings'
mapping: mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
target_entity_type_id: target_entity_type_id:
type: string type: string
label: 'Target Entity Type ID' label: 'Target Entity Type ID'
......
...@@ -82,7 +82,7 @@ public function form(array $form, array &$form_state) { ...@@ -82,7 +82,7 @@ public function form(array $form, array &$form_state) {
$form['description'] = array( $form['description'] = array(
'#type' => 'textarea', '#type' => 'textarea',
'#default_value' => $comment_type->description, '#default_value' => $comment_type->getDescription(),
'#description' => t('Describe this comment type. The text will be displayed on the <em>Comment types</em> administration overview page'), '#description' => t('Describe this comment type. The text will be displayed on the <em>Comment types</em> administration overview page'),
'#title' => t('Description'), '#title' => t('Description'),
); );
......
...@@ -47,28 +47,28 @@ class CommentType extends ConfigEntityBundleBase implements CommentTypeInterface ...@@ -47,28 +47,28 @@ class CommentType extends ConfigEntityBundleBase implements CommentTypeInterface
* *
* @var string * @var string
*/ */
public $id; protected $id;
/** /**
* The comment type label. * The comment type label.
* *
* @var string * @var string
*/ */
public $label; protected $label;
/** /**
* The description of the comment type. * The description of the comment type.
* *
* @var string * @var string
*/ */
public $description; protected $description;
/** /**
* The target entity type. * The target entity type.
* *
* @var string * @var string
*/ */
public $target_entity_type_id; protected $target_entity_type_id;
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -111,11 +111,12 @@ function testDiff() { ...@@ -111,11 +111,12 @@ function testDiff() {
$diff = \Drupal::service('config.manager')->diff($active, $staging, 'config_test.dynamic.' . $new_test_entity_id, $config_name); $diff = \Drupal::service('config.manager')->diff($active, $staging, 'config_test.dynamic.' . $new_test_entity_id, $config_name);
$edits = $diff->getEdits(); $edits = $diff->getEdits();
$this->assertEqual($edits[0]->type, 'change', 'The second item in the diff is a copy.'); $this->assertEqual($edits[0]->type, 'copy', 'The first item in the diff is a copy.');
$this->assertEqual($edits[0]->orig, array('id: ' . $new_test_entity_id)); $this->assertEqual($edits[1]->type, 'change', 'The second item in the diff is a change.');
$this->assertEqual($edits[0]->closing, array('id: ' . $test_entity_id)); $this->assertEqual($edits[1]->orig, array('id: ' . $new_test_entity_id));
$this->assertEqual($edits[1]->type, 'copy', 'The second item in the diff is a copy.'); $this->assertEqual($edits[1]->closing, array('id: ' . $test_entity_id));
$this->assertEqual(count($edits), 2, 'There are two items in the diff.'); $this->assertEqual($edits[2]->type, 'copy', 'The third item in the diff is a copy.');
$this->assertEqual(count($edits), 3, 'There are three items in the diff.');
} }
/** /**
......
...@@ -46,7 +46,6 @@ function testCRUD() { ...@@ -46,7 +46,6 @@ function testCRUD() {
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->id; $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->id;
// Verify default properties on a newly created empty entity. // Verify default properties on a newly created empty entity.
$empty = entity_create('config_test'); $empty = entity_create('config_test');
$this->assertIdentical($empty->id, NULL);
$this->assertTrue($empty->uuid); $this->assertTrue($empty->uuid);
$this->assertIdentical($empty->label, NULL); $this->assertIdentical($empty->label, NULL);
$this->assertIdentical($empty->style, NULL); $this->assertIdentical($empty->style, NULL);
...@@ -105,7 +104,6 @@ function testCRUD() { ...@@ -105,7 +104,6 @@ function testCRUD() {
'label' => $this->randomString(), 'label' => $this->randomString(),
'style' => $this->randomName(), 'style' => $this->randomName(),
)); ));
$this->assertIdentical($config_test->id, $expected['id']);
$this->assertTrue($config_test->uuid); $this->assertTrue($config_test->uuid);
$this->assertNotEqual($config_test->uuid, $empty->uuid); $this->assertNotEqual($config_test->uuid, $empty->uuid);
$this->assertIdentical($config_test->label, $expected['label']); $this->assertIdentical($config_test->label, $expected['label']);
...@@ -159,7 +157,7 @@ function testCRUD() { ...@@ -159,7 +157,7 @@ function testCRUD() {
try { try {
$id_length_config_test->save(); $id_length_config_test->save();
$this->pass(String::format("config_test entity with ID length @length was saved.", array( $this->pass(String::format("config_test entity with ID length @length was saved.", array(
'@length' => strlen($id_length_config_test->id)) '@length' => strlen($id_length_config_test->id()))
)); ));
} }
catch (ConfigEntityIdLengthException $e) { catch (ConfigEntityIdLengthException $e) {
...@@ -173,7 +171,7 @@ function testCRUD() { ...@@ -173,7 +171,7 @@ function testCRUD() {
try { try {
$id_length_config_test->save(); $id_length_config_test->save();
$this->pass(String::format("config_test entity with ID length @length was saved.", array( $this->pass(String::format("config_test entity with ID length @length was saved.", array(
'@length' => strlen($id_length_config_test->id), '@length' => strlen($id_length_config_test->id()),
))); )));
} }
catch (ConfigEntityIdLengthException $e) { catch (ConfigEntityIdLengthException $e) {
...@@ -187,13 +185,13 @@ function testCRUD() { ...@@ -187,13 +185,13 @@ function testCRUD() {
try { try {
$status = $id_length_config_test->save(); $status = $id_length_config_test->save();
$this->fail(String::format("config_test entity with ID length @length exceeding the maximum allowed length of @max saved successfully", array( $this->fail(String::format("config_test entity with ID length @length exceeding the maximum allowed length of @max saved successfully", array(
'@length' => strlen($id_length_config_test->id), '@length' => strlen($id_length_config_test->id()),
'@max' => static::MAX_ID_LENGTH, '@max' => static::MAX_ID_LENGTH,
))); )));
} }
catch (ConfigEntityIdLengthException $e) { catch (ConfigEntityIdLengthException $e) {
$this->pass(String::format("config_test entity with ID length @length exceeding the maximum allowed length of @max failed to save", array( $this->pass(String::format("config_test entity with ID length @length exceeding the maximum allowed length of @max failed to save", array(
'@length' => strlen($id_length_config_test->id), '@length' => strlen($id_length_config_test->id()),
'@max' => static::MAX_ID_LENGTH, '@max' => static::MAX_ID_LENGTH,
))); )));
} }
...@@ -221,7 +219,7 @@ function testCRUD() { ...@@ -221,7 +219,7 @@ function testCRUD() {
$this->assertIdentical($config_test->getOriginalId(), $old_id); $this->assertIdentical($config_test->getOriginalId(), $old_id);
// Rename. // Rename.
$config_test->id = $new_id; $config_test->set('id', $new_id);
$this->assertIdentical($config_test->id(), $new_id); $this->assertIdentical($config_test->id(), $new_id);
$status = $config_test->save(); $status = $config_test->save();
$this->assertIdentical($status, SAVED_UPDATED); $this->assertIdentical($status, SAVED_UPDATED);
......
...@@ -56,15 +56,15 @@ function testImport() { ...@@ -56,15 +56,15 @@ function testImport() {
// Create new config entity. // Create new config entity.
$original_dynamic_data = array( $original_dynamic_data = array(
'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id,
'status' => TRUE,
'dependencies' => array(),
'id' => 'new', 'id' => 'new',
'label' => 'New', 'label' => 'New',
'weight' => 0, 'weight' => 0,
'style' => '', 'style' => '',
'test_dependencies' => array(), 'test_dependencies' => array(),
'status' => TRUE,
'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id,
'dependencies' => array(),
'protected_property' => '', 'protected_property' => '',
); );
$staging->write($dynamic_name, $original_dynamic_data); $staging->write($dynamic_name, $original_dynamic_data);
...@@ -349,31 +349,31 @@ function testImportErrorLog() { ...@@ -349,31 +349,31 @@ function testImportErrorLog() {
$uuid = $this->container->get('uuid'); $uuid = $this->container->get('uuid');
$values_primary = array( $values_primary = array(
'uuid' => $uuid->generate(),
'langcode' => 'en',
'status' => TRUE,
'dependencies' => array(),
'id' => 'primary', 'id' => 'primary',
'label' => 'Primary', 'label' => 'Primary',
'weight' => 0, 'weight' => 0,
'style' => NULL, 'style' => NULL,
'test_dependencies' => array(), 'test_dependencies' => array(),
'status' => TRUE,
'uuid' => $uuid->generate(),
'langcode' => 'en',
'dependencies' => array(),
'protected_property' => null, 'protected_property' => null,
); );
$staging->write($name_primary, $values_primary); $staging->write($name_primary, $values_primary);
$values_secondary = array( $values_secondary = array(
'id' => 'secondary',
'label' => 'Secondary Sync',
'weight' => 0,
'style' => NULL,
'test_dependencies' => array(),
'status' => TRUE,
'uuid' => $uuid->generate(), 'uuid' => $uuid->generate(),
'langcode' => 'en', 'langcode' => 'en',
'status' => TRUE,
// Add a dependency on primary, to ensure that is synced first. // Add a dependency on primary, to ensure that is synced first.
'dependencies' => array( 'dependencies' => array(
'entity' => array($name_primary), 'entity' => array($name_primary),
), ),
'id' => 'secondary',
'label' => 'Secondary Sync',
'weight' => 0,
'style' => NULL,
'test_dependencies' => array(),
'protected_property' => null, 'protected_property' => null,
); );
$staging->write($name_secondary, $values_secondary); $staging->write($name_secondary, $values_secondary);
......
...@@ -170,15 +170,15 @@ function testNew() { ...@@ -170,15 +170,15 @@ function testNew() {
// Create new config entity. // Create new config entity.
$original_dynamic_data = array( $original_dynamic_data = array(
'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id,
'status' => TRUE,
'dependencies' => array(),
'id' => 'new', 'id' => 'new',
'label' => 'New', 'label' => 'New',
'weight' => 0, 'weight' => 0,
'style' => '', 'style' => '',
'test_dependencies' => array(), 'test_dependencies' => array(),
'status' => TRUE,
'uuid' => '30df59bd-7b03-4cf7-bb35-d42fc49f0651',
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id,
'dependencies' => array(),
'protected_property' => '', 'protected_property' => '',
); );
$staging->write($dynamic_name, $original_dynamic_data); $staging->write($dynamic_name, $original_dynamic_data);
......
...@@ -135,12 +135,8 @@ function testSchemaMapping() { ...@@ -135,12 +135,8 @@ function testSchemaMapping() {
$expected['label'] = 'Image style'; $expected['label'] = 'Image style';
$expected['class'] = '\Drupal\Core\Config\Schema\Mapping'; $expected['class'] = '\Drupal\Core\Config\Schema\Mapping';
$expected['mapping']['name']['type'] = 'string'; $expected['mapping']['name']['type'] = 'string';
$expected['mapping']['id']['label'] = 'ID';
$expected['mapping']['id']['type'] = 'string';
$expected['mapping']['uuid']['type'] = 'string'; $expected['mapping']['uuid']['type'] = 'string';
$expected['mapping']['uuid']['label'] = 'UUID'; $expected['mapping']['uuid']['label'] = 'UUID';
$expected['mapping']['label']['type'] = 'label';
$expected['mapping']['label']['label'] = 'Label';
$expected['mapping']['langcode']['type'] = 'string'; $expected['mapping']['langcode']['type'] = 'string';
$expected['mapping']['langcode']['label'] = 'Default language'; $expected['mapping']['langcode']['label'] = 'Default language';
$expected['mapping']['status']['type'] = 'boolean'; $expected['mapping']['status']['type'] = 'boolean';
...@@ -148,6 +144,8 @@ function testSchemaMapping() { ...@@ -148,6 +144,8 @@ function testSchemaMapping() {
$expected['mapping']['dependencies']['type'] = 'config_dependencies'; $expected['mapping']['dependencies']['type'] = 'config_dependencies';
$expected['mapping']['dependencies']['label'] = 'Dependencies'; $expected['mapping']['dependencies']['label'] = 'Dependencies';
$expected['mapping']['name']['type'] = 'string'; $expected['mapping']['name']['type'] = 'string';
$expected['mapping']['label']['type'] = 'label';
$expected['mapping']['label']['label'] = 'Label';
$expected['mapping']['effects']['type'] = 'sequence'; $expected['mapping']['effects']['type'] = 'sequence';
$expected['mapping']['effects']['sequence'][0]['type'] = 'mapping';