View source
<?php
namespace Drupal\rdf\Tests;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\taxonomy\Tests\TaxonomyTestBase;
class EntityReferenceFieldAttributesTest extends TaxonomyTestBase {
public static $modules = array(
'rdf',
'field_test',
'file',
'image',
);
protected $fieldName;
protected $vocabulary;
protected function setUp() {
parent::setUp();
$web_user = $this
->drupalCreateUser(array(
'bypass node access',
'administer taxonomy',
));
$this
->drupalLogin($web_user);
$this->vocabulary = $this
->createVocabulary();
$this->fieldName = 'field_taxonomy_test';
$handler_settings = array(
'target_bundles' => array(
$this->vocabulary
->id() => $this->vocabulary
->id(),
),
'auto_create' => TRUE,
);
$this
->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
entity_get_form_display('node', 'article', 'default')
->setComponent($this->fieldName, array(
'type' => 'options_select',
))
->save();
entity_get_display('node', 'article', 'full')
->setComponent($this->fieldName, array(
'type' => 'entity_reference_label',
))
->save();
rdf_get_mapping('node', 'article')
->setFieldMapping($this->fieldName, array(
'properties' => array(
'dc:subject',
),
'mapping_type' => 'rel',
))
->save();
rdf_get_mapping('taxonomy_term', $this->vocabulary
->id())
->setBundleMapping(array(
'types' => array(
'skos:Concept',
),
))
->setFieldMapping('name', array(
'properties' => array(
'rdfs:label',
),
))
->save();
}
function testNodeTeaser() {
entity_get_display('node', 'article', 'teaser')
->setComponent($this->fieldName, array(
'type' => 'entity_reference_label',
))
->save();
$term1 = $this
->createTerm($this->vocabulary);
$term2 = $this
->createTerm($this->vocabulary);
$taxonomy_term_1_uri = $term1
->url('canonical', [
'absolute' => TRUE,
]);
$taxonomy_term_2_uri = $term2
->url('canonical', [
'absolute' => TRUE,
]);
$node = $this
->drupalCreateNode(array(
'type' => 'article',
));
$node
->set($this->fieldName, array(
array(
'target_id' => $term1
->id(),
),
array(
'target_id' => $term2
->id(),
),
));
$node_render_array = entity_view_multiple(array(
$node,
), 'teaser');
$html = \Drupal::service('renderer')
->renderRoot($node_render_array);
$parser = new \EasyRdf_Parser_Rdfa();
$graph = new \EasyRdf_Graph();
$base_uri = \Drupal::url('<front>', [], [
'absolute' => TRUE,
]);
$parser
->parse($graph, $html, 'rdfa', $base_uri);
$node_uri = $node
->url('canonical', [
'absolute' => TRUE,
]);
$expected_value = array(
'type' => 'uri',
'value' => $taxonomy_term_1_uri,
);
$this
->assertTrue($graph
->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
$expected_value = array(
'type' => 'uri',
'value' => $taxonomy_term_2_uri,
);
$this
->assertTrue($graph
->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
$expected_value = array(
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
);
$expected_value = array(
'type' => 'literal',
'value' => $term1
->getName(),
);
$expected_value = array(
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
);
$expected_value = array(
'type' => 'literal',
'value' => $term2
->getName(),
);
}
}