Issue #1886108 by linclark, scor: Rewrite CommentAttributesTest to parse RDFa.
parent
eec970060c
commit
fe15dd23de
|
@ -2,7 +2,7 @@
|
|||
|
||||
/**
|
||||
* @file
|
||||
* Definition of Drupal\rdf\Tests\CommentAttributesTest.
|
||||
* Contains Drupal\rdf\Tests\CommentAttributesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\rdf\Tests;
|
||||
|
@ -14,25 +14,16 @@ use Drupal\comment\Tests\CommentTestBase;
|
|||
*/
|
||||
class CommentAttributesTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Use the standard profile.
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @todo Remove this dependency if possible.
|
||||
*/
|
||||
protected $profile = 'standard';
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('comment', 'rdf', 'rdf_test');
|
||||
public static $modules = array('comment', 'rdf');
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'RDF comment mapping',
|
||||
'name' => 'RDFa markup for comments',
|
||||
'description' => 'Tests the RDFa markup of comments.',
|
||||
'group' => 'RDF',
|
||||
);
|
||||
|
@ -41,9 +32,6 @@ class CommentAttributesTest extends CommentTestBase {
|
|||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer permissions', 'administer blocks'));
|
||||
$this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'access user profiles'));
|
||||
|
||||
// Enables anonymous user comments.
|
||||
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
|
||||
'access comments' => TRUE,
|
||||
|
@ -57,34 +45,41 @@ class CommentAttributesTest extends CommentTestBase {
|
|||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
|
||||
// Creates the nodes on which the test comments will be posted.
|
||||
$this->drupalLogin($this->web_user);
|
||||
$this->node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
|
||||
$this->node2 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
|
||||
$this->drupalLogout();
|
||||
// Prepares commonly used URIs.
|
||||
$this->base_uri = url('<front>', array('absolute' => TRUE));
|
||||
$this->node_uri = url('node/' . $this->node->nid, array('absolute' => TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the presence of the RDFa markup for the number of comments.
|
||||
*/
|
||||
public function testNumberOfCommentsRdfaMarkup() {
|
||||
// Posts 2 comments as a registered user.
|
||||
$this->drupalLogin($this->web_user);
|
||||
$this->postComment($this->node1, $this->randomName(), $this->randomName());
|
||||
$this->postComment($this->node1, $this->randomName(), $this->randomName());
|
||||
// Posts 2 comments on behalf of registered user.
|
||||
$this->saveComment($this->node->nid, $this->web_user->uid);
|
||||
$this->saveComment($this->node->nid, $this->web_user->uid);
|
||||
|
||||
// Tests number of comments in teaser view.
|
||||
$this->drupalGet('node');
|
||||
$comment_count_teaser = $this->xpath('//div[contains(@typeof, "sioc:Item")]//li[contains(@class, "comment-comments")]/a[contains(@property, "sioc:num_replies") and contains(@content, "2") and @datatype="xsd:integer"]');
|
||||
$this->assertTrue(!empty($comment_count_teaser), 'RDFa markup for the number of comments found on teaser view.');
|
||||
$comment_count_link = $this->xpath('//div[@about=:url]//a[contains(@property, "sioc:num_replies") and @rel=""]', array(':url' => url("node/{$this->node1->nid}")));
|
||||
$this->assertTrue(!empty($comment_count_link), 'Empty rel attribute found in comment count link.');
|
||||
$this->drupalLogin($this->web_user);
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node'), 'rdfa', $this->base_uri);
|
||||
|
||||
// Tests number of comments in full node view.
|
||||
$this->drupalGet('node/' . $this->node1->nid);
|
||||
$node_url = url('node/' . $this->node1->nid);
|
||||
$comment_count_teaser = $this->xpath('/html/head/meta[@about=:node-url and @property="sioc:num_replies" and @content="2" and @datatype="xsd:integer"]', array(':node-url' => $node_url));
|
||||
$this->assertTrue(!empty($comment_count_teaser), 'RDFa markup for the number of comments found on full node view.');
|
||||
// Number of comments.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => 2,
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#integer',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($this->node_uri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of teaser view (sioc:num_replies).');
|
||||
// Makes sure we don't generate the wrong statement for number of comments.
|
||||
$node_comments_uri = url('node/' . $this->node->nid, array('fragment' => 'comments', 'absolute' => TRUE));
|
||||
$this->assertFalse($graph->hasProperty($node_comments_uri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of teaser view mode (sioc:num_replies).');
|
||||
|
||||
// Tests number of comments in full node view, expected value is the same.
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->nid), 'rdfa', $this->base_uri);
|
||||
$this->assertTrue($graph->hasProperty($this->node_uri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of full node view mode (sioc:num_replies).');;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,77 +89,80 @@ class CommentAttributesTest extends CommentTestBase {
|
|||
* on comments from registered and anonymous users.
|
||||
*/
|
||||
public function testCommentRdfaMarkup() {
|
||||
|
||||
// Posts comment #1 as a registered user.
|
||||
$this->drupalLogin($this->web_user);
|
||||
$comment1_subject = $this->randomName();
|
||||
$comment1_body = $this->randomName();
|
||||
$comment1 = $this->postComment($this->node1, $comment1_body, $comment1_subject);
|
||||
// Posts comment #1 on behalf of registered user.
|
||||
$comment1 = $this->saveComment($this->node->nid, $this->web_user->uid);
|
||||
|
||||
// Tests comment #1 with access to the user profile.
|
||||
$this->drupalGet('node/' . $this->node1->nid);
|
||||
$this->_testBasicCommentRdfaMarkup($comment1);
|
||||
$this->drupalLogin($this->web_user);
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->nid), 'rdfa', $this->base_uri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment1);
|
||||
|
||||
// Tests comment #1 with no access to the user profile (as anonymous user).
|
||||
$this->drupalLogout();
|
||||
$this->drupalGet('node/' . $this->node1->nid);
|
||||
$this->_testBasicCommentRdfaMarkup($comment1);
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->nid), 'rdfa', $this->base_uri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment1);
|
||||
|
||||
// Posts comment #2 as anonymous user.
|
||||
$comment2_subject = $this->randomName();
|
||||
$comment2_body = $this->randomName();
|
||||
$anonymous_user = array();
|
||||
$anonymous_user['name'] = $this->randomName();
|
||||
$anonymous_user['mail'] = 'tester@simpletest.org';
|
||||
$anonymous_user['homepage'] = 'http://example.org/';
|
||||
$comment2 = $this->postComment($this->node2, $comment2_body, $comment2_subject, $anonymous_user);
|
||||
$this->drupalGet('node/' . $this->node2->nid);
|
||||
$comment2 = $this->saveComment($this->node->nid, NULL, $anonymous_user);
|
||||
|
||||
// Tests comment #2 as anonymous user.
|
||||
$this->_testBasicCommentRdfaMarkup($comment2, $anonymous_user);
|
||||
// Tests the RDFa markup for the homepage (specific to anonymous comments).
|
||||
$comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[contains(@class, "username") and @typeof="sioc:UserAccount" and @property="foaf:name" and @datatype="" and @href="http://example.org/" and contains(@rel, "foaf:page")]');
|
||||
$this->assertTrue(!empty($comment_homepage), 'RDFa markup for the homepage of anonymous user found.');
|
||||
// There should be no about attribute on anonymous comments.
|
||||
$comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[@about]');
|
||||
$this->assertTrue(empty($comment_homepage), 'No about attribute is present on anonymous user comment.');
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->nid), 'rdfa', $this->base_uri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
|
||||
|
||||
// Tests comment #2 as logged in user.
|
||||
$this->drupalLogin($this->web_user);
|
||||
$this->drupalGet('node/' . $this->node2->nid);
|
||||
$this->_testBasicCommentRdfaMarkup($comment2, $anonymous_user);
|
||||
// Tests the RDFa markup for the homepage (specific to anonymous comments).
|
||||
$comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[contains(@class, "username") and @typeof="sioc:UserAccount" and @property="foaf:name" and @datatype="" and @href="http://example.org/" and contains(@rel, "foaf:page")]');
|
||||
$this->assertTrue(!empty($comment_homepage), "RDFa markup for the homepage of anonymous user found.");
|
||||
// There should be no about attribute on anonymous comments.
|
||||
$comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[@about]');
|
||||
$this->assertTrue(empty($comment_homepage), "No about attribute is present on anonymous user comment.");
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->nid), 'rdfa', $this->base_uri);
|
||||
$this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests RDF comment replies.
|
||||
*/
|
||||
public function testCommentReplyOfRdfaMarkup() {
|
||||
// Posts comment #1 as a registered user.
|
||||
// Posts comment #1 on behalf of registered user.
|
||||
$this->drupalLogin($this->web_user);
|
||||
$comments[] = $this->postComment($this->node1, $this->randomName(), $this->randomName());
|
||||
$comment_1 = $this->saveComment($this->node->nid, $this->web_user->uid);
|
||||
|
||||
// Tests the reply_of relationship of a first level comment.
|
||||
$result = $this->xpath("(id('comments')//div[contains(@class,'comment ')])[position()=1]//span[@rel='sioc:reply_of' and @resource=:node]", array(':node' => url("node/{$this->node1->nid}")));
|
||||
$this->assertEqual(1, count($result), 'RDFa markup referring to the node is present.');
|
||||
$result = $this->xpath("(id('comments')//div[contains(@class,'comment ')])[position()=1]//span[@rel='sioc:reply_of' and @resource=:comment]", array(':comment' => url('comment/1#comment-1')));
|
||||
$this->assertFalse($result, 'No RDFa markup referring to the comment itself is present.');
|
||||
$comment_1_uri = url('comment/' . $comment_1->id(), array('fragment' => 'comment-' . $comment_1->id(), 'absolute' => TRUE));
|
||||
|
||||
// Posts a reply to the first comment.
|
||||
$this->drupalGet('comment/reply/' . $this->node1->nid . '/' . $comments[0]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
|
||||
$comment_2 = $this->saveComment($this->node->nid, $this->web_user->uid, NULL, $comment_1->id());
|
||||
$comment_2_uri = url('comment/' . $comment_2->id(), array('fragment' => 'comment-' . $comment_2->id(), 'absolute' => TRUE));
|
||||
|
||||
$parser = new \EasyRdf_Parser_Rdfa();
|
||||
$graph = new \EasyRdf_Graph();
|
||||
$parser->parse($graph, $this->drupalGet('node/' . $this->node->nid), 'rdfa', $this->base_uri);
|
||||
|
||||
// Tests the reply_of relationship of a first level comment.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->node_uri,
|
||||
);
|
||||
$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).');
|
||||
|
||||
// Tests the reply_of relationship of a second level comment.
|
||||
$result = $this->xpath("(id('comments')//div[contains(@class,'comment ')])[position()=2]//span[@rel='sioc:reply_of' and @resource=:node]", array(':node' => url("node/{$this->node1->nid}")));
|
||||
$this->assertEqual(1, count($result), 'RDFa markup referring to the node is present.');
|
||||
$result = $this->xpath("(id('comments')//div[contains(@class,'comment ')])[position()=2]//span[@rel='sioc:reply_of' and @resource=:comment]", array(':comment' => url('comment/1', array('fragment' => 'comment-1'))));
|
||||
$this->assertEqual(1, count($result), 'RDFa markup referring to the parent comment is present.');
|
||||
$comments = $this->xpath("(id('comments')//div[contains(@class,'comment ')])[position()=2]");
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $this->node_uri,
|
||||
);
|
||||
$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).');
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $comment_1_uri,
|
||||
);
|
||||
$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).');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -177,18 +175,116 @@ class CommentAttributesTest extends CommentTestBase {
|
|||
* @param $account
|
||||
* An array containing information about an anonymous user.
|
||||
*/
|
||||
function _testBasicCommentRdfaMarkup($comment, $account = array()) {
|
||||
$comment_container = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]');
|
||||
$this->assertTrue(!empty($comment_container), 'Comment RDF type for comment found.');
|
||||
$comment_title = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//h3[@property="dc:title"]');
|
||||
$this->assertEqual((string) $comment_title[0]->a, $comment->subject->value, 'RDFa markup for the comment title found.');
|
||||
$comment_date = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//*[contains(@property, "dc:date") and contains(@property, "dc:created")]');
|
||||
$this->assertTrue(!empty($comment_date), 'RDFa markup for the date of the comment found.');
|
||||
// The author tag can be either a or span
|
||||
$comment_author = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/*[contains(@class, "username") and @typeof="sioc:UserAccount" and @property="foaf:name" and @datatype=""]');
|
||||
function _testBasicCommentRdfaMarkup($graph, $comment, $account = array()) {
|
||||
$comment_uri = url('comment/' . $comment->id(), array('fragment' => 'comment-' . $comment->id(), 'absolute' => TRUE));
|
||||
|
||||
// Comment type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://rdfs.org/sioc/types#Comment',
|
||||
);
|
||||
$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).');
|
||||
// Comment type.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://rdfs.org/sioc/ns#Post',
|
||||
);
|
||||
$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).');
|
||||
|
||||
// Comment title.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $comment->subject->value,
|
||||
'lang' => 'en',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment title found in RDF output (dc:title).');
|
||||
|
||||
// Comment date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date('c', $comment->created->value),
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');
|
||||
// Comment date.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => date('c', $comment->created->value),
|
||||
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');
|
||||
|
||||
// Comment body.
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $comment->comment_body->value . "\n",
|
||||
'lang' => 'en',
|
||||
);
|
||||
$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).');
|
||||
|
||||
// The comment author can be a registered user or an anonymous user.
|
||||
if ($comment->uid->value > 0) {
|
||||
$author_uri = url('user/' . $comment->uid->value, array('absolute' => TRUE));
|
||||
// Comment relation to author.
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => $author_uri,
|
||||
);
|
||||
$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).');
|
||||
}
|
||||
else {
|
||||
// The author is expected to be a blank node.
|
||||
$author_uri = $graph->get($comment_uri, '<http://rdfs.org/sioc/ns#has_creator>');
|
||||
$this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator).');
|
||||
}
|
||||
|
||||
// Author name.
|
||||
$name = empty($account["name"]) ? $this->web_user->name : $account["name"] . " (not verified)";
|
||||
$this->assertEqual((string) $comment_author[0], $name, 'RDFa markup for the comment author found.');
|
||||
$comment_body = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//div[@class="content"]//div[contains(@class, "comment-body")]//div[@property="content:encoded"]');
|
||||
$this->assertEqual((string) $comment_body[0]->p, $comment->comment_body->value, 'RDFa markup for the comment body found.');
|
||||
$expected_value = array(
|
||||
'type' => 'literal',
|
||||
'value' => $name,
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'Comment author name found in RDF output (foaf:name).');
|
||||
|
||||
// Comment author homepage (only for anonymous authors).
|
||||
if ($comment->uid->value == 0) {
|
||||
$expected_value = array(
|
||||
'type' => 'uri',
|
||||
'value' => 'http://example.org/',
|
||||
);
|
||||
$this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/page', $expected_value), 'Comment author link found in RDF output (foaf:page).');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comment entity.
|
||||
*
|
||||
* @param $nid
|
||||
* Node id which will hold the comment.
|
||||
* @param $uid
|
||||
* User id of the author of the comment. Can be NULL if $contact provided.
|
||||
* @param $contact
|
||||
* Set to NULL for no contact info, TRUE to ignore success checking, and
|
||||
* array of values to set contact info.
|
||||
* @param $pid
|
||||
* Comment id of the parent comment in a thread.
|
||||
*/
|
||||
function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
|
||||
$values = array(
|
||||
'nid' => $nid,
|
||||
'uid' => $uid,
|
||||
'pid' => $pid,
|
||||
'node_type' => 'comment_node_article',
|
||||
'subject' => $this->randomName(),
|
||||
'comment_body' => $this->randomName(),
|
||||
'status' => 1,
|
||||
);
|
||||
if ($contact) {
|
||||
$values += $contact;
|
||||
}
|
||||
|
||||
$comment = entity_create('comment', $values);
|
||||
$comment->save();
|
||||
return $comment;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue