annotate core/modules/rdf/src/Tests/CommentAttributesTest.php @ 15:e200cb7efeb3

Update Drupal core to 8.5.3 via Composer
author Chris Cannam
date Thu, 26 Apr 2018 11:26:54 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\rdf\Tests;
Chris@0 4
Chris@0 5 use Drupal\comment\CommentInterface;
Chris@0 6 use Drupal\comment\CommentManagerInterface;
Chris@0 7 use Drupal\comment\Tests\CommentTestBase;
Chris@0 8 use Drupal\user\RoleInterface;
Chris@0 9 use Drupal\comment\Entity\Comment;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Tests the RDFa markup of comments.
Chris@0 13 *
Chris@0 14 * @group rdf
Chris@0 15 */
Chris@0 16 class CommentAttributesTest extends CommentTestBase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Modules to enable.
Chris@0 20 *
Chris@0 21 * @var array
Chris@0 22 */
Chris@0 23 public static $modules = ['views', 'node', 'comment', 'rdf'];
Chris@0 24
Chris@0 25 /**
Chris@0 26 * URI of the front page of the Drupal site.
Chris@0 27 *
Chris@0 28 * @var string
Chris@0 29 */
Chris@0 30 protected $baseUri;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * URI of the test node created by CommentTestBase::setUp().
Chris@0 34 *
Chris@0 35 * @var string
Chris@0 36 */
Chris@0 37 protected $nodeUri;
Chris@0 38
Chris@0 39 protected function setUp() {
Chris@0 40 parent::setUp();
Chris@0 41
Chris@0 42 // Enables anonymous user comments.
Chris@0 43 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
Chris@0 44 'access comments' => TRUE,
Chris@0 45 'post comments' => TRUE,
Chris@0 46 'skip comment approval' => TRUE,
Chris@0 47 ]);
Chris@0 48 // Allows anonymous to leave their contact information.
Chris@0 49 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
Chris@0 50 $this->setCommentPreview(DRUPAL_OPTIONAL);
Chris@0 51 $this->setCommentForm(TRUE);
Chris@0 52 $this->setCommentSubject(TRUE);
Chris@0 53 $this->setCommentSettings('comment_default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
Chris@0 54
Chris@0 55 // Prepares commonly used URIs.
Chris@0 56 $this->baseUri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
Chris@0 57 $this->nodeUri = $this->node->url('canonical', ['absolute' => TRUE]);
Chris@0 58
Chris@0 59 // Set relation between node and comment.
Chris@0 60 $article_mapping = rdf_get_mapping('node', 'article');
Chris@0 61 $comment_count_mapping = [
Chris@0 62 'properties' => ['sioc:num_replies'],
Chris@0 63 'datatype' => 'xsd:integer',
Chris@0 64 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::rawValue'],
Chris@0 65 ];
Chris@0 66 $article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save();
Chris@0 67
Chris@0 68 // Save user mapping.
Chris@0 69 $user_mapping = rdf_get_mapping('user', 'user');
Chris@0 70 $username_mapping = [
Chris@0 71 'properties' => ['foaf:name'],
Chris@0 72 ];
Chris@0 73 $user_mapping->setFieldMapping('name', $username_mapping)->save();
Chris@0 74 $user_mapping->setFieldMapping('homepage', ['properties' => ['foaf:page'], 'mapping_type' => 'rel'])->save();
Chris@0 75
Chris@0 76 // Save comment mapping.
Chris@0 77 $mapping = rdf_get_mapping('comment', 'comment');
Chris@0 78 $mapping->setBundleMapping(['types' => ['sioc:Post', 'sioct:Comment']])->save();
Chris@0 79 $field_mappings = [
Chris@0 80 'subject' => [
Chris@0 81 'properties' => ['dc:title'],
Chris@0 82 ],
Chris@0 83 'created' => [
Chris@0 84 'properties' => ['dc:date', 'dc:created'],
Chris@0 85 'datatype' => 'xsd:dateTime',
Chris@0 86 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
Chris@0 87 ],
Chris@0 88 'changed' => [
Chris@0 89 'properties' => ['dc:modified'],
Chris@0 90 'datatype' => 'xsd:dateTime',
Chris@0 91 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
Chris@0 92 ],
Chris@0 93 'comment_body' => [
Chris@0 94 'properties' => ['content:encoded'],
Chris@0 95 ],
Chris@0 96 'pid' => [
Chris@0 97 'properties' => ['sioc:reply_of'],
Chris@0 98 'mapping_type' => 'rel',
Chris@0 99 ],
Chris@0 100 'uid' => [
Chris@0 101 'properties' => ['sioc:has_creator'],
Chris@0 102 'mapping_type' => 'rel',
Chris@0 103 ],
Chris@0 104 'name' => [
Chris@0 105 'properties' => ['foaf:name'],
Chris@0 106 ],
Chris@0 107 ];
Chris@0 108 // Iterate over shared field mappings and save.
Chris@0 109 foreach ($field_mappings as $field_name => $field_mapping) {
Chris@0 110 $mapping->setFieldMapping($field_name, $field_mapping)->save();
Chris@0 111 }
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * Tests the presence of the RDFa markup for the number of comments.
Chris@0 116 */
Chris@0 117 public function testNumberOfCommentsRdfaMarkup() {
Chris@0 118 // Posts 2 comments on behalf of registered user.
Chris@0 119 $this->saveComment($this->node->id(), $this->webUser->id());
Chris@0 120 $this->saveComment($this->node->id(), $this->webUser->id());
Chris@0 121
Chris@0 122 // Tests number of comments in teaser view.
Chris@0 123 $this->drupalLogin($this->webUser);
Chris@0 124 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 125 $graph = new \EasyRdf_Graph();
Chris@0 126 $parser->parse($graph, $this->drupalGet('node'), 'rdfa', $this->baseUri);
Chris@0 127
Chris@0 128 // Number of comments.
Chris@0 129 $expected_value = [
Chris@0 130 'type' => 'literal',
Chris@0 131 'value' => 2,
Chris@0 132 'datatype' => 'http://www.w3.org/2001/XMLSchema#integer',
Chris@0 133 ];
Chris@0 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).');
Chris@0 135
Chris@0 136 // Tests number of comments in full node view, expected value is the same.
Chris@0 137 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 138 $graph = new \EasyRdf_Graph();
Chris@0 139 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
Chris@0 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).');
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Tests comment author link markup has not been broken by RDF.
Chris@0 145 */
Chris@0 146 public function testCommentRdfAuthorMarkup() {
Chris@0 147 // Post a comment as a registered user.
Chris@0 148 $this->saveComment($this->node->id(), $this->webUser->id());
Chris@0 149
Chris@0 150 // Give the user access to view user profiles so the profile link shows up.
Chris@0 151 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']);
Chris@0 152 $this->drupalLogin($this->webUser);
Chris@0 153
Chris@0 154 // Ensure that the author link still works properly after the author output
Chris@0 155 // is modified by the RDF module.
Chris@0 156 $this->drupalGet('node/' . $this->node->id());
Chris@0 157 $this->assertLink($this->webUser->getUsername());
Chris@0 158 $this->assertLinkByHref('user/' . $this->webUser->id());
Chris@0 159 }
Chris@0 160
Chris@0 161 /**
Chris@0 162 * Tests if RDFa markup for meta information is present in comments.
Chris@0 163 *
Chris@0 164 * Tests presence of RDFa markup for the title, date and author and homepage
Chris@0 165 * on comments from registered and anonymous users.
Chris@0 166 */
Chris@0 167 public function testCommentRdfaMarkup() {
Chris@0 168 // Posts comment #1 on behalf of registered user.
Chris@0 169 $comment1 = $this->saveComment($this->node->id(), $this->webUser->id());
Chris@0 170
Chris@0 171 // Tests comment #1 with access to the user profile.
Chris@0 172 $this->drupalLogin($this->webUser);
Chris@0 173 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 174 $graph = new \EasyRdf_Graph();
Chris@0 175 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
Chris@0 176 $this->_testBasicCommentRdfaMarkup($graph, $comment1);
Chris@0 177
Chris@0 178 // Tests comment #1 with no access to the user profile (as anonymous user).
Chris@0 179 $this->drupalLogout();
Chris@0 180 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 181 $graph = new \EasyRdf_Graph();
Chris@0 182 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
Chris@0 183 $this->_testBasicCommentRdfaMarkup($graph, $comment1);
Chris@0 184
Chris@0 185 // Posts comment #2 as anonymous user.
Chris@0 186 $anonymous_user = [];
Chris@0 187 $anonymous_user['name'] = $this->randomMachineName();
Chris@0 188 $anonymous_user['mail'] = 'tester@simpletest.org';
Chris@0 189 $anonymous_user['homepage'] = 'http://example.org/';
Chris@0 190 $comment2 = $this->saveComment($this->node->id(), 0, $anonymous_user);
Chris@0 191
Chris@0 192 // Tests comment #2 as anonymous user.
Chris@0 193 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 194 $graph = new \EasyRdf_Graph();
Chris@0 195 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
Chris@0 196 $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
Chris@0 197
Chris@0 198 // Tests comment #2 as logged in user.
Chris@0 199 $this->drupalLogin($this->webUser);
Chris@0 200 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 201 $graph = new \EasyRdf_Graph();
Chris@0 202 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
Chris@0 203 $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
Chris@0 204 }
Chris@0 205
Chris@0 206 /**
Chris@0 207 * Tests RDF comment replies.
Chris@0 208 */
Chris@0 209 public function testCommentReplyOfRdfaMarkup() {
Chris@0 210 // Posts comment #1 on behalf of registered user.
Chris@0 211 $this->drupalLogin($this->webUser);
Chris@0 212 $comment_1 = $this->saveComment($this->node->id(), $this->webUser->id());
Chris@0 213
Chris@0 214 $comment_1_uri = $comment_1->url('canonical', ['absolute' => TRUE]);
Chris@0 215
Chris@0 216 // Posts a reply to the first comment.
Chris@0 217 $comment_2 = $this->saveComment($this->node->id(), $this->webUser->id(), NULL, $comment_1->id());
Chris@0 218 $comment_2_uri = $comment_2->url('canonical', ['absolute' => TRUE]);
Chris@0 219
Chris@0 220 $parser = new \EasyRdf_Parser_Rdfa();
Chris@0 221 $graph = new \EasyRdf_Graph();
Chris@0 222 $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
Chris@0 223
Chris@0 224 // Tests the reply_of relationship of a first level comment.
Chris@0 225 $expected_value = [
Chris@0 226 'type' => 'uri',
Chris@0 227 'value' => $this->nodeUri,
Chris@0 228 ];
Chris@0 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).');
Chris@0 230
Chris@0 231 // Tests the reply_of relationship of a second level comment.
Chris@0 232 $expected_value = [
Chris@0 233 'type' => 'uri',
Chris@0 234 'value' => $this->nodeUri,
Chris@0 235 ];
Chris@0 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).');
Chris@0 237 $expected_value = [
Chris@0 238 'type' => 'uri',
Chris@0 239 'value' => $comment_1_uri,
Chris@0 240 ];
Chris@0 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).');
Chris@0 242 }
Chris@0 243
Chris@0 244 /**
Chris@0 245 * Helper function for testCommentRdfaMarkup().
Chris@0 246 *
Chris@0 247 * Tests the current page for basic comment RDFa markup.
Chris@0 248 *
Chris@0 249 * @param $comment
Chris@0 250 * Comment object.
Chris@0 251 * @param $account
Chris@0 252 * An array containing information about an anonymous user.
Chris@0 253 */
Chris@0 254 public function _testBasicCommentRdfaMarkup($graph, CommentInterface $comment, $account = []) {
Chris@0 255 $comment_uri = $comment->url('canonical', ['absolute' => TRUE]);
Chris@0 256
Chris@0 257 // Comment type.
Chris@0 258 $expected_value = [
Chris@0 259 'type' => 'uri',
Chris@0 260 'value' => 'http://rdfs.org/sioc/types#Comment',
Chris@0 261 ];
Chris@0 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).');
Chris@0 263 // Comment type.
Chris@0 264 $expected_value = [
Chris@0 265 'type' => 'uri',
Chris@0 266 'value' => 'http://rdfs.org/sioc/ns#Post',
Chris@0 267 ];
Chris@0 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).');
Chris@0 269
Chris@0 270 // Comment title.
Chris@0 271 $expected_value = [
Chris@0 272 'type' => 'literal',
Chris@0 273 'value' => $comment->getSubject(),
Chris@0 274 'lang' => 'en',
Chris@0 275 ];
Chris@0 276 $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');
Chris@0 277
Chris@0 278 // Comment date.
Chris@0 279 $expected_value = [
Chris@0 280 'type' => 'literal',
Chris@0 281 'value' => format_date($comment->getCreatedTime(), 'custom', 'c', 'UTC'),
Chris@0 282 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
Chris@0 283 ];
Chris@0 284 $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');
Chris@0 285 // Comment date.
Chris@0 286 $expected_value = [
Chris@0 287 'type' => 'literal',
Chris@0 288 'value' => format_date($comment->getCreatedTime(), 'custom', 'c', 'UTC'),
Chris@0 289 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
Chris@0 290 ];
Chris@0 291 $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');
Chris@0 292
Chris@0 293 // Comment body.
Chris@0 294 $expected_value = [
Chris@0 295 'type' => 'literal',
Chris@0 296 'value' => $comment->comment_body->value . "\n",
Chris@0 297 'lang' => 'en',
Chris@0 298 ];
Chris@0 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).');
Chris@0 300
Chris@0 301 // The comment author can be a registered user or an anonymous user.
Chris@0 302 if ($comment->getOwnerId() > 0) {
Chris@0 303 $author_uri = \Drupal::url('entity.user.canonical', ['user' => $comment->getOwnerId()], ['absolute' => TRUE]);
Chris@0 304 // Comment relation to author.
Chris@0 305 $expected_value = [
Chris@0 306 'type' => 'uri',
Chris@0 307 'value' => $author_uri,
Chris@0 308 ];
Chris@0 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).');
Chris@0 310 }
Chris@0 311 else {
Chris@0 312 // The author is expected to be a blank node.
Chris@0 313 $author_uri = $graph->get($comment_uri, '<http://rdfs.org/sioc/ns#has_creator>');
Chris@0 314 if ($author_uri instanceof \EasyRdf_Resource) {
Chris@0 315 $this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator) and author is blank node.');
Chris@0 316 }
Chris@0 317 else {
Chris@0 318 $this->fail('Comment relation to author found in RDF output (sioc:has_creator).');
Chris@0 319 }
Chris@0 320 }
Chris@0 321
Chris@0 322 // Author name.
Chris@0 323 $name = empty($account["name"]) ? $this->webUser->getUsername() : $account["name"] . " (not verified)";
Chris@0 324 $expected_value = [
Chris@0 325 'type' => 'literal',
Chris@0 326 'value' => $name,
Chris@0 327 ];
Chris@0 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).');
Chris@0 329
Chris@0 330 // Comment author homepage (only for anonymous authors).
Chris@0 331 if ($comment->getOwnerId() == 0) {
Chris@0 332 $expected_value = [
Chris@0 333 'type' => 'uri',
Chris@0 334 'value' => 'http://example.org/',
Chris@0 335 ];
Chris@0 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).');
Chris@0 337 }
Chris@0 338 }
Chris@0 339
Chris@0 340 /**
Chris@0 341 * Creates a comment entity.
Chris@0 342 *
Chris@0 343 * @param $nid
Chris@0 344 * Node id which will hold the comment.
Chris@0 345 * @param $uid
Chris@0 346 * User id of the author of the comment. Can be NULL if $contact provided.
Chris@0 347 * @param $contact
Chris@0 348 * Set to NULL for no contact info, TRUE to ignore success checking, and
Chris@0 349 * array of values to set contact info.
Chris@0 350 * @param $pid
Chris@0 351 * Comment id of the parent comment in a thread.
Chris@0 352 *
Chris@0 353 * @return \Drupal\comment\Entity\Comment
Chris@0 354 * The saved comment.
Chris@0 355 */
Chris@0 356 public function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
Chris@0 357 $values = [
Chris@0 358 'entity_id' => $nid,
Chris@0 359 'entity_type' => 'node',
Chris@0 360 'field_name' => 'comment',
Chris@0 361 'uid' => $uid,
Chris@0 362 'pid' => $pid,
Chris@0 363 'subject' => $this->randomMachineName(),
Chris@0 364 'comment_body' => $this->randomMachineName(),
Chris@0 365 'status' => 1,
Chris@0 366 ];
Chris@0 367 if ($contact) {
Chris@0 368 $values += $contact;
Chris@0 369 }
Chris@0 370
Chris@0 371 $comment = Comment::create($values);
Chris@0 372 $comment->save();
Chris@0 373 return $comment;
Chris@0 374 }
Chris@0 375
Chris@0 376 }