3 namespace Drupal\rdf\Tests;
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\image\Tests\ImageFieldTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\file\Entity\File;
11 * Tests the RDFa markup of imagefields.
15 class ImageFieldAttributesTest extends ImageFieldTestBase {
22 public static $modules = ['rdf', 'image'];
25 * The name of the image field used in the test.
32 * The file object used in the test.
34 * @var \Drupal\file\FileInterface
39 * The node object used in the test.
41 * @var \Drupal\node\NodeInterface
45 protected function setUp() {
48 $this->fieldName = 'field_image';
50 // Create the image field.
51 $this->createImageField($this->fieldName, 'article');
53 // Set the RDF mapping for the new field.
54 rdf_get_mapping('node', 'article')
55 ->setFieldMapping($this->fieldName, [
56 'properties' => ['og:image'],
57 'mapping_type' => 'rel',
59 ->setBundleMapping(['types' => []])
62 // Get the test image that simpletest provides.
63 $image = current($this->drupalGetTestFiles('image'));
65 // Save a node with the image.
66 $nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName());
67 $this->node = Node::load($nid);
68 $this->file = File::load($this->node->{$this->fieldName}->target_id);
72 * Tests that image fields in teasers have correct resources.
74 public function testNodeTeaser() {
75 // Set the display options for the teaser.
78 'settings' => ['image_style' => 'medium', 'image_link' => 'content'],
80 $display = entity_get_display('node', 'article', 'teaser');
81 $display->setComponent($this->fieldName, $display_options)
85 $node_render_array = node_view($this->node, 'teaser');
86 $html = \Drupal::service('renderer')->renderRoot($node_render_array);
89 $parser = new \EasyRdf_Parser_Rdfa();
90 $graph = new \EasyRdf_Graph();
91 $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
92 $parser->parse($graph, $html, 'rdfa', $base_uri);
94 // Construct the node and image URIs for testing.
95 $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
96 $image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri());
98 // Test relations from node to image.
101 'value' => $image_uri,
103 $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).');
108 'value' => 'http://xmlns.com/foaf/0.1/Image',
110 $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).');