diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php index 59aef23..cb605a3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php @@ -124,8 +124,6 @@ function testFieldUpgradeToConfig() { // Check that the configuration for the 'body' field is correct. $config = \Drupal::config('field.field.node.body')->get(); - // This will be necessary to retrieve the table name. - $field_entity = new Field($config); // We cannot predict the value of the UUID, we just check it's present. $this->assertFalse(empty($config['uuid'])); $field_uuid = $config['uuid']; @@ -178,11 +176,86 @@ function testFieldUpgradeToConfig() { )); } - // Check that field values in a pre-existing node are read correctly. - $body = node_load(1)->get('body'); - $this->assertEqual($body->value, 'Some value'); - $this->assertEqual($body->summary, 'Some summary'); - $this->assertEqual($body->format, 'filtered_html'); + // Check that the field that was shared in two entity types got split into + // two separate config entities. + $config = \Drupal::config('field.field.node.test_shared_field')->get(); + // We cannot predict the value of the UUID, we just check it's present. + $this->assertFalse(empty($config['uuid'])); + $field_uuid_node = $config['uuid']; + unset($config['uuid']); + $this->assertEqual($config, array( + 'id' => 'node.test_shared_field', + 'name' => 'test_shared_field', + 'type' => 'text', + 'module' => 'text', + 'active' => '1', + 'entity_type' => 'node', + 'settings' => array( + 'max_length' => '255', + ), + 'locked' => 0, + 'cardinality' => 1, + 'translatable' => 0, + 'indexes' => array( + 'format' => array('format') + ), + 'status' => 1, + 'langcode' => 'und', + )); + $config = \Drupal::config('field.field.user.test_shared_field')->get(); + // We cannot predict the value of the UUID, we just check it's present. + $this->assertFalse(empty($config['uuid'])); + $field_uuid_user = $config['uuid']; + unset($config['uuid']); + $this->assertEqual($config, array( + 'id' => 'user.test_shared_field', + 'name' => 'test_shared_field', + 'type' => 'text', + 'module' => 'text', + 'active' => '1', + 'entity_type' => 'user', + 'settings' => array( + 'max_length' => '255', + ), + 'locked' => 0, + 'cardinality' => 1, + 'translatable' => 0, + 'indexes' => array( + 'format' => array('format') + ), + 'status' => 1, + 'langcode' => 'und', + )); + + // Check that the corresponding instances point to the correct field UUIDs. + $config = \Drupal::config('field.instance.node.article.test_shared_field')->get(); + $this->assertEqual($config['field_uuid'], $field_uuid_node); + $config = \Drupal::config('field.instance.user.user.test_shared_field')->get(); + $this->assertEqual($config['field_uuid'], $field_uuid_user); + + // Check that field values in the pre-existing node are read correctly. + $node = node_load(1); + $this->assertEqual($node->body->getValue(), array( + 0 => array( + 'value' => 'Some value', + 'summary' => 'Some summary', + 'format' => 'filtered_html', + ), + )); + $this->assertEqual($node->test_shared_field->getValue(), array( + 0 => array( + 'value' => 'Shared field: value for node 1', + 'format' => 'filtered_html', + ), + )); + // Check that field values in the pre-existing user are read correctly. + $account = user_load(1); + $this->assertEqual($account->test_shared_field->getValue(), array( + 0 => array( + 'value' => 'Shared field: value for user 1', + 'format' => 'filtered_html', + ), + )); // Check that the definition of a deleted field is stored in state rather // than config. diff --git a/core/modules/system/tests/upgrade/drupal-7.field.database.php b/core/modules/system/tests/upgrade/drupal-7.field.database.php index 74f69fb..4ab16b6 100644 --- a/core/modules/system/tests/upgrade/drupal-7.field.database.php +++ b/core/modules/system/tests/upgrade/drupal-7.field.database.php @@ -55,6 +55,241 @@ )) ->execute(); +// Add a field shared across different entity types (instance on article nodes +// and users). +$field_id = db_insert('field_config') + ->fields(array( + 'field_name' => 'test_shared_field', + 'type' => 'text', + 'module' => 'text', + 'active' => 1, + 'storage_type' => 'field_sql_storage', + 'storage_module' => 'field_sql_storage', + 'storage_active' => 1, + 'locked' => 0, + 'data' => serialize(array( + 'entity_types' => array(), + 'settings' => array( + 'max_length' => 255, + ), + 'storage' => array( + 'type' => 'field_sql_storage', + 'settings' => array(), + 'module' => 'field_sql_storage', + 'active' => 1, + ), + 'indexes' => array( + 'format' => array(0 => 'format') + ), + 'foreign keys' => array( + 'format' => array( + 'table' => 'filter_format', + 'columns' => array('format' => 'format') + ) + ) + )), + 'cardinality' => 1, + 'translatable' => 0, + 'deleted' => 1, + )) + ->execute(); +db_insert('field_config_instance') + ->fields(array( + 'field_id' => $field_id, + 'field_name' => 'test_shared_field', + 'entity_type' => 'node', + 'bundle' => 'article', + 'data' => serialize(array( + 'label' => 'Long text', + 'description' => '', + 'required' => FALSE, + 'widget' => array( + 'type' => 'text_textfield', + 'weight' => 4, + 'module' => 'text', + 'active' => 1, + 'settings' => array( + 'size' => 60, + ), + ), + 'settings' => array( + 'text_processing' => 0, + 'user_register_form' => FALSE, + ), + 'display' => array( + 'default' => array( + 'label' => 'above', + 'type' => 'text_default', + 'settings' => array(), + 'module' => 'text', + 'weight' => 10, + ), + ), + )), + 'deleted' => 1 + )) + ->execute(); +db_insert('field_config_instance') + ->fields(array( + 'field_id' => $field_id, + 'field_name' => 'test_shared_field', + 'entity_type' => 'user', + 'bundle' => 'user', + 'data' => serialize(array( + 'label' => 'Shared field', + 'description' => '', + 'required' => FALSE, + 'widget' => array( + 'type' => 'text_textfield', + 'weight' => 4, + 'module' => 'text', + 'active' => 1, + 'settings' => array( + 'size' => 60, + ), + ), + 'settings' => array( + 'text_processing' => 0, + 'user_register_form' => FALSE, + ), + 'display' => array( + 'default' => array( + 'label' => 'above', + 'type' => 'text_default', + 'settings' => array(), + 'module' => 'text', + 'weight' => 10, + ), + ), + )), + 'deleted' => 1 + )) + ->execute(); +// Create the corresponding storage tables. +$schema = array( + 'fields' => array( + 'entity_type' => array( + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'bundle' => array( + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'deleted' => array( + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + ), + 'entity_id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'revision_id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ), + 'language' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + ), + 'delta' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'test_shared_field_value' => array( + 'type' => 'varchar', + 'size' => 255, + 'not null' => FALSE, + ), + 'test_shared_field_format' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ), + ), + 'primary key' => array( + 'entity_type', + 'entity_id', + 'deleted', + 'delta', + 'language', + ), + 'indexes' => array( + 'entity_type' => array( + 'entity_type', + ), + 'bundle' => array( + 'bundle', + ), + 'deleted' => array( + 'deleted', + ), + 'entity_id' => array( + 'entity_id', + ), + 'revision_id' => array( + 'revision_id', + ), + 'language' => array( + 'language', + ), + 'test_shared_field_format' => array( + 'test_shared_field_format', + ), + ), + 'foreign keys' => array( + 'test_shared_field_format' => array( + 'table' => 'filter_format', + 'columns' => array( + 'test_shared_field_format' => 'format', + ), + ), + ), + 'module' => 'field_sql_storage', + 'name' => 'field_data_test_shared_field', +); +db_create_table('field_data_test_shared_field', $schema); +$schema['primary key'] = array( + 'entity_type', + 'entity_id', + 'revision_id', + 'deleted', + 'delta', + 'language', +); +$schema['name'] = 'field_revision_test_shared_field'; +db_create_table('field_revision_test_shared_field', $schema); + +// Add a value for the 'test_shared_field' field on user 1. +$field_data_row = array( + 'entity_type' => 'user', + 'bundle' => 'user', + 'deleted' => '0', + 'entity_id' => '1', + 'revision_id' => '1', + 'language' => 'und', + 'delta' => '0', + 'test_shared_field_value' => 'Shared field: value for user 1', + 'test_shared_field_format' => 'filtered_html', +); +db_insert('field_data_test_shared_field') + ->fields($field_data_row) + ->execute(); +db_insert('field_revision_test_shared_field') + ->fields($field_data_row) + ->execute(); + // Add one node. db_insert('node') ->fields(array( @@ -88,7 +323,7 @@ 'sticky' => '0', )) ->execute(); - +// Add a value for the 'body' field. $field_data_row = array( 'entity_type' => 'node', 'bundle' => 'article', @@ -107,6 +342,24 @@ db_insert('field_revision_body') ->fields($field_data_row) ->execute(); +// Add a value for the 'test_shared_field' field. +$field_data_row = array( + 'entity_type' => 'node', + 'bundle' => 'article', + 'deleted' => '0', + 'entity_id' => '1', + 'revision_id' => '1', + 'language' => 'und', + 'delta' => '0', + 'test_shared_field_value' => 'Shared field: value for node 1', + 'test_shared_field_format' => 'filtered_html', +); +db_insert('field_data_test_shared_field') + ->fields($field_data_row) + ->execute(); +db_insert('field_revision_test_shared_field') + ->fields($field_data_row) + ->execute(); // Add a deleted field and instance. $field_id = db_insert('field_config')