3 namespace Drupal\Tests\rdf\Functional;
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentManagerInterface;
7 use Drupal\Tests\comment\Functional\CommentTestBase;
8 use Drupal\user\RoleInterface;
9 use Drupal\comment\Entity\Comment;
12 * Tests the RDFa markup of comments.
16 class CommentAttributesTest extends CommentTestBase {
23 public static $modules = ['views', 'node', 'comment', 'rdf'];
26 * URI of the front page of the Drupal site.
33 * URI of the test node created by CommentTestBase::setUp().
39 protected function setUp() {
42 // Enables anonymous user comments.
43 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
44 'access comments' => TRUE,
45 'post comments' => TRUE,
46 'skip comment approval' => TRUE,
48 // Allows anonymous to leave their contact information.
49 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
50 $this->setCommentPreview(DRUPAL_OPTIONAL);
51 $this->setCommentForm(TRUE);
52 $this->setCommentSubject(TRUE);
53 $this->setCommentSettings('comment_default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
55 // Prepares commonly used URIs.
56 $this->baseUri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
57 $this->nodeUri = $this->node->url('canonical', ['absolute' => TRUE]);
59 // Set relation between node and comment.
60 $article_mapping = rdf_get_mapping('node', 'article');
61 $comment_count_mapping = [
62 'properties' => ['sioc:num_replies'],
63 'datatype' => 'xsd:integer',
64 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::rawValue'],
66 $article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save();
69 $user_mapping = rdf_get_mapping('user', 'user');
71 'properties' => ['foaf:name'],
73 $user_mapping->setFieldMapping('name', $username_mapping)->save();
74 $user_mapping->setFieldMapping('homepage', ['properties' => ['foaf:page'], 'mapping_type' => 'rel'])->save();
76 // Save comment mapping.
77 $mapping = rdf_get_mapping('comment', 'comment');
78 $mapping->setBundleMapping(['types' => ['sioc:Post', 'sioct:Comment']])->save();
81 'properties' => ['dc:title'],
84 'properties' => ['dc:date', 'dc:created'],
85 'datatype' => 'xsd:dateTime',
86 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
89 'properties' => ['dc:modified'],
90 'datatype' => 'xsd:dateTime',
91 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
94 'properties' => ['content:encoded'],
97 'properties' => ['sioc:reply_of'],
98 'mapping_type' => 'rel',
101 'properties' => ['sioc:has_creator'],
102 'mapping_type' => 'rel',
105 'properties' => ['foaf:name'],
108 // Iterate over shared field mappings and save.
109 foreach ($field_mappings as $field_name => $field_mapping) {
110 $mapping->setFieldMapping($field_name, $field_mapping)->save();
115 * Tests the presence of the RDFa markup for the number of comments.
117 public function testNumberOfCommentsRdfaMarkup() {
118 // Posts 2 comments on behalf of registered user.
119 $this->saveComment($this->node->id(), $this->webUser->id());
120 $this->saveComment($this->node->id(), $this->webUser->id());
122 // Tests number of comments in teaser view.
123 $this->drupalLogin($this->webUser);
124 $parser = new \EasyRdf_Parser_Rdfa();
125 $graph = new \EasyRdf_Graph();
126 $parser->parse($graph, $this->drupalGet('node'), 'rdfa', $this->baseUri);
128 // Number of comments.
132 'datatype' => 'http://www.w3.org/2001/XMLSchema#integer',
134 $this->assertTrue($graph->hasProperty($this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of teaser view (sioc:num_replies).');
136 // Tests number of comments in full node view, expected value is the same.
137 $parser = new \EasyRdf_Parser_Rdfa();
138 $graph = new \EasyRdf_Graph();
139 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
140 $this->assertTrue($graph->hasProperty($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).');
144 * Tests comment author link markup has not been broken by RDF.
146 public function testCommentRdfAuthorMarkup() {
147 // Post a comment as a registered user.
148 $this->saveComment($this->node->id(), $this->webUser->id());
150 // Give the user access to view user profiles so the profile link shows up.
151 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']);
152 $this->drupalLogin($this->webUser);
154 // Ensure that the author link still works properly after the author output
155 // is modified by the RDF module.
156 $this->drupalGet('node/' . $this->node->id());
157 $this->assertLink($this->webUser->getUsername());
158 $this->assertLinkByHref('user/' . $this->webUser->id());
162 * Tests if RDFa markup for meta information is present in comments.
164 * Tests presence of RDFa markup for the title, date and author and homepage
165 * on comments from registered and anonymous users.
167 public function testCommentRdfaMarkup() {
168 // Posts comment #1 on behalf of registered user.
169 $comment1 = $this->saveComment($this->node->id(), $this->webUser->id());
171 // Tests comment #1 with access to the user profile.
172 $this->drupalLogin($this->webUser);
173 $parser = new \EasyRdf_Parser_Rdfa();
174 $graph = new \EasyRdf_Graph();
175 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
176 $this->_testBasicCommentRdfaMarkup($graph, $comment1);
178 // Tests comment #1 with no access to the user profile (as anonymous user).
179 $this->drupalLogout();
180 $parser = new \EasyRdf_Parser_Rdfa();
181 $graph = new \EasyRdf_Graph();
182 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
183 $this->_testBasicCommentRdfaMarkup($graph, $comment1);
185 // Posts comment #2 as anonymous user.
186 $anonymous_user = [];
187 $anonymous_user['name'] = $this->randomMachineName();
188 $anonymous_user['mail'] = 'tester@simpletest.org';
189 $anonymous_user['homepage'] = 'http://example.org/';
190 $comment2 = $this->saveComment($this->node->id(), 0, $anonymous_user);
192 // Tests comment #2 as anonymous user.
193 $parser = new \EasyRdf_Parser_Rdfa();
194 $graph = new \EasyRdf_Graph();
195 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
196 $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
198 // Tests comment #2 as logged in user.
199 $this->drupalLogin($this->webUser);
200 $parser = new \EasyRdf_Parser_Rdfa();
201 $graph = new \EasyRdf_Graph();
202 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
203 $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
207 * Tests RDF comment replies.
209 public function testCommentReplyOfRdfaMarkup() {
210 // Posts comment #1 on behalf of registered user.
211 $this->drupalLogin($this->webUser);
212 $comment_1 = $this->saveComment($this->node->id(), $this->webUser->id());
214 $comment_1_uri = $comment_1->url('canonical', ['absolute' => TRUE]);
216 // Posts a reply to the first comment.
217 $comment_2 = $this->saveComment($this->node->id(), $this->webUser->id(), NULL, $comment_1->id());
218 $comment_2_uri = $comment_2->url('canonical', ['absolute' => TRUE]);
220 $parser = new \EasyRdf_Parser_Rdfa();
221 $graph = new \EasyRdf_Graph();
222 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
224 // Tests the reply_of relationship of a first level comment.
227 'value' => $this->nodeUri,
229 $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).');
231 // Tests the reply_of relationship of a second level comment.
234 'value' => $this->nodeUri,
236 $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).');
239 'value' => $comment_1_uri,
241 $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).');
245 * Helper function for testCommentRdfaMarkup().
247 * Tests the current page for basic comment RDFa markup.
252 * An array containing information about an anonymous user.
254 public function _testBasicCommentRdfaMarkup($graph, CommentInterface $comment, $account = []) {
255 $comment_uri = $comment->url('canonical', ['absolute' => TRUE]);
260 'value' => 'http://rdfs.org/sioc/types#Comment',
262 $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).');
266 'value' => 'http://rdfs.org/sioc/ns#Post',
268 $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).');
273 'value' => $comment->getSubject(),
276 $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');
281 'value' => format_date($comment->getCreatedTime(), 'custom', 'c', 'UTC'),
282 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
284 $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');
288 'value' => format_date($comment->getCreatedTime(), 'custom', 'c', 'UTC'),
289 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
291 $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');
296 'value' => $comment->comment_body->value . "\n",
299 $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).');
301 // The comment author can be a registered user or an anonymous user.
302 if ($comment->getOwnerId() > 0) {
303 $author_uri = \Drupal::url('entity.user.canonical', ['user' => $comment->getOwnerId()], ['absolute' => TRUE]);
304 // Comment relation to author.
307 'value' => $author_uri,
309 $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).');
312 // The author is expected to be a blank node.
313 $author_uri = $graph->get($comment_uri, '<http://rdfs.org/sioc/ns#has_creator>');
314 if ($author_uri instanceof \EasyRdf_Resource) {
315 $this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator) and author is blank node.');
318 $this->fail('Comment relation to author found in RDF output (sioc:has_creator).');
323 $name = empty($account["name"]) ? $this->webUser->getUsername() : $account["name"] . " (not verified)";
328 $this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'Comment author name found in RDF output (foaf:name).');
330 // Comment author homepage (only for anonymous authors).
331 if ($comment->getOwnerId() == 0) {
334 'value' => 'http://example.org/',
336 $this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/page', $expected_value), 'Comment author link found in RDF output (foaf:page).');
341 * Creates a comment entity.
344 * Node id which will hold the comment.
346 * User id of the author of the comment. Can be NULL if $contact provided.
348 * Set to NULL for no contact info, TRUE to ignore success checking, and
349 * array of values to set contact info.
351 * Comment id of the parent comment in a thread.
353 * @return \Drupal\comment\Entity\Comment
356 public function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
359 'entity_type' => 'node',
360 'field_name' => 'comment',
363 'subject' => $this->randomMachineName(),
364 'comment_body' => $this->randomMachineName(),
371 $comment = Comment::create($values);