feeds_processor_node.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. * @file
  4. * Tests for plugins/FeedsNodeProcessor.inc.
  5. */
  6. /**
  7. * Test aggregating a feed as node items.
  8. */
  9. class FeedsRSStoNodesTest extends FeedsWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'RSS import to nodes',
  13. 'description' => 'Tests a feed configuration that is attached to a content type, uses HTTP fetcher, common syndication parser and a node processor. Repeats the same test for an importer configuration that is not attached to a content type and for a configuration that is attached to a content type and uses the file fetcher.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. /**
  18. * Set up test.
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. // Set the front page to show 20 nodes so we can easily see what is aggregated.
  23. variable_set('default_nodes_main', 20);
  24. // Set the teaser length display to unlimited otherwise tests looking for
  25. // text on nodes will fail.
  26. $edit = array('fields[body][type]' => 'text_default');
  27. $this->drupalPost('admin/structure/types/manage/article/display/teaser', $edit, 'Save');
  28. // Create an importer configuration.
  29. $this->createImporterConfiguration('Syndication', 'syndication');
  30. $this->addMappings('syndication',
  31. array(
  32. 0 => array(
  33. 'source' => 'title',
  34. 'target' => 'title',
  35. 'unique' => FALSE,
  36. ),
  37. 1 => array(
  38. 'source' => 'description',
  39. 'target' => 'body',
  40. ),
  41. 2 => array(
  42. 'source' => 'timestamp',
  43. 'target' => 'created',
  44. ),
  45. 3 => array(
  46. 'source' => 'url',
  47. 'target' => 'url',
  48. 'unique' => TRUE,
  49. ),
  50. 4 => array(
  51. 'source' => 'guid',
  52. 'target' => 'guid',
  53. 'unique' => TRUE,
  54. ),
  55. )
  56. );
  57. }
  58. /**
  59. * Test node creation, refreshing/deleting feeds and feed items.
  60. */
  61. public function test() {
  62. $nid = $this->createFeedNode();
  63. // Assert 10 items aggregated after creation of the node.
  64. $this->assertText('Created 10 nodes');
  65. $article_nid = db_query_range("SELECT nid FROM {node} WHERE type = 'article'", 0, 1)->fetchField();
  66. $this->assertEqual("Created by FeedsNodeProcessor", db_query("SELECT nr.log FROM {node} n JOIN {node_revision} nr ON n.vid = nr.vid WHERE n.nid = :nid", array(':nid' => $article_nid))->fetchField());
  67. // Navigate to feed node, there should be Feeds tabs visible.
  68. $this->drupalGet("node/$nid");
  69. $this->assertRaw("node/$nid/import");
  70. $this->assertRaw("node/$nid/delete-items");
  71. // Assert accuracy of aggregated information.
  72. $this->drupalGet('node');
  73. $this->assertRaw('<span class="username">Anonymous (not verified)</span>');
  74. $this->assertDevseedFeedContent();
  75. // Assert DB status.
  76. $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_item} fi ON fi.entity_type = 'node' AND n.nid = fi.entity_id")->fetchField();
  77. $this->assertEqual($count, 10, 'Accurate number of items in database.');
  78. // Assert default input format on first imported feed node.
  79. // NEEDS update.
  80. // $format = db_query_range("SELECT nr.format FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid JOIN {node_revision} nr ON n.vid = nr.vid", 0, 1)->fetchField();
  81. // $this->assertEqual($format, filter_fallback_format(), 'Using default Input format.');
  82. // Import again.
  83. $this->drupalPost("node/$nid/import", array(), 'Import');
  84. $this->assertText('There are no new nodes');
  85. // Assert DB status, there still shouldn't be more than 10 items.
  86. $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_item} fi ON fi.entity_type = 'node' AND n.nid = fi.entity_id")->fetchField();
  87. $this->assertEqual($count, 10, 'Accurate number of items in database.');
  88. // All of the above tests should have produced published nodes, set default
  89. // to unpublished, import again.
  90. $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_item} fi ON fi.entity_type = 'node' AND n.nid = fi.entity_id WHERE n.status = 1")->fetchField();
  91. $this->assertEqual($count, 10, 'All items are published.');
  92. $edit = array(
  93. 'node_options[status]' => FALSE,
  94. );
  95. $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
  96. $this->drupalPost("node/$nid/delete-items", array(), 'Delete');
  97. $this->drupalPost("node/$nid/import", array(), 'Import');
  98. $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_item} fi ON fi.entity_type = 'node' AND n.nid = fi.entity_id WHERE n.status = 0")->fetchField();
  99. $this->assertEqual($count, 10, 'No items are published.');
  100. $edit = array(
  101. 'node_options[status]' => TRUE,
  102. );
  103. $this->drupalPost('admin/structure/types/manage/article', $edit, t('Save content type'));
  104. $this->drupalPost("node/$nid/delete-items", array(), 'Delete');
  105. // Enable replace existing and import updated feed file.
  106. $this->drupalPost("node/$nid/import", array(), 'Import');
  107. $this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => 1));
  108. $feed_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed_changes.rss2';
  109. $this->editFeedNode($nid, $feed_url);
  110. $this->drupalPost("node/$nid/import", array(), 'Import');
  111. $this->assertText('Updated 2 nodes');
  112. // Assert accuracy of aggregated content (check 2 updates, one original).
  113. $this->drupalGet('node');
  114. $this->assertText('Managing News Translation Workflow: Two Way Translation Updates');
  115. $this->assertText('Presenting on Features in Drupal and Managing News');
  116. $this->assertText('Scaling the Open Atrium UI');
  117. // Import again.
  118. $this->drupalPost("node/$nid/import", array(), 'Import');
  119. $this->assertText('There are no new nodes');
  120. $this->assertFeedItemCount(10);
  121. // Now delete all items.
  122. $this->drupalPost("node/$nid/delete-items", array(), 'Delete');
  123. $this->assertText('Deleted 10 nodes');
  124. $this->assertFeedItemCount(0);
  125. // Change author and turn off authorization.
  126. $this->auth_user = $this->drupalCreateUser(array('access content'));
  127. $this->setSettings('syndication', 'FeedsNodeProcessor', array('author' => $this->auth_user->name, 'authorize' => FALSE));
  128. // Change input format.
  129. $this->setSettings('syndication', 'FeedsNodeProcessor', array('input_format' => 'plain_text'));
  130. // Import again.
  131. $this->drupalPost("node/$nid/import", array(), 'Import');
  132. $this->assertText('Created 10 nodes');
  133. // Assert author.
  134. $this->drupalGet('node');
  135. $this->assertPattern('/<span class="username">' . check_plain($this->auth_user->name) . '<\/span>/');
  136. $count = db_query("SELECT COUNT(*) FROM {feeds_item} fi JOIN {node} n ON fi.entity_type = 'node' AND fi.entity_id = n.nid WHERE n.uid = :uid", array(':uid' => $this->auth_user->uid))->fetchField();
  137. $this->assertEqual($count, 10, 'Accurate number of items in database.');
  138. // Assert input format.
  139. // NEEDS update.
  140. // $format = db_query_range("SELECT nr.format FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid JOIN {node_revision} nr ON n.vid = nr.vid", 0, 1)->fetchField();
  141. // $this->assertEqual($format, filter_fallback_format() + 1, 'Set non-default Input format.');
  142. // Set to update existing, remove authorship of above nodes and import again.
  143. $this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => 2));
  144. $nids = db_query("SELECT nid FROM {node} n INNER JOIN {feeds_item} fi ON fi.entity_type = 'node' AND n.nid = fi.entity_id")->fetchCol();
  145. db_update('node')
  146. ->fields(array('uid' => 0))
  147. ->condition('nid', $nids, 'IN')
  148. ->execute();
  149. db_update('feeds_item')
  150. ->fields(array('hash' => ''))
  151. ->condition('entity_type', 'node')
  152. ->condition('entity_id', $nids, 'IN')
  153. ->execute();
  154. $this->drupalPost("node/$nid/import", array(), 'Import');
  155. $this->drupalGet('node');
  156. $this->assertNoPattern('/<span class="username">' . check_plain($this->auth_user->name) . '<\/span>/');
  157. $count = db_query("SELECT COUNT(*) FROM {feeds_item} fi JOIN {node} n ON fi.entity_type = 'node' AND fi.entity_id = n.nid WHERE n.uid = :uid", array(':uid' => $this->auth_user->uid))->fetchField();
  158. $this->assertEqual($count, 0, 'Accurate number of items in database.');
  159. // Map feed node's author to feed item author, update - feed node's items
  160. // should now be assigned to feed node author.
  161. $this->addMappings('syndication',
  162. array(
  163. 5 => array(
  164. 'source' => 'parent:uid',
  165. 'target' => 'uid',
  166. ),
  167. )
  168. );
  169. $this->drupalPost("node/$nid/import", array(), 'Import');
  170. $this->drupalGet('node');
  171. $this->assertNoPattern('/<span class="username">' . check_plain($this->auth_user->name) . '<\/span>/');
  172. $uid = db_query("SELECT uid FROM {node} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
  173. $count = db_query("SELECT COUNT(*) FROM {node} WHERE uid = :uid", array(':uid' => $uid))->fetchField();
  174. $this->assertEqual($count, 11, 'All feed item nodes are assigned to feed node author.');
  175. // Login with new user with only access content permissions.
  176. $this->drupalLogin($this->auth_user);
  177. // Navigate to feed node, there should be no Feeds tabs visible.
  178. $this->drupalGet("node/$nid");
  179. $this->assertNoRaw("node/$nid/import");
  180. $this->assertNoRaw("node/$nid/delete-items");
  181. // Now create a second feed configuration that is not attached to a content
  182. // type and run tests on importing/purging.
  183. // Login with sufficient permissions.
  184. $this->drupalLogin($this->admin_user);
  185. // Remove all items again so that next test can check for them.
  186. $this->drupalPost("node/$nid/delete-items", array(), 'Delete');
  187. // Create an importer, not attached to content type.
  188. $this->createImporterConfiguration('Syndication standalone', 'syndication_standalone');
  189. $edit = array(
  190. 'content_type' => '',
  191. );
  192. $this->drupalPost('admin/structure/feeds/syndication_standalone/settings', $edit, 'Save');
  193. $this->addMappings('syndication_standalone',
  194. array(
  195. 0 => array(
  196. 'source' => 'title',
  197. 'target' => 'title',
  198. 'unique' => FALSE,
  199. ),
  200. 1 => array(
  201. 'source' => 'description',
  202. 'target' => 'body',
  203. ),
  204. 2 => array(
  205. 'source' => 'timestamp',
  206. 'target' => 'created',
  207. ),
  208. 3 => array(
  209. 'source' => 'url',
  210. 'target' => 'url',
  211. 'unique' => TRUE,
  212. ),
  213. 4 => array(
  214. 'source' => 'guid',
  215. 'target' => 'guid',
  216. 'unique' => TRUE,
  217. ),
  218. )
  219. );
  220. // Import, assert 10 items aggregated after creation of the node.
  221. $this->importURL('syndication_standalone');
  222. $this->assertText('Created 10 nodes');
  223. // Assert accuracy of aggregated information.
  224. $this->drupalGet('node');
  225. $this->assertDevseedFeedContent();
  226. $this->assertFeedItemCount(10);
  227. // Import again.
  228. $this->drupalPost('import/syndication_standalone', array(), 'Import');
  229. $this->assertText('There are no new nodes');
  230. $this->assertFeedItemCount(10);
  231. // Enable replace existing and import updated feed file.
  232. $this->setSettings('syndication_standalone', 'FeedsNodeProcessor', array('update_existing' => 1));
  233. $feed_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed_changes.rss2';
  234. $this->importURL('syndication_standalone', $feed_url);
  235. $this->assertText('Updated 2 nodes');
  236. // Assert accuracy of aggregated information (check 2 updates, one orig).
  237. $this->drupalGet('node');
  238. $this->assertText('Managing News Translation Workflow: Two Way Translation Updates');
  239. $this->assertText('Presenting on Features in Drupal and Managing News');
  240. $this->assertText('Scaling the Open Atrium UI');
  241. // Import again.
  242. $this->drupalPost('import/syndication_standalone', array(), 'Import');
  243. $this->assertText('There are no new nodes');
  244. $this->assertFeedItemCount(10);
  245. // Now delete all items.
  246. $this->drupalPost('import/syndication_standalone/delete-items', array(), 'Delete');
  247. $this->assertText('Deleted 10 nodes');
  248. $this->assertFeedItemCount(0);
  249. // Import again, we should find new content.
  250. $this->drupalPost('import/syndication_standalone', array(), 'Import');
  251. $this->assertText('Created 10 nodes');
  252. $this->assertFeedItemCount(10);
  253. // Login with new user with only access content permissions.
  254. $this->drupalLogin($this->auth_user);
  255. // Navigate to feed import form, access should be denied.
  256. $this->drupalGet('import/syndication_standalone');
  257. $this->assertResponse(403);
  258. // Use File Fetcher.
  259. $this->drupalLogin($this->admin_user);
  260. $edit = array('plugin_key' => 'FeedsFileFetcher');
  261. $this->drupalPost('admin/structure/feeds/syndication_standalone/fetcher', $edit, 'Save');
  262. $edit = array(
  263. 'allowed_extensions' => 'rss2',
  264. );
  265. $this->drupalPost('admin/structure/feeds/syndication_standalone/settings/FeedsFileFetcher', $edit, 'Save');
  266. // Create a feed node.
  267. $edit = array(
  268. 'files[feeds]' => $this->absolutePath() . '/tests/feeds/drupalplanet.rss2',
  269. );
  270. $this->drupalPost('import/syndication_standalone', $edit, 'Import');
  271. $this->assertText('Created 25 nodes');
  272. }
  273. /**
  274. * Check that the total number of entries in the feeds_item table is correct.
  275. */
  276. function assertFeedItemCount($num) {
  277. $count = db_query("SELECT COUNT(*) FROM {feeds_item} WHERE entity_type = 'node'")->fetchField();
  278. $this->assertEqual($count, $num, 'Accurate number of items in database.');
  279. }
  280. /**
  281. * Check thet contents of the current page for the DS feed.
  282. */
  283. function assertDevseedFeedContent() {
  284. $this->assertText('Open Atrium Translation Workflow: Two Way Translation Updates');
  285. $this->assertText('Tue, 10/06/2009');
  286. $this->assertText('A new translation process for Open Atrium &amp; integration with Localize Drupal');
  287. $this->assertText('Week in DC Tech: October 5th Edition');
  288. $this->assertText('Mon, 10/05/2009');
  289. $this->assertText('There are some great technology events happening this week');
  290. $this->assertText('Mapping Innovation at the World Bank with Open Atrium');
  291. $this->assertText('Fri, 10/02/2009');
  292. $this->assertText('is being used as a base platform for collaboration at the World Bank');
  293. $this->assertText('September GeoDC Meetup Tonight');
  294. $this->assertText('Wed, 09/30/2009');
  295. $this->assertText('Today is the last Wednesday of the month');
  296. $this->assertText('Week in DC Tech: September 28th Edition');
  297. $this->assertText('Mon, 09/28/2009');
  298. $this->assertText('Looking to geek out this week? There are a bunch of');
  299. $this->assertText('Open Data for Microfinance: The New MIXMarket.org');
  300. $this->assertText('Thu, 09/24/2009');
  301. $this->assertText('There are profiles for every country that the MIX Market is hosting.');
  302. $this->assertText('Integrating the Siteminder Access System in an Open Atrium-based Intranet');
  303. $this->assertText('Tue, 09/22/2009');
  304. $this->assertText('In addition to authentication, the Siteminder system');
  305. $this->assertText('Week in DC Tech: September 21 Edition');
  306. $this->assertText('Mon, 09/21/2009');
  307. $this->assertText('an interesting variety of technology events happening in Washington, DC ');
  308. $this->assertText('s Software Freedom Day: Impressions &amp; Photos');
  309. $this->assertText('Mon, 09/21/2009');
  310. $this->assertText('Presenting on Features in Drupal and Open Atrium');
  311. $this->assertText('Scaling the Open Atrium UI');
  312. $this->assertText('Fri, 09/18/2009');
  313. $this->assertText('The first major change is switching');
  314. }
  315. /**
  316. * Test validation of feed URLs.
  317. */
  318. function testFeedURLValidation() {
  319. $edit['feeds[FeedsHTTPFetcher][source]'] = 'invalid://url';
  320. $this->drupalPost('node/add/page', $edit, 'Save');
  321. $this->assertText('The URL invalid://url is invalid.');
  322. }
  323. /**
  324. * Test using non-normal URLs like feed:// and webcal://.
  325. */
  326. function testOddFeedSchemes() {
  327. $url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2';
  328. $schemes = array('feed', 'webcal');
  329. $item_count = 0;
  330. foreach ($schemes as $scheme) {
  331. $feed_url = strtr($url, array('http://' => $scheme . '://', 'https://' => $scheme . '://'));
  332. $edit['feeds[FeedsHTTPFetcher][source]'] = $feed_url;
  333. $this->drupalPost('node/add/page', $edit, 'Save');
  334. $this->assertText('Basic page Development Seed - Technological Solutions for Progressive Organizations has been created.');
  335. $this->assertText('Created 10 nodes.');
  336. $this->assertFeedItemCount($item_count + 10);
  337. $item_count += 10;
  338. }
  339. }
  340. /**
  341. * Test that feed elements and links are not found on non-feed nodes.
  342. */
  343. function testNonFeedNodeUI() {
  344. // There should not be feed links on an article node.
  345. $non_feed_node = $this->drupalCreateNode(array('type' => 'article'));
  346. $this->drupalGet('node/' . $non_feed_node->nid);
  347. $this->assertNoLinkByHref('node/' . $non_feed_node->nid . '/import');
  348. $this->assertNoLink('Delete items');
  349. // Navigate to a non-feed node form, there should be no Feed field visible.
  350. $this->drupalGet('node/add/article');
  351. $this->assertNoFieldByName('feeds[FeedsHTTPFetcher][source]');
  352. }
  353. /**
  354. * Test that nodes will not be created if the user is unauthorized to create
  355. * them.
  356. */
  357. public function testAuthorize() {
  358. // Create a user with limited permissions. We can't use
  359. // $this->drupalCreateUser here because we need to to set a specific user
  360. // name.
  361. $edit = array(
  362. 'name' => 'Development Seed',
  363. 'mail' => 'devseed@example.com',
  364. 'pass' => user_password(),
  365. 'status' => 1,
  366. );
  367. $account = user_save(drupal_anonymous_user(), $edit);
  368. // Adding a mapping to the user_name will invoke authorization.
  369. $this->addMappings('syndication',
  370. array(
  371. 5 => array(
  372. 'source' => 'author_name',
  373. 'target' => 'user_name',
  374. ),
  375. )
  376. );
  377. $nid = $this->createFeedNode();
  378. $this->assertText('Failed importing 10 nodes.');
  379. $this->assertText('User ' . $account->name . ' is not authorized to create content type article.');
  380. $node_count = db_query("SELECT COUNT(*) FROM {node}")->fetchField();
  381. // We should have 1 node, the feed node.
  382. $this->assertEqual($node_count, 1, t('Correct number of nodes in the database.'));
  383. // Give the user our admin powers.
  384. $edit = array(
  385. 'roles' => $this->admin_user->roles,
  386. );
  387. $account = user_save($account, $edit);
  388. $this->drupalPost("node/$nid/import", array(), 'Import');
  389. $this->assertText('Created 10 nodes.');
  390. $node_count = db_query("SELECT COUNT(*) FROM {node}")->fetchField();
  391. $this->assertEqual($node_count, 11, t('Correct number of nodes in the database.'));
  392. }
  393. }