diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfMappingUpgradePathTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfMappingUpgradePathTest.php new file mode 100644 index 0000000..1891c23 --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfMappingUpgradePathTest.php @@ -0,0 +1,141 @@ + 'RDF mapping upgrade test', + 'description' => 'Upgrade tests with RDF mapping data.', + 'group' => 'RDF', + ); + } + + public function setUp() { + // Path to the database dump files. + $this->databaseDumpFiles = array( + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.bare.standard_all.database.php.gz', + drupal_get_path('module', 'rdf') . '/tests/drupal-7.rdf.database.php', + ); + parent::setUp(); + } + + /** + * Tests to see if RDF mappings were upgraded. + */ + public function testRdfMappingUpgrade() { + $this->assertTrue(db_table_exists('rdf_mapping'), 'RDF mapping table exists.'); + $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + + $this->_testUnalteredMappingUpgrade(); + $this->_testAlteredMappingUpgrade(); + $this->_testReverseRelationUpgrade(); + + } + + /** + * Helper function to test upgrade of unaltered mappings. + */ + protected function _testUnalteredMappingUpgrade() { + $config = rdf_get_mapping('node', 'page'); + + // Test bundle mapping. + $mapping = $config->getBundleMapping(); + $expected_mapping = array( + 'types' => array('foaf:Document'), + ); + $this->assertEqual($mapping, $expected_mapping, 'Unaltered bundle mapping upgraded correctly.'); + + // Test field mapping - property. + $mapping = $config->getFieldMapping('title'); + $expected_mapping = $this->prepareExpectedFieldMapping(array( + 'properties' => array('dc:title'), + )); + $this->assertEqual($mapping, $expected_mapping, 'Unaltered field mapping upgraded correctly.'); + + // Test field mapping - property with datatype and callback. + $mapping = $config->getFieldMapping('created'); + $expected_mapping = $this->prepareExpectedFieldMapping(array( + 'properties' => array('dc:date', 'dc:created'), + 'datatype' => 'xsd:dateTime', + 'datatype_callback' => 'date_iso8601', + )); + $this->assertEqual($mapping, $expected_mapping, 'Unaltered field mapping with datatype and datatype callback upgraded correctly.'); + + // Test field mapping - rel. + $mapping = $config->getFieldMapping('uid'); + $expected_mapping = $this->prepareExpectedFieldMapping(array( + 'properties' => array('sioc:has_creator'), + 'mapping_type' => 'rel', + )); + $this->assertEqual($mapping, $expected_mapping, 'Unaltered field mapping with rel mapping type upgraded correctly.'); + } + + /** + * Helper function to test upgrade of altered mappings. + */ + protected function _testAlteredMappingUpgrade() { + $config = rdf_get_mapping('node', 'article'); + + // Test bundle mapping. + $mapping = $config->getBundleMapping(); + $expected_mapping = array( + 'types' => array('foo:Type'), + ); + $this->assertEqual($mapping, $expected_mapping, 'Overriden bundle mapping upgraded correctly.'); + + // Test field mapping. + $mapping = $config->getFieldMapping('field_image'); + $expected_mapping = $this->prepareExpectedFieldMapping(array( + 'properties' => array('foo:image'), + )); + $this->assertEqual($expected_mapping, $mapping, 'Overriden field mapping is upgraded correctly.'); + + // Test field mapping. + $mapping = $config->getFieldMapping('changed'); + $expected_mapping = $this->prepareExpectedFieldMapping(array()); + $this->assertEqual($expected_mapping, $mapping, 'Empty field mapping from overriden mapping is upgraded correctly.'); + } + + /** + * Helper function to test handling of deprecated reverse relation mappings. + */ + protected function _testReverseRelationUpgrade() { + // Test field mapping - rev. + $config = rdf_get_mapping('node', 'rev_test'); + $mapping = $config->getFieldMapping('field_rev'); + $expected_mapping = $this->prepareExpectedFieldMapping(array()); + $this->assertEqual($mapping, $expected_mapping, 'Reverse relation mapping has been dropped.'); + } + + /** + * Prepares the expected mapping array with default values. + * + * @param array $mapping + * The significant values of the expected mapping. + * + * @return array + * The full mapping array, including default values. + */ + protected function prepareExpectedFieldMapping($mapping) { + $default_mapping = array( + 'properties' => NULL, + 'datatype' => NULL, + 'datatype_callback' => NULL, + 'mapping_type' => NULL, + ); + return array_merge($default_mapping, $mapping); + } + +} diff --git a/core/modules/rdf/rdf.install b/core/modules/rdf/rdf.install index be2cde1..feda96a 100644 --- a/core/modules/rdf/rdf.install +++ b/core/modules/rdf/rdf.install @@ -4,3 +4,57 @@ * @file * Install, update and uninstall functions for the rdf module. */ + +use Drupal\Component\Uuid\Uuid; + +/** + * Convert RDF mappings to configuration. + * + * @ingroup config_upgrade + */ +function rdf_update_8000() { + $uuid = new Uuid(); + $query = db_query("SELECT * FROM {rdf_mapping}"); + + // Iterate through all the stored mappings. + while ($row = $query->fetchAssoc()) { + $mapping = unserialize($row['mapping']); + $entity_type = $row['type']; + $bundle = $row['bundle']; + + // Create a config object for the mapping. + $config = config("rdf.mapping.$entity_type.$bundle") + ->set('id', "$entity_type.$bundle") + ->set('uuid', $uuid->generate()) + ->set('targetEntityType', $entity_type) + ->set('bundle', $bundle); + + // Add the bundle and field mappings. + $field_mappings = array(); + foreach ($mapping as $key => $value) { + // If this is the rdftype entry, add the types to the config object. + if ($key == 'rdftype') { + $config->set('types', $value); + } + // Otherwise, this key is a field. Process the field mapping into the new + // structure. + else { + // Since reverse relations are not supported in D8, drop any mappings + // which use the 'rev' type. + if (!empty($value['type'])) { + if ($value['type'] == 'rev') { + continue; + } + $field_mappings[$key]['mapping_type'] = $value['type']; + } + $field_mappings[$key]['properties'] = !empty($value['predicates']) ? $value['predicates'] : NULL; + $field_mappings[$key]['datatype'] = !empty($value['datatype']) ? $value['datatype'] : NULL; + $field_mappings[$key]['datatype_callback'] = !empty($value['callback']) ? $value['callback'] : NULL; + } + } + $config->set('fieldMappings', $field_mappings); + + // Save the mapping config object. + $config->save(); + } +} diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 14f2364..8d3ec02 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -68,7 +68,7 @@ function rdf_help($path, $arg) { * The RdfMapping object. */ function rdf_get_mapping($entity_type, $bundle) { - // Try loading the display from configuration. + // Try loading the mapping from configuration. $mapping = entity_load('rdf_mapping', $entity_type . '.' . $bundle); // If not found, create a fresh mapping object. diff --git a/core/modules/rdf/tests/drupal-7.rdf.database.php b/core/modules/rdf/tests/drupal-7.rdf.database.php new file mode 100644 index 0000000..aa0a9f9 --- /dev/null +++ b/core/modules/rdf/tests/drupal-7.rdf.database.php @@ -0,0 +1,51 @@ + array( + 'predicates' => array('foo:rev'), + 'type' => 'rev', + ), + 'field_notrev' => array( + 'predicates' => array('foo:rel'), + 'type' => 'rel', + ), +); +db_insert('rdf_mapping') + ->fields(array( + 'type', + 'bundle', + 'mapping', + )) + ->values(array( + 'type' => 'node', + 'bundle'=> 'rev_test', + 'mapping' => serialize($rev_mapping), + )) + ->execute(); + +// Alter the stored mapping for articles. +$altered_mapping = array( + 'rdftype' => array('foo:Type'), + 'field_image' => array( + 'predicates' => array('foo:image'), + ), +); +db_update('rdf_mapping') + ->fields(array( + 'type' => 'node', + 'bundle'=> 'article', + 'mapping' => serialize($altered_mapping), + )) + ->condition('type', 'node') + ->condition('bundle', 'article') + ->execute();