d4d7d35948e6cde5434bb6f7b1bfdc5f60307941
[yaffs-website] / web / core / modules / rdf / src / Tests / ImageFieldAttributesTest.php
1 <?php
2
3 namespace Drupal\rdf\Tests;
4
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\image\Tests\ImageFieldTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\file\Entity\File;
9
10 /**
11  * Tests the RDFa markup of imagefields.
12  *
13  * @group rdf
14  */
15 class ImageFieldAttributesTest extends ImageFieldTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['rdf', 'image'];
23
24   /**
25    * The name of the image field used in the test.
26    *
27    * @var string
28    */
29   protected $fieldName;
30
31   /**
32    * The file object used in the test.
33    *
34    * @var \Drupal\file\FileInterface
35    */
36   protected $file;
37
38   /**
39    * The node object used in the test.
40    *
41    * @var \Drupal\node\NodeInterface
42    */
43   protected $node;
44
45   protected function setUp() {
46     parent::setUp();
47
48     $this->fieldName = 'field_image';
49
50     // Create the image field.
51     $this->createImageField($this->fieldName, 'article');
52
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',
58       ])
59       ->setBundleMapping(['types' => []])
60       ->save();
61
62     // Get the test image that simpletest provides.
63     $image = current($this->drupalGetTestFiles('image'));
64
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);
69   }
70
71   /**
72    * Tests that image fields in teasers have correct resources.
73    */
74   public function testNodeTeaser() {
75     // Set the display options for the teaser.
76     $display_options = [
77       'type' => 'image',
78       'settings' => ['image_style' => 'medium', 'image_link' => 'content'],
79     ];
80     $display = entity_get_display('node', 'article', 'teaser');
81     $display->setComponent($this->fieldName, $display_options)
82       ->save();
83
84     // Render the teaser.
85     $node_render_array = node_view($this->node, 'teaser');
86     $html = \Drupal::service('renderer')->renderRoot($node_render_array);
87
88     // Parse the teaser.
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);
93
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());
97
98     // Test relations from node to image.
99     $expected_value = [
100       'type' => 'uri',
101       'value' => $image_uri,
102     ];
103     $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).');
104
105     // Test image type.
106     $expected_value = [
107       'type' => 'uri',
108       'value' => 'http://xmlns.com/foaf/0.1/Image',
109     ];
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).');
111   }
112
113 }