You are here

public function CommentAttributesTest::_testBasicCommentRdfaMarkup in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rdf/tests/src/Functional/CommentAttributesTest.php \Drupal\Tests\rdf\Functional\CommentAttributesTest::_testBasicCommentRdfaMarkup()
  2. 9 core/modules/rdf/tests/src/Functional/CommentAttributesTest.php \Drupal\Tests\rdf\Functional\CommentAttributesTest::_testBasicCommentRdfaMarkup()

Helper function for testCommentRdfaMarkup().

Tests the current page for basic comment RDFa markup.

Parameters

\Drupal\comment\CommentInterface $comment: Comment object.

array|null $account: (optional) An array containing information about an anonymous user. Defaults to NULL.

File

core/modules/rdf/tests/src/Functional/CommentAttributesTest.php, line 258

Class

CommentAttributesTest
Tests the RDFa markup of comments.

Namespace

Drupal\Tests\rdf\Functional

Code

public function _testBasicCommentRdfaMarkup(CommentInterface $comment, $account = NULL) {
  $this
    ->drupalGet($this->node
    ->toUrl());
  $comment_uri = $comment
    ->toUrl('canonical', [
    'absolute' => TRUE,
  ])
    ->toString();

  // Comment type.
  $expected_value = [
    'type' => 'uri',
    'value' => 'http://rdfs.org/sioc/types#Comment',
  ];
  $this
    ->assertTrue($this
    ->hasRdfProperty($this
    ->getSession()
    ->getPage()
    ->getContent(), $this->baseUri, $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 = [
    'type' => 'uri',
    'value' => 'http://rdfs.org/sioc/ns#Post',
  ];
  $this
    ->assertTrue($this
    ->hasRdfProperty($this
    ->getSession()
    ->getPage()
    ->getContent(), $this->baseUri, $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 = [
    'type' => 'literal',
    'value' => $comment
      ->getSubject(),
    'lang' => 'en',
  ];
  $this
    ->assertTrue($this
    ->hasRdfProperty($this
    ->getSession()
    ->getPage()
    ->getContent(), $this->baseUri, $comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');

  // Comment date.
  $expected_value = [
    'type' => 'literal',
    'value' => $this->container
      ->get('date.formatter')
      ->format($comment
      ->getCreatedTime(), 'custom', 'c', 'UTC'),
    'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
  ];
  $this
    ->assertTrue($this
    ->hasRdfProperty($this
    ->getSession()
    ->getPage()
    ->getContent(), $this->baseUri, $comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');

  // Comment date.
  $expected_value = [
    'type' => 'literal',
    'value' => $this->container
      ->get('date.formatter')
      ->format($comment
      ->getCreatedTime(), 'custom', 'c', 'UTC'),
    'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
  ];
  $this
    ->assertTrue($this
    ->hasRdfProperty($this
    ->getSession()
    ->getPage()
    ->getContent(), $this->baseUri, $comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');

  // Comment body.
  $expected_value = [
    'type' => 'literal',
    'value' => $comment->comment_body->value . "\n",
    'lang' => 'en',
  ];
  $this
    ->assertTrue($this
    ->hasRdfProperty($this
    ->getSession()
    ->getPage()
    ->getContent(), $this->baseUri, $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) {

    // Comment relation to author.
    $expected_value = [
      'type' => 'uri',
      'value' => Url::fromRoute('entity.user.canonical', [
        'user' => $comment
          ->getOwnerId(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString(),
    ];
    $this
      ->assertTrue($this
      ->hasRdfProperty($this
      ->getSession()
      ->getPage()
      ->getContent(), $this->baseUri, $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.
    $this
      ->assertTrue($this
      ->rdfElementIsBlankNode($this
      ->getSession()
      ->getPage()
      ->getContent(), $this->baseUri, $comment_uri, '<http://rdfs.org/sioc/ns#has_creator>'));
  }

  // Author name.
  $name = $account ? $account
    ->getDisplayName() . " (not verified)" : $this->webUser
    ->getDisplayName();
  $expected_value = [
    'type' => 'literal',
    'value' => $name,
  ];
  $this
    ->assertTrue($this
    ->hasRdfChildProperty($this
    ->getSession()
    ->getPage()
    ->getContent(), $this->baseUri, $comment_uri, '<http://rdfs.org/sioc/ns#has_creator>', '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 = [
      'type' => 'uri',
      'value' => 'http://example.org/',
    ];
    $this
      ->assertTrue($this
      ->hasRdfChildProperty($this
      ->getSession()
      ->getPage()
      ->getContent(), $this->baseUri, $comment_uri, '<http://rdfs.org/sioc/ns#has_creator>', 'http://xmlns.com/foaf/0.1/page', $expected_value), 'Comment author link found in RDF output (foaf:page).');
  }
}