Esempio n. 1
0
 /**
  * Creates a node of type article and tests its RDFa markup.
  */
 function testNodeAttributes()
 {
     // Create node with single quotation mark title to ensure it does not get
     // escaped more than once.
     $node = $this->drupalCreateNode(array('type' => 'article', 'title' => $this->randomMachineName(8) . "'"));
     $node_uri = $node->url('canonical', ['absolute' => TRUE]);
     $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
     // Parses front page where the node is displayed in its teaser form.
     $parser = new \EasyRdf_Parser_Rdfa();
     $graph = new \EasyRdf_Graph();
     $parser->parse($graph, $this->drupalGet('node/' . $node->id()), 'rdfa', $base_uri);
     // Inspects RDF graph output.
     // Node type.
     $expected_value = array('type' => 'uri', 'value' => 'http://rdfs.org/sioc/ns#Item');
     $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).');
     // Node type.
     $expected_value = array('type' => 'uri', 'value' => 'http://xmlns.com/foaf/0.1/Document');
     $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).');
     // Node title.
     $expected_value = array('type' => 'literal', 'value' => $node->getTitle(), 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Node title found in RDF output (dc:title).');
     // Node date (date format must be UTC).
     $expected_value = array('type' => 'literal', 'value' => \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'custom', 'c', 'UTC'), 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime');
     $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Node date found in RDF output (dc:date).');
     // Node date (date format must be UTC).
     $expected_value = array('type' => 'literal', 'value' => \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'custom', 'c', 'UTC'), 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime');
     $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Node date found in RDF output (dc:created).');
 }
 /**
  * Tests if file fields in teasers have correct resources.
  *
  * Ensure that file fields have the correct resource as the object in RDFa
  * when displayed as a teaser.
  */
 function testNodeTeaser()
 {
     // Render the teaser.
     $node_render_array = entity_view_multiple(array($this->node), 'teaser');
     $html = \Drupal::service('renderer')->renderRoot($node_render_array);
     // Parses front page where the node is displayed in its teaser form.
     $parser = new \EasyRdf_Parser_Rdfa();
     $graph = new \EasyRdf_Graph();
     $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
     $parser->parse($graph, $html, 'rdfa', $base_uri);
     $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
     $file_uri = file_create_url($this->file->getFileUri());
     // Node relation to attached file.
     $expected_value = array('type' => 'uri', 'value' => $file_uri);
     $this->assertTrue($graph->hasProperty($node_uri, 'http://www.w3.org/2000/01/rdf-schema#seeAlso', $expected_value), 'Node to file relation found in RDF output (rdfs:seeAlso).');
     $this->drupalGet('node');
 }
 /**
  * Tests if default mapping for user is being used.
  *
  * Creates a random user and ensures the default mapping for the user is
  * being used.
  */
 function testUserAttributesInMarkup()
 {
     // Creates users that should and should not be truncated
     // by template_preprocess_username (20 characters)
     // one of these users tests right on the cusp (20).
     $user1 = $this->drupalCreateUser(array('access user profiles'));
     $authors = array($this->drupalCreateUser(array(), $this->randomMachineName(30)), $this->drupalCreateUser(array(), $this->randomMachineName(20)), $this->drupalCreateUser(array(), $this->randomMachineName(5)));
     $this->drupalLogin($user1);
     $this->drupalCreateContentType(array('type' => 'article'));
     /** @var \Drupal\user\UserInterface[] $authors */
     foreach ($authors as $author) {
         $account_uri = $author->url('canonical', ['absolute' => TRUE]);
         // Parses the user profile page where the default bundle mapping for user
         // should be used.
         $parser = new \EasyRdf_Parser_Rdfa();
         $graph = new \EasyRdf_Graph();
         $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
         $parser->parse($graph, $this->drupalGet('user/' . $author->id()), 'rdfa', $base_uri);
         // Inspects RDF graph output.
         // User type.
         $expected_value = array('type' => 'uri', 'value' => 'http://rdfs.org/sioc/ns#UserAccount');
         $this->assertTrue($graph->hasProperty($account_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'User type found in RDF output (sioc:UserAccount).');
         // User name.
         $expected_value = array('type' => 'literal', 'value' => $author->getUsername());
         $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
         // User creates a node.
         $this->drupalLogin($author);
         $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
         $this->drupalLogin($user1);
         // Parses the node created by the user.
         $parser = new \EasyRdf_Parser_Rdfa();
         $graph = new \EasyRdf_Graph();
         $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
         $parser->parse($graph, $this->drupalGet('node/' . $node->id()), 'rdfa', $base_uri);
         // Ensures the default bundle mapping for user is used on the Authored By
         // information on the node.
         $expected_value = array('type' => 'uri', 'value' => 'http://rdfs.org/sioc/ns#UserAccount');
         $this->assertTrue($graph->hasProperty($account_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'User type found in RDF output (sioc:UserAccount).');
         // User name.
         $expected_value = array('type' => 'literal', 'value' => $author->getUsername());
         $this->assertTrue($graph->hasProperty($account_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'User name found in RDF output (foaf:name).');
     }
 }
 /**
  * Tests if file fields in teasers have correct resources.
  *
  * Ensure that file fields have the correct resource as the object in RDFa
  * when displayed as a teaser.
  */
 function testNodeTeaser()
 {
     // Set the teaser display to show this field.
     entity_get_display('node', 'article', 'teaser')->setComponent($this->fieldName, array('type' => 'taxonomy_term_reference_link'))->save();
     // Create a term in each vocabulary.
     $term1 = $this->createTerm($this->vocabulary);
     $term2 = $this->createTerm($this->vocabulary);
     $taxonomy_term_1_uri = url('taxonomy/term/' . $term1->id(), array('absolute' => TRUE));
     $taxonomy_term_2_uri = url('taxonomy/term/' . $term2->id(), array('absolute' => TRUE));
     // Create the node.
     $node = $this->drupalCreateNode(array('type' => 'article'));
     $node->set($this->fieldName, array(array('target_id' => $term1->id()), array('target_id' => $term2->id())));
     // Render the node.
     $node_render_array = entity_view_multiple(array($node), 'teaser');
     $html = drupal_render($node_render_array);
     // Parse the teaser.
     $parser = new \EasyRdf_Parser_Rdfa();
     $graph = new \EasyRdf_Graph();
     $base_uri = url('<front>', array('absolute' => TRUE));
     $parser->parse($graph, $html, 'rdfa', $base_uri);
     // Node relations to taxonomy terms.
     $node_uri = url('node/' . $node->id(), array('absolute' => TRUE));
     $expected_value = array('type' => 'uri', 'value' => $taxonomy_term_1_uri);
     $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
     $expected_value = array('type' => 'uri', 'value' => $taxonomy_term_2_uri);
     $this->assertTrue($graph->hasProperty($node_uri, 'http://purl.org/dc/terms/subject', $expected_value), 'Node to term relation found in RDF output (dc:subject).');
     // Taxonomy terms triples.
     // Term 1.
     $expected_value = array('type' => 'uri', 'value' => 'http://www.w3.org/2004/02/skos/core#Concept');
     // @todo enable with https://drupal.org/node/2072791
     //$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).');
     $expected_value = array('type' => 'literal', 'value' => $term1->getName());
     //$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).');
     // Term 2.
     $expected_value = array('type' => 'uri', 'value' => 'http://www.w3.org/2004/02/skos/core#Concept');
     //$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).');
     $expected_value = array('type' => 'literal', 'value' => $term2->getName());
     //$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).');
 }
 /**
  * Creates a random term and ensures the RDF output is correct.
  */
 function testTaxonomyTermRdfaAttributes()
 {
     $term = $this->createTerm($this->vocabulary);
     $term_uri = $term->url('canonical', ['absolute' => TRUE]);
     // Parses the term's page and checks that the RDF output is correct.
     $parser = new \EasyRdf_Parser_Rdfa();
     $graph = new \EasyRdf_Graph();
     $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
     $parser->parse($graph, $this->drupalGet('taxonomy/term/' . $term->id()), 'rdfa', $base_uri);
     // Inspects RDF graph output.
     // Term type.
     $expected_value = array('type' => 'uri', 'value' => 'http://www.w3.org/2004/02/skos/core#Concept');
     $this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Term type found in RDF output (skos:Concept).');
     // Term label.
     $expected_value = array('type' => 'literal', 'value' => $term->getName(), 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Term label found in RDF output (rdfs:label).');
     // Term label.
     $expected_value = array('type' => 'literal', 'value' => $term->getName(), 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($term_uri, 'http://www.w3.org/2004/02/skos/core#prefLabel', $expected_value), 'Term label found in RDF output (skos:prefLabel).');
     // @todo Add test for term description once it is a field:
     // https://drupal.org/node/569434
 }
 /**
  * Tests that image fields in teasers have correct resources.
  */
 function testNodeTeaser()
 {
     // Set the display options for the teaser.
     $display_options = array('type' => 'image', 'settings' => array('image_style' => 'medium', 'image_link' => 'content'));
     $display = entity_get_display('node', 'article', 'teaser');
     $display->setComponent($this->fieldName, $display_options)->save();
     // Render the teaser.
     $node_render_array = node_view($this->node, 'teaser');
     $html = \Drupal::service('renderer')->renderRoot($node_render_array);
     // Parse the teaser.
     $parser = new \EasyRdf_Parser_Rdfa();
     $graph = new \EasyRdf_Graph();
     $base_uri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
     $parser->parse($graph, $html, 'rdfa', $base_uri);
     // Construct the node and image URIs for testing.
     $node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
     $image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri());
     // Test relations from node to image.
     $expected_value = array('type' => 'uri', 'value' => $image_uri);
     $this->assertTrue($graph->hasProperty($node_uri, 'http://ogp.me/ns#image', $expected_value), 'Node to file relation found in RDF output (og:image).');
     // Test image type.
     $expected_value = array('type' => 'uri', 'value' => 'http://xmlns.com/foaf/0.1/Image');
     $this->assertTrue($graph->hasProperty($image_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Image type found in RDF output (foaf:Image).');
 }
Esempio n. 7
0
 /**
  * Get the EasyRdf_Graph object for a page.
  *
  * @param \Drupal\Core\Url $url
  *   The URL object for the page.
  *
  * @return \EasyRdf_Graph
  *   The RDF graph object.
  */
 protected function getRdfGraph(Url $url)
 {
     $parser = new \EasyRdf_Parser_Rdfa();
     $graph = new \EasyRdf_Graph();
     $parser->parse($graph, $this->drupalGet($url), 'rdfa', $this->baseUri);
     return $graph;
 }
 /**
  * Tests RDF comment replies.
  */
 public function testCommentReplyOfRdfaMarkup()
 {
     // Posts comment #1 on behalf of registered user.
     $this->drupalLogin($this->web_user);
     $comment_1 = $this->saveComment($this->node->id(), $this->web_user->id());
     $comment_1_uri = url('comment/' . $comment_1->id(), array('absolute' => TRUE));
     // Posts a reply to the first comment.
     $comment_2 = $this->saveComment($this->node->id(), $this->web_user->id(), NULL, $comment_1->id());
     $comment_2_uri = url('comment/' . $comment_2->id(), array('absolute' => TRUE));
     $parser = new \EasyRdf_Parser_Rdfa();
     $graph = new \EasyRdf_Graph();
     $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->base_uri);
     // Tests the reply_of relationship of a first level comment.
     $expected_value = array('type' => 'uri', 'value' => $this->node_uri);
     $this->assertTrue($graph->hasProperty($comment_1_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its node found in RDF output (sioc:reply_of).');
     // Tests the reply_of relationship of a second level comment.
     $expected_value = array('type' => 'uri', 'value' => $this->node_uri);
     $this->assertTrue($graph->hasProperty($comment_2_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its node found in RDF output (sioc:reply_of).');
     $expected_value = array('type' => 'uri', 'value' => $comment_1_uri);
     $this->assertTrue($graph->hasProperty($comment_2_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its parent comment found in RDF output (sioc:reply_of).');
 }