You are here

function CommentAttributesTest::_testBasicCommentRdfaMarkup in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rdf/src/Tests/CommentAttributesTest.php \Drupal\rdf\Tests\CommentAttributesTest::_testBasicCommentRdfaMarkup()

Helper function for testCommentRdfaMarkup().

Tests the current page for basic comment RDFa markup.

Parameters

$comment: Comment object.

$account: An array containing information about an anonymous user.

1 call to CommentAttributesTest::_testBasicCommentRdfaMarkup()
CommentAttributesTest::testCommentRdfaMarkup in core/modules/rdf/src/Tests/CommentAttributesTest.php
Tests if RDFa markup for meta information is present in comments.

File

core/modules/rdf/src/Tests/CommentAttributesTest.php, line 258
Contains \Drupal\rdf\Tests\CommentAttributesTest.

Class

CommentAttributesTest
Tests the RDFa markup of comments.

Namespace

Drupal\rdf\Tests

Code

function _testBasicCommentRdfaMarkup($graph, CommentInterface $comment, $account = array()) {
  $comment_uri = $comment
    ->url('canonical', array(
    'absolute' => TRUE,
  ));

  // Comment type.
  $expected_value = array(
    'type' => 'uri',
    'value' => 'http://rdfs.org/sioc/types#Comment',
  );
  $this
    ->assertTrue($graph
    ->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioct:Comment).');

  // Comment type.
  $expected_value = array(
    'type' => 'uri',
    'value' => 'http://rdfs.org/sioc/ns#Post',
  );
  $this
    ->assertTrue($graph
    ->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioc:Post).');

  // Comment title.
  $expected_value = array(
    'type' => 'literal',
    'value' => $comment
      ->getSubject(),
    'lang' => 'en',
  );
  $this
    ->assertTrue($graph
    ->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');

  // Comment date.
  $expected_value = array(
    'type' => 'literal',
    'value' => format_date($comment
      ->getCreatedTime(), 'custom', 'c', 'UTC'),
    'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
  );
  $this
    ->assertTrue($graph
    ->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');

  // Comment date.
  $expected_value = array(
    'type' => 'literal',
    'value' => format_date($comment
      ->getCreatedTime(), 'custom', 'c', 'UTC'),
    'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
  );
  $this
    ->assertTrue($graph
    ->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');

  // Comment body.
  $expected_value = array(
    'type' => 'literal',
    'value' => $comment->comment_body->value . "\n",
    'lang' => 'en',
  );
  $this
    ->assertTrue($graph
    ->hasProperty($comment_uri, 'http://purl.org/rss/1.0/modules/content/encoded', $expected_value), 'Comment body found in RDF output (content:encoded).');

  // The comment author can be a registered user or an anonymous user.
  if ($comment
    ->getOwnerId() > 0) {
    $author_uri = \Drupal::url('entity.user.canonical', [
      'user' => $comment
        ->getOwnerId(),
    ], array(
      'absolute' => TRUE,
    ));

    // Comment relation to author.
    $expected_value = array(
      'type' => 'uri',
      'value' => $author_uri,
    );
    $this
      ->assertTrue($graph
      ->hasProperty($comment_uri, 'http://rdfs.org/sioc/ns#has_creator', $expected_value), 'Comment relation to author found in RDF output (sioc:has_creator).');
  }
  else {

    // The author is expected to be a blank node.
    $author_uri = $graph
      ->get($comment_uri, '<http://rdfs.org/sioc/ns#has_creator>');
    if ($author_uri instanceof \EasyRdf_Resource) {
      $this
        ->assertTrue($author_uri
        ->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator) and author is blank node.');
    }
    else {
      $this
        ->fail('Comment relation to author found in RDF output (sioc:has_creator).');
    }
  }

  // Author name.
  $name = empty($account["name"]) ? $this->webUser
    ->getUsername() : $account["name"] . " (not verified)";
  $expected_value = array(
    'type' => 'literal',
    'value' => $name,
  );
  $this
    ->assertTrue($graph
    ->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'Comment author name found in RDF output (foaf:name).');

  // Comment author homepage (only for anonymous authors).
  if ($comment
    ->getOwnerId() == 0) {
    $expected_value = array(
      'type' => 'uri',
      'value' => 'http://example.org/',
    );
    $this
      ->assertTrue($graph
      ->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/page', $expected_value), 'Comment author link found in RDF output (foaf:page).');
  }
}