View source
<?php
namespace Drupal\Tests\rdf\Functional;
use Drupal\Core\Url;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\Tests\comment\Functional\CommentTestBase;
use Drupal\Tests\rdf\Traits\RdfParsingTrait;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
use Drupal\comment\Entity\Comment;
class CommentAttributesTest extends CommentTestBase {
use RdfParsingTrait;
protected static $modules = [
'views',
'node',
'comment',
'rdf',
'user_hooks_test',
];
protected $defaultTheme = 'stark';
protected $baseUri;
protected $nodeUri;
protected function setUp() : void {
parent::setUp();
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => TRUE,
]);
$this
->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT);
$this
->setCommentPreview(DRUPAL_OPTIONAL);
$this
->setCommentForm(TRUE);
$this
->setCommentSubject(TRUE);
$this
->setCommentSettings('comment_default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->baseUri = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])
->toString();
$this->nodeUri = $this->node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$article_mapping = rdf_get_mapping('node', 'article');
$comment_count_mapping = [
'properties' => [
'sioc:num_replies',
],
'datatype' => 'xsd:integer',
'datatype_callback' => [
'callable' => 'Drupal\\rdf\\CommonDataConverter::rawValue',
],
];
$article_mapping
->setFieldMapping('comment_count', $comment_count_mapping)
->save();
$user_mapping = rdf_get_mapping('user', 'user');
$username_mapping = [
'properties' => [
'foaf:name',
],
];
$user_mapping
->setFieldMapping('name', $username_mapping)
->save();
$user_mapping
->setFieldMapping('homepage', [
'properties' => [
'foaf:page',
],
'mapping_type' => 'rel',
])
->save();
$mapping = rdf_get_mapping('comment', 'comment');
$mapping
->setBundleMapping([
'types' => [
'sioc:Post',
'sioct:Comment',
],
])
->save();
$field_mappings = [
'subject' => [
'properties' => [
'dc:title',
],
],
'created' => [
'properties' => [
'dc:date',
'dc:created',
],
'datatype' => 'xsd:dateTime',
'datatype_callback' => [
'callable' => 'Drupal\\rdf\\CommonDataConverter::dateIso8601Value',
],
],
'changed' => [
'properties' => [
'dc:modified',
],
'datatype' => 'xsd:dateTime',
'datatype_callback' => [
'callable' => 'Drupal\\rdf\\CommonDataConverter::dateIso8601Value',
],
],
'comment_body' => [
'properties' => [
'content:encoded',
],
],
'pid' => [
'properties' => [
'sioc:reply_of',
],
'mapping_type' => 'rel',
],
'uid' => [
'properties' => [
'sioc:has_creator',
],
'mapping_type' => 'rel',
],
'name' => [
'properties' => [
'foaf:name',
],
],
];
foreach ($field_mappings as $field_name => $field_mapping) {
$mapping
->setFieldMapping($field_name, $field_mapping)
->save();
}
}
public function testNumberOfCommentsRdfaMarkup() {
$this
->saveComment($this->node
->id(), $this->webUser
->id());
$this
->saveComment($this->node
->id(), $this->webUser
->id());
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node');
$expected_value = [
'type' => 'literal',
'value' => 2,
'datatype' => 'http://www.w3.org/2001/XMLSchema#integer',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of teaser view (sioc:num_replies).');
$this
->drupalGet($this->node
->toUrl());
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of full node view mode (sioc:num_replies).');
}
public function testCommentRdfAuthorMarkup() {
\Drupal::state()
->set('user_hooks_test_user_format_name_alter', TRUE);
$this
->saveComment($this->node
->id(), $this->webUser
->id());
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
'access user profiles',
]);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->linkExistsExact($this->webUser
->getDisplayName());
$this
->assertSession()
->linkByHrefExists('user/' . $this->webUser
->id());
}
public function testCommentRdfaMarkup() {
\Drupal::state()
->set('user_hooks_test_user_format_name_alter', TRUE);
$comment1 = $this
->saveComment($this->node
->id(), $this->webUser
->id());
$this
->drupalLogin($this->webUser);
$this
->_testBasicCommentRdfaMarkup($comment1);
$this
->drupalLogout();
$this
->_testBasicCommentRdfaMarkup($comment1);
$anonymous_user = [];
$anonymous_user['name'] = $this
->randomMachineName();
$anonymous_user['mail'] = 'tester@simpletest.org';
$anonymous_user['homepage'] = 'http://example.org/';
$comment2 = $this
->saveComment($this->node
->id(), 0, $anonymous_user);
$anonymous = User::load(0);
$this
->_testBasicCommentRdfaMarkup($comment2, $anonymous);
$this
->drupalLogin($this->webUser);
$this
->_testBasicCommentRdfaMarkup($comment2, $anonymous);
}
public function testCommentReplyOfRdfaMarkup() {
$this
->drupalLogin($this->webUser);
$comment_1 = $this
->saveComment($this->node
->id(), $this->webUser
->id());
$comment_1_uri = $comment_1
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$comment_2 = $this
->saveComment($this->node
->id(), $this->webUser
->id(), NULL, $comment_1
->id());
$comment_2_uri = $comment_2
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$this
->drupalGet($this->node
->toUrl());
$expected_value = [
'type' => 'uri',
'value' => $this->nodeUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $comment_1_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its node found in RDF output (sioc:reply_of).');
$expected_value = [
'type' => 'uri',
'value' => $this->nodeUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $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 = [
'type' => 'uri',
'value' => $comment_1_uri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $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).');
}
public function _testBasicCommentRdfaMarkup(CommentInterface $comment, $account = NULL) {
$this
->drupalGet($this->node
->toUrl());
$comment_uri = $comment
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$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).');
$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).');
$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).');
$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).');
$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).');
$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).');
if ($comment
->getOwnerId() > 0) {
$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 {
$this
->assertTrue($this
->rdfElementIsBlankNode($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $comment_uri, '<http://rdfs.org/sioc/ns#has_creator>'));
}
$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).');
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).');
}
}
public function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
$values = [
'entity_id' => $nid,
'entity_type' => 'node',
'field_name' => 'comment',
'uid' => $uid,
'pid' => $pid,
'subject' => $this
->randomMachineName(),
'comment_body' => $this
->randomMachineName(),
'status' => 1,
];
if ($contact) {
$values += $contact;
}
$comment = Comment::create($values);
$comment
->save();
return $comment;
}
}