Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / rdf / tests / src / Functional / EntityReferenceFieldAttributesTest.php
1 <?php
2
3 namespace Drupal\Tests\rdf\Functional;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Tests\taxonomy\Functional\TaxonomyTestBase;
7
8 /**
9  * Tests RDFa markup generation for taxonomy term fields.
10  *
11  * @group rdf
12  */
13 class EntityReferenceFieldAttributesTest extends TaxonomyTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['rdf', 'field_test', 'file', 'image'];
21
22   /**
23    * The name of the taxonomy term reference field used in the test.
24    *
25    * @var string
26    */
27   protected $fieldName;
28
29   /**
30    * The vocabulary object used in the test.
31    *
32    * @var \Drupal\taxonomy\VocabularyInterface
33    */
34   protected $vocabulary;
35
36   protected function setUp() {
37     parent::setUp();
38
39     $web_user = $this->drupalCreateUser(['bypass node access', 'administer taxonomy']);
40     $this->drupalLogin($web_user);
41     $this->vocabulary = $this->createVocabulary();
42
43     // Create the field.
44     $this->fieldName = 'field_taxonomy_test';
45     $handler_settings = [
46       'target_bundles' => [
47         $this->vocabulary->id() => $this->vocabulary->id(),
48       ],
49       'auto_create' => TRUE,
50     ];
51     $this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
52
53     entity_get_form_display('node', 'article', 'default')
54       ->setComponent($this->fieldName, ['type' => 'options_select'])
55       ->save();
56     entity_get_display('node', 'article', 'full')
57       ->setComponent($this->fieldName, ['type' => 'entity_reference_label'])
58       ->save();
59
60     // Set the RDF mapping for the new field.
61     rdf_get_mapping('node', 'article')
62       ->setFieldMapping($this->fieldName, [
63         'properties' => ['dc:subject'],
64         'mapping_type' => 'rel',
65       ])
66       ->save();
67
68     rdf_get_mapping('taxonomy_term', $this->vocabulary->id())
69       ->setBundleMapping(['types' => ['skos:Concept']])
70       ->setFieldMapping('name', ['properties' => ['rdfs:label']])
71       ->save();
72   }
73
74   /**
75    * Tests if file fields in teasers have correct resources.
76    *
77    * Ensure that file fields have the correct resource as the object in RDFa
78    * when displayed as a teaser.
79    */
80   public function testNodeTeaser() {
81     // Set the teaser display to show this field.
82     entity_get_display('node', 'article', 'teaser')
83       ->setComponent($this->fieldName, ['type' => 'entity_reference_label'])
84       ->save();
85
86     // Create a term in each vocabulary.
87     $term1 = $this->createTerm($this->vocabulary);
88     $term2 = $this->createTerm($this->vocabulary);
89     $taxonomy_term_1_uri = $term1->url('canonical', ['absolute' => TRUE]);
90     $taxonomy_term_2_uri = $term2->url('canonical', ['absolute' => TRUE]);
91
92     // Create the node.
93     $node = $this->drupalCreateNode(['type' => 'article']);
94     $node->set($this->fieldName, [
95       ['target_id' => $term1->id()],
96       ['target_id' => $term2->id()],
97     ]);
98
99     // Render the node.
100     $node_render_array = entity_view_multiple([$node], 'teaser');
101     $html = \Drupal::service('renderer')->renderRoot($node_render_array);
102
103     // Parse the teaser.
104     $parser = new \EasyRdf_Parser_Rdfa();
105     $graph = new \EasyRdf_Graph();
106     $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
107     $parser->parse($graph, $html, 'rdfa', $base_uri);
108
109     // Node relations to taxonomy terms.
110     $node_uri = $node->url('canonical', ['absolute' => TRUE]);
111     $expected_value = [
112       'type' => 'uri',
113       'value' => $taxonomy_term_1_uri,
114     ];
115     $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
116     $expected_value = [
117       'type' => 'uri',
118       'value' => $taxonomy_term_2_uri,
119     ];
120     $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
121     // Taxonomy terms triples.
122     // Term 1.
123     $expected_value = [
124       'type' => 'uri',
125       'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
126     ];
127     // @todo Enable with https://www.drupal.org/node/2072791.
128     //$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
129     $expected_value = [
130       'type' => 'literal',
131       'value' => $term1->getName(),
132     ];
133     //$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
134     // Term 2.
135     $expected_value = [
136       'type' => 'uri',
137       'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
138     ];
139     //$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
140     $expected_value = [
141       'type' => 'literal',
142       'value' => $term2->getName(),
143     ];
144     //$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
145   }
146
147 }