drupal/modules/rdf/rdf.test

512 lines
22 KiB
Plaintext
Raw Normal View History

<?php
// $Id$
/**
* @file
* Tests for RDF functionality.
*/
class RdfMappingHookTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'RDF mapping hook',
'description' => 'Test hook_rdf_mapping().',
'group' => 'RDF',
);
}
function setUp() {
parent::setUp('rdf', 'rdf_test', 'field_test');
}
/**
* Test that hook_rdf_mapping() correctly returns and processes mapping.
*/
function testMapping() {
// Test that the mapping is returned correctly by the hook.
$mapping = rdf_mapping_load('test_entity', 'test_bundle');
$this->assertIdentical($mapping['rdftype'], array('sioc:Post'), t('Mapping for rdftype is sioc:Post.'));
$this->assertIdentical($mapping['title'], array('predicates' => array('dc:title')), t('Mapping for title is dc:title.'));
$this->assertIdentical($mapping['created'], array(
'predicates' => array('dc:created'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
), t('Mapping for created is dc:created with datatype xsd:dateTime and callback date_iso8601.'));
$this->assertIdentical($mapping['uid'], array('predicates' => array('sioc:has_creator', 'dc:creator'), 'type' => 'rel'), t('Mapping for uid is sioc:has_creator and dc:creator, and type is rel.'));
$mapping = rdf_mapping_load('test_entity', 'test_bundle_no_mapping');
$this->assertEqual($mapping, array(), t('Empty array returned when an entity type, bundle pair has no mapping.'));
}
}
class RdfMarkupTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'RDFa markup',
'description' => 'Test RDFa markup generation.',
'group' => 'RDF',
);
}
function setUp() {
parent::setUp('rdf', 'field_test', 'rdf_test');
}
/**
* Test rdf_rdfa_attributes().
*/
function testDrupalRdfaAtributes() {
// Same value as the one in the HTML tag (no callback function).
$expected_attributes = array(
'property' => array('dc:title'),
);
$mapping = rdf_mapping_load('test_entity', 'test_bundle');
$attributes = rdf_rdfa_attributes($mapping['title']);
ksort($expected_attributes);
ksort($attributes);
$this->assertEqual($expected_attributes, $attributes);
// Value different from the one in the HTML tag (callback function).
$date = 1252750327;
$isoDate = date('c', $date);
$expected_attributes = array(
'datatype' => 'xsd:dateTime',
'property' => array('dc:created'),
'content' => $isoDate,
);
$mapping = rdf_mapping_load('test_entity', 'test_bundle');
$attributes = rdf_rdfa_attributes($mapping['created'], $date);
ksort($expected_attributes);
ksort($attributes);
$this->assertEqual($expected_attributes, $attributes);
// Same value as the one in the HTML tag with datatype.
$expected_attributes = array(
'datatype' => 'foo:bar1type',
'property' => array('foo:bar1'),
);
$mapping = rdf_mapping_load('test_entity', 'test_bundle');
$attributes = rdf_rdfa_attributes($mapping['foobar1']);
ksort($expected_attributes);
ksort($attributes);
$this->assertEqual($expected_attributes, $attributes);
// ObjectProperty mapping (rel).
$expected_attributes = array(
'rel' => array('sioc:has_creator', 'dc:creator'),
);
$mapping = rdf_mapping_load('test_entity', 'test_bundle');
$attributes = rdf_rdfa_attributes($mapping['foobar_objproperty1']);
ksort($expected_attributes);
ksort($attributes);
$this->assertEqual($expected_attributes, $attributes);
// Inverse ObjectProperty mapping (rev).
$expected_attributes = array(
'rev' => array('sioc:reply_of'),
);
$mapping = rdf_mapping_load('test_entity', 'test_bundle');
$attributes = rdf_rdfa_attributes($mapping['foobar_objproperty2']);
ksort($expected_attributes);
ksort($attributes);
$this->assertEqual($expected_attributes, $attributes);
}
/**
* Ensure that file fields have the correct resource as the object in RDFa
* when displayed as a teaser.
*/
function testAttributesInMarkupFile() {
// Create a user to post the image.
$admin_user = $this->drupalCreateUser(array('edit own article content', 'revert revisions', 'administer content types'));
$this->drupalLogin($admin_user);
$langcode = LANGUAGE_NONE;
$bundle_name = "article";
// Create file field.
$file_field = 'file_test';
$edit = array(
'_add_new_field[label]' => $file_field,
'_add_new_field[field_name]' => $file_field,
'_add_new_field[type]' => 'file',
'_add_new_field[widget_type]' => 'file_generic',
);
$this->drupalPost('admin/structure/types/manage/' . $bundle_name . '/fields', $edit, t('Save'));
// Set the RDF mapping for the new field.
$rdf_mapping = rdf_mapping_load('node', $bundle_name);
$rdf_mapping += array('field_' . $file_field => array('predicates' => array('rdfs:seeAlso'), 'type' => 'rel'));
$rdf_mapping_save = array('mapping' => $rdf_mapping, 'type' => 'node', 'bundle' => $bundle_name);
rdf_mapping_save($rdf_mapping_save);
// Get the test file that simpletest provides.
$file = current($this->drupalGetTestFiles('text'));
// Prepare image variables.
$image_field = "field_image";
// Get the test image that simpletest provides.
$image = current($this->drupalGetTestFiles('image'));
// Create an array for drupalPost with the field names as the keys and
// the uris for the test files as the values.
$edit = array("files[field_" . $file_field . "_" . $langcode . "_0]" => drupal_realpath($file->uri),
"files[" . $image_field . "_" . $langcode . "_0]" => drupal_realpath($image->uri));
// Create node and save, then edit node to upload files.
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Get filenames and nid for comparison with HTML output.
$file_filename = $file->filename;
$image_filename = $image->filename;
$nid = $node->nid;
// Navigate to front page, where node is displayed in teaser form.
$this->drupalGet('node');
// We only check to make sure that the resource attribute contains '.txt'
// instead of the full file name because the filename is altered on upload.
$file_rel = $this->xpath('//div[contains(@about, :node-uri)]//div[contains(@rel, "rdfs:seeAlso") and contains(@resource, ".txt")]', array(
':node-uri' => 'node/' . $nid,
));
$this->assertTrue(!empty($file_rel), t('Attribute \'rel\' set on file field. Attribute \'resource\' is also set.'));
$image_rel = $this->xpath('//div[contains(@about, :node-uri)]//div[contains(@rel, "rdfs:seeAlso") and contains(@resource, :image)]//img[contains(@typeof, "foaf:Image")]', array(
':node-uri' => 'node/' . $nid,
':image' => $image_filename,
));
$this->assertTrue(!empty($image_rel), t('Attribute \'rel\' set on image field. Attribute \'resource\' is also set.'));
}
}
class RdfCrudTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'RDF mapping CRUD functions',
'description' => 'Test the RDF mapping CRUD functions.',
'group' => 'RDF',
);
}
function setUp() {
parent::setUp('rdf', 'rdf_test');
}
/**
* Test inserting, loading, updating, and deleting RDF mappings.
*/
function testCRUD() {
// Verify loading of a default mapping.
$mapping = _rdf_mapping_load('test_entity', 'test_bundle');
$this->assertTrue(count($mapping), t('Default mapping was found.'));
// Verify saving a mapping.
$mapping = array(
'type' => 'crud_test_entity',
'bundle' => 'crud_test_bundle',
'mapping' => array(
'rdftype' => array('sioc:Post'),
'title' => array(
'predicates' => array('dc:title'),
),
'uid' => array(
'predicates' => array('sioc:has_creator', 'dc:creator'),
'type' => 'rel',
),
),
);
$this->assertTrue(rdf_mapping_save($mapping) === SAVED_NEW, t('Mapping was saved.'));
// Read the raw record from the {rdf_mapping} table.
$result = db_query('SELECT * FROM {rdf_mapping} WHERE type = :type AND bundle = :bundle', array(':type' => $mapping['type'], ':bundle' => $mapping['bundle']));
$stored_mapping = $result->fetchAssoc();
$stored_mapping['mapping'] = unserialize($stored_mapping['mapping']);
$this->assertEqual($mapping, $stored_mapping, t('Mapping was stored properly in the {rdf_mapping} table.'));
// Verify loading of saved mapping.
$this->assertEqual($mapping['mapping'], _rdf_mapping_load($mapping['type'], $mapping['bundle']), t('Saved mapping loaded successfully.'));
// Verify updating of mapping.
$mapping['mapping']['title'] = array(
'predicates' => array('dc2:bar2'),
);
$this->assertTrue(rdf_mapping_save($mapping) === SAVED_UPDATED, t('Mapping was updated.'));
// Read the raw record from the {rdf_mapping} table.
$result = db_query('SELECT * FROM {rdf_mapping} WHERE type = :type AND bundle = :bundle', array(':type' => $mapping['type'], ':bundle' => $mapping['bundle']));
$stored_mapping = $result->fetchAssoc();
$stored_mapping['mapping'] = unserialize($stored_mapping['mapping']);
$this->assertEqual($mapping, $stored_mapping, t('Updated mapping was stored properly in the {rdf_mapping} table.'));
// Verify loading of saved mapping.
$this->assertEqual($mapping['mapping'], _rdf_mapping_load($mapping['type'], $mapping['bundle']), t('Saved mapping loaded successfully.'));
// Verify deleting of mapping.
$this->assertTrue(rdf_mapping_delete($mapping['type'], $mapping['bundle']), t('Mapping was deleted.'));
$this->assertFalse(_rdf_mapping_load($mapping['type'], $mapping['bundle']), t('Deleted mapping is no longer found in the database.'));
}
}
class RdfMappingDefinitionTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'RDF mapping definition functionality',
'description' => 'Test the different types of RDF mappings and ensure the proper RDFa markup in included in nodes and user profile pages.',
'group' => 'RDF',
);
}
function setUp() {
parent::setUp('rdf', 'rdf_test', 'blog');
}
/**
* Create a node of type blog and test whether the RDF mapping defined for
* this node type in rdf_test.module is used in the node page.
*/
function testAttributesInMarkup1() {
$node = $this->drupalCreateNode(array('type' => 'blog'));
$this->drupalGet('node/' . $node->nid);
$this->assertRaw('typeof="sioct:Weblog"');
// Ensure the default bundle mapping for node is used. These attributes come
// from the node default bundle definition.
$this->assertRaw('property="dc:title"');
$this->assertRaw('property="dc:date dc:created"');
}
/**
* Create a content type and a node of type test_bundle_hook_install and test
* whether the RDF mapping defined in rdf_test.install is used.
*/
function testAttributesInMarkup2() {
$type = $this->drupalCreateContentType(array('type' => 'test_bundle_hook_install'));
$node = $this->drupalCreateNode(array('type' => 'test_bundle_hook_install'));
$this->drupalGet('node/' . $node->nid);
$this->assertRaw('typeof="foo:mapping_install1 bar:mapping_install2"');
// Ensure the default bundle mapping for node is used. These attributes come
// from the node default bundle definition.
$this->assertRaw('property="dc:title"');
$this->assertRaw('property="dc:date dc:created"');
}
/**
* Create a random content type and node and ensure the default mapping for
* node is used.
*/
function testAttributesInMarkup3() {
$type = $this->drupalCreateContentType();
$node = $this->drupalCreateNode(array('type' => $type->type));
$this->drupalGet('node/' . $node->nid);
$this->assertRaw('typeof="sioc:Item foaf:Document"');
// Ensure the default bundle mapping for node is used. These attributes come
// from the node default bundle definition.
$this->assertRaw('property="dc:title"');
$this->assertRaw('property="dc:date dc:created"');
}
/**
* Create a random user and ensure the default mapping for user is used.
*/
function testUserAttributesInMarkup() {
// Create two users, one with access to user profiles.
$user1 = $this->drupalCreateUser(array('access user profiles'));
$user2 = $this->drupalCreateUser();
$username = $user2->name;
$this->drupalLogin($user1);
// Browse to the user profile page.
$this->drupalGet('user/' . $user2->uid);
// Ensure the default bundle mapping for user is used on the user profile
// page. These attributes come from the user default bundle definition.
$account_uri = url('user/' . $user2->uid);
$person_uri = url('user/' . $user2->uid, array('fragment' => 'me'));
$user2_profile_about = $this->xpath('//div[@class="profile" and @typeof="sioc:User" and @about=:account-uri]', array(
':account-uri' => $account_uri,
));
$this->assertTrue(!empty($user2_profile_about), t('RDFa markup found on user profile page'));
$user_account_holder = $this->xpath('//meta[contains(@typeof, "foaf:Person") and @about=:person-uri and @resource=:account-uri and contains(@rel, "foaf:account")]', array(
':person-uri' => $person_uri,
':account-uri' => $account_uri,
));
$this->assertTrue(!empty($user_account_holder), t('URI created for account holder and username set on sioc:User.'));
$user_username = $this->xpath('//meta[@about=:account-uri and contains(@property, "foaf:name") and @content=:username]', array(
':account-uri' => $account_uri,
':username' => $username,
));
$this->assertTrue(!empty($user_username), t('foaf:name set on username.'));
// User 2 creates node.
$this->drupalLogin($user2);
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
$this->drupalLogin($user1);
$this->drupalGet('node/' . $node->nid);
// Ensures the default bundle mapping for user is used on the Authored By
// information on the node.
$author_about = $this->xpath('//a[@typeof="sioc:User" and @about=:account-uri and @property="foaf:name" and contains(@xml:lang, "")]', array(
':account-uri' => $account_uri,
));
$this->assertTrue(!empty($author_about), t('RDFa markup found on author information on post. xml:lang on username is set to empty string.'));
}
/**
* Creates a random term and ensures the right RDFa markup is used.
*/
function testTaxonomyTermRdfaAttributes() {
$vocabulary = TaxonomyWebTestCase::createVocabulary();
$term = TaxonomyWebTestCase::createTerm($vocabulary);
// Views the term and checks that the RDFa markup is correct.
$this->drupalGet('taxonomy/term/' . $term->tid);
$term_url = url('taxonomy/term/' . $term->tid);
$term_name = $term->name;
$term_rdfa_meta = $this->xpath('//meta[@typeof="skos:Concept" and @about=:term-url and contains(@property, "rdfs:label") and contains(@property, "skos:prefLabel") and @content=:term-name]', array(
':term-url' => $term_url,
':term-name' => $term_name,
));
$this->assertTrue(!empty($term_rdfa_meta), t('RDFa markup found on term page.'));
}
}
class RdfCommentAttributesTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'RDF comment mapping',
'description' => 'Tests the RDFa markup of comments.',
'group' => 'RDF',
);
}
public function setUp() {
parent::setUp('rdf', 'rdf_test', 'comment');
// 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,
));
}
public function testAttributesInTeaser() {
$node = $this->drupalCreateNode(array('type' => 'article', 'uid' => 1, 'promote' => 1));
CommentHelperCase::postComment($node, $this->randomName(), $this->randomName());
$this->drupalGet('');
$comment_count_link = $this->xpath('//div[@about=:url]//a[contains(@property, "sioc:num_replies") and @rel=""]', array(':url' => url("node/$node->nid")));
$this->assertTrue(!empty($comment_count_link), t('Empty rel attribute found in comment count link.'));
}
}
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');
// 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]', array(':url' => $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=""]', array(':url' => $url));
$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")]', array(':url' => $url));
$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]', array(':url' => $url));
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") and @datatype="xsd:integer"]', array(':url' => $url));
$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=:date]', array(':url' => $url, ':date' => $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") and @datatype="xsd:integer"]', array(':url' => $url));
$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=:date]', array(':url' => $url, ':date' => $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)));
}
}