Chris@0: drupalCreateUser(['bypass node access', 'administer taxonomy']); Chris@0: $this->drupalLogin($web_user); Chris@0: $this->vocabulary = $this->createVocabulary(); Chris@0: Chris@0: // Create the field. Chris@0: $this->fieldName = 'field_taxonomy_test'; Chris@0: $handler_settings = [ Chris@0: 'target_bundles' => [ Chris@0: $this->vocabulary->id() => $this->vocabulary->id(), Chris@0: ], Chris@0: 'auto_create' => TRUE, Chris@0: ]; Chris@0: $this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@0: Chris@0: entity_get_form_display('node', 'article', 'default') Chris@0: ->setComponent($this->fieldName, ['type' => 'options_select']) Chris@0: ->save(); Chris@0: entity_get_display('node', 'article', 'full') Chris@0: ->setComponent($this->fieldName, ['type' => 'entity_reference_label']) Chris@0: ->save(); Chris@0: Chris@0: // Set the RDF mapping for the new field. Chris@0: rdf_get_mapping('node', 'article') Chris@0: ->setFieldMapping($this->fieldName, [ Chris@0: 'properties' => ['dc:subject'], Chris@0: 'mapping_type' => 'rel', Chris@0: ]) Chris@0: ->save(); Chris@0: Chris@0: rdf_get_mapping('taxonomy_term', $this->vocabulary->id()) Chris@0: ->setBundleMapping(['types' => ['skos:Concept']]) Chris@0: ->setFieldMapping('name', ['properties' => ['rdfs:label']]) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if file fields in teasers have correct resources. Chris@0: * Chris@0: * Ensure that file fields have the correct resource as the object in RDFa Chris@0: * when displayed as a teaser. Chris@0: */ Chris@0: public function testNodeTeaser() { Chris@0: // Set the teaser display to show this field. Chris@0: entity_get_display('node', 'article', 'teaser') Chris@0: ->setComponent($this->fieldName, ['type' => 'entity_reference_label']) Chris@0: ->save(); Chris@0: Chris@0: // Create a term in each vocabulary. Chris@0: $term1 = $this->createTerm($this->vocabulary); Chris@0: $term2 = $this->createTerm($this->vocabulary); Chris@18: $taxonomy_term_1_uri = $term1->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@18: $taxonomy_term_2_uri = $term2->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@0: Chris@0: // Create the node. Chris@0: $node = $this->drupalCreateNode(['type' => 'article']); Chris@0: $node->set($this->fieldName, [ Chris@0: ['target_id' => $term1->id()], Chris@0: ['target_id' => $term2->id()], Chris@0: ]); Chris@0: Chris@0: // Render the node. Chris@0: $node_render_array = entity_view_multiple([$node], 'teaser'); Chris@0: $html = \Drupal::service('renderer')->renderRoot($node_render_array); Chris@0: Chris@0: // Parse the teaser. Chris@0: $parser = new \EasyRdf_Parser_Rdfa(); Chris@0: $graph = new \EasyRdf_Graph(); Chris@18: $base_uri = Url::fromRoute('', [], ['absolute' => TRUE])->toString(); Chris@0: $parser->parse($graph, $html, 'rdfa', $base_uri); Chris@0: Chris@0: // Node relations to taxonomy terms. Chris@18: $node_uri = $node->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@0: $expected_value = [ Chris@0: 'type' => 'uri', Chris@0: 'value' => $taxonomy_term_1_uri, Chris@0: ]; Chris@0: $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: $expected_value = [ Chris@0: 'type' => 'uri', Chris@0: 'value' => $taxonomy_term_2_uri, Chris@0: ]; Chris@0: $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: // Taxonomy terms triples. Chris@0: // Term 1. Chris@0: $expected_value = [ Chris@0: 'type' => 'uri', Chris@0: 'value' => 'http://www.w3.org/2004/02/skos/core#Concept', Chris@0: ]; Chris@0: // @todo Enable with https://www.drupal.org/node/2072791. Chris@0: // $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: $expected_value = [ Chris@0: 'type' => 'literal', Chris@0: 'value' => $term1->getName(), Chris@0: ]; Chris@0: // $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: // Term 2. Chris@0: $expected_value = [ Chris@0: 'type' => 'uri', Chris@0: 'value' => 'http://www.w3.org/2004/02/skos/core#Concept', Chris@0: ]; Chris@0: // $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: $expected_value = [ Chris@0: 'type' => 'literal', Chris@0: 'value' => $term2->getName(), Chris@0: ]; Chris@0: // $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: } Chris@0: Chris@0: }