feeds_processor_node.test 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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' => 'Processor: Node',
  13. 'description' => 'Tests for the node processor.',
  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. // Enable unpublish missing nodes and import updated feed file.
  254. $this->setSettings('syndication_standalone', 'FeedsNodeProcessor', array('update_non_existent' => FEEDS_UNPUBLISH_NON_EXISTENT, 'update_existing' => FEEDS_REPLACE_EXISTING));
  255. $missing_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed_missing.rss2';
  256. $this->importURL('syndication_standalone', $missing_url);
  257. $this->assertText('Unpublished 1 node');
  258. $this->assertFeedItemCount(10);
  259. // Import again to ensure the message that one node is unpublished doesn't
  260. // reappear (since the node was already unpublished during the previous
  261. // import).
  262. $this->drupalPost('import/syndication_standalone', array(), 'Import');
  263. $this->assertText('There are no new nodes');
  264. $this->assertFeedItemCount(10);
  265. // Re-import the original feed to ensure the unpublished node is updated,
  266. // even though the item is the same since the last time it was available in
  267. // the feed. Fact is that the node was not available in the previous import
  268. // and that should be seen as a change.
  269. $this->importURL('syndication_standalone', $feed_url);
  270. $this->assertText('Updated 1 node');
  271. $this->assertFeedItemCount(10);
  272. // Now delete all items.
  273. $this->drupalPost('import/syndication_standalone/delete-items', array(), 'Delete');
  274. $this->assertText('Deleted 10 nodes');
  275. $this->assertFeedItemCount(0);
  276. // Import again, to reset node counts.
  277. $this->importURL('syndication_standalone', $feed_url);
  278. $this->assertText('Created 10 nodes');
  279. $this->assertFeedItemCount(10);
  280. // Change settings to delete non-existent nodes from feed.
  281. $this->setSettings('syndication_standalone', 'FeedsNodeProcessor', array('update_non_existent' => 'delete'));
  282. $this->importURL('syndication_standalone', $missing_url);
  283. $this->assertText('Removed 1 node');
  284. $this->assertFeedItemCount(9);
  285. // Now delete all items.
  286. $this->drupalPost('import/syndication_standalone/delete-items', array(), 'Delete');
  287. $this->assertText('Deleted 9 nodes');
  288. $this->assertFeedItemCount(0);
  289. // Login with new user with only access content permissions.
  290. $this->drupalLogin($this->auth_user);
  291. // Navigate to feed import form, access should be denied.
  292. $this->drupalGet('import/syndication_standalone');
  293. $this->assertResponse(403);
  294. // Use File Fetcher.
  295. $this->drupalLogin($this->admin_user);
  296. $edit = array('plugin_key' => 'FeedsFileFetcher');
  297. $this->drupalPost('admin/structure/feeds/syndication_standalone/fetcher', $edit, 'Save');
  298. $edit = array(
  299. 'allowed_extensions' => 'rss2',
  300. );
  301. $this->drupalPost('admin/structure/feeds/syndication_standalone/settings/FeedsFileFetcher', $edit, 'Save');
  302. // Create a feed node.
  303. $edit = array(
  304. 'files[feeds]' => $this->absolutePath() . '/tests/feeds/drupalplanet.rss2',
  305. );
  306. $this->drupalPost('import/syndication_standalone', $edit, 'Import');
  307. $this->assertText('Created 25 nodes');
  308. }
  309. /**
  310. * Check that the total number of entries in the feeds_item table is correct.
  311. */
  312. public function assertFeedItemCount($num) {
  313. $count = db_query("SELECT COUNT(*) FROM {feeds_item} WHERE entity_type = 'node'")->fetchField();
  314. $this->assertEqual($count, $num, 'Accurate number of items in database.');
  315. }
  316. /**
  317. * Check thet contents of the current page for the DS feed.
  318. */
  319. public function assertDevseedFeedContent() {
  320. $this->assertText('Open Atrium Translation Workflow: Two Way Translation Updates');
  321. $this->assertText('Tue, 10/06/2009');
  322. $this->assertText('A new translation process for Open Atrium &amp; integration with Localize Drupal');
  323. $this->assertText('Week in DC Tech: October 5th Edition');
  324. $this->assertText('Mon, 10/05/2009');
  325. $this->assertText('There are some great technology events happening this week');
  326. $this->assertText('Mapping Innovation at the World Bank with Open Atrium');
  327. $this->assertText('Fri, 10/02/2009');
  328. $this->assertText('is being used as a base platform for collaboration at the World Bank');
  329. $this->assertText('September GeoDC Meetup Tonight');
  330. $this->assertText('Wed, 09/30/2009');
  331. $this->assertText('Today is the last Wednesday of the month');
  332. $this->assertText('Week in DC Tech: September 28th Edition');
  333. $this->assertText('Mon, 09/28/2009');
  334. $this->assertText('Looking to geek out this week? There are a bunch of');
  335. $this->assertText('Open Data for Microfinance: The New MIXMarket.org');
  336. $this->assertText('Thu, 09/24/2009');
  337. $this->assertText('There are profiles for every country that the MIX Market is hosting.');
  338. $this->assertText('Integrating the Siteminder Access System in an Open Atrium-based Intranet');
  339. $this->assertText('Tue, 09/22/2009');
  340. $this->assertText('In addition to authentication, the Siteminder system');
  341. $this->assertText('Week in DC Tech: September 21 Edition');
  342. $this->assertText('Mon, 09/21/2009');
  343. $this->assertText('an interesting variety of technology events happening in Washington, DC ');
  344. $this->assertText('s Software Freedom Day: Impressions &amp; Photos');
  345. $this->assertText('Mon, 09/21/2009');
  346. $this->assertText('Presenting on Features in Drupal and Open Atrium');
  347. $this->assertText('Scaling the Open Atrium UI');
  348. $this->assertText('Fri, 09/18/2009');
  349. $this->assertText('The first major change is switching');
  350. }
  351. /**
  352. * Test validation of feed URLs.
  353. */
  354. public function testFeedURLValidation() {
  355. $edit['feeds[FeedsHTTPFetcher][source]'] = 'invalid://url';
  356. $this->drupalPost('node/add/page', $edit, 'Save');
  357. $this->assertText('The URL invalid://url is invalid.');
  358. }
  359. /**
  360. * Test using non-normal URLs like feed:// and webcal://.
  361. */
  362. public function testOddFeedSchemes() {
  363. $url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2';
  364. $schemes = array('feed', 'webcal');
  365. $item_count = 0;
  366. foreach ($schemes as $scheme) {
  367. $feed_url = strtr($url, array('http://' => $scheme . '://', 'https://' => $scheme . '://'));
  368. $edit['feeds[FeedsHTTPFetcher][source]'] = $feed_url;
  369. $this->drupalPost('node/add/page', $edit, 'Save');
  370. $this->assertText('Basic page Development Seed - Technological Solutions for Progressive Organizations has been created.');
  371. $this->assertText('Created 10 nodes.');
  372. $this->assertFeedItemCount($item_count + 10);
  373. $item_count += 10;
  374. }
  375. }
  376. /**
  377. * Test that feed elements and links are not found on non-feed nodes.
  378. */
  379. public function testNonFeedNodeUI() {
  380. // There should not be feed links on an article node.
  381. $non_feed_node = $this->drupalCreateNode(array('type' => 'article'));
  382. $this->drupalGet('node/' . $non_feed_node->nid);
  383. $this->assertNoLinkByHref('node/' . $non_feed_node->nid . '/import');
  384. $this->assertNoLink('Delete items');
  385. // Navigate to a non-feed node form, there should be no Feed field visible.
  386. $this->drupalGet('node/add/article');
  387. $this->assertNoFieldByName('feeds[FeedsHTTPFetcher][source]');
  388. }
  389. /**
  390. * Test that nodes will not be created if the user is unauthorized to create
  391. * them.
  392. */
  393. public function testAuthorize() {
  394. // Create a user with limited permissions. We can't use
  395. // $this->drupalCreateUser here because we need to to set a specific user
  396. // name.
  397. $edit = array(
  398. 'name' => 'Development Seed',
  399. 'mail' => 'devseed@example.com',
  400. 'pass' => user_password(),
  401. 'status' => 1,
  402. );
  403. $account = user_save(drupal_anonymous_user(), $edit);
  404. // Adding a mapping to the user_name will invoke authorization.
  405. $this->addMappings('syndication',
  406. array(
  407. 5 => array(
  408. 'source' => 'author_name',
  409. 'target' => 'user_name',
  410. ),
  411. )
  412. );
  413. $nid = $this->createFeedNode();
  414. $this->assertText('Failed importing 10 nodes.');
  415. $this->assertText('The user ' . $account->name . ' is not authorized to create content of type article.');
  416. $node_count = db_query("SELECT COUNT(*) FROM {node}")->fetchField();
  417. // We should have 1 node, the feed node.
  418. $this->assertEqual($node_count, 1, t('Correct number of nodes in the database.'));
  419. // Give the user our admin powers.
  420. $edit = array(
  421. 'roles' => $this->admin_user->roles,
  422. );
  423. $account = user_save($account, $edit);
  424. $this->drupalPost("node/$nid/import", array(), 'Import');
  425. $this->assertText('Created 10 nodes.');
  426. $node_count = db_query("SELECT COUNT(*) FROM {node}")->fetchField();
  427. $this->assertEqual($node_count, 11, t('Correct number of nodes in the database.'));
  428. }
  429. /**
  430. * Tests expiring nodes.
  431. */
  432. public function testExpiry() {
  433. // Create importer configuration.
  434. $this->setSettings('syndication', NULL, array('content_type' => ''));
  435. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  436. 'expire' => 2592000,
  437. ));
  438. // Create importer.
  439. $this->importURL('syndication');
  440. // Set date of a few nodes to current date so they don't expire.
  441. $edit = array(
  442. 'date' => date('Y-m-d'),
  443. );
  444. $this->drupalPost('node/2/edit', $edit, 'Save');
  445. $this->assertText(date('m/d/Y'), 'Found correct date.');
  446. $this->drupalPost('node/5/edit', $edit, 'Save');
  447. $this->assertText(date('m/d/Y'), 'Found correct date.');
  448. // Run cron to schedule jobs.
  449. $this->cronRun();
  450. // Set feeds source expire to run immediately.
  451. db_update('job_schedule')
  452. ->fields(array(
  453. 'next' => 0,
  454. ))
  455. ->condition('name', 'feeds_source_expire')
  456. ->execute();
  457. // Run cron to execute scheduled jobs.
  458. $this->cronRun();
  459. // Query the feeds_items table and count the number of entries.
  460. $row_count = db_query('SELECT COUNT(*) FROM {feeds_item}')->fetchField();
  461. // Check that number of feeds items is equal to the expected items.
  462. $this->assertEqual($row_count, 2, 'Nodes expired.');
  463. }
  464. /**
  465. * Tests process in background option.
  466. */
  467. public function testImportInBackground() {
  468. // Just remove the mappings rather than creating a new importer.
  469. $this->removeMappings('syndication', $this->getCurrentMappings('syndication'));
  470. // Set our process limit to something simple.
  471. variable_set('feeds_process_limit', 5);
  472. $this->setPlugin('syndication', 'FeedsFileFetcher');
  473. $this->setPlugin('syndication', 'FeedsCSVParser');
  474. $this->setSettings('syndication', NULL, array(
  475. 'content_type' => '',
  476. 'process_in_background' => TRUE,
  477. 'import_period' => FEEDS_SCHEDULE_NEVER,
  478. ));
  479. $this->addMappings('syndication', array(
  480. 0 => array(
  481. 'source' => 'title',
  482. 'target' => 'title',
  483. ),
  484. 1 => array(
  485. 'source' => 'GUID',
  486. 'target' => 'guid',
  487. 'unique' => TRUE,
  488. ),
  489. ));
  490. $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/many_nodes_ordered.csv');
  491. $this->assertEqual(5, db_query("SELECT COUNT(*) FROM {node}")->fetchField());
  492. // The feed should still be scheduled because it is being processed.
  493. // @see https://drupal.org/node/2275893
  494. $this->cronRun();
  495. $this->assertEqual(86, db_query("SELECT COUNT(*) FROM {node}")->fetchField());
  496. }
  497. /**
  498. * Tests skip new items.
  499. */
  500. public function testSkipNewItems() {
  501. // Include FeedsProcessor.inc so processor related constants are available.
  502. module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
  503. // Attach to standalone importer.
  504. $this->setSettings('syndication', NULL, array('content_type' => ''));
  505. // Set that new items should not be imported.
  506. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  507. 'insert_new' => FEEDS_SKIP_NEW,
  508. 'update_existing' => FEEDS_SKIP_EXISTING,
  509. ));
  510. // Make title unique target.
  511. $this->removeMappings('syndication', $this->getCurrentMappings('syndication'));
  512. $this->addMappings('syndication', array(
  513. 0 => array(
  514. 'source' => 'title',
  515. 'target' => 'title',
  516. 'unique' => TRUE,
  517. ),
  518. 1 => array(
  519. 'source' => 'description',
  520. 'target' => 'body',
  521. ),
  522. 2 => array(
  523. 'source' => 'timestamp',
  524. 'target' => 'created',
  525. ),
  526. ));
  527. // Do a first import, no nodes should be created.
  528. $edit = array(
  529. 'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2',
  530. );
  531. $this->drupalPost('import/syndication', $edit, 'Import');
  532. $this->assertText('There are no new nodes');
  533. // Now create two nodes with titles that are present in the source
  534. // "developmentseed.rss2".
  535. $this->drupalCreateNode(array(
  536. 'type' => 'article',
  537. 'title' => 'Open Atrium Translation Workflow: Two Way Translation Updates',
  538. ));
  539. $this->drupalCreateNode(array(
  540. 'type' => 'article',
  541. 'title' => 'Week in DC Tech: October 5th Edition',
  542. ));
  543. // Import again. Since the processor is set to not update as well, nothing
  544. // should be imported.
  545. $this->drupalPost('import/syndication', array(), 'Import');
  546. $this->assertText('There are no new nodes');
  547. // Now set importer to update existing.
  548. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  549. 'update_existing' => FEEDS_UPDATE_EXISTING,
  550. ));
  551. // And import again. Two nodes should be updated.
  552. $this->drupalPost('import/syndication', array(), 'Import');
  553. $this->assertText('Updated 2 nodes.');
  554. // Change "insert_new" setting to insert new items to verify if changing the
  555. // setting later has the effect that new items will be imported as yet.
  556. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  557. 'insert_new' => FEEDS_INSERT_NEW,
  558. ));
  559. // Import. Eight nodes should be created. No nodes should be updated.
  560. $this->drupalPost('import/syndication', array(), 'Import');
  561. $this->assertText('Created 8 nodes.');
  562. $this->assertNoText('Updated 2 nodes.');
  563. }
  564. /**
  565. * Tests if the target "changed" works as expected.
  566. */
  567. public function testChangedTarget() {
  568. // Create and configure importer.
  569. $this->createImporterConfiguration('Content CSV', 'csv');
  570. $this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
  571. $this->setPlugin('csv', 'FeedsFileFetcher');
  572. $this->setPlugin('csv', 'FeedsCSVParser');
  573. $this->addMappings('csv', array(
  574. 0 => array(
  575. 'source' => 'title',
  576. 'target' => 'title',
  577. ),
  578. // Borrow the timestamp value from the "created" column in the csv.
  579. 1 => array(
  580. 'source' => 'created',
  581. 'target' => 'changed',
  582. ),
  583. ));
  584. // Import CSV file.
  585. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
  586. $this->assertText('Created 2 nodes');
  587. // Assert changed date of nodes.
  588. $expected_values = array(
  589. 1 => array(
  590. 'changed' => 1251936720,
  591. ),
  592. 2 => array(
  593. 'changed' => 1251932360,
  594. ),
  595. );
  596. for ($i = 1; $i <= 2; $i++) {
  597. $node = node_load($i);
  598. $this->assertEqual($expected_values[$i]['changed'], $node->changed);
  599. }
  600. }
  601. /**
  602. * Tests the FeedsSource::pushImport() method.
  603. */
  604. public function testPushImport() {
  605. // Attach to standalone importer.
  606. $this->setSettings('syndication', NULL, array('content_type' => ''));
  607. $raw = file_get_contents(dirname(__FILE__) . '/feeds/developmentseed.rss2');
  608. feeds_source('syndication', 0)->pushImport(new FeedsFetcherResult($raw));
  609. $this->assertEqual(10, db_query("SELECT COUNT(*) FROM {node}")->fetchField());
  610. }
  611. /**
  612. * Tests the FeedsSource::pushImport() method with a CSV file.
  613. */
  614. public function testPushImportWithCSV() {
  615. // Attach to standalone importer and configure.
  616. $this->setSettings('syndication', NULL, array('content_type' => ''));
  617. $this->setPlugin('syndication', 'FeedsCSVParser');
  618. $this->removeMappings('syndication', $this->getCurrentMappings('syndication'));
  619. $this->addMappings('syndication', array(
  620. 0 => array(
  621. 'source' => 'title',
  622. 'target' => 'title',
  623. ),
  624. ));
  625. $raw = file_get_contents($this->absolutePath() . '/tests/feeds/many_nodes.csv');
  626. feeds_source('syndication', 0)->pushImport(new FeedsFetcherResult($raw));
  627. $this->assertEqual(86, db_query("SELECT COUNT(*) FROM {node}")->fetchField());
  628. }
  629. /**
  630. * Tests if target item is not updated when only non-mapped data on the source changed.
  631. */
  632. public function testIrrelevantUpdate() {
  633. // Include FeedsProcessor.inc so processor related constants are available.
  634. module_load_include('inc', 'feeds', 'plugins/FeedsProcessor');
  635. // Attach to standalone importer and configure.
  636. $this->setSettings('syndication', NULL, array('content_type' => ''));
  637. $this->setPlugin('syndication', 'FeedsFileFetcher');
  638. $this->setPlugin('syndication', 'FeedsCSVParser');
  639. $this->removeMappings('syndication', $this->getCurrentMappings('syndication'));
  640. $this->addMappings('syndication', array(
  641. 0 => array(
  642. 'source' => 'name',
  643. 'target' => 'title',
  644. 'unique' => TRUE,
  645. ),
  646. ));
  647. // Import file.
  648. $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/users.csv');
  649. $this->assertText('Created 5 nodes');
  650. // Ensure that no nodes are updated when only non-mapped columns changed.
  651. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  652. 'skip_hash_check' => FALSE,
  653. 'update_existing' => FEEDS_UPDATE_EXISTING,
  654. ));
  655. $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/users_updated.csv');
  656. $this->assertText('There are no new nodes.');
  657. }
  658. }