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