function EntityReferenceFieldAttributesTest::testNodeTeaser
Same name in other branches
- 8.9.x core/modules/rdf/tests/src/Functional/EntityReferenceFieldAttributesTest.php \Drupal\Tests\rdf\Functional\EntityReferenceFieldAttributesTest::testNodeTeaser()
Tests if file fields in teasers have correct resources.
Ensure that file fields have the correct resource as the object in RDFa when displayed as a teaser.
File
-
core/
modules/ rdf/ tests/ src/ Functional/ EntityReferenceFieldAttributesTest.php, line 109
Class
- EntityReferenceFieldAttributesTest
- Tests RDFa markup generation for taxonomy term fields.
Namespace
Drupal\Tests\rdf\FunctionalCode
public function testNodeTeaser() {
// Set the teaser display to show this field.
\Drupal::service('entity_display.repository')->getViewDisplay('node', 'article', 'teaser')
->setComponent($this->fieldName, [
'type' => 'entity_reference_label',
])
->save();
// Create a term in each vocabulary.
$term1 = $this->createTerm($this->vocabulary);
$term2 = $this->createTerm($this->vocabulary);
$taxonomy_term_1_uri = $term1->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$taxonomy_term_2_uri = $term2->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
// Create the node.
$node = $this->drupalCreateNode([
'type' => 'article',
]);
$node->set($this->fieldName, [
[
'target_id' => $term1->id(),
],
[
'target_id' => $term2->id(),
],
]);
// Render the node.
$node_render_array = \Drupal::entityTypeManager()->getViewBuilder('node')
->view($node, 'teaser');
$html = \Drupal::service('renderer')->renderRoot($node_render_array);
// Node relations to taxonomy terms.
$node_uri = $node->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$expected_value = [
'type' => 'uri',
'value' => $taxonomy_term_1_uri,
];
$this->assertTrue($this->hasRdfProperty($html, $this->baseUri, $node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
$expected_value = [
'type' => 'uri',
'value' => $taxonomy_term_2_uri,
];
$this->assertTrue($this->hasRdfProperty($html, $this->baseUri, $node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
// Taxonomy terms triples.
// Term 1.
$expected_value = [
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
];
// @todo Enable with https://www.drupal.org/node/2072791.
// $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).');
$expected_value = [
'type' => 'literal',
'value' => $term1->getName(),
];
// $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).');
// Term 2.
$expected_value = [
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
];
// $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).');
$expected_value = [
'type' => 'literal',
'value' => $term2->getName(),
];
// $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).');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.