rdf.test 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. /**
  3. * @file
  4. * Tests for rdf.module.
  5. */
  6. class RdfMappingHookTestCase extends DrupalWebTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'RDF mapping hook',
  10. 'description' => 'Test hook_rdf_mapping().',
  11. 'group' => 'RDF',
  12. );
  13. }
  14. function setUp() {
  15. parent::setUp('rdf', 'rdf_test', 'field_test');
  16. }
  17. /**
  18. * Test that hook_rdf_mapping() correctly returns and processes mapping.
  19. */
  20. function testMapping() {
  21. // Test that the mapping is returned correctly by the hook.
  22. $mapping = rdf_mapping_load('test_entity', 'test_bundle');
  23. $this->assertIdentical($mapping['rdftype'], array('sioc:Post'), 'Mapping for rdftype is sioc:Post.');
  24. $this->assertIdentical($mapping['title'], array('predicates' => array('dc:title')), 'Mapping for title is dc:title.');
  25. $this->assertIdentical($mapping['created'], array(
  26. 'predicates' => array('dc:created'),
  27. 'datatype' => 'xsd:dateTime',
  28. 'callback' => 'date_iso8601',
  29. ), t('Mapping for created is dc:created with datatype xsd:dateTime and callback date_iso8601.'));
  30. $this->assertIdentical($mapping['uid'], array('predicates' => array('sioc:has_creator', 'dc:creator'), 'type' => 'rel'), 'Mapping for uid is sioc:has_creator and dc:creator, and type is rel.');
  31. $mapping = rdf_mapping_load('test_entity', 'test_bundle_no_mapping');
  32. $this->assertEqual($mapping, array(), 'Empty array returned when an entity type, bundle pair has no mapping.');
  33. }
  34. }
  35. /**
  36. * Test RDFa markup generation.
  37. */
  38. class RdfRdfaMarkupTestCase extends DrupalWebTestCase {
  39. public static function getInfo() {
  40. return array(
  41. 'name' => 'RDFa markup',
  42. 'description' => 'Test RDFa markup generation.',
  43. 'group' => 'RDF',
  44. );
  45. }
  46. function setUp() {
  47. parent::setUp('rdf', 'field_test', 'rdf_test');
  48. }
  49. /**
  50. * Test rdf_rdfa_attributes().
  51. */
  52. function testDrupalRdfaAttributes() {
  53. // Same value as the one in the HTML tag (no callback function).
  54. $expected_attributes = array(
  55. 'property' => array('dc:title'),
  56. );
  57. $mapping = rdf_mapping_load('test_entity', 'test_bundle');
  58. $attributes = rdf_rdfa_attributes($mapping['title']);
  59. ksort($expected_attributes);
  60. ksort($attributes);
  61. $this->assertEqual($expected_attributes, $attributes);
  62. // Value different from the one in the HTML tag (callback function).
  63. $date = 1252750327;
  64. $isoDate = date('c', $date);
  65. $expected_attributes = array(
  66. 'datatype' => 'xsd:dateTime',
  67. 'property' => array('dc:created'),
  68. 'content' => $isoDate,
  69. );
  70. $mapping = rdf_mapping_load('test_entity', 'test_bundle');
  71. $attributes = rdf_rdfa_attributes($mapping['created'], $date);
  72. ksort($expected_attributes);
  73. ksort($attributes);
  74. $this->assertEqual($expected_attributes, $attributes);
  75. // Same value as the one in the HTML tag with datatype.
  76. $expected_attributes = array(
  77. 'datatype' => 'foo:bar1type',
  78. 'property' => array('foo:bar1'),
  79. );
  80. $mapping = rdf_mapping_load('test_entity', 'test_bundle');
  81. $attributes = rdf_rdfa_attributes($mapping['foobar1']);
  82. ksort($expected_attributes);
  83. ksort($attributes);
  84. $this->assertEqual($expected_attributes, $attributes);
  85. // ObjectProperty mapping (rel).
  86. $expected_attributes = array(
  87. 'rel' => array('sioc:has_creator', 'dc:creator'),
  88. );
  89. $mapping = rdf_mapping_load('test_entity', 'test_bundle');
  90. $attributes = rdf_rdfa_attributes($mapping['foobar_objproperty1']);
  91. ksort($expected_attributes);
  92. ksort($attributes);
  93. $this->assertEqual($expected_attributes, $attributes);
  94. // Inverse ObjectProperty mapping (rev).
  95. $expected_attributes = array(
  96. 'rev' => array('sioc:reply_of'),
  97. );
  98. $mapping = rdf_mapping_load('test_entity', 'test_bundle');
  99. $attributes = rdf_rdfa_attributes($mapping['foobar_objproperty2']);
  100. ksort($expected_attributes);
  101. ksort($attributes);
  102. $this->assertEqual($expected_attributes, $attributes);
  103. }
  104. /**
  105. * Ensure that file fields have the correct resource as the object in RDFa
  106. * when displayed as a teaser.
  107. */
  108. function testAttributesInMarkupFile() {
  109. // Create a user to post the image.
  110. $admin_user = $this->drupalCreateUser(array('edit own article content', 'revert revisions', 'administer content types'));
  111. $this->drupalLogin($admin_user);
  112. $langcode = LANGUAGE_NONE;
  113. $bundle_name = "article";
  114. $field_name = 'file_test';
  115. $field = array(
  116. 'field_name' => $field_name,
  117. 'type' => 'file',
  118. );
  119. field_create_field($field);
  120. $instance = array(
  121. 'field_name' => $field_name,
  122. 'entity_type' => 'node',
  123. 'bundle' => $bundle_name,
  124. 'display' => array(
  125. 'teaser' => array(
  126. 'type' => 'file_default',
  127. ),
  128. ),
  129. );
  130. field_create_instance($instance);
  131. // Set the RDF mapping for the new field.
  132. $rdf_mapping = rdf_mapping_load('node', $bundle_name);
  133. $rdf_mapping += array($field_name => array('predicates' => array('rdfs:seeAlso'), 'type' => 'rel'));
  134. $rdf_mapping_save = array('mapping' => $rdf_mapping, 'type' => 'node', 'bundle' => $bundle_name);
  135. rdf_mapping_save($rdf_mapping_save);
  136. // Get the test file that simpletest provides.
  137. $file = current($this->drupalGetTestFiles('text'));
  138. // Prepare image variables.
  139. $image_field = "field_image";
  140. // Get the test image that simpletest provides.
  141. $image = current($this->drupalGetTestFiles('image'));
  142. // Create an array for drupalPost with the field names as the keys and
  143. // the URIs for the test files as the values.
  144. $edit = array("files[" . $field_name . "_" . $langcode . "_0]" => drupal_realpath($file->uri),
  145. "files[" . $image_field . "_" . $langcode . "_0]" => drupal_realpath($image->uri));
  146. // Create node and save, then edit node to upload files.
  147. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  148. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  149. // Get filenames and nid for comparison with HTML output.
  150. $file_filename = $file->filename;
  151. $image_filename = $image->filename;
  152. $nid = $node->nid;
  153. // Navigate to front page, where node is displayed in teaser form.
  154. $this->drupalGet('node');
  155. // We only check to make sure that the resource attribute contains '.txt'
  156. // instead of the full file name because the filename is altered on upload.
  157. $file_rel = $this->xpath('//div[contains(@about, :node-uri)]//div[contains(@rel, "rdfs:seeAlso") and contains(@resource, ".txt")]', array(
  158. ':node-uri' => 'node/' . $nid,
  159. ));
  160. $this->assertTrue(!empty($file_rel), "Attribute 'rel' set on file field. Attribute 'resource' is also set.");
  161. $image_rel = $this->xpath('//div[contains(@about, :node-uri)]//div[contains(@rel, "rdfs:seeAlso") and contains(@resource, :image)]//img[contains(@typeof, "foaf:Image")]', array(
  162. ':node-uri' => 'node/' . $nid,
  163. ':image' => $image_filename,
  164. ));
  165. $this->assertTrue(!empty($image_rel), "Attribute 'rel' set on image field. Attribute 'resource' is also set.");
  166. // Edits the node to add tags.
  167. $tag1 = $this->randomName(8);
  168. $tag2 = $this->randomName(8);
  169. $edit = array();
  170. $edit['field_tags[' . LANGUAGE_NONE . ']'] = "$tag1, $tag2";
  171. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  172. // Ensures the RDFa markup for the relationship between the node and its
  173. // tags is correct.
  174. $term_rdfa_meta = $this->xpath('//div[@about=:node-url and contains(@typeof, "sioc:Item") and contains(@typeof, "foaf:Document")]//ul[@class="links"]/li[@rel="dc:subject"]/a[@typeof="skos:Concept" and @datatype="" and text()=:term-name]', array(
  175. ':node-url' => url('node/' . $node->nid),
  176. ':term-name' => $tag1,
  177. ));
  178. $this->assertTrue(!empty($term_rdfa_meta), 'Property dc:subject is present for the tag1 field item.');
  179. $term_rdfa_meta = $this->xpath('//div[@about=:node-url and contains(@typeof, "sioc:Item") and contains(@typeof, "foaf:Document")]//ul[@class="links"]/li[@rel="dc:subject"]/a[@typeof="skos:Concept" and @datatype="" and text()=:term-name]', array(
  180. ':node-url' => url('node/' . $node->nid),
  181. ':term-name' => $tag2,
  182. ));
  183. $this->assertTrue(!empty($term_rdfa_meta), 'Property dc:subject is present for the tag2 field item.');
  184. }
  185. }
  186. class RdfCrudTestCase extends DrupalWebTestCase {
  187. public static function getInfo() {
  188. return array(
  189. 'name' => 'RDF mapping CRUD functions',
  190. 'description' => 'Test the RDF mapping CRUD functions.',
  191. 'group' => 'RDF',
  192. );
  193. }
  194. function setUp() {
  195. parent::setUp('rdf', 'rdf_test');
  196. }
  197. /**
  198. * Test inserting, loading, updating, and deleting RDF mappings.
  199. */
  200. function testCRUD() {
  201. // Verify loading of a default mapping.
  202. $mapping = _rdf_mapping_load('test_entity', 'test_bundle');
  203. $this->assertTrue(count($mapping), 'Default mapping was found.');
  204. // Verify saving a mapping.
  205. $mapping = array(
  206. 'type' => 'crud_test_entity',
  207. 'bundle' => 'crud_test_bundle',
  208. 'mapping' => array(
  209. 'rdftype' => array('sioc:Post'),
  210. 'title' => array(
  211. 'predicates' => array('dc:title'),
  212. ),
  213. 'uid' => array(
  214. 'predicates' => array('sioc:has_creator', 'dc:creator'),
  215. 'type' => 'rel',
  216. ),
  217. ),
  218. );
  219. $this->assertTrue(rdf_mapping_save($mapping) === SAVED_NEW, 'Mapping was saved.');
  220. // Read the raw record from the {rdf_mapping} table.
  221. $result = db_query('SELECT * FROM {rdf_mapping} WHERE type = :type AND bundle = :bundle', array(':type' => $mapping['type'], ':bundle' => $mapping['bundle']));
  222. $stored_mapping = $result->fetchAssoc();
  223. $stored_mapping['mapping'] = unserialize($stored_mapping['mapping']);
  224. $this->assertEqual($mapping, $stored_mapping, 'Mapping was stored properly in the {rdf_mapping} table.');
  225. // Verify loading of saved mapping.
  226. $this->assertEqual($mapping['mapping'], _rdf_mapping_load($mapping['type'], $mapping['bundle']), 'Saved mapping loaded successfully.');
  227. // Verify updating of mapping.
  228. $mapping['mapping']['title'] = array(
  229. 'predicates' => array('dc2:bar2'),
  230. );
  231. $this->assertTrue(rdf_mapping_save($mapping) === SAVED_UPDATED, 'Mapping was updated.');
  232. // Read the raw record from the {rdf_mapping} table.
  233. $result = db_query('SELECT * FROM {rdf_mapping} WHERE type = :type AND bundle = :bundle', array(':type' => $mapping['type'], ':bundle' => $mapping['bundle']));
  234. $stored_mapping = $result->fetchAssoc();
  235. $stored_mapping['mapping'] = unserialize($stored_mapping['mapping']);
  236. $this->assertEqual($mapping, $stored_mapping, 'Updated mapping was stored properly in the {rdf_mapping} table.');
  237. // Verify loading of saved mapping.
  238. $this->assertEqual($mapping['mapping'], _rdf_mapping_load($mapping['type'], $mapping['bundle']), 'Saved mapping loaded successfully.');
  239. // Verify deleting of mapping.
  240. $this->assertTrue(rdf_mapping_delete($mapping['type'], $mapping['bundle']), 'Mapping was deleted.');
  241. $this->assertFalse(_rdf_mapping_load($mapping['type'], $mapping['bundle']), 'Deleted mapping is no longer found in the database.');
  242. }
  243. }
  244. class RdfMappingDefinitionTestCase extends TaxonomyWebTestCase {
  245. public static function getInfo() {
  246. return array(
  247. 'name' => 'RDF mapping definition functionality',
  248. 'description' => 'Test the different types of RDF mappings and ensure the proper RDFa markup in included in nodes and user profile pages.',
  249. 'group' => 'RDF',
  250. );
  251. }
  252. function setUp() {
  253. parent::setUp('rdf', 'rdf_test', 'blog');
  254. }
  255. /**
  256. * Create a node of type blog and test whether the RDF mapping defined for
  257. * this node type in rdf_test.module is used in the node page.
  258. */
  259. function testAttributesInMarkup1() {
  260. $node = $this->drupalCreateNode(array('type' => 'blog'));
  261. $isoDate = date('c', $node->changed);
  262. $url = url('node/' . $node->nid);
  263. $this->drupalGet('node/' . $node->nid);
  264. // Ensure the default bundle mapping for node is used. These attributes come
  265. // from the node default bundle definition.
  266. $blog_title = $this->xpath("//meta[@property='dc:title' and @content='$node->title']");
  267. $blog_meta = $this->xpath("//div[(@about='$url') and (@typeof='sioct:Weblog')]//span[contains(@property, 'dc:date') and contains(@property, 'dc:created') and @datatype='xsd:dateTime' and @content='$isoDate']");
  268. $this->assertTrue(!empty($blog_title), 'Property dc:title is present in meta tag.');
  269. $this->assertTrue(!empty($blog_meta), 'RDF type is present on post. Properties dc:date and dc:created are present on post date.');
  270. }
  271. /**
  272. * Create a content type and a node of type test_bundle_hook_install and test
  273. * whether the RDF mapping defined in rdf_test.install is used.
  274. */
  275. function testAttributesInMarkup2() {
  276. $type = $this->drupalCreateContentType(array('type' => 'test_bundle_hook_install'));
  277. // Create node with single quotation mark title to ensure it does not get
  278. // escaped more than once.
  279. $node = $this->drupalCreateNode(array(
  280. 'type' => 'test_bundle_hook_install',
  281. 'title' => $this->randomName(8) . "'",
  282. ));
  283. $isoDate = date('c', $node->changed);
  284. $url = url('node/' . $node->nid);
  285. $this->drupalGet('node/' . $node->nid);
  286. // Ensure the mapping defined in rdf_module.test is used.
  287. $test_bundle_title = $this->xpath('//meta[@property="dc:title" and @content="' . $node->title . '"]');
  288. $test_bundle_meta = $this->xpath("//div[(@about='$url') and contains(@typeof, 'foo:mapping_install1') and contains(@typeof, 'bar:mapping_install2')]//span[contains(@property, 'dc:date') and contains(@property, 'dc:created') and @datatype='xsd:dateTime' and @content='$isoDate']");
  289. $this->assertTrue(!empty($test_bundle_title), 'Property dc:title is present in meta tag.');
  290. $this->assertTrue(!empty($test_bundle_meta), 'RDF type is present on post. Properties dc:date and dc:created are present on post date.');
  291. }
  292. /**
  293. * Create a random content type and node and ensure the default mapping for
  294. * node is used.
  295. */
  296. function testAttributesInMarkup3() {
  297. $type = $this->drupalCreateContentType();
  298. $node = $this->drupalCreateNode(array('type' => $type->type));
  299. $isoDate = date('c', $node->changed);
  300. $url = url('node/' . $node->nid);
  301. $this->drupalGet('node/' . $node->nid);
  302. // Ensure the default bundle mapping for node is used. These attributes come
  303. // from the node default bundle definition.
  304. $random_bundle_title = $this->xpath("//meta[@property='dc:title' and @content='$node->title']");
  305. $random_bundle_meta = $this->xpath("//div[(@about='$url') and contains(@typeof, 'sioc:Item') and contains(@typeof, 'foaf:Document')]//span[contains(@property, 'dc:date') and contains(@property, 'dc:created') and @datatype='xsd:dateTime' and @content='$isoDate']");
  306. $this->assertTrue(!empty($random_bundle_title), 'Property dc:title is present in meta tag.');
  307. $this->assertTrue(!empty($random_bundle_meta), 'RDF type is present on post. Properties dc:date and dc:created are present on post date.');
  308. }
  309. /**
  310. * Create a random user and ensure the default mapping for user is used.
  311. */
  312. function testUserAttributesInMarkup() {
  313. // Create two users, one with access to user profiles.
  314. $user1 = $this->drupalCreateUser(array('access user profiles'));
  315. $user2 = $this->drupalCreateUser();
  316. $username = $user2->name;
  317. $this->drupalLogin($user1);
  318. // Browse to the user profile page.
  319. $this->drupalGet('user/' . $user2->uid);
  320. // Ensure the default bundle mapping for user is used on the user profile
  321. // page. These attributes come from the user default bundle definition.
  322. $account_uri = url('user/' . $user2->uid);
  323. $person_uri = url('user/' . $user2->uid, array('fragment' => 'me'));
  324. $user2_profile_about = $this->xpath('//div[@class="profile" and @typeof="sioc:UserAccount" and @about=:account-uri]', array(
  325. ':account-uri' => $account_uri,
  326. ));
  327. $this->assertTrue(!empty($user2_profile_about), 'RDFa markup found on user profile page');
  328. $user_account_holder = $this->xpath('//meta[contains(@typeof, "foaf:Person") and @about=:person-uri and @resource=:account-uri and contains(@rel, "foaf:account")]', array(
  329. ':person-uri' => $person_uri,
  330. ':account-uri' => $account_uri,
  331. ));
  332. $this->assertTrue(!empty($user_account_holder), 'URI created for account holder and username set on sioc:UserAccount.');
  333. $user_username = $this->xpath('//meta[@about=:account-uri and contains(@property, "foaf:name") and @content=:username]', array(
  334. ':account-uri' => $account_uri,
  335. ':username' => $username,
  336. ));
  337. $this->assertTrue(!empty($user_username), 'foaf:name set on username.');
  338. // User 2 creates node.
  339. $this->drupalLogin($user2);
  340. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  341. $this->drupalLogin($user1);
  342. $this->drupalGet('node/' . $node->nid);
  343. // Ensures the default bundle mapping for user is used on the Authored By
  344. // information on the node.
  345. $author_about = $this->xpath('//a[@typeof="sioc:UserAccount" and @about=:account-uri and @property="foaf:name" and @datatype="" and contains(@xml:lang, "")]', array(
  346. ':account-uri' => $account_uri,
  347. ));
  348. $this->assertTrue(!empty($author_about), 'RDFa markup found on author information on post. xml:lang on username is set to empty string.');
  349. }
  350. /**
  351. * Creates a random term and ensures the right RDFa markup is used.
  352. */
  353. function testTaxonomyTermRdfaAttributes() {
  354. $vocabulary = $this->createVocabulary();
  355. $term = $this->createTerm($vocabulary);
  356. // Views the term and checks that the RDFa markup is correct.
  357. $this->drupalGet('taxonomy/term/' . $term->tid);
  358. $term_url = url('taxonomy/term/' . $term->tid);
  359. $term_name = $term->name;
  360. $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(
  361. ':term-url' => $term_url,
  362. ':term-name' => $term_name,
  363. ));
  364. $this->assertTrue(!empty($term_rdfa_meta), 'RDFa markup found on term page.');
  365. }
  366. }
  367. class RdfCommentAttributesTestCase extends CommentHelperCase {
  368. public static function getInfo() {
  369. return array(
  370. 'name' => 'RDF comment mapping',
  371. 'description' => 'Tests the RDFa markup of comments.',
  372. 'group' => 'RDF',
  373. );
  374. }
  375. public function setUp() {
  376. parent::setUp('comment', 'rdf', 'rdf_test');
  377. $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer permissions', 'administer blocks'));
  378. $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'access user profiles'));
  379. // Enables anonymous user comments.
  380. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  381. 'access comments' => TRUE,
  382. 'post comments' => TRUE,
  383. 'skip comment approval' => TRUE,
  384. ));
  385. // Allows anonymous to leave their contact information.
  386. $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
  387. $this->setCommentPreview(DRUPAL_OPTIONAL);
  388. $this->setCommentForm(TRUE);
  389. $this->setCommentSubject(TRUE);
  390. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
  391. // Creates the nodes on which the test comments will be posted.
  392. $this->drupalLogin($this->web_user);
  393. $this->node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  394. $this->node2 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  395. $this->drupalLogout();
  396. }
  397. /**
  398. * Tests the presence of the RDFa markup for the number of comments.
  399. */
  400. public function testNumberOfCommentsRdfaMarkup() {
  401. // Posts 2 comments as a registered user.
  402. $this->drupalLogin($this->web_user);
  403. $this->postComment($this->node1, $this->randomName(), $this->randomName());
  404. $this->postComment($this->node1, $this->randomName(), $this->randomName());
  405. // Tests number of comments in teaser view.
  406. $this->drupalGet('node');
  407. $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"]');
  408. $this->assertTrue(!empty($comment_count_teaser), 'RDFa markup for the number of comments found on teaser view.');
  409. $comment_count_link = $this->xpath('//div[@about=:url]//a[contains(@property, "sioc:num_replies") and @rel=""]', array(':url' => url("node/{$this->node1->nid}")));
  410. $this->assertTrue(!empty($comment_count_link), 'Empty rel attribute found in comment count link.');
  411. // Tests number of comments in full node view.
  412. $this->drupalGet('node/' . $this->node1->nid);
  413. $node_url = url('node/' . $this->node1->nid);
  414. $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));
  415. $this->assertTrue(!empty($comment_count_teaser), 'RDFa markup for the number of comments found on full node view.');
  416. }
  417. /**
  418. * Tests the presence of the RDFa markup for the title, date and author and
  419. * homepage on registered users and anonymous comments.
  420. */
  421. public function testCommentRdfaMarkup() {
  422. // Posts comment #1 as a registered user.
  423. $this->drupalLogin($this->web_user);
  424. $comment1_subject = $this->randomName();
  425. $comment1_body = $this->randomName();
  426. $comment1 = $this->postComment($this->node1, $comment1_body, $comment1_subject);
  427. // Tests comment #1 with access to the user profile.
  428. $this->drupalGet('node/' . $this->node1->nid);
  429. $this->_testBasicCommentRdfaMarkup($comment1);
  430. // Tests comment #1 with no access to the user profile (as anonymous user).
  431. $this->drupalLogout();
  432. $this->drupalGet('node/' . $this->node1->nid);
  433. $this->_testBasicCommentRdfaMarkup($comment1);
  434. // Posts comment #2 as anonymous user.
  435. $comment2_subject = $this->randomName();
  436. $comment2_body = $this->randomName();
  437. $anonymous_user = array();
  438. $anonymous_user['name'] = $this->randomName();
  439. $anonymous_user['mail'] = 'tester@simpletest.org';
  440. $anonymous_user['homepage'] = 'http://example.org/';
  441. $comment2 = $this->postComment($this->node2, $comment2_body, $comment2_subject, $anonymous_user);
  442. $this->drupalGet('node/' . $this->node2->nid);
  443. // Tests comment #2 as anonymous user.
  444. $this->_testBasicCommentRdfaMarkup($comment2, $anonymous_user);
  445. // Tests the RDFa markup for the homepage (specific to anonymous comments).
  446. $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")]');
  447. $this->assertTrue(!empty($comment_homepage), 'RDFa markup for the homepage of anonymous user found.');
  448. // There should be no about attribute on anonymous comments.
  449. $comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[@about]');
  450. $this->assertTrue(empty($comment_homepage), 'No about attribute is present on anonymous user comment.');
  451. // Tests comment #2 as logged in user.
  452. $this->drupalLogin($this->web_user);
  453. $this->drupalGet('node/' . $this->node2->nid);
  454. $this->_testBasicCommentRdfaMarkup($comment2, $anonymous_user);
  455. // Tests the RDFa markup for the homepage (specific to anonymous comments).
  456. $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")]');
  457. $this->assertTrue(!empty($comment_homepage), "RDFa markup for the homepage of anonymous user found.");
  458. // There should be no about attribute on anonymous comments.
  459. $comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[@about]');
  460. $this->assertTrue(empty($comment_homepage), "No about attribute is present on anonymous user comment.");
  461. }
  462. /**
  463. * Test RDF comment replies.
  464. */
  465. public function testCommentReplyOfRdfaMarkup() {
  466. // Posts comment #1 as a registered user.
  467. $this->drupalLogin($this->web_user);
  468. $comments[] = $this->postComment($this->node1, $this->randomName(), $this->randomName());
  469. // Tests the reply_of relationship of a first level comment.
  470. $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}")));
  471. $this->assertEqual(1, count($result), 'RDFa markup referring to the node is present.');
  472. $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')));
  473. $this->assertFalse($result, 'No RDFa markup referring to the comment itself is present.');
  474. // Posts a reply to the first comment.
  475. $this->drupalGet('comment/reply/' . $this->node1->nid . '/' . $comments[0]->id);
  476. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  477. // Tests the reply_of relationship of a second level comment.
  478. $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}")));
  479. $this->assertEqual(1, count($result), 'RDFa markup referring to the node is present.');
  480. $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'))));
  481. $this->assertEqual(1, count($result), 'RDFa markup referring to the parent comment is present.');
  482. $comments = $this->xpath("(id('comments')//div[contains(@class,'comment ')])[position()=2]");
  483. }
  484. /**
  485. * Helper function for testCommentRdfaMarkup().
  486. *
  487. * Tests the current page for basic comment RDFa markup.
  488. *
  489. * @param $comment
  490. * Comment object.
  491. * @param $account
  492. * An array containing information about an anonymous user.
  493. */
  494. function _testBasicCommentRdfaMarkup($comment, $account = array()) {
  495. $comment_container = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]');
  496. $this->assertTrue(!empty($comment_container), "Comment RDF type for comment found.");
  497. $comment_title = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//h3[@property="dc:title"]');
  498. $this->assertEqual((string)$comment_title[0]->a, $comment->subject, "RDFa markup for the comment title found.");
  499. $comment_date = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//*[contains(@property, "dc:date") and contains(@property, "dc:created")]');
  500. $this->assertTrue(!empty($comment_date), "RDFa markup for the date of the comment found.");
  501. // The author tag can be either a or span
  502. $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=""]');
  503. $name = empty($account["name"]) ? $this->web_user->name : $account["name"] . " (not verified)";
  504. $this->assertEqual((string)$comment_author[0], $name, "RDFa markup for the comment author found.");
  505. $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"]');
  506. $this->assertEqual((string)$comment_body[0]->p, $comment->comment, "RDFa markup for the comment body found.");
  507. }
  508. }
  509. class RdfTrackerAttributesTestCase extends DrupalWebTestCase {
  510. public static function getInfo() {
  511. return array(
  512. 'name' => 'RDF tracker page mapping',
  513. 'description' => 'Test the mapping for the tracker page and ensure the proper RDFa markup in included.',
  514. 'group' => 'RDF',
  515. );
  516. }
  517. function setUp() {
  518. parent::setUp('rdf', 'rdf_test', 'tracker');
  519. // Enable anonymous posting of content.
  520. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  521. 'create article content' => TRUE,
  522. 'access comments' => TRUE,
  523. 'post comments' => TRUE,
  524. 'skip comment approval' => TRUE,
  525. ));
  526. }
  527. /**
  528. * Create nodes as both admin and anonymous user and test for correct RDFa
  529. * markup on the tracker page for those nodes and their comments.
  530. */
  531. function testAttributesInTracker() {
  532. // Create node as anonymous user.
  533. $node_anon = $this->drupalCreateNode(array('type' => 'article', 'uid' => 0));
  534. // Create node as admin user.
  535. $node_admin = $this->drupalCreateNode(array('type' => 'article', 'uid' => 1));
  536. // Pass both the anonymously posted node and the administrator posted node
  537. // through to test for the RDF attributes.
  538. $this->_testBasicTrackerRdfaMarkup($node_anon);
  539. $this->_testBasicTrackerRdfaMarkup($node_admin);
  540. }
  541. /**
  542. * Helper function for testAttributesInTracker().
  543. *
  544. * Tests the tracker page for RDFa markup.
  545. *
  546. * @param $node
  547. * The node just created.
  548. */
  549. function _testBasicTrackerRdfaMarkup($node) {
  550. $url = url('node/' . $node->nid);
  551. $user = ($node->uid == 0) ? 'Anonymous user' : 'Registered user';
  552. // Navigate to tracker page.
  553. $this->drupalGet('tracker');
  554. // Tests whether the about property is applied. This is implicit in the
  555. // success of the following tests, but making it explicit will make
  556. // debugging easier in case of failure.
  557. $tracker_about = $this->xpath('//tr[@about=:url]', array(':url' => $url));
  558. $this->assertTrue(!empty($tracker_about), format_string('About attribute found on table row for @user content.', array('@user'=> $user)));
  559. // Tests whether the title has the correct property attribute.
  560. $tracker_title = $this->xpath('//tr[@about=:url]/td[@property="dc:title" and @datatype=""]', array(':url' => $url));
  561. $this->assertTrue(!empty($tracker_title), format_string('Title property attribute found on @user content.', array('@user'=> $user)));
  562. // Tests whether the relationship between the content and user has been set.
  563. $tracker_user = $this->xpath('//tr[@about=:url]//td[contains(@rel, "sioc:has_creator")]//*[contains(@typeof, "sioc:UserAccount") and contains(@property, "foaf:name")]', array(':url' => $url));
  564. $this->assertTrue(!empty($tracker_user), format_string('Typeof and name property attributes found on @user.', array('@user'=> $user)));
  565. // There should be an about attribute on logged in users and no about
  566. // attribute for anonymous users.
  567. $tracker_user = $this->xpath('//tr[@about=:url]//td[@rel="sioc:has_creator"]/*[@about]', array(':url' => $url));
  568. if ($node->uid == 0) {
  569. $this->assertTrue(empty($tracker_user), format_string('No about attribute is present on @user.', array('@user'=> $user)));
  570. }
  571. elseif ($node->uid > 0) {
  572. $this->assertTrue(!empty($tracker_user), format_string('About attribute is present on @user.', array('@user'=> $user)));
  573. }
  574. // Tests whether the property has been set for number of comments.
  575. $tracker_replies = $this->xpath('//tr[@about=:url]//td[contains(@property, "sioc:num_replies") and contains(@content, "0") and @datatype="xsd:integer"]', array(':url' => $url));
  576. $this->assertTrue($tracker_replies, format_string('Num replies property and content attributes found on @user content.', array('@user'=> $user)));
  577. // Tests that the appropriate RDFa markup to annotate the latest activity
  578. // date has been added to the tracker output before comments have been
  579. // posted, meaning the latest activity reflects changes to the node itself.
  580. $isoDate = date('c', $node->changed);
  581. $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));
  582. $this->assertTrue(!empty($tracker_activity), format_string('Latest activity date and changed properties found when there are no comments on @user content. Latest activity date content is correct.', array('@user'=> $user)));
  583. // Tests that the appropriate RDFa markup to annotate the latest activity
  584. // date has been added to the tracker output after a comment is posted.
  585. $comment = array(
  586. 'subject' => $this->randomName(),
  587. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
  588. );
  589. $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
  590. $this->drupalGet('tracker');
  591. // Tests whether the property has been set for number of comments.
  592. $tracker_replies = $this->xpath('//tr[@about=:url]//td[contains(@property, "sioc:num_replies") and contains(@content, "1") and @datatype="xsd:integer"]', array(':url' => $url));
  593. $this->assertTrue($tracker_replies, format_string('Num replies property and content attributes found on @user content.', array('@user'=> $user)));
  594. // Need to query database directly to obtain last_activity_date because
  595. // it cannot be accessed via node_load().
  596. $result = db_query('SELECT t.changed FROM {tracker_node} t WHERE t.nid = (:nid)', array(':nid' => $node->nid));
  597. foreach ($result as $node) {
  598. $expected_last_activity_date = $node->changed;
  599. }
  600. $isoDate = date('c', $expected_last_activity_date);
  601. $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));
  602. $this->assertTrue(!empty($tracker_activity), format_string('Latest activity date found when there are comments on @user content. Latest activity date content is correct.', array('@user'=> $user)));
  603. }
  604. }
  605. /**
  606. * Tests for RDF namespaces declaration with hook_rdf_namespaces().
  607. */
  608. class RdfGetRdfNamespacesTestCase extends DrupalWebTestCase {
  609. public static function getInfo() {
  610. return array(
  611. 'name' => 'RDF namespaces',
  612. 'description' => 'Test hook_rdf_namespaces() and ensure only "safe" namespaces are returned.',
  613. 'group' => 'RDF',
  614. );
  615. }
  616. function setUp() {
  617. parent::setUp('rdf', 'rdf_test');
  618. }
  619. /**
  620. * Test getting RDF namesapces.
  621. */
  622. function testGetRdfNamespaces() {
  623. // Get all RDF namespaces.
  624. $ns = rdf_get_namespaces();
  625. $this->assertEqual($ns['rdfs'], 'http://www.w3.org/2000/01/rdf-schema#', 'A prefix declared once is included.');
  626. $this->assertEqual($ns['foaf'], 'http://xmlns.com/foaf/0.1/', 'The same prefix declared in several implementations of hook_rdf_namespaces() is valid as long as all the namespaces are the same.');
  627. $this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', 'Two prefixes can be assigned the same namespace.');
  628. $this->assertTrue(!isset($ns['dc']), 'A prefix with conflicting namespaces is discarded.');
  629. }
  630. }