annotate core/modules/rdf/tests/src/Functional/NodeAttributesTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children c2387f117808
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\node\Tests\NodeTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests the RDFa markup of Nodes.
Chris@0 9 *
Chris@0 10 * @group rdf
Chris@0 11 */
Chris@0 12 class NodeAttributesTest extends NodeTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Modules to enable.
Chris@0 16 *
Chris@0 17 * @var array
Chris@0 18 */
Chris@0 19 public static $modules = ['rdf'];
Chris@0 20
Chris@0 21 protected function setUp() {
Chris@0 22 parent::setUp();
Chris@0 23
Chris@0 24 rdf_get_mapping('node', 'article')
Chris@0 25 ->setBundleMapping([
Chris@0 26 'types' => ['sioc:Item', 'foaf:Document'],
Chris@0 27 ])
Chris@0 28 ->setFieldMapping('title', [
Chris@0 29 'properties' => ['dc:title'],
Chris@0 30 ])
Chris@0 31 ->setFieldMapping('created', [
Chris@0 32 'properties' => ['dc:date', 'dc:created'],
Chris@0 33 'datatype' => 'xsd:dateTime',
Chris@0 34 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
Chris@0 35 ])
Chris@0 36 ->save();
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Creates a node of type article and tests its RDFa markup.
Chris@0 41 */
Chris@0 42 public function testNodeAttributes() {
Chris@0 43 // Create node with single quotation mark title to ensure it does not get
Chris@0 44 // escaped more than once.
Chris@0 45 $node = $this->drupalCreateNode([
Chris@0 46 'type' => 'article',
Chris@0 47 'title' => $this->randomMachineName(8) . "'",
Chris@0 48 ]);
Chris@0 49
Chris@0 50 $node_uri = $node->url('canonical', ['absolute' => TRUE]);
Chris@0 51 $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
Chris@0 52
Chris@0 53 // Parses front page where the node is displayed in its teaser form.
Chris@0 54 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 55 $graph = new \EasyRdf_Graph();
Chris@0 56 $parser->parse($graph, $this->drupalGet('node/' . $node->id()), 'rdfa', $base_uri);
Chris@0 57
Chris@0 58 // Inspects RDF graph output.
Chris@0 59 // Node type.
Chris@0 60 $expected_value = [
Chris@0 61 'type' => 'uri',
Chris@0 62 'value' => 'http://rdfs.org/sioc/ns#Item',
Chris@0 63 ];
Chris@0 64 $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Node type found in RDF output (sioc:Item).');
Chris@0 65 // Node type.
Chris@0 66 $expected_value = [
Chris@0 67 'type' => 'uri',
Chris@0 68 'value' => 'http://xmlns.com/foaf/0.1/Document',
Chris@0 69 ];
Chris@0 70 $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Node type found in RDF output (foaf:Document).');
Chris@0 71 // Node title.
Chris@0 72 $expected_value = [
Chris@0 73 'type' => 'literal',
Chris@0 74 'value' => $node->getTitle(),
Chris@0 75 'lang' => 'en',
Chris@0 76 ];
Chris@0 77 $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Node title found in RDF output (dc:title).');
Chris@0 78 // Node date (date format must be UTC).
Chris@0 79 $expected_value = [
Chris@0 80 'type' => 'literal',
Chris@0 81 'value' => \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'custom', 'c', 'UTC'),
Chris@0 82 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
Chris@0 83 ];
Chris@0 84 $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Node date found in RDF output (dc:date).');
Chris@0 85 // Node date (date format must be UTC).
Chris@0 86 $expected_value = [
Chris@0 87 'type' => 'literal',
Chris@0 88 'value' => \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'custom', 'c', 'UTC'),
Chris@0 89 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
Chris@0 90 ];
Chris@0 91 $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Node date found in RDF output (dc:created).');
Chris@0 92 }
Chris@0 93
Chris@0 94 }