#614508 by linclark and scor: Annotate tracker page with RDFa.
parent
8a70cd09ca
commit
811be8f77f
|
@ -795,6 +795,12 @@ function node_rdf_mapping() {
|
|||
),
|
||||
'comment_count' => array(
|
||||
'predicates' => array('sioc:num_replies'),
|
||||
'datatype' => 'xsd:integer',
|
||||
),
|
||||
'last_activity' => array(
|
||||
'predicates' => array('sioc:last_activity_date'),
|
||||
'datatype' => 'xsd:dateTime',
|
||||
'callback' => 'date_iso8601',
|
||||
),
|
||||
),
|
||||
),
|
||||
|
|
|
@ -204,3 +204,119 @@ class RdfMappingDefinitionTestCase extends DrupalWebTestCase {
|
|||
$this->assertTrue(!empty($user_profile_about), t('RDFa markup found on user profile page'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class RdfTrackerAttributesTestCase extends DrupalWebTestCase {
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'RDF tracker page mapping',
|
||||
'description' => 'Test the mapping for the tracker page and ensure the proper RDFa markup in included.',
|
||||
'group' => 'RDF',
|
||||
);
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
parent::setUp('rdf', 'rdf_test', 'tracker');
|
||||
// We need to trigger rdf_modules_installed() because
|
||||
// hook_modules_installed() is not automatically invoked during testing.
|
||||
rdf_modules_installed(array('rdf_test', 'node'));
|
||||
// entity_info caches must be cleared during testing. This is done
|
||||
// automatically during the manual installation.
|
||||
cache_clear_all('entity_info', 'cache');
|
||||
drupal_static_reset('entity_get_info');
|
||||
// Enable anonymous posting of content.
|
||||
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
|
||||
'create article content' => TRUE,
|
||||
'access comments' => TRUE,
|
||||
'post comments' => TRUE,
|
||||
'post comments without approval' => TRUE,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create nodes as both admin and anonymous user and test for correct RDFa
|
||||
* markup on the tracker page for those nodes and their comments.
|
||||
*/
|
||||
function testAttributesInTracker() {
|
||||
// Create node as anonymous user.
|
||||
$node_anon = $this->drupalCreateNode(array('type' => 'article', 'uid' => 0));
|
||||
// Create node as admin user.
|
||||
$node_admin = $this->drupalCreateNode(array('type' => 'article', 'uid' => 1));
|
||||
|
||||
// Pass both the anonymously posted node and the administrator posted node
|
||||
// through to test for the RDF attributes.
|
||||
$this->_testBasicTrackerRdfaMarkup($node_anon);
|
||||
$this->_testBasicTrackerRdfaMarkup($node_admin);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for testAttributesInTracker().
|
||||
*
|
||||
* Tests the tracker page for RDFa markup.
|
||||
*
|
||||
* @param $node
|
||||
* The node just created.
|
||||
*/
|
||||
function _testBasicTrackerRdfaMarkup($node) {
|
||||
$url = url('node/' . $node->nid);
|
||||
|
||||
$user = ($node->uid == 0) ? 'Anonymous user' : 'Registered user';
|
||||
|
||||
// Navigate to tracker page.
|
||||
$this->drupalGet('tracker');
|
||||
|
||||
// Tests whether the about property is applied. This is implicit in the
|
||||
// success of the following tests, but making it explicit will make
|
||||
// debugging easier in case of failure.
|
||||
$tracker_about = $this->xpath("//tr[@about='$url']");
|
||||
$this->assertTrue(!empty($tracker_about), t('About attribute found on table row for @user content.', array('@user'=> $user)));
|
||||
|
||||
// Tests whether the title has the correct property attribute.
|
||||
$tracker_title = $this->xpath("//tr[@about='$url']/td[@property='dc:title' and @datatype='']");
|
||||
$this->assertTrue(!empty($tracker_title), t('Title property attribute found on @user content.', array('@user'=> $user)));
|
||||
|
||||
// Tests whether the relationship between the content and user has been set.
|
||||
$tracker_user = $this->xpath("//tr[@about='$url']//td[contains(@rel, 'sioc:has_creator')]//*[contains(@typeof, 'sioc:User') and contains(@property, 'foaf:name')]");
|
||||
$this->assertTrue(!empty($tracker_user), t('Typeof and name property attributes found on @user.', array('@user'=> $user)));
|
||||
// There should be an about attribute on logged in users and no about
|
||||
// attribute for anonymous users.
|
||||
$tracker_user = $this->xpath("//tr[@about='$url']//td[@rel='sioc:has_creator']/*[@about]");
|
||||
if ($node->uid == 0) {
|
||||
$this->assertTrue(empty($tracker_user), t('No about attribute is present on @user.', array('@user'=> $user)));
|
||||
}
|
||||
elseif ($node->uid > 0) {
|
||||
$this->assertTrue(!empty($tracker_user), t('About attribute is present on @user.', array('@user'=> $user)));
|
||||
}
|
||||
|
||||
// Tests whether the property has been set for number of comments.
|
||||
$tracker_replies = $this->xpath("//tr[@about='$url']//td[contains(@property, 'sioc:num_replies') and contains(@content, '0')]");
|
||||
$this->assertTrue($tracker_replies, t('Num replies property and content attributes found on @user content.', array('@user'=> $user)));
|
||||
|
||||
// Tests that the appropriate RDFa markup to annotate the latest activity
|
||||
// date has been added to the tracker output before comments have been
|
||||
// posted, meaning the latest activity reflects changes to the node itself.
|
||||
$isoDate = date('c', $node->changed);
|
||||
$tracker_activity = $this->xpath("//tr[@about='$url']//td[contains(@property, 'dc:modified') and contains(@property, 'sioc:last_activity_date') and contains(@datatype, 'xsd:dateTime') and @content='$isoDate']");
|
||||
$this->assertTrue(!empty($tracker_activity), t('Latest activity date and changed properties found when there are no comments on @user content. Latest activity date content is correct.', array('@user'=> $user)));
|
||||
|
||||
// Tests that the appropriate RDFa markup to annotate the latest activity
|
||||
// date has been added to the tracker output after a comment is posted.
|
||||
CommentHelperCase::postComment($node, $this->randomName(), $this->randomName());
|
||||
$this->drupalGet('tracker');
|
||||
|
||||
// Tests whether the property has been set for number of comments.
|
||||
$tracker_replies = $this->xpath("//tr[@about='$url']//td[contains(@property, 'sioc:num_replies') and contains(@content, '1')]");
|
||||
$this->assertTrue($tracker_replies, t('Num replies property and content attributes found on @user content.', array('@user'=> $user)));
|
||||
|
||||
// Need to query database directly to obtain last_activity_date because
|
||||
// it cannot be accessed via node_load().
|
||||
$result = db_query('SELECT t.changed FROM {tracker_node} t WHERE t.nid = (:nid)', array(':nid' => $node->nid));
|
||||
foreach ($result as $node) {
|
||||
$expected_last_activity_date = $node->changed;
|
||||
}
|
||||
$isoDate = date('c', $expected_last_activity_date);
|
||||
$tracker_activity = $this->xpath("//tr[@about='$url']//td[@property='sioc:last_activity_date' and @datatype='xsd:dateTime' and @content='$isoDate']");
|
||||
$this->assertTrue(!empty($tracker_activity), t('Latest activity date found when there are comments on @user content. Latest activity date content is correct.', array('@user'=> $user)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,13 +59,51 @@ function tracker_page($account = NULL, $set_title = FALSE) {
|
|||
}
|
||||
}
|
||||
|
||||
$rows[] = array(
|
||||
check_plain(node_type_get_name($node->type)),
|
||||
l($node->title, 'node/' . $node->nid) . ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed))),
|
||||
theme('username', array('account' => $node)),
|
||||
array('class' => array('replies'), 'data' => $comments),
|
||||
t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))
|
||||
$row = array(
|
||||
'type' => check_plain(node_type_get_name($node->type)),
|
||||
'title' => array('data' => l($node->title, 'node/' . $node->nid) . ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed)))),
|
||||
'author' => array('data' => theme('username', array('account' => $node))),
|
||||
'replies' => array('class' => array('replies'), 'data' => $comments),
|
||||
'last updated' => array('data' => t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))),
|
||||
);
|
||||
|
||||
// Adds extra RDFa markup to the $row array if the RDF module is enabled.
|
||||
if (function_exists('rdf_mapping_load')) {
|
||||
// Each node is not loaded for performance reasons, as a result we need
|
||||
// to retrieve the RDF mapping for each node type.
|
||||
$mapping = rdf_mapping_load('node', $node->type);
|
||||
// Adds RDFa markup to the title of the node. Because the RDFa markup is
|
||||
// added to the td tag which might contain HTML code, we specify an
|
||||
// empty datatype to ensure the value of the title read by the RDFa
|
||||
// parsers is a plain literal.
|
||||
$row['title'] += rdf_rdfa_attributes($mapping['title']) + array('datatype' => '');
|
||||
// Annotates the td tag containing the author of the node.
|
||||
$row['author'] += rdf_rdfa_attributes($mapping['uid']);
|
||||
// Annotates the td tag containing the number of replies. We add the
|
||||
// content attribute to ensure that only the comment count is used as
|
||||
// the value for 'num_replies'. Otherwise, other text such as a link
|
||||
// to the number of new comments could be included in the 'num_replies'
|
||||
// value.
|
||||
$row['replies'] += rdf_rdfa_attributes($mapping['comment_count']);
|
||||
$row['replies'] += array('content' => $node->comment_count);
|
||||
// If the node has no comments, we assume the node itself was modified
|
||||
// and apply 'changed' in addition to 'last_activity'. If there are
|
||||
// comments present, we cannot infer whether the node itself was
|
||||
// modified or a comment was posted, so we use only 'last_activity'.
|
||||
$mapping_last_activity = rdf_rdfa_attributes($mapping['last_activity'], $node->last_activity);
|
||||
if ($node->comment_count == 0) {
|
||||
$mapping_changed = rdf_rdfa_attributes($mapping['changed'], $node->last_activity);
|
||||
$mapping_last_activity['property'] = array_merge($mapping_last_activity['property'], $mapping_changed['property']);
|
||||
}
|
||||
$row['last updated'] += $mapping_last_activity;
|
||||
|
||||
// We need to add the about attribute on the tr tag to specify which
|
||||
// node the RDFa annoatations above apply to. We move the content of
|
||||
// $row to a 'data' sub array so we can specify attributes for the row.
|
||||
$row = array('data' => $row);
|
||||
$row['about'] = url('node/' . $node->nid);
|
||||
}
|
||||
$rows[] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue