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