annotate core/modules/rdf/tests/src/Functional/EntityReferenceFieldAttributesTest.php @ 19:fa3358dc1485 tip

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