annotate core/modules/rdf/tests/src/Functional/EntityReferenceFieldAttributesTest.php @ 16:c2387f117808

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