'RDF mapping hook', 'description' => 'Test hook_rdf_mapping().', 'group' => 'RDF', ); } function setUp() { parent::setUp('rdf', 'rdf_test', 'field_test'); // We need to trigger rdf_modules_installed() because // hook_modules_installed() is not automatically invoked during testing. rdf_modules_installed(array('rdf_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'); rdf_modules_installed(array('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/$nid')]//div[contains(@rel, 'rdfs:seeAlso') and contains(@resource, '.txt')]"); $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/$nid')]//div[contains(@rel, 'rdfs:seeAlso') and contains(@resource, '$image_filename')]//img[contains(@typeof, 'foaf:Image')]"); $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() { $test_mapping = rdf_test_rdf_mapping(); // Verify loading of a default mapping. $this->assertFalse(count(_rdf_mapping_load('test_entity', 'test_bundle')), t('Default mapping was found.')); // Verify saving a mapping. $mapping = (array) $test_mapping; rdf_mapping_save($mapping[0]); $this->assertEqual($mapping[0]['mapping'], $test_mapping[0]['mapping'], t('Saved mapping equals default mapping.')); $this->assertTrue(rdf_mapping_save($mapping[1]) === SAVED_NEW, t('Second mapping was inserted.')); $this->assertEqual($mapping[1]['mapping'], _rdf_mapping_load($test_mapping[1]['type'], $test_mapping[1]['bundle']), t('Second mapping equals default mapping.')); // Verify loading of saved mapping. $this->assertEqual($mapping[0]['mapping'], _rdf_mapping_load($test_mapping[0]['type'], $test_mapping[0]['bundle']), t('Saved mapping equals loaded default mapping.')); // Verify updating of mapping. $mapping[0]['mapping']['boofar'] = array( 'predicates' => array('foo:bar'), ); $this->assertTrue(rdf_mapping_save($mapping[0]) === SAVED_UPDATED, t('Mapping was updated.')); $this->assertEqual($mapping[0]['mapping'], _rdf_mapping_load($test_mapping[0]['type'], $test_mapping[0]['bundle']), t('Updated and loaded mapping are equal.')); // Verify deleting of mapping. $this->assertTrue(rdf_mapping_delete($test_mapping[0]['type'], $test_mapping[0]['bundle']), t('Mapping was deleted.')); $this->assertFalse(_rdf_mapping_load($test_mapping[0]['type'], $test_mapping[0]['bundle']), t('Deleted mapping is no longer found.')); } } 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'); // 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. entity_info_cache_clear(); } /** * 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(); $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. $profile_url = url('user/' . $user2->uid); $user2_profile_about = $this->xpath("//div[@class='profile' and @typeof='sioc:User' and @about='$profile_url']"); $this->assertTrue(!empty($user2_profile_about), t('RDFa markup found on user profile page')); // 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='$profile_url' and @property='foaf:name' and contains(@xml:lang, '')]"); $this->assertTrue(!empty($author_about), t('RDFa markup found on author information on post. xml:lang on username is set to empty string.')); } } 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') and @datatype='xsd:integer']"); $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') and @datatype='xsd:integer']"); $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))); } }