3 namespace Drupal\Tests\rdf\Functional;
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\Tests\image\Functional\ImageFieldTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\file\Entity\File;
9 use Drupal\Tests\TestFileCreationTrait;
12 * Tests the RDFa markup of imagefields.
16 class ImageFieldAttributesTest extends ImageFieldTestBase {
18 use TestFileCreationTrait {
19 getTestFiles as drupalGetTestFiles;
27 public static $modules = ['rdf', 'image'];
30 * The name of the image field used in the test.
37 * The file object used in the test.
39 * @var \Drupal\file\FileInterface
44 * The node object used in the test.
46 * @var \Drupal\node\NodeInterface
50 protected function setUp() {
53 $this->fieldName = 'field_image';
55 // Create the image field.
56 $this->createImageField($this->fieldName, 'article');
58 // Set the RDF mapping for the new field.
59 rdf_get_mapping('node', 'article')
60 ->setFieldMapping($this->fieldName, [
61 'properties' => ['og:image'],
62 'mapping_type' => 'rel',
64 ->setBundleMapping(['types' => []])
67 // Get the test image that simpletest provides.
68 $image = current($this->drupalGetTestFiles('image'));
70 // Save a node with the image.
71 $nid = $this->uploadNodeImage($image, $this->fieldName, 'article', $this->randomMachineName());
72 $this->node = Node::load($nid);
73 $this->file = File::load($this->node->{$this->fieldName}->target_id);
77 * Tests that image fields in teasers have correct resources.
79 public function testNodeTeaser() {
80 // Set the display options for the teaser.
83 'settings' => ['image_style' => 'medium', 'image_link' => 'content'],
85 $display = entity_get_display('node', 'article', 'teaser');
86 $display->setComponent($this->fieldName, $display_options)
90 $node_render_array = node_view($this->node, 'teaser');
91 $html = \Drupal::service('renderer')->renderRoot($node_render_array);
94 $parser = new \EasyRdf_Parser_Rdfa();
95 $graph = new \EasyRdf_Graph();
96 $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
97 $parser->parse($graph, $html, 'rdfa', $base_uri);
99 // Construct the node and image URIs for testing.
100 $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
101 $image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri());
103 // Test relations from node to image.
106 'value' => $image_uri,
108 $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).');
113 'value' => 'http://xmlns.com/foaf/0.1/Image',
115 $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).');