rdf.test 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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'), t('Mapping for rdftype is sioc:Post.'));
  24. $this->assertIdentical($mapping['title'], array('predicates' => array('dc:title')), t('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'), t('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(), t('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), t('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), t('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 text()=:term-name]', array(
  175. ':node-url' => url('node/' . $node->nid),
  176. ':term-name' => $tag1,
  177. ));
  178. $this->assertTrue(!empty($term_rdfa_meta), t('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 text()=:term-name]', array(
  180. ':node-url' => url('node/' . $node->nid),
  181. ':term-name' => $tag2,
  182. ));
  183. $this->assertTrue(!empty($term_rdfa_meta), t('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), t('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, t('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, t('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']), t('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, t('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, t('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']), t('Saved mapping loaded successfully.'));
  239. // Verify deleting of mapping.
  240. $this->assertTrue(rdf_mapping_delete($mapping['type'], $mapping['bundle']), t('Mapping was deleted.'));
  241. $this->assertFalse(_rdf_mapping_load($mapping['type'], $mapping['bundle']), t('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), t('Property dc:title is present in meta tag.'));
  269. $this->assertTrue(!empty($blog_meta), t('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. $node = $this->drupalCreateNode(array('type' => 'test_bundle_hook_install'));
  278. $isoDate = date('c', $node->changed);
  279. $url = url('node/' . $node->nid);
  280. $this->drupalGet('node/' . $node->nid);
  281. // Ensure the mapping defined in rdf_module.test is used.
  282. $test_bundle_title = $this->xpath("//meta[@property='dc:title' and @content='$node->title']");
  283. $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']");
  284. $this->assertTrue(!empty($test_bundle_title), t('Property dc:title is present in meta tag.'));
  285. $this->assertTrue(!empty($test_bundle_meta), t('RDF type is present on post. Properties dc:date and dc:created are present on post date.'));
  286. }
  287. /**
  288. * Create a random content type and node and ensure the default mapping for
  289. * node is used.
  290. */
  291. function testAttributesInMarkup3() {
  292. $type = $this->drupalCreateContentType();
  293. $node = $this->drupalCreateNode(array('type' => $type->type));
  294. $isoDate = date('c', $node->changed);
  295. $url = url('node/' . $node->nid);
  296. $this->drupalGet('node/' . $node->nid);
  297. // Ensure the default bundle mapping for node is used. These attributes come
  298. // from the node default bundle definition.
  299. $random_bundle_title = $this->xpath("//meta[@property='dc:title' and @content='$node->title']");
  300. $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']");
  301. $this->assertTrue(!empty($random_bundle_title), t('Property dc:title is present in meta tag.'));
  302. $this->assertTrue(!empty($random_bundle_meta), t('RDF type is present on post. Properties dc:date and dc:created are present on post date.'));
  303. }
  304. /**
  305. * Create a random user and ensure the default mapping for user is used.
  306. */
  307. function testUserAttributesInMarkup() {
  308. // Create two users, one with access to user profiles.
  309. $user1 = $this->drupalCreateUser(array('access user profiles'));
  310. $user2 = $this->drupalCreateUser();
  311. $username = $user2->name;
  312. $this->drupalLogin($user1);
  313. // Browse to the user profile page.
  314. $this->drupalGet('user/' . $user2->uid);
  315. // Ensure the default bundle mapping for user is used on the user profile
  316. // page. These attributes come from the user default bundle definition.
  317. $account_uri = url('user/' . $user2->uid);
  318. $person_uri = url('user/' . $user2->uid, array('fragment' => 'me'));
  319. $user2_profile_about = $this->xpath('//div[@class="profile" and @typeof="sioc:UserAccount" and @about=:account-uri]', array(
  320. ':account-uri' => $account_uri,
  321. ));
  322. $this->assertTrue(!empty($user2_profile_about), t('RDFa markup found on user profile page'));
  323. $user_account_holder = $this->xpath('//meta[contains(@typeof, "foaf:Person") and @about=:person-uri and @resource=:account-uri and contains(@rel, "foaf:account")]', array(
  324. ':person-uri' => $person_uri,
  325. ':account-uri' => $account_uri,
  326. ));
  327. $this->assertTrue(!empty($user_account_holder), t('URI created for account holder and username set on sioc:UserAccount.'));
  328. $user_username = $this->xpath('//meta[@about=:account-uri and contains(@property, "foaf:name") and @content=:username]', array(
  329. ':account-uri' => $account_uri,
  330. ':username' => $username,
  331. ));
  332. $this->assertTrue(!empty($user_username), t('foaf:name set on username.'));
  333. // User 2 creates node.
  334. $this->drupalLogin($user2);
  335. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  336. $this->drupalLogin($user1);
  337. $this->drupalGet('node/' . $node->nid);
  338. // Ensures the default bundle mapping for user is used on the Authored By
  339. // information on the node.
  340. $author_about = $this->xpath('//a[@typeof="sioc:UserAccount" and @about=:account-uri and @property="foaf:name" and contains(@xml:lang, "")]', array(
  341. ':account-uri' => $account_uri,
  342. ));
  343. $this->assertTrue(!empty($author_about), t('RDFa markup found on author information on post. xml:lang on username is set to empty string.'));
  344. }
  345. /**
  346. * Creates a random term and ensures the right RDFa markup is used.
  347. */
  348. function testTaxonomyTermRdfaAttributes() {
  349. $vocabulary = $this->createVocabulary();
  350. $term = $this->createTerm($vocabulary);
  351. // Views the term and checks that the RDFa markup is correct.
  352. $this->drupalGet('taxonomy/term/' . $term->tid);
  353. $term_url = url('taxonomy/term/' . $term->tid);
  354. $term_name = $term->name;
  355. $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(
  356. ':term-url' => $term_url,
  357. ':term-name' => $term_name,
  358. ));
  359. $this->assertTrue(!empty($term_rdfa_meta), t('RDFa markup found on term page.'));
  360. }
  361. }
  362. class RdfCommentAttributesTestCase extends CommentHelperCase {
  363. public static function getInfo() {
  364. return array(
  365. 'name' => 'RDF comment mapping',
  366. 'description' => 'Tests the RDFa markup of comments.',
  367. 'group' => 'RDF',
  368. );
  369. }
  370. public function setUp() {
  371. parent::setUp('comment', 'rdf', 'rdf_test');
  372. $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer permissions', 'administer blocks'));
  373. $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create article content', 'access user profiles'));
  374. // Enables anonymous user comments.
  375. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  376. 'access comments' => TRUE,
  377. 'post comments' => TRUE,
  378. 'skip comment approval' => TRUE,
  379. ));
  380. // Allows anonymous to leave their contact information.
  381. $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
  382. $this->setCommentPreview(DRUPAL_OPTIONAL);
  383. $this->setCommentForm(TRUE);
  384. $this->setCommentSubject(TRUE);
  385. $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, t('Comment paging changed.'));
  386. // Creates the nodes on which the test comments will be posted.
  387. $this->drupalLogin($this->web_user);
  388. $this->node1 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  389. $this->node2 = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  390. $this->drupalLogout();
  391. }
  392. /**
  393. * Tests the presence of the RDFa markup for the number of comments.
  394. */
  395. public function testNumberOfCommentsRdfaMarkup() {
  396. // Posts 2 comments as a registered user.
  397. $this->drupalLogin($this->web_user);
  398. $this->postComment($this->node1, $this->randomName(), $this->randomName());
  399. $this->postComment($this->node1, $this->randomName(), $this->randomName());
  400. // Tests number of comments in teaser view.
  401. $this->drupalGet('node');
  402. $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"]');
  403. $this->assertTrue(!empty($comment_count_teaser), t('RDFa markup for the number of comments found on teaser view.'));
  404. $comment_count_link = $this->xpath('//div[@about=:url]//a[contains(@property, "sioc:num_replies") and @rel=""]', array(':url' => url("node/{$this->node1->nid}")));
  405. $this->assertTrue(!empty($comment_count_link), t('Empty rel attribute found in comment count link.'));
  406. // Tests number of comments in full node view.
  407. $this->drupalGet('node/' . $this->node1->nid);
  408. $node_url = url('node/' . $this->node1->nid);
  409. $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));
  410. $this->assertTrue(!empty($comment_count_teaser), t('RDFa markup for the number of comments found on full node view.'));
  411. }
  412. /**
  413. * Tests the presence of the RDFa markup for the title, date and author and
  414. * homepage on registered users and anonymous comments.
  415. */
  416. public function testCommentRdfaMarkup() {
  417. // Posts comment #1 as a registered user.
  418. $this->drupalLogin($this->web_user);
  419. $comment1_subject = $this->randomName();
  420. $comment1_body = $this->randomName();
  421. $comment1 = $this->postComment($this->node1, $comment1_body, $comment1_subject);
  422. // Tests comment #1 with access to the user profile.
  423. $this->drupalGet('node/' . $this->node1->nid);
  424. $this->_testBasicCommentRdfaMarkup($comment1);
  425. // Tests comment #1 with no access to the user profile (as anonymous user).
  426. $this->drupalLogout();
  427. $this->drupalGet('node/' . $this->node1->nid);
  428. $this->_testBasicCommentRdfaMarkup($comment1);
  429. // Posts comment #2 as anonymous user.
  430. $comment2_subject = $this->randomName();
  431. $comment2_body = $this->randomName();
  432. $anonymous_user = array();
  433. $anonymous_user['name'] = $this->randomName();
  434. $anonymous_user['mail'] = 'tester@simpletest.org';
  435. $anonymous_user['homepage'] = 'http://example.org/';
  436. $comment2 = $this->postComment($this->node2, $comment2_body, $comment2_subject, $anonymous_user);
  437. $this->drupalGet('node/' . $this->node2->nid);
  438. // Tests comment #2 as anonymous user.
  439. $this->_testBasicCommentRdfaMarkup($comment2, $anonymous_user);
  440. // Tests the RDFa markup for the homepage (specific to anonymous comments).
  441. $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 @href="http://example.org/" and contains(@rel, "foaf:page")]');
  442. $this->assertTrue(!empty($comment_homepage), t('RDFa markup for the homepage of anonymous user found.'));
  443. // There should be no about attribute on anonymous comments.
  444. $comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[@about]');
  445. $this->assertTrue(empty($comment_homepage), t('No about attribute is present on anonymous user comment.'));
  446. // Tests comment #2 as logged in user.
  447. $this->drupalLogin($this->web_user);
  448. $this->drupalGet('node/' . $this->node2->nid);
  449. $this->_testBasicCommentRdfaMarkup($comment2, $anonymous_user);
  450. // Tests the RDFa markup for the homepage (specific to anonymous comments).
  451. $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 @href="http://example.org/" and contains(@rel, "foaf:page")]');
  452. $this->assertTrue(!empty($comment_homepage), t("RDFa markup for the homepage of anonymous user found."));
  453. // There should be no about attribute on anonymous comments.
  454. $comment_homepage = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//span[@rel="sioc:has_creator"]/a[@about]');
  455. $this->assertTrue(empty($comment_homepage), t("No about attribute is present on anonymous user comment."));
  456. }
  457. /**
  458. * Test RDF comment replies.
  459. */
  460. public function testCommentReplyOfRdfaMarkup() {
  461. // Posts comment #1 as a registered user.
  462. $this->drupalLogin($this->web_user);
  463. $comments[] = $this->postComment($this->node1, $this->randomName(), $this->randomName());
  464. // Tests the reply_of relationship of a first level comment.
  465. $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}")));
  466. $this->assertEqual(1, count($result), t('RDFa markup referring to the node is present.'));
  467. $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')));
  468. $this->assertFalse($result, t('No RDFa markup referring to the comment itself is present.'));
  469. // Posts a reply to the first comment.
  470. $this->drupalGet('comment/reply/' . $this->node1->nid . '/' . $comments[0]->id);
  471. $comments[] = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
  472. // Tests the reply_of relationship of a second level comment.
  473. $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}")));
  474. $this->assertEqual(1, count($result), t('RDFa markup referring to the node is present.'));
  475. $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'))));
  476. $this->assertEqual(1, count($result), t('RDFa markup referring to the parent comment is present.'));
  477. $comments = $this->xpath("(id('comments')//div[contains(@class,'comment ')])[position()=2]");
  478. }
  479. /**
  480. * Helper function for testCommentRdfaMarkup().
  481. *
  482. * Tests the current page for basic comment RDFa markup.
  483. *
  484. * @param $comment
  485. * Comment object.
  486. * @param $account
  487. * An array containing information about an anonymous user.
  488. */
  489. function _testBasicCommentRdfaMarkup($comment, $account = array()) {
  490. $comment_container = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]');
  491. $this->assertTrue(!empty($comment_container), t("Comment RDF type for comment found."));
  492. $comment_title = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//h3[@property="dc:title"]');
  493. $this->assertEqual((string)$comment_title[0]->a, $comment->subject, t("RDFa markup for the comment title found."));
  494. $comment_date = $this->xpath('//div[contains(@class, "comment") and contains(@typeof, "sioct:Comment")]//*[contains(@property, "dc:date") and contains(@property, "dc:created")]');
  495. $this->assertTrue(!empty($comment_date), t("RDFa markup for the date of the comment found."));
  496. // The author tag can be either a or span
  497. $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"]');
  498. $name = empty($account["name"]) ? $this->web_user->name : $account["name"] . " (not verified)";
  499. $this->assertEqual((string)$comment_author[0], $name, t("RDFa markup for the comment author found."));
  500. $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"]');
  501. $this->assertEqual((string)$comment_body[0]->p, $comment->comment, t("RDFa markup for the comment body found."));
  502. }
  503. }
  504. class RdfTrackerAttributesTestCase extends DrupalWebTestCase {
  505. public static function getInfo() {
  506. return array(
  507. 'name' => 'RDF tracker page mapping',
  508. 'description' => 'Test the mapping for the tracker page and ensure the proper RDFa markup in included.',
  509. 'group' => 'RDF',
  510. );
  511. }
  512. function setUp() {
  513. parent::setUp('rdf', 'rdf_test', 'tracker');
  514. // Enable anonymous posting of content.
  515. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  516. 'create article content' => TRUE,
  517. 'access comments' => TRUE,
  518. 'post comments' => TRUE,
  519. 'skip comment approval' => TRUE,
  520. ));
  521. }
  522. /**
  523. * Create nodes as both admin and anonymous user and test for correct RDFa
  524. * markup on the tracker page for those nodes and their comments.
  525. */
  526. function testAttributesInTracker() {
  527. // Create node as anonymous user.
  528. $node_anon = $this->drupalCreateNode(array('type' => 'article', 'uid' => 0));
  529. // Create node as admin user.
  530. $node_admin = $this->drupalCreateNode(array('type' => 'article', 'uid' => 1));
  531. // Pass both the anonymously posted node and the administrator posted node
  532. // through to test for the RDF attributes.
  533. $this->_testBasicTrackerRdfaMarkup($node_anon);
  534. $this->_testBasicTrackerRdfaMarkup($node_admin);
  535. }
  536. /**
  537. * Helper function for testAttributesInTracker().
  538. *
  539. * Tests the tracker page for RDFa markup.
  540. *
  541. * @param $node
  542. * The node just created.
  543. */
  544. function _testBasicTrackerRdfaMarkup($node) {
  545. $url = url('node/' . $node->nid);
  546. $user = ($node->uid == 0) ? 'Anonymous user' : 'Registered user';
  547. // Navigate to tracker page.
  548. $this->drupalGet('tracker');
  549. // Tests whether the about property is applied. This is implicit in the
  550. // success of the following tests, but making it explicit will make
  551. // debugging easier in case of failure.
  552. $tracker_about = $this->xpath('//tr[@about=:url]', array(':url' => $url));
  553. $this->assertTrue(!empty($tracker_about), t('About attribute found on table row for @user content.', array('@user'=> $user)));
  554. // Tests whether the title has the correct property attribute.
  555. $tracker_title = $this->xpath('//tr[@about=:url]/td[@property="dc:title" and @datatype=""]', array(':url' => $url));
  556. $this->assertTrue(!empty($tracker_title), t('Title property attribute found on @user content.', array('@user'=> $user)));
  557. // Tests whether the relationship between the content and user has been set.
  558. $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));
  559. $this->assertTrue(!empty($tracker_user), t('Typeof and name property attributes found on @user.', array('@user'=> $user)));
  560. // There should be an about attribute on logged in users and no about
  561. // attribute for anonymous users.
  562. $tracker_user = $this->xpath('//tr[@about=:url]//td[@rel="sioc:has_creator"]/*[@about]', array(':url' => $url));
  563. if ($node->uid == 0) {
  564. $this->assertTrue(empty($tracker_user), t('No about attribute is present on @user.', array('@user'=> $user)));
  565. }
  566. elseif ($node->uid > 0) {
  567. $this->assertTrue(!empty($tracker_user), t('About attribute is present on @user.', array('@user'=> $user)));
  568. }
  569. // Tests whether the property has been set for number of comments.
  570. $tracker_replies = $this->xpath('//tr[@about=:url]//td[contains(@property, "sioc:num_replies") and contains(@content, "0") and @datatype="xsd:integer"]', array(':url' => $url));
  571. $this->assertTrue($tracker_replies, t('Num replies property and content attributes found on @user content.', array('@user'=> $user)));
  572. // Tests that the appropriate RDFa markup to annotate the latest activity
  573. // date has been added to the tracker output before comments have been
  574. // posted, meaning the latest activity reflects changes to the node itself.
  575. $isoDate = date('c', $node->changed);
  576. $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));
  577. $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)));
  578. // Tests that the appropriate RDFa markup to annotate the latest activity
  579. // date has been added to the tracker output after a comment is posted.
  580. $comment = array(
  581. 'subject' => $this->randomName(),
  582. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
  583. );
  584. $this->drupalPost('comment/reply/' . $node->nid, $comment, t('Save'));
  585. $this->drupalGet('tracker');
  586. // Tests whether the property has been set for number of comments.
  587. $tracker_replies = $this->xpath('//tr[@about=:url]//td[contains(@property, "sioc:num_replies") and contains(@content, "1") and @datatype="xsd:integer"]', array(':url' => $url));
  588. $this->assertTrue($tracker_replies, t('Num replies property and content attributes found on @user content.', array('@user'=> $user)));
  589. // Need to query database directly to obtain last_activity_date because
  590. // it cannot be accessed via node_load().
  591. $result = db_query('SELECT t.changed FROM {tracker_node} t WHERE t.nid = (:nid)', array(':nid' => $node->nid));
  592. foreach ($result as $node) {
  593. $expected_last_activity_date = $node->changed;
  594. }
  595. $isoDate = date('c', $expected_last_activity_date);
  596. $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));
  597. $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)));
  598. }
  599. }
  600. /**
  601. * Tests for RDF namespaces declaration with hook_rdf_namespaces().
  602. */
  603. class RdfGetRdfNamespacesTestCase extends DrupalWebTestCase {
  604. public static function getInfo() {
  605. return array(
  606. 'name' => 'RDF namespaces',
  607. 'description' => 'Test hook_rdf_namespaces() and ensure only "safe" namespaces are returned.',
  608. 'group' => 'RDF',
  609. );
  610. }
  611. function setUp() {
  612. parent::setUp('rdf', 'rdf_test');
  613. }
  614. /**
  615. * Test getting RDF namesapces.
  616. */
  617. function testGetRdfNamespaces() {
  618. // Get all RDF namespaces.
  619. $ns = rdf_get_namespaces();
  620. $this->assertEqual($ns['rdfs'], 'http://www.w3.org/2000/01/rdf-schema#', t('A prefix declared once is included.'));
  621. $this->assertEqual($ns['foaf'], 'http://xmlns.com/foaf/0.1/', t('The same prefix declared in several implementations of hook_rdf_namespaces() is valid as long as all the namespaces are the same.'));
  622. $this->assertEqual($ns['foaf1'], 'http://xmlns.com/foaf/0.1/', t('Two prefixes can be assigned the same namespace.'));
  623. $this->assertTrue(!isset($ns['dc']), t('A prefix with conflicting namespaces is discarded.'));
  624. }
  625. }