diff --git a/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/MappingSubscriber.php b/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/MappingSubscriber.php index f168fa0..2552870 100644 --- a/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/MappingSubscriber.php +++ b/core/modules/rdf/lib/Drupal/rdf/EventSubscriber/MappingSubscriber.php @@ -37,10 +37,6 @@ public function mapTypesFromInput(\Drupal\rdf\Mapping\MapTypesFromInputEvent $ev public function mapBundleForOutput(\Drupal\rdf\Mapping\MapBundleForOutputEvent $event) { $term_schema = $event->getTermSchema(); - // @todo When the $schema is SYNDICATION, allow sites to exclude the site - // schema URI from the mapping. - $site_schema_curie = $term_schema->getCurie(); - $event->addTypes(array($site_schema_curie)); $config = $term_schema->getMappingConfig(); $types = $config->get('types'); @@ -51,10 +47,6 @@ public function mapBundleForOutput(\Drupal\rdf\Mapping\MapBundleForOutputEvent $ public function mapFieldForOutput(\Drupal\rdf\Mapping\MapFieldForOutputEvent $event) { $term_schema = $event->getTermSchema(); - // @todo When the $schema is SYNDICATION, allow sites to exclude the site - // schema URI from the mapping. - $site_schema_curie = $term_schema->getCurie(); - $event->addPredicates(array($site_schema_curie)); $config = $term_schema->getMappingConfig(); $properties = $config->get('properties'); diff --git a/core/modules/rdf/lib/Drupal/rdf/Mapping/MapFieldForOutputEvent.php b/core/modules/rdf/lib/Drupal/rdf/Mapping/MapFieldForOutputEvent.php index ce4869a..c9bab59 100644 --- a/core/modules/rdf/lib/Drupal/rdf/Mapping/MapFieldForOutputEvent.php +++ b/core/modules/rdf/lib/Drupal/rdf/Mapping/MapFieldForOutputEvent.php @@ -63,8 +63,8 @@ public function addPredicates($curies) { * @param string|array $curie */ public function setDatatype($curie) { - if (!is_array($curie)) { - $curie = array($curie); + if (is_array($curie)) { + $curie = reset($curie); } $this->datatype = $curie; } diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php index dbdba31..1fae064 100644 --- a/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php @@ -48,6 +48,8 @@ public function setUp() { // Prepares commonly used URIs. $this->base_uri = url('', array('absolute' => TRUE)); $this->node_uri = url('node/' . $this->node->nid, array('absolute' => TRUE)); + + rdf_save_standard_profile_mappings(); } /** @@ -235,7 +237,12 @@ function _testBasicCommentRdfaMarkup($graph, $comment, $account = array()) { else { // The author is expected to be a blank node. $author_uri = $graph->get($comment_uri, ''); - $this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator).'); + if (!is_object($author_uri)) { + $this->fail('Comment relation to author found in RDF output (sioc:has_creator).'); + } + else { + $this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator).'); + } } // Author name. diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/FileFieldAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/FileFieldAttributesTest.php new file mode 100644 index 0000000..a996b34 --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/FileFieldAttributesTest.php @@ -0,0 +1,104 @@ + 'RDFa markup for files', + 'description' => 'Tests the RDFa markup of filefields.', + 'group' => 'RDF', + ); + } + + public function setUp() { + parent::setUp(); + $this->field_name = strtolower($this->randomName()); + + $this->mappingManager = RdfTestHelper::getMockMappingManager(); + + $type_name = 'article'; + $field_settings = array( + 'display_field' => '1', + 'display_default' => '1', + ); + $instance_settings = array( + 'description_field' => '1', + ); + $widget_settings = array(); + $this->createFileField($this->field_name, $type_name, $field_settings, $instance_settings, $widget_settings); + + // Set the teaser display to show this field. + entity_get_display('node', 'article', 'teaser') + ->setComponent($this->field_name, array( + 'type' => 'file_default', + )) + ->save(); + + // Set the RDF mapping for the new field. + $config = $this->mappingManager->getFieldMappingConfig('node', 'article', $this->field_name); + $field_mapping = array_merge($config->get(), array('properties' => array('rdfs:seeAlso'), 'mapping_type' => 'rel')); + $config->setData($field_mapping)->save(); + + $test_file = $this->getTestFile('text'); + + // Create a new node with the uploaded file. + $nid = $this->uploadNodeFile($test_file, $this->field_name, $type_name); + + $this->node = node_load($nid, TRUE); + $this->file = file_load($this->node->{$this->field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']); + + } + + /** + * Tests if file fields in teasers have correct resources. + * + * Ensure that file fields have the correct resource as the object in RDFa + * when displayed as a teaser. + */ + function testNodeTeaser() { + $node = $this->node; + $node_file = $this->file; + + // Render the teaser. + $node_render_array = entity_view_multiple(array($node), 'teaser'); + $html = drupal_render($node_render_array); + + // Parses front page where the node is displayed in its teaser form. + $parser = new \EasyRdf_Parser_Rdfa(); + $graph = new \EasyRdf_Graph(); + $base_uri = url('', array('absolute' => TRUE)); + $parser->parse($graph, $html, 'rdfa', $base_uri); + + $node_uri = url('node/' . $node->nid, array('absolute' => TRUE)); + $file_uri = file_create_url($node_file->uri); + + // Node relation to attached file. + $expected_value = array( + 'type' => 'uri', + 'value' => $file_uri, + ); + $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).'); + $this->drupalGet('node'); + } +} diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/ImageFieldAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/ImageFieldAttributesTest.php new file mode 100644 index 0000000..42e4a4f --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/ImageFieldAttributesTest.php @@ -0,0 +1,102 @@ + 'RDFa markup for imagefield', + 'description' => 'Tests the RDFa markup of imagefields.', + 'group' => 'RDF', + ); + } + + public function setUp() { + parent::setUp(); + + $this->field_name = 'field_image'; + + $this->mappingManager = RdfTestHelper::getMockMappingManager(); + + // Create the image field. + $this->createImageField($this->field_name, 'article'); + + // Set the RDF mapping for the new field. + $config = $this->mappingManager->getFieldMappingConfig('node', 'article', $this->field_name); + $field_mapping = array_merge($config->get(), array('properties' => array('og:image'), 'mapping_type' => 'rel')); + $config->setData($field_mapping)->save(); + + // Get the test image that simpletest provides. + $image = current($this->drupalGetTestFiles('image')); + + // Save a node with the image. + $nid = $this->uploadNodeImage($image, $this->field_name, 'article'); + $this->node = node_load($nid); + $this->file = file_load($this->node->{$this->field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']); + } + + /** + * Tests that image fields in teasers have correct resources. + */ + function testNodeTeaser() { + $node = $this->node; + $node_file = $this->file; + + // Set the display options for the teaser. + $display_options = array( + 'type' => 'image', + 'settings' => array('image_style'=> 'medium', 'image_link' => 'content'), + ); + $display = entity_get_display('node', 'article', 'teaser'); + $display->setComponent($this->field_name, $display_options) + ->save(); + + // Render the teaser. + $node_render_array = node_view($node, 'teaser'); + $html = drupal_render($node_render_array); + + // Parse the teaser. + $parser = new \EasyRdf_Parser_Rdfa(); + $graph = new \EasyRdf_Graph(); + $base_uri = url('', array('absolute' => TRUE)); + $parser->parse($graph, $html, 'rdfa', $base_uri); + + // Construct the node and image URIs for testing. + $node_uri = url('node/' . $node->nid, array('absolute' => TRUE)); + $image_uri = image_style_url('medium', $node_file->uri); + + // Test relations from node to image. + $expected_value = array( + 'type' => 'uri', + 'value' => $image_uri, + ); + $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).'); + + // Test image type. + $expected_value = array( + 'type' => 'uri', + 'value' => 'http://xmlns.com/foaf/0.1/Image', + ); + $this->assertTrue($graph->hasProperty($image_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Image type found in RDF output (foaf:Image).'); + } +} diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfTestHelper.php b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfTestHelper.php new file mode 100644 index 0000000..b8b0f97 --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfTestHelper.php @@ -0,0 +1,28 @@ +addSubscriber(new MappingSubscriber()); + $site_schema_manager = new SiteSchemaManager(new MemoryBackend('cache')); + return new RdfMappingManager($dispatcher, $site_schema_manager); + } + +} diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaAttributesTest.php new file mode 100644 index 0000000..5ebdf5e --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaAttributesTest.php @@ -0,0 +1,151 @@ + 'RDFa attributes', + 'description' => 'Test RDFa attribute generation from RDF mapping.', + 'group' => 'RDF', + ); + } + + public function setUp() { + parent::setUp(); + // Set up the mock mapping manager. + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber(new MappingSubscriber()); + $site_schema_manager = new SiteSchemaManager(new MemoryBackend('cache')); + $this->mappingManager = new RdfMappingManager($dispatcher, $site_schema_manager); + } + + /** + * Test attribute creation for mappings which use 'property'. + */ + function testProperty() { + $properties = array('dc:title'); + + $mapping = array( + 'properties' => $properties, + ); + $expected_attributes = array( + 'property' => $properties, + ); + + $this->_testAttributes($expected_attributes, $mapping); + } + + /** + * Test attribute creation for mappings which use 'datatype'. + */ + function testDatatype() { + $properties = array('foo:bar1'); + $datatype = 'foo:bar1type'; + + $mapping = array( + 'datatype' => $datatype, + 'properties' => $properties, + ); + $expected_attributes = array( + 'datatype' => $datatype, + 'property' => $properties, + ); + + $this->_testAttributes($expected_attributes, $mapping); + } + + /** + * Test attribute creation for mappings which override human-readable content. + */ + function testDatatypeCallback() { + $properties = array('dc:created'); + $datatype = 'xsd:dateTime'; + + $date = 1252750327; + $isoDate = date('c', $date); + + $mapping = array( + 'datatype' => $datatype, + 'properties' => $properties, + 'datatype_callback' => 'date_iso8601', + ); + $expected_attributes = array( + 'datatype' => $datatype, + 'property' => $properties, + 'content' => $isoDate, + ); + + $this->_testAttributes($expected_attributes, $mapping, $date); + } + + /** + * Test attribute creation for mappings which use 'rel'. + */ + function testRel() { + $properties = array('sioc:has_creator', 'dc:creator'); + + $mapping = array( + 'properties' => $properties, + 'mapping_type' => 'rel', + ); + $expected_attributes = array( + 'rel' => $properties, + ); + + $this->_testAttributes($expected_attributes, $mapping); + } + + /** + * Helper function to test attribute generation. + * + * @param array $field_mapping + * The field mapping to merge into the RDF mapping config. + * @param array $expected_attributes + * The expected return of rdf_rdfa_attributes. + */ + protected function _testAttributes($expected_attributes, $field_mapping, $data = NULL) { + $this->saveMapping($field_mapping); + + $mapping = $this->mappingManager->getFieldMapping('node', 'article', 'field_test'); + $attributes = rdf_rdfa_attributes($mapping, $data); + ksort($expected_attributes); + ksort($attributes); + $this->assertEqual($expected_attributes, $attributes); + } + + /** + * Helper function to save mapping using config system. + * + * @param array $field_mapping + * The field mapping. + */ + protected function saveMapping($field_mapping) { + $config = $this->mappingManager->getFieldMappingConfig('node', 'article', 'field_test'); + $field_mapping = array_merge($config->get(), $field_mapping); + $config->setData($field_mapping)->save(); + } +} diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaMarkupTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaMarkupTest.php deleted file mode 100644 index 5104b6b..0000000 --- a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaMarkupTest.php +++ /dev/null @@ -1,269 +0,0 @@ - 'RDFa markup', - 'description' => 'Test RDFa markup generation.', - 'group' => 'RDF', - ); - } - - /** - * Overrides \Drupal\simpletest\DrupalUnitTestBase::setUp(). - */ - public function setUp() { - parent::setUp(); - // Set up the mock mapping manager. - $dispatcher = new EventDispatcher(); - $dispatcher->addSubscriber(new MappingSubscriber()); - $site_schema_manager = new SiteSchemaManager(new MemoryBackend('cache')); - $this->mappingManager = new RdfMappingManager($dispatcher, $site_schema_manager); - } - - function testDrupalRdfaAttributes() { - // Same value as the one in the HTML tag (no callback function). - $expected_attributes = array( - 'property' => array('dc:title'), - ); - $mapping = rdf_mapping_load('test_entity', 'test_bundle'); - $attributes = rdf_rdfa_attributes($mapping['title']); - ksort($expected_attributes); - ksort($attributes); - $this->assertEqual($expected_attributes, $attributes); - - // Value different from the one in the HTML tag (callback function). - $date = 1252750327; - $isoDate = date('c', $date); - $expected_attributes = array( - 'datatype' => 'xsd:dateTime', - 'property' => array('dc:created'), - 'content' => $isoDate, - ); - $mapping = rdf_mapping_load('test_entity', 'test_bundle'); - $attributes = rdf_rdfa_attributes($mapping['created'], $date); - ksort($expected_attributes); - ksort($attributes); - $this->assertEqual($expected_attributes, $attributes); - - // Same value as the one in the HTML tag with datatype. - $expected_attributes = array( - 'datatype' => 'foo:bar1type', - 'property' => array('foo:bar1'), - ); - $mapping = rdf_mapping_load('test_entity', 'test_bundle'); - $attributes = rdf_rdfa_attributes($mapping['foobar1']); - ksort($expected_attributes); - ksort($attributes); - $this->assertEqual($expected_attributes, $attributes); - - // ObjectProperty mapping (rel). - $expected_attributes = array( - 'rel' => array('sioc:has_creator', 'dc:creator'), - ); - $mapping = rdf_mapping_load('test_entity', 'test_bundle'); - $attributes = rdf_rdfa_attributes($mapping['foobar_objproperty1']); - ksort($expected_attributes); - ksort($attributes); - $this->assertEqual($expected_attributes, $attributes); - - // Inverse ObjectProperty mapping (rev). - $expected_attributes = array( - 'rev' => array('sioc:reply_of'), - ); - $mapping = rdf_mapping_load('test_entity', 'test_bundle'); - $attributes = rdf_rdfa_attributes($mapping['foobar_objproperty2']); - ksort($expected_attributes); - ksort($attributes); - $this->assertEqual($expected_attributes, $attributes); - } - - /** - * Tests if file fields in teasers have correct resources. - * - * Ensure that file fields have the correct resource as the object in RDFa - * when displayed as a teaser. - */ - function testAttributesInMarkupFile() { - // Create a user to post the image. - $admin_user = $this->drupalCreateUser(array('edit own article content', 'revert article revisions', 'administer content types')); - $this->drupalLogin($admin_user); - - $langcode = Language::LANGCODE_NOT_SPECIFIED; - $bundle_name = "article"; - - $field_name = 'file_test'; - $field = array( - 'field_name' => $field_name, - 'type' => 'file', - ); - field_create_field($field); - $instance = array( - 'field_name' => $field_name, - 'entity_type' => 'node', - 'bundle' => $bundle_name, - ); - field_create_instance($instance); - - entity_get_form_display('node', $bundle_name, 'default') - ->setComponent($field_name, array( - 'type' => 'file_generic', - )) - ->save(); - entity_get_display('node', $bundle_name, 'teaser') - ->setComponent($field_name, array( - 'type' => 'file_default', - )) - ->save(); - - // Set the RDF mapping for the new field. - $config = $this->mappingManager->getFieldMappingConfig('node', 'article', $field_name); - $field_mapping = array_merge($config->get(), array('properties' => array('rdfs:seeAlso'), 'mapping_type' => 'rel')); - $config->setData($field_mapping)->save(); - - // Get the test file that simpletest provides. - $file = current($this->drupalGetTestFiles('text')); - - // Prepare image variables. - $image_field = "field_image"; - // Get the test image that simpletest provides. - $image = current($this->drupalGetTestFiles('image')); - - // Create an array for drupalPost with the field names as the keys and - // the URIs for the test files as the values. - $edit = array("files[" . $field_name . "_" . $langcode . "_0]" => drupal_realpath($file->uri), - "files[" . $image_field . "_" . $langcode . "_0]" => drupal_realpath($image->uri)); - - // Create node and save, then edit node to upload files. - $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); - $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); - - // Prepares filenames for lookup in RDF graph. - $node = node_load($node->nid); - $node_uri = url('node/' . $node->nid, array('absolute' => TRUE)); - $file_uri = file_create_url(file_load($node->file_test['und'][0]['fid'])->uri); - $image_uri = image_style_url('medium', file_load($node->field_image['und'][0]['fid'])->uri); - $base_uri = url('', array('absolute' => TRUE)); - - // Edits the node to add tags. - $tag1 = $this->randomName(8); - $tag2 = $this->randomName(8); - $edit = array(); - $edit['field_tags[' . Language::LANGCODE_NOT_SPECIFIED . ']'] = "$tag1, $tag2"; - $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); - $term_1_id = key(taxonomy_term_load_multiple_by_name($tag1)); - $taxonomy_term_1_uri = url('taxonomy/term/' . $term_1_id, array('absolute' => TRUE)); - $term_2_id = key(taxonomy_term_load_multiple_by_name($tag2)); - $taxonomy_term_2_uri = url('taxonomy/term/' . $term_2_id, array('absolute' => TRUE)); - - // Parses front page where the node is displayed in its teaser form. - $parser = new \EasyRdf_Parser_Rdfa(); - $graph = new \EasyRdf_Graph(); - $parser->parse($graph, $this->drupalGet('node'), 'rdfa', $base_uri); - $rdf_output = $graph->toArray(); - - // Inspects RDF graph output. - // Node relations to attached file. - $expected_value = array( - 'type' => 'uri', - 'value' => $file_uri, - ); - $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).'); - - // Node relations to attached image. - $expected_value = array( - 'type' => 'uri', - 'value' => $image_uri, - ); - $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).'); - $expected_value = array( - 'type' => 'uri', - 'value' => $image_uri, - ); - $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).'); - // Image type. - $expected_value = array( - 'type' => 'uri', - 'value' => 'http://xmlns.com/foaf/0.1/Image', - ); - $this->assertTrue($graph->hasProperty($image_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Image type found in RDF output (foaf:Image).'); - - // Node relations to taxonomy terms. - $expected_value = array( - 'type' => 'uri', - 'value' => $taxonomy_term_1_uri, - ); - $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).'); - $expected_value = array( - 'type' => 'uri', - 'value' => $taxonomy_term_2_uri, - ); - $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).'); - - // Taxonomy terms triples. - // Term 1. - $expected_value = array( - 'type' => 'uri', - 'value' => 'http://www.w3.org/2004/02/skos/core#Concept', - ); - $this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).'); - $expected_value = array( - 'type' => 'literal', - 'value' => $tag1, - 'lang' => 'en', - ); - $this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).'); - $expected_value = array( - 'type' => 'literal', - 'value' => $tag1, - 'lang' => 'en', - ); - $this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/2004/02/skos/core#prefLabel', $expected_value), 'Taxonomy term name found in RDF output (skos:prefLabel).'); - // Term 2. - $expected_value = array( - 'type' => 'uri', - 'value' => 'http://www.w3.org/2004/02/skos/core#Concept', - ); - $this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).'); - $expected_value = array( - 'type' => 'literal', - 'value' => $tag2, - 'lang' => 'en', - ); - $this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).'); - $expected_value = array( - 'type' => 'literal', - 'value' => $tag2, - 'lang' => 'en', - ); - $this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/2004/02/skos/core#prefLabel', $expected_value), 'Taxonomy term name found in RDF output (skos:prefLabel).'); - } -} diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/TaxonomyTermFieldAttributesTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/TaxonomyTermFieldAttributesTest.php new file mode 100644 index 0000000..1eb373b --- /dev/null +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/TaxonomyTermFieldAttributesTest.php @@ -0,0 +1,189 @@ + 'RDFa markup for taxonomy term fields', + 'description' => 'Tests the RDFa markup of taxonomy term fields.', + 'group' => 'RDF', + ); + } + + public function setUp() { + parent::setUp(); + + $this->mappingManager = RdfTestHelper::getMockMappingManager(); + + $web_user = $this->drupalCreateUser(array('bypass node access', 'administer taxonomy')); + $this->drupalLogin($web_user); + $this->vocabulary = $this->createVocabulary(); + + // Setup a field and instance. + $this->field_name = 'field_taxonomy_test'; + + // Create the field. + $this->createTaxonomyTermReferenceField($this->field_name, $this->vocabulary); + + // Set the RDF mapping for the new field. + $config = $this->mappingManager->getFieldMappingConfig('node', 'article', $this->field_name); + $field_mapping = array_merge($config->get(), array('properties' => array('dc:subject'), 'mapping_type' => 'rel')); + $config->setData($field_mapping)->save(); + + // Set RDF mapping for term bundle. + $config = $this->mappingManager->getBundleMappingConfig('taxonomy_term', $this->vocabulary->id()); + $bundle_mapping = array_merge($config->get(), array('types' => array('skos:Concept'))); + $config->setData($bundle_mapping)->save(); + // Set RDF mapping for name field on term bundle. + $config = $this->mappingManager->getFieldMappingConfig('taxonomy_term', $this->vocabulary->id(), 'name'); + $field_mapping = array_merge($config->get(), array('properties' => array('rdfs:label'))); + $config->setData($field_mapping)->save(); + + } + + /** + * Tests if file fields in teasers have correct resources. + * + * Ensure that file fields have the correct resource as the object in RDFa + * when displayed as a teaser. + */ + function testNodeTeaser() { + // Set the teaser display to show this field. + entity_get_display('node', 'article', 'teaser') + ->setComponent($this->field_name, array( + 'type' => 'taxonomy_term_reference_link', + )) + ->save(); + + // Create a term in each vocabulary. + $term1 = $this->createTerm($this->vocabulary); + $term2 = $this->createTerm($this->vocabulary); + $taxonomy_term_1_uri = url('taxonomy/term/' . $term1->id(), array('absolute' => TRUE)); + $taxonomy_term_2_uri = url('taxonomy/term/' . $term2->id(), array('absolute' => TRUE)); + + // Submit an entity with both terms. + $langcode = Language::LANGCODE_NOT_SPECIFIED; + $this->drupalGet('node/add/article'); + $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is displayed'); + $edit = array( + "title" => $this->randomName(), + "{$this->field_name}[$langcode][]" => array($term1->id(), $term2->id()), + ); + $this->drupalPost(NULL, $edit, t('Save')); + preg_match('|node/(\d+)|', $this->url, $match); + $id = $match[1]; + + // Render the teaser. + $node = node_load($id); + $node_uri = url('node/' . $node->nid, array('absolute' => TRUE)); + $node_render_array = entity_view_multiple(array($node), 'teaser'); + $html = drupal_render($node_render_array); + + // Parse the teaser. + $parser = new \EasyRdf_Parser_Rdfa(); + $graph = new \EasyRdf_Graph(); + $base_uri = url('', array('absolute' => TRUE)); + $parser->parse($graph, $html, 'rdfa', $base_uri); + + // Node relations to taxonomy terms. + $expected_value = array( + 'type' => 'uri', + 'value' => $taxonomy_term_1_uri, + ); + $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).'); + $expected_value = array( + 'type' => 'uri', + 'value' => $taxonomy_term_2_uri, + ); + $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).'); + // Taxonomy terms triples. + // Term 1. + $expected_value = array( + 'type' => 'uri', + 'value' => 'http://www.w3.org/2004/02/skos/core#Concept', + ); + $this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).'); + $expected_value = array( + 'type' => 'literal', + 'value' => $term1->label(), + 'lang' => 'en', + ); + $this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).'); + // Term 2. + $expected_value = array( + 'type' => 'uri', + 'value' => 'http://www.w3.org/2004/02/skos/core#Concept', + ); + $this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).'); + $expected_value = array( + 'type' => 'literal', + 'value' => $term2->label(), + 'lang' => 'en', + ); + $this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).'); + } + + /** + * Create the taxonomy term reference field for testing. + * + * @param $field_name + * The name of the field to create. + * @param $vocabulary + * The vocabulary that the field should use. + * + * @todo Move this to TaxonomyTestBase, like the other field modules. + */ + protected function createTaxonomyTermReferenceField($field_name, $vocabulary) { + $field = array( + 'field_name' => $field_name, + 'type' => 'taxonomy_term_reference', + 'cardinality' => FIELD_CARDINALITY_UNLIMITED, + 'settings' => array( + 'allowed_values' => array( + array( + 'vocabulary' => $vocabulary->id(), + 'parent' => '0', + ), + ), + ) + ); + field_create_field($field); + $instance = array( + 'field_name' => $field_name, + 'entity_type' => 'node', + 'bundle' => 'article', + ); + field_create_instance($instance); + entity_get_form_display('node', 'article', 'default') + ->setComponent($field_name, array( + 'type' => 'options_select', + )) + ->save(); + entity_get_display('node', 'article', 'full') + ->setComponent($field_name, array( + 'type' => 'taxonomy_term_reference_link', + )) + ->save(); + } +} diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index cad6ac0..22ed7af 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -268,7 +268,8 @@ function rdf_preprocess_node(&$variables) { // attribute indicates its RDF type (e.g., foaf:Document, sioc:Person, and so // on.) $mapping_manager = drupal_container()->get('rdf.mapping_manager'); - $bundle_mapping = $mapping_manager->getBundleMapping('node', $variables['type']); + $bundle = $variables['node']->bundle(); + $bundle_mapping = $mapping_manager->getBundleMapping('node', $bundle); $variables['attributes']['about'] = empty($variables['node_url']) ? NULL: $variables['node_url']; $variables['attributes']['typeof'] = empty($bundle_mapping['types']) ? NULL : $bundle_mapping['types']; @@ -276,7 +277,7 @@ function rdf_preprocess_node(&$variables) { // added to the

tag which might contain HTML code, we specify an empty // datatype to ensure the value of the title read by the RDFa parsers is a // literal. - $title_mapping = $mapping_manager->getFieldMapping('node', $variables['type'], 'title'); + $title_mapping = $mapping_manager->getFieldMapping('node', $bundle, 'title'); $variables['title_attributes']['property'] = empty($title_mapping['properties']) ? NULL : $title_mapping['properties']; $variables['title_attributes']['datatype'] = ''; @@ -297,7 +298,7 @@ function rdf_preprocess_node(&$variables) { } // Adds RDFa markup for the date. - $created_mapping = $mapping_manager->getFieldMapping('node', $variables['type'], 'created'); + $created_mapping = $mapping_manager->getFieldMapping('node', $bundle, 'created'); if (!empty($created_mapping)) { $date_attributes = rdf_rdfa_attributes($created_mapping, $variables['node']->created); $variables['rdf_template_variable_attributes']['date'] = $date_attributes; @@ -306,7 +307,7 @@ function rdf_preprocess_node(&$variables) { } } // Adds RDFa markup for the relation between the node and its author. - $uid_mapping = $mapping_manager->getFieldMapping('node', $variables['type'], 'uid'); + $uid_mapping = $mapping_manager->getFieldMapping('node', $bundle, 'uid'); if (!empty($uid_mapping)) { $variables['rdf_template_variable_attributes']['name']['rel'] = $uid_mapping['properties']; if ($variables['submitted']) { @@ -315,13 +316,15 @@ function rdf_preprocess_node(&$variables) { } // Adds RDFa markup annotating the number of comments a node has. - $comment_count_mapping = $mapping_manager->getFieldMapping('node', $variables['type'], 'comment_count'); + $comment_count_mapping = $mapping_manager->getFieldMapping('node', $bundle, 'comment_count'); if (isset($variables['node']->comment_count) && !empty($comment_count_mapping['properties'])) { // Annotates the 'x comments' link in teaser view. if (isset($variables['content']['links']['comment']['#links']['comment-comments'])) { $comment_count_attributes['property'] = $comment_count_mapping['properties']; $comment_count_attributes['content'] = $variables['node']->comment_count; - $comment_count_attributes['datatype'] = $comment_count_mapping['datatype']; + if (isset($comment_count_mapping['datatype'])) { + $comment_count_attributes['datatype'] = $comment_count_mapping['datatype']; + } // According to RDFa parsing rule number 4, a new subject URI is created // from the href attribute if no rel/rev attribute is present. To get the // original node URL from the about attribute of the parent container we @@ -409,17 +412,19 @@ function rdf_preprocess_user(&$variables) { // Adds the markup for username as language neutral literal, see // rdf_preprocess_username(). $name_mapping = $mapping_manager->getFieldMapping('user', 'user', 'name'); - $username_meta = array( - '#tag' => 'meta', - '#attributes' => array( - 'about' => url($uri['path'], $uri['options']), - 'property' => $name_mapping['properties'], - 'content' => $account->name, - 'lang' => '', - ) - ); + if (!empty($name_mapping['properties'])) { + $username_meta = array( + '#tag' => 'meta', + '#attributes' => array( + 'about' => url($uri['path'], $uri['options']), + 'property' => $name_mapping['properties'], + 'content' => $account->name, + 'lang' => '', + ) + ); + drupal_add_html_head($username_meta, 'rdf_user_username'); + } drupal_add_html_head($account_holder_meta, 'rdf_user_account_holder'); - drupal_add_html_head($username_meta, 'rdf_user_username'); } } @@ -576,11 +581,11 @@ function rdf_field_attach_view_alter(&$output, $context) { if (isset($item['entity'])) { $term = $item['entity']; $mapping_manager = drupal_container()->get('rdf.mapping_manager'); - $bundle_mapping = $mapping_manager->getBundleMapping('taxonomy_term', $term->vid); + $bundle_mapping = $mapping_manager->getBundleMapping('taxonomy_term', $term->bundle()); if (!empty($bundle_mapping['types'])) { $element[$delta]['#options']['attributes']['typeof'] = $bundle_mapping['types']; } - $name_field_mapping = $mapping_manager->getFieldMapping('taxonomy_term', $term->vid, 'name'); + $name_field_mapping = $mapping_manager->getFieldMapping('taxonomy_term', $term->bundle(), 'name'); if (!empty($name_field_mapping['properties'])) { // A property attribute is used with an empty datatype attribute so // the term name is parsed as a plain literal in RDFa 1.0 and 1.1. @@ -699,3 +704,139 @@ function theme_rdf_metadata($variables) { } return $output; } + +function rdf_save_standard_profile_mappings() { + // Save the Node RDF mapping configuration. + $rdf_mapping_manager = drupal_container()->get('rdf.mapping_manager'); + $node_shared_field_mappings = array( + 'title' => array( + 'properties' => array('dc:title'), + ), + 'created' => array( + 'properties' => array('dc:date', 'dc:created'), + 'datatype' => 'xsd:dateTime', + 'datatype_callback' => 'date_iso8601', + ), + 'changed' => array( + 'properties' => array('dc:modified'), + 'datatype' => 'xsd:dateTime', + 'datatype_callback' => 'date_iso8601', + ), + 'body' => array( + 'properties' => array('content:encoded'), + ), + 'uid' => array( + 'properties' => array('sioc:has_creator'), + 'mapping_type' => 'rel', + ), + 'name' => array( + 'properties' => array('foaf:name'), + ), + 'comment_count' => array( + 'properties' => array('sioc:num_replies'), + 'datatype' => 'xsd:integer', + ), + 'last_activity' => array( + 'properties' => array('sioc:last_activity_date'), + 'datatype' => 'xsd:dateTime', + 'datatype_callback' => 'date_iso8601', + ), + ); + // Save the bundle mapping and shared field mappings for both node bundles. + foreach (array('article', 'page') as $bundle) { + // Save bundle mapping config. + $config = $rdf_mapping_manager->getBundleMappingConfig('node', $bundle); + $bundle_mapping = array_merge($config->get(), array('types' => array('foaf:Document', 'sioc:Item'))); + $config->setData($bundle_mapping)->save(); + // Iterate over shared field mappings and save. + foreach ($node_shared_field_mappings as $field_name => $field_mapping) { + $config = $rdf_mapping_manager->getFieldMappingConfig('node', $bundle, $field_name); + $field_mapping = array_merge($config->get(), $field_mapping); + $config->setData($field_mapping)->save(); + } + } + + // Save the RDF mappings for fields which are unique to articles. + $field_image_mapping = array( + 'properties' => array('og:image', 'rdfs:seeAlso'), + 'mapping_type' => 'rel', + ); + $field_tags_mapping = array( + 'properties' => array('dc:subject'), + 'mapping_type' => 'rel', + ); + // Configure RDF mapping for field_image on Article bundle. + $config = $rdf_mapping_manager->getFieldMappingConfig('node', 'article', 'field_image'); + $field_mapping = array_merge($config->get(), $field_image_mapping); + $config->setData($field_mapping)->save(); + // Configure RDF mapping for field_tags on Article bundle. + $config = $rdf_mapping_manager->getFieldMappingConfig('node', 'article', 'field_tags'); + $field_mapping = array_merge($config->get(), $field_tags_mapping); + $config->setData($field_mapping)->save(); + + // Save comment RDF mapping configuration for article. + $rdf_mapping_manager = drupal_container()->get('rdf.mapping_manager'); + // Save bundle mapping config. + $config = $rdf_mapping_manager->getBundleMappingConfig('comment', 'comment_node_article'); + $bundle_mapping = array_merge($config->get(), array('types' => array('sioc:Post', 'sioct:Comment'))); + $config->setData($bundle_mapping)->save(); + $comment_shared_field_mappings = array( + 'title' => array( + 'properties' => array('dc:title'), + ), + 'created' => array( + 'properties' => array('dc:date', 'dc:created'), + 'datatype' => 'xsd:dateTime', + 'datatype_callback' => 'date_iso8601', + ), + 'changed' => array( + 'properties' => array('dc:modified'), + 'datatype' => 'xsd:dateTime', + 'datatype_callback' => 'date_iso8601', + ), + 'comment_body' => array( + 'properties' => array('content:encoded'), + ), + 'pid' => array( + 'properties' => array('sioc:reply_of'), + 'mapping_type' => 'rel', + ), + 'uid' => array( + 'properties' => array('sioc:has_creator'), + 'mapping_type' => 'rel', + ), + 'name' => array( + 'properties' => array('foaf:name'), + ), + ); + // Iterate over shared field mappings and save. + foreach ($comment_shared_field_mappings as $field_name => $field_mapping) { + $config = $rdf_mapping_manager->getFieldMappingConfig('comment', 'comment_node_article', $field_name); + $field_mapping = array_merge($config->get(), $field_mapping); + $config->setData($field_mapping)->save(); + } + + // Configure RDF mapping for User bundle. + $config = $rdf_mapping_manager->getBundleMappingConfig('user', 'user'); + $bundle_mapping = array_merge($config->get(), array('types' => array('sioc:UserAccount'))); + $config->setData($bundle_mapping)->save(); + // @todo Change to reflect field names once User uses real fields. + // Configure RDF mapping for name field on User bundle. + $config = $rdf_mapping_manager->getFieldMappingConfig('user', 'user', 'name'); + $field_mapping = array_merge($config->get(), array('properties' => array('foaf:name'))); + $config->setData($field_mapping)->save(); + // Configure RDF mapping for homepage field on User bundle. + $config = $rdf_mapping_manager->getFieldMappingConfig('user', 'user', 'homepage'); + $field_mapping = array_merge($config->get(), array('properties' => array('foaf:page'), 'mapping_type' => 'rel')); + $config->setData($field_mapping)->save(); + + // Configure RDF mapping for Tags bundle. + $config = $rdf_mapping_manager->getBundleMappingConfig('taxonomy_term', 'tags'); + $bundle_mapping = array_merge($config->get(), array('types' => array('skos:Concept'))); + $config->setData($bundle_mapping)->save(); + // Configure RDF mapping for name field on Tags bundle. + $config = $rdf_mapping_manager->getFieldMappingConfig('taxonomy_term', 'tags', 'name'); + $field_mapping = array_merge($config->get(), array('properties' => array('rdfs:label', 'skos:prefLabel'))); + $config->setData($field_mapping)->save(); +} + diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 6ddfe60..6a3c48e 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -121,136 +121,5 @@ function standard_install() { config('system.theme')->set('admin', 'seven')->save(); variable_set('node_admin_theme', '1'); - // Save the Node RDF mapping configuration. - $rdf_mapping_manager = drupal_container()->get('rdf.mapping_manager'); - $node_shared_field_mappings = array( - 'title' => array( - 'properties' => array('dc:title'), - ), - 'created' => array( - 'properties' => array('dc:date', 'dc:created'), - 'datatype' => 'xsd:dateTime', - 'datatype_callback' => 'date_iso8601', - ), - 'changed' => array( - 'properties' => array('dc:modified'), - 'datatype' => 'xsd:dateTime', - 'datatype_callback' => 'date_iso8601', - ), - 'body' => array( - 'properties' => array('content:encoded'), - ), - 'uid' => array( - 'properties' => array('sioc:has_creator'), - 'mapping_type' => 'rel', - ), - 'name' => array( - 'properties' => array('foaf:name'), - ), - 'comment_count' => array( - 'properties' => array('sioc:num_replies'), - 'datatype' => 'xsd:integer', - ), - 'last_activity' => array( - 'properties' => array('sioc:last_activity_date'), - 'datatype' => 'xsd:dateTime', - 'datatype_callback' => 'date_iso8601', - ), - ); - // Save the bundle mapping and shared field mappings for both node bundles. - foreach (array('article', 'page') as $bundle) { - // Save bundle mapping config. - $config = $rdf_mapping_manager->getBundleMappingConfig('node', $bundle); - $bundle_mapping = array_merge($config->get(), array('types' => array('foaf:Document', 'sioc:Item'))); - $config->setData($bundle_mapping)->save(); - // Iterate over shared field mappings and save. - foreach ($node_shared_field_mappings as $field_name => $field_mapping) { - $config = $rdf_mapping_manager->getFieldMappingConfig('node', $bundle, $field_name); - $field_mapping = array_merge($config->get(), $field_mapping); - $config->setData($field_mapping)->save(); - } - } - - // Save the RDF mappings for fields which are unique to articles. - $field_image_mapping = array( - 'properties' => array('og:image', 'rdfs:seeAlso'), - 'mapping_type' => 'rel', - ); - $field_tags_mapping = array( - 'properties' => array('dc:subject'), - 'mapping_type' => 'rel', - ); - // Configure RDF mapping for field_image on Article bundle. - $config = $rdf_mapping_manager->getFieldMappingConfig('node', 'article', 'field_image'); - $field_mapping = array_merge($config->get(), $field_image_mapping); - $config->setData($field_mapping)->save(); - // Configure RDF mapping for field_tags on Article bundle. - $config = $rdf_mapping_manager->getFieldMappingConfig('node', 'article', 'field_tags'); - $field_mapping = array_merge($config->get(), $field_tags_mapping); - $config->setData($field_mapping)->save(); - - // Save comment RDF mapping configuration for article. - $rdf_mapping_manager = drupal_container()->get('rdf.mapping_manager'); - // Save bundle mapping config. - $config = $rdf_mapping_manager->getBundleMappingConfig('comment', 'comment_node_article'); - $bundle_mapping = array_merge($config->get(), array('types' => array('sioc:Post', 'sioct:Comment'))); - $config->setData($bundle_mapping)->save(); - $comment_shared_field_mappings = array( - 'title' => array( - 'properties' => array('dc:title'), - ), - 'created' => array( - 'properties' => array('dc:date', 'dc:created'), - 'datatype' => 'xsd:dateTime', - 'datatype_callback' => 'date_iso8601', - ), - 'changed' => array( - 'properties' => array('dc:modified'), - 'datatype' => 'xsd:dateTime', - 'datatype_callback' => 'date_iso8601', - ), - 'comment_body' => array( - 'properties' => array('content:encoded'), - ), - 'pid' => array( - 'properties' => array('sioc:reply_of'), - 'mapping_type' => 'rel', - ), - 'uid' => array( - 'properties' => array('sioc:has_creator'), - 'mapping_type' => 'rel', - ), - 'name' => array( - 'properties' => array('foaf:name'), - ), - ); - // Iterate over shared field mappings and save. - foreach ($comment_shared_field_mappings as $field_name => $field_mapping) { - $config = $rdf_mapping_manager->getFieldMappingConfig('comment', 'comment_node_article', $field_name); - $field_mapping = array_merge($config->get(), $field_mapping); - $config->setData($field_mapping)->save(); - } - - // Configure RDF mapping for User bundle. - $config = $rdf_mapping_manager->getBundleMappingConfig('user', 'user'); - $bundle_mapping = array_merge($config->get(), array('types' => array('sioc:UserAccount'))); - $config->setData($bundle_mapping)->save(); - // @todo Change to reflect field names once User uses real fields. - // Configure RDF mapping for name field on User bundle. - $config = $rdf_mapping_manager->getFieldMappingConfig('user', 'user', 'name'); - $field_mapping = array_merge($config->get(), array('properties' => array('foaf:name'))); - $config->setData($field_mapping)->save(); - // Configure RDF mapping for homepage field on User bundle. - $config = $rdf_mapping_manager->getFieldMappingConfig('user', 'user', 'homepage'); - $field_mapping = array_merge($config->get(), array('properties' => array('foaf:page'), 'mapping_type' => 'rel')); - $config->setData($field_mapping)->save(); - - // Configure RDF mapping for Tags bundle. - $config = $rdf_mapping_manager->getBundleMappingConfig('taxonomy_term', 'tags'); - $bundle_mapping = array_merge($config->get(), array('types' => array('skos:Concept'))); - $config->setData($bundle_mapping)->save(); - // Configure RDF mapping for name field on Tags bundle. - $config = $rdf_mapping_manager->getFieldMappingConfig('taxonomy_term', 'tags', 'name'); - $field_mapping = array_merge($config->get(), array('properties' => array('rdfs:label', 'skos:prefLabel'))); - $config->setData($field_mapping)->save(); + rdf_save_standard_profile_mappings(); }