annotate core/modules/rdf/tests/src/Functional/CommentAttributesTest.php @ 19:fa3358dc1485 tip

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