Chris@0: fieldName = 'field_image'; Chris@0: Chris@0: // Create the image field. Chris@0: $this->createImageField($this->fieldName, 'article'); Chris@0: Chris@0: // Set the RDF mapping for the new field. Chris@0: rdf_get_mapping('node', 'article') Chris@0: ->setFieldMapping($this->fieldName, [ Chris@0: 'properties' => ['og:image'], Chris@0: 'mapping_type' => 'rel', Chris@0: ]) Chris@0: ->setBundleMapping(['types' => []]) Chris@0: ->save(); Chris@0: Chris@0: // Get the test image that simpletest provides. Chris@0: $image = current($this->drupalGetTestFiles('image')); Chris@0: Chris@0: // Save a node with the image. Chris@0: $nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName()); Chris@0: $this->node = Node::load($nid); Chris@0: $this->file = File::load($this->node->{$this->fieldName}->target_id); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that image fields in teasers have correct resources. Chris@0: */ Chris@0: public function testNodeTeaser() { Chris@0: // Set the display options for the teaser. Chris@0: $display_options = [ Chris@0: 'type' => 'image', Chris@0: 'settings' => ['image_style' => 'medium', 'image_link' => 'content'], Chris@0: ]; Chris@0: $display = entity_get_display('node', 'article', 'teaser'); Chris@0: $display->setComponent($this->fieldName, $display_options) Chris@0: ->save(); Chris@0: Chris@0: // Render the teaser. Chris@0: $node_render_array = node_view($this->node, 'teaser'); Chris@0: $html = \Drupal::service('renderer')->renderRoot($node_render_array); Chris@0: Chris@0: // Parse the teaser. Chris@0: $parser = new \EasyRdf_Parser_Rdfa(); Chris@0: $graph = new \EasyRdf_Graph(); Chris@0: $base_uri = \Drupal::url('', [], ['absolute' => TRUE]); Chris@0: $parser->parse($graph, $html, 'rdfa', $base_uri); Chris@0: Chris@0: // Construct the node and image URIs for testing. Chris@0: $node_uri = $this->node->url('canonical', ['absolute' => TRUE]); Chris@0: $image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri()); Chris@0: Chris@0: // Test relations from node to image. Chris@0: $expected_value = [ Chris@0: 'type' => 'uri', Chris@0: 'value' => $image_uri, Chris@0: ]; Chris@0: $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).'); Chris@0: Chris@0: // Test image type. Chris@0: $expected_value = [ Chris@0: 'type' => 'uri', Chris@0: 'value' => 'http://xmlns.com/foaf/0.1/Image', Chris@0: ]; Chris@0: $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).'); Chris@0: } Chris@0: Chris@0: }