Chris@16: TRUE, Chris@16: 'post comments' => TRUE, Chris@16: 'skip comment approval' => TRUE, Chris@16: ]); Chris@16: // Allows anonymous to leave their contact information. Chris@18: $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT); Chris@16: $this->setCommentPreview(DRUPAL_OPTIONAL); Chris@16: $this->setCommentForm(TRUE); Chris@16: $this->setCommentSubject(TRUE); Chris@16: $this->setCommentSettings('comment_default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); Chris@16: Chris@16: // Prepares commonly used URIs. Chris@18: $this->baseUri = Url::fromRoute('', [], ['absolute' => TRUE])->toString(); Chris@18: $this->nodeUri = $this->node->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@16: Chris@16: // Set relation between node and comment. Chris@16: $article_mapping = rdf_get_mapping('node', 'article'); Chris@16: $comment_count_mapping = [ Chris@16: 'properties' => ['sioc:num_replies'], Chris@16: 'datatype' => 'xsd:integer', Chris@16: 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::rawValue'], Chris@16: ]; Chris@16: $article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save(); Chris@16: Chris@16: // Save user mapping. Chris@16: $user_mapping = rdf_get_mapping('user', 'user'); Chris@16: $username_mapping = [ Chris@16: 'properties' => ['foaf:name'], Chris@16: ]; Chris@16: $user_mapping->setFieldMapping('name', $username_mapping)->save(); Chris@16: $user_mapping->setFieldMapping('homepage', ['properties' => ['foaf:page'], 'mapping_type' => 'rel'])->save(); Chris@16: Chris@16: // Save comment mapping. Chris@16: $mapping = rdf_get_mapping('comment', 'comment'); Chris@16: $mapping->setBundleMapping(['types' => ['sioc:Post', 'sioct:Comment']])->save(); Chris@16: $field_mappings = [ Chris@16: 'subject' => [ Chris@16: 'properties' => ['dc:title'], Chris@16: ], Chris@16: 'created' => [ Chris@16: 'properties' => ['dc:date', 'dc:created'], Chris@16: 'datatype' => 'xsd:dateTime', Chris@16: 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'], Chris@16: ], Chris@16: 'changed' => [ Chris@16: 'properties' => ['dc:modified'], Chris@16: 'datatype' => 'xsd:dateTime', Chris@16: 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'], Chris@16: ], Chris@16: 'comment_body' => [ Chris@16: 'properties' => ['content:encoded'], Chris@16: ], Chris@16: 'pid' => [ Chris@16: 'properties' => ['sioc:reply_of'], Chris@16: 'mapping_type' => 'rel', Chris@16: ], Chris@16: 'uid' => [ Chris@16: 'properties' => ['sioc:has_creator'], Chris@16: 'mapping_type' => 'rel', Chris@16: ], Chris@16: 'name' => [ Chris@16: 'properties' => ['foaf:name'], Chris@16: ], Chris@16: ]; Chris@16: // Iterate over shared field mappings and save. Chris@16: foreach ($field_mappings as $field_name => $field_mapping) { Chris@16: $mapping->setFieldMapping($field_name, $field_mapping)->save(); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests the presence of the RDFa markup for the number of comments. Chris@16: */ Chris@16: public function testNumberOfCommentsRdfaMarkup() { Chris@16: // Posts 2 comments on behalf of registered user. Chris@16: $this->saveComment($this->node->id(), $this->webUser->id()); Chris@16: $this->saveComment($this->node->id(), $this->webUser->id()); Chris@16: Chris@16: // Tests number of comments in teaser view. Chris@16: $this->drupalLogin($this->webUser); Chris@16: $parser = new \EasyRdf_Parser_Rdfa(); Chris@16: $graph = new \EasyRdf_Graph(); Chris@16: $parser->parse($graph, $this->drupalGet('node'), 'rdfa', $this->baseUri); Chris@16: Chris@16: // Number of comments. Chris@16: $expected_value = [ Chris@16: 'type' => 'literal', Chris@16: 'value' => 2, Chris@16: 'datatype' => 'http://www.w3.org/2001/XMLSchema#integer', Chris@16: ]; Chris@16: $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: Chris@16: // Tests number of comments in full node view, expected value is the same. Chris@16: $parser = new \EasyRdf_Parser_Rdfa(); Chris@16: $graph = new \EasyRdf_Graph(); Chris@16: $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri); Chris@16: $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: } Chris@16: Chris@16: /** Chris@16: * Tests comment author link markup has not been broken by RDF. Chris@16: */ Chris@16: public function testCommentRdfAuthorMarkup() { Chris@16: // Post a comment as a registered user. Chris@16: $this->saveComment($this->node->id(), $this->webUser->id()); Chris@16: Chris@16: // Give the user access to view user profiles so the profile link shows up. Chris@16: user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']); Chris@16: $this->drupalLogin($this->webUser); Chris@16: Chris@16: // Ensure that the author link still works properly after the author output Chris@16: // is modified by the RDF module. Chris@16: $this->drupalGet('node/' . $this->node->id()); Chris@18: $this->assertLink($this->webUser->getAccountName()); Chris@16: $this->assertLinkByHref('user/' . $this->webUser->id()); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests if RDFa markup for meta information is present in comments. Chris@16: * Chris@16: * Tests presence of RDFa markup for the title, date and author and homepage Chris@16: * on comments from registered and anonymous users. Chris@16: */ Chris@16: public function testCommentRdfaMarkup() { Chris@16: // Posts comment #1 on behalf of registered user. Chris@16: $comment1 = $this->saveComment($this->node->id(), $this->webUser->id()); Chris@16: Chris@16: // Tests comment #1 with access to the user profile. Chris@16: $this->drupalLogin($this->webUser); Chris@16: $parser = new \EasyRdf_Parser_Rdfa(); Chris@16: $graph = new \EasyRdf_Graph(); Chris@16: $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri); Chris@16: $this->_testBasicCommentRdfaMarkup($graph, $comment1); Chris@16: Chris@16: // Tests comment #1 with no access to the user profile (as anonymous user). Chris@16: $this->drupalLogout(); Chris@16: $parser = new \EasyRdf_Parser_Rdfa(); Chris@16: $graph = new \EasyRdf_Graph(); Chris@16: $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri); Chris@16: $this->_testBasicCommentRdfaMarkup($graph, $comment1); Chris@16: Chris@16: // Posts comment #2 as anonymous user. Chris@16: $anonymous_user = []; Chris@16: $anonymous_user['name'] = $this->randomMachineName(); Chris@16: $anonymous_user['mail'] = 'tester@simpletest.org'; Chris@16: $anonymous_user['homepage'] = 'http://example.org/'; Chris@16: $comment2 = $this->saveComment($this->node->id(), 0, $anonymous_user); Chris@16: Chris@16: // Tests comment #2 as anonymous user. Chris@16: $parser = new \EasyRdf_Parser_Rdfa(); Chris@16: $graph = new \EasyRdf_Graph(); Chris@16: $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri); Chris@16: $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user); Chris@16: Chris@16: // Tests comment #2 as logged in user. Chris@16: $this->drupalLogin($this->webUser); Chris@16: $parser = new \EasyRdf_Parser_Rdfa(); Chris@16: $graph = new \EasyRdf_Graph(); Chris@16: $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri); Chris@16: $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests RDF comment replies. Chris@16: */ Chris@16: public function testCommentReplyOfRdfaMarkup() { Chris@16: // Posts comment #1 on behalf of registered user. Chris@16: $this->drupalLogin($this->webUser); Chris@16: $comment_1 = $this->saveComment($this->node->id(), $this->webUser->id()); Chris@16: Chris@18: $comment_1_uri = $comment_1->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@16: Chris@16: // Posts a reply to the first comment. Chris@16: $comment_2 = $this->saveComment($this->node->id(), $this->webUser->id(), NULL, $comment_1->id()); Chris@18: $comment_2_uri = $comment_2->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@16: Chris@16: $parser = new \EasyRdf_Parser_Rdfa(); Chris@16: $graph = new \EasyRdf_Graph(); Chris@16: $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri); Chris@16: Chris@16: // Tests the reply_of relationship of a first level comment. Chris@16: $expected_value = [ Chris@16: 'type' => 'uri', Chris@16: 'value' => $this->nodeUri, Chris@16: ]; Chris@16: $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: Chris@16: // Tests the reply_of relationship of a second level comment. Chris@16: $expected_value = [ Chris@16: 'type' => 'uri', Chris@16: 'value' => $this->nodeUri, Chris@16: ]; Chris@16: $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: $expected_value = [ Chris@16: 'type' => 'uri', Chris@16: 'value' => $comment_1_uri, Chris@16: ]; Chris@16: $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: } Chris@16: Chris@16: /** Chris@16: * Helper function for testCommentRdfaMarkup(). Chris@16: * Chris@16: * Tests the current page for basic comment RDFa markup. Chris@16: * Chris@16: * @param $comment Chris@16: * Comment object. Chris@16: * @param $account Chris@16: * An array containing information about an anonymous user. Chris@16: */ Chris@16: public function _testBasicCommentRdfaMarkup($graph, CommentInterface $comment, $account = []) { Chris@18: $comment_uri = $comment->toUrl('canonical', ['absolute' => TRUE])->toString(); Chris@16: Chris@16: // Comment type. Chris@16: $expected_value = [ Chris@16: 'type' => 'uri', Chris@16: 'value' => 'http://rdfs.org/sioc/types#Comment', Chris@16: ]; Chris@16: $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: // Comment type. Chris@16: $expected_value = [ Chris@16: 'type' => 'uri', Chris@16: 'value' => 'http://rdfs.org/sioc/ns#Post', Chris@16: ]; Chris@16: $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: Chris@16: // Comment title. Chris@16: $expected_value = [ Chris@16: 'type' => 'literal', Chris@16: 'value' => $comment->getSubject(), Chris@16: 'lang' => 'en', Chris@16: ]; Chris@16: $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).'); Chris@16: Chris@16: // Comment date. Chris@16: $expected_value = [ Chris@16: 'type' => 'literal', Chris@18: 'value' => $this->container->get('date.formatter')->format($comment->getCreatedTime(), 'custom', 'c', 'UTC'), Chris@16: 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime', Chris@16: ]; Chris@16: $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).'); Chris@16: // Comment date. Chris@16: $expected_value = [ Chris@16: 'type' => 'literal', Chris@18: 'value' => $this->container->get('date.formatter')->format($comment->getCreatedTime(), 'custom', 'c', 'UTC'), Chris@16: 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime', Chris@16: ]; Chris@16: $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).'); Chris@16: Chris@16: // Comment body. Chris@16: $expected_value = [ Chris@16: 'type' => 'literal', Chris@16: 'value' => $comment->comment_body->value . "\n", Chris@16: 'lang' => 'en', Chris@16: ]; Chris@16: $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: Chris@16: // The comment author can be a registered user or an anonymous user. Chris@16: if ($comment->getOwnerId() > 0) { Chris@18: $author_uri = Url::fromRoute('entity.user.canonical', ['user' => $comment->getOwnerId()], ['absolute' => TRUE])->toString(); Chris@16: // Comment relation to author. Chris@16: $expected_value = [ Chris@16: 'type' => 'uri', Chris@16: 'value' => $author_uri, Chris@16: ]; Chris@16: $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: } Chris@16: else { Chris@16: // The author is expected to be a blank node. Chris@16: $author_uri = $graph->get($comment_uri, ''); Chris@16: if ($author_uri instanceof \EasyRdf_Resource) { Chris@16: $this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator) and author is blank node.'); Chris@16: } Chris@16: else { Chris@16: $this->fail('Comment relation to author found in RDF output (sioc:has_creator).'); Chris@16: } Chris@16: } Chris@16: Chris@16: // Author name. Chris@18: $name = empty($account["name"]) ? $this->webUser->getAccountName() : $account["name"] . " (not verified)"; Chris@16: $expected_value = [ Chris@16: 'type' => 'literal', Chris@16: 'value' => $name, Chris@16: ]; Chris@16: $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: Chris@16: // Comment author homepage (only for anonymous authors). Chris@16: if ($comment->getOwnerId() == 0) { Chris@16: $expected_value = [ Chris@16: 'type' => 'uri', Chris@16: 'value' => 'http://example.org/', Chris@16: ]; Chris@16: $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: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * Creates a comment entity. Chris@16: * Chris@16: * @param $nid Chris@16: * Node id which will hold the comment. Chris@16: * @param $uid Chris@16: * User id of the author of the comment. Can be NULL if $contact provided. Chris@16: * @param $contact Chris@16: * Set to NULL for no contact info, TRUE to ignore success checking, and Chris@16: * array of values to set contact info. Chris@16: * @param $pid Chris@16: * Comment id of the parent comment in a thread. Chris@16: * Chris@16: * @return \Drupal\comment\Entity\Comment Chris@16: * The saved comment. Chris@16: */ Chris@16: public function saveComment($nid, $uid, $contact = NULL, $pid = 0) { Chris@16: $values = [ Chris@16: 'entity_id' => $nid, Chris@16: 'entity_type' => 'node', Chris@16: 'field_name' => 'comment', Chris@16: 'uid' => $uid, Chris@16: 'pid' => $pid, Chris@16: 'subject' => $this->randomMachineName(), Chris@16: 'comment_body' => $this->randomMachineName(), Chris@16: 'status' => 1, Chris@16: ]; Chris@16: if ($contact) { Chris@16: $values += $contact; Chris@16: } Chris@16: Chris@16: $comment = Comment::create($values); Chris@16: $comment->save(); Chris@16: return $comment; Chris@16: } Chris@16: Chris@16: }