View source
<?php
namespace Drupal\Tests\rdf\Functional;
use Drupal\Core\Url;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\rdf\Traits\RdfParsingTrait;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestBase;
class EntityReferenceFieldAttributesTest extends TaxonomyTestBase {
use RdfParsingTrait;
protected static $modules = [
'rdf',
'field_test',
'file',
'image',
];
protected $defaultTheme = 'stark';
protected $baseUri;
protected $fieldName;
protected $vocabulary;
protected function setUp() : void {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'bypass node access',
'administer taxonomy',
]);
$this
->drupalLogin($web_user);
$this->vocabulary = $this
->createVocabulary();
$this->fieldName = 'field_taxonomy_test';
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', 'article')
->setComponent($this->fieldName, [
'type' => 'options_select',
])
->save();
$display_repository
->getViewDisplay('node', 'article', 'full')
->setComponent($this->fieldName, [
'type' => 'entity_reference_label',
])
->save();
rdf_get_mapping('node', 'article')
->setFieldMapping($this->fieldName, [
'properties' => [
'dc:subject',
],
'mapping_type' => 'rel',
])
->save();
rdf_get_mapping('taxonomy_term', $this->vocabulary
->id())
->setBundleMapping([
'types' => [
'skos:Concept',
],
])
->setFieldMapping('name', [
'properties' => [
'rdfs:label',
],
])
->save();
$this->baseUri = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])
->toString();
}
public function testNodeTeaser() {
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article', 'teaser')
->setComponent($this->fieldName, [
'type' => 'entity_reference_label',
])
->save();
$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();
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$node
->set($this->fieldName, [
[
'target_id' => $term1
->id(),
],
[
'target_id' => $term2
->id(),
],
]);
$node_render_array = \Drupal::entityTypeManager()
->getViewBuilder('node')
->view($node, 'teaser');
$html = \Drupal::service('renderer')
->renderRoot($node_render_array);
$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).');
$expected_value = [
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
];
$expected_value = [
'type' => 'literal',
'value' => $term1
->getName(),
];
$expected_value = [
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
];
$expected_value = [
'type' => 'literal',
'value' => $term2
->getName(),
];
}
}