ad743580dc79a150cc073a9463c080e9f340b919
[yaffs-website] / web / core / modules / rdf / tests / src / Functional / FileFieldAttributesTest.php
1 <?php
2
3 namespace Drupal\Tests\rdf\Functional;
4
5 use Drupal\file\Tests\FileFieldTestBase;
6 use Drupal\file\Entity\File;
7
8 /**
9  * Tests the RDFa markup of filefields.
10  *
11  * @group rdf
12  */
13 class FileFieldAttributesTest extends FileFieldTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['rdf', 'file'];
21
22   /**
23    * The name of the file field used in the test.
24    *
25    * @var string
26    */
27   protected $fieldName;
28
29   /**
30    * The file object used in the test.
31    *
32    * @var \Drupal\file\FileInterface
33    */
34   protected $file;
35
36   /**
37    * The node object used in the test.
38    *
39    * @var \Drupal\node\NodeInterface
40    */
41   protected $node;
42
43   protected function setUp() {
44     parent::setUp();
45     $node_storage = $this->container->get('entity.manager')->getStorage('node');
46     $this->fieldName = strtolower($this->randomMachineName());
47
48     $type_name = 'article';
49     $this->createFileField($this->fieldName, 'node', $type_name);
50
51     // Set the teaser display to show this field.
52     entity_get_display('node', 'article', 'teaser')
53       ->setComponent($this->fieldName, ['type' => 'file_default'])
54       ->save();
55
56     // Set the RDF mapping for the new field.
57     $mapping = rdf_get_mapping('node', 'article');
58     $mapping->setFieldMapping($this->fieldName, ['properties' => ['rdfs:seeAlso'], 'mapping_type' => 'rel'])->save();
59
60     $test_file = $this->getTestFile('text');
61
62     // Create a new node with the uploaded file.
63     $nid = $this->uploadNodeFile($test_file, $this->fieldName, $type_name);
64
65     $node_storage->resetCache([$nid]);
66     $this->node = $node_storage->load($nid);
67     $this->file = File::load($this->node->{$this->fieldName}->target_id);
68   }
69
70   /**
71    * Tests if file fields in teasers have correct resources.
72    *
73    * Ensure that file fields have the correct resource as the object in RDFa
74    * when displayed as a teaser.
75    */
76   public function testNodeTeaser() {
77     // Render the teaser.
78     $node_render_array = entity_view_multiple([$this->node], 'teaser');
79     $html = \Drupal::service('renderer')->renderRoot($node_render_array);
80
81     // Parses front page where the node is displayed in its teaser form.
82     $parser = new \EasyRdf_Parser_Rdfa();
83     $graph = new \EasyRdf_Graph();
84     $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
85     $parser->parse($graph, $html, 'rdfa', $base_uri);
86
87     $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
88     $file_uri = file_create_url($this->file->getFileUri());
89
90     // Node relation to attached file.
91     $expected_value = [
92       'type' => 'uri',
93       'value' => $file_uri,
94     ];
95     $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).');
96     $this->drupalGet('node');
97   }
98
99 }