3 namespace Drupal\Tests\rdf\Functional;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\taxonomy\Tests\TaxonomyTestBase;
9 * Tests RDFa markup generation for taxonomy term fields.
13 class EntityReferenceFieldAttributesTest extends TaxonomyTestBase {
20 public static $modules = ['rdf', 'field_test', 'file', 'image'];
23 * The name of the taxonomy term reference field used in the test.
30 * The vocabulary object used in the test.
32 * @var \Drupal\taxonomy\VocabularyInterface
34 protected $vocabulary;
36 protected function setUp() {
39 $web_user = $this->drupalCreateUser(['bypass node access', 'administer taxonomy']);
40 $this->drupalLogin($web_user);
41 $this->vocabulary = $this->createVocabulary();
44 $this->fieldName = 'field_taxonomy_test';
47 $this->vocabulary->id() => $this->vocabulary->id(),
49 'auto_create' => TRUE,
51 $this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
53 entity_get_form_display('node', 'article', 'default')
54 ->setComponent($this->fieldName, ['type' => 'options_select'])
56 entity_get_display('node', 'article', 'full')
57 ->setComponent($this->fieldName, ['type' => 'entity_reference_label'])
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',
68 rdf_get_mapping('taxonomy_term', $this->vocabulary->id())
69 ->setBundleMapping(['types' => ['skos:Concept']])
70 ->setFieldMapping('name', ['properties' => ['rdfs:label']])
75 * Tests if file fields in teasers have correct resources.
77 * Ensure that file fields have the correct resource as the object in RDFa
78 * when displayed as a teaser.
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'])
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]);
93 $node = $this->drupalCreateNode(['type' => 'article']);
94 $node->set($this->fieldName, [
95 ['target_id' => $term1->id()],
96 ['target_id' => $term2->id()],
100 $node_render_array = entity_view_multiple([$node], 'teaser');
101 $html = \Drupal::service('renderer')->renderRoot($node_render_array);
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);
109 // Node relations to taxonomy terms.
110 $node_uri = $node->url('canonical', ['absolute' => TRUE]);
113 'value' => $taxonomy_term_1_uri,
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).');
118 'value' => $taxonomy_term_2_uri,
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.
125 'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
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).');
131 'value' => $term1->getName(),
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).');
137 'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
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).');
142 'value' => $term2->getName(),
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).');