Skip navigation
Help

aggregator.test

  1. drupal
    1. 7 drupal/modules/aggregator/aggregator.test

Tests for aggregator.module.

Classes

NameDescription
AddFeedTestCase
AggregatorCronTestCase
AggregatorRenderingTestCase
AggregatorTestCase
CategorizeFeedItemTestCase
CategorizeFeedTestCase
FeedParserTestCaseTests for feed parsing.
ImportOPMLTestCase
RemoveFeedItemTestCase
RemoveFeedTestCase
UpdateFeedItemTestCase
UpdateFeedTestCase

File

drupal/modules/aggregator/aggregator.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for aggregator.module.
  5. */
  6. class AggregatorTestCase extends DrupalWebTestCase {
  7. function setUp() {
  8. parent::setUp('aggregator', 'aggregator_test');
  9. $web_user = $this->drupalCreateUser(array('administer news feeds', 'access news feeds', 'create article content'));
  10. $this->drupalLogin($web_user);
  11. }
  12. /**
  13. * Create an aggregator feed (simulate form submission on admin/config/services/aggregator/add/feed).
  14. *
  15. * @param $feed_url
  16. * If given, feed will be created with this URL, otherwise /rss.xml will be used.
  17. * @return $feed
  18. * Full feed object if possible.
  19. *
  20. * @see getFeedEditArray()
  21. */
  22. function createFeed($feed_url = NULL) {
  23. $edit = $this->getFeedEditArray($feed_url);
  24. $this->drupalPost('admin/config/services/aggregator/add/feed', $edit, t('Save'));
  25. $this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title'])));
  26. $feed = db_query("SELECT * FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $edit['title'], ':url' => $edit['url']))->fetch();
  27. $this->assertTrue(!empty($feed), t('The feed found in database.'));
  28. return $feed;
  29. }
  30. /**
  31. * Delete an aggregator feed.
  32. *
  33. * @param $feed
  34. * Feed object representing the feed.
  35. */
  36. function deleteFeed($feed) {
  37. $this->drupalPost('admin/config/services/aggregator/edit/feed/' . $feed->fid, array(), t('Delete'));
  38. $this->assertRaw(t('The feed %title has been deleted.', array('%title' => $feed->title)), t('Feed deleted successfully.'));
  39. }
  40. /**
  41. * Return a randomly generated feed edit array.
  42. *
  43. * @param $feed_url
  44. * If given, feed will be created with this URL, otherwise /rss.xml will be used.
  45. * @return
  46. * A feed array.
  47. */
  48. function getFeedEditArray($feed_url = NULL) {
  49. $feed_name = $this->randomName(10);
  50. if (!$feed_url) {
  51. $feed_url = url('rss.xml', array(
  52. 'query' => array('feed' => $feed_name),
  53. 'absolute' => TRUE,
  54. ));
  55. }
  56. $edit = array(
  57. 'title' => $feed_name,
  58. 'url' => $feed_url,
  59. 'refresh' => '900',
  60. );
  61. return $edit;
  62. }
  63. /**
  64. * Return the count of the randomly created feed array.
  65. *
  66. * @return
  67. * Number of feed items on default feed created by createFeed().
  68. */
  69. function getDefaultFeedItemCount() {
  70. // Our tests are based off of rss.xml, so let's find out how many elements should be related.
  71. $feed_count = db_query_range('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1', 0, variable_get('feed_default_items', 10))->fetchField();
  72. return $feed_count > 10 ? 10 : $feed_count;
  73. }
  74. /**
  75. * Update feed items (simulate click to admin/config/services/aggregator/update/$fid).
  76. *
  77. * @param $feed
  78. * Feed object representing the feed.
  79. * @param $expected_count
  80. * Expected number of feed items.
  81. */
  82. function updateFeedItems(&$feed, $expected_count) {
  83. // First, let's ensure we can get to the rss xml.
  84. $this->drupalGet($feed->url);
  85. $this->assertResponse(200, t('!url is reachable.', array('!url' => $feed->url)));
  86. // Refresh the feed (simulated link click).
  87. $this->drupalGet('admin/config/services/aggregator/update/' . $feed->fid);
  88. // Ensure we have the right number of items.
  89. $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid));
  90. $items = array();
  91. $feed->items = array();
  92. foreach ($result as $item) {
  93. $feed->items[] = $item->iid;
  94. }
  95. $feed->item_count = count($feed->items);
  96. $this->assertEqual($expected_count, $feed->item_count, t('Total items in feed equal to the total items in database (!val1 != !val2)', array('!val1' => $expected_count, '!val2' => $feed->item_count)));
  97. }
  98. /**
  99. * Confirm item removal from a feed.
  100. *
  101. * @param $feed
  102. * Feed object representing the feed.
  103. */
  104. function removeFeedItems($feed) {
  105. $this->drupalPost('admin/config/services/aggregator/remove/' . $feed->fid, array(), t('Remove items'));
  106. $this->assertRaw(t('The news items from %title have been removed.', array('%title' => $feed->title)), t('Feed items removed.'));
  107. }
  108. /**
  109. * Add and remove feed items and ensure that the count is zero.
  110. *
  111. * @param $feed
  112. * Feed object representing the feed.
  113. * @param $expected_count
  114. * Expected number of feed items.
  115. */
  116. function updateAndRemove($feed, $expected_count) {
  117. $this->updateFeedItems($feed, $expected_count);
  118. $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
  119. $this->assertTrue($count);
  120. $this->removeFeedItems($feed);
  121. $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
  122. $this->assertTrue($count == 0);
  123. }
  124. /**
  125. * Pull feed categories from aggregator_category_feed table.
  126. *
  127. * @param $feed
  128. * Feed object representing the feed.
  129. */
  130. function getFeedCategories($feed) {
  131. // add the categories to the feed so we can use them
  132. $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $feed->fid));
  133. foreach ($result as $category) {
  134. $feed->categories[] = $category->cid;
  135. }
  136. }
  137. /**
  138. * Pull categories from aggregator_category table.
  139. */
  140. function getCategories() {
  141. $categories = array();
  142. $result = db_query('SELECT * FROM {aggregator_category}');
  143. foreach ($result as $category) {
  144. $categories[$category->cid] = $category;
  145. }
  146. return $categories;
  147. }
  148. /**
  149. * Check if the feed name and url is unique.
  150. *
  151. * @param $feed_name
  152. * String containing the feed name to check.
  153. * @param $feed_url
  154. * String containing the feed url to check.
  155. * @return
  156. * TRUE if feed is unique.
  157. */
  158. function uniqueFeed($feed_name, $feed_url) {
  159. $result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed_name, ':url' => $feed_url))->fetchField();
  160. return (1 == $result);
  161. }
  162. /**
  163. * Create a valid OPML file from an array of feeds.
  164. *
  165. * @param $feeds
  166. * An array of feeds.
  167. * @return
  168. * Path to valid OPML file.
  169. */
  170. function getValidOpml($feeds) {
  171. // Properly escape URLs so that XML parsers don't choke on them.
  172. foreach ($feeds as &$feed) {
  173. $feed['url'] = htmlspecialchars($feed['url']);
  174. }
  175. /**
  176. * Does not have an XML declaration, must pass the parser.
  177. */
  178. $opml = <<<EOF
  179. <opml version="1.0">
  180. <head></head>
  181. <body>
  182. <!-- First feed to be imported. -->
  183. <outline text="{$feeds[0]['title']}" xmlurl="{$feeds[0]['url']}" />
  184. <!-- Second feed. Test string delimitation and attribute order. -->
  185. <outline xmlurl='{$feeds[1]['url']}' text='{$feeds[1]['title']}'/>
  186. <!-- Test for duplicate URL and title. -->
  187. <outline xmlurl="{$feeds[0]['url']}" text="Duplicate URL"/>
  188. <outline xmlurl="http://duplicate.title" text="{$feeds[1]['title']}"/>
  189. <!-- Test that feeds are only added with required attributes. -->
  190. <outline text="{$feeds[2]['title']}" />
  191. <outline xmlurl="{$feeds[2]['url']}" />
  192. </body>
  193. </opml>
  194. EOF;
  195. $path = 'public://valid-opml.xml';
  196. return file_unmanaged_save_data($opml, $path);
  197. }
  198. /**
  199. * Create an invalid OPML file.
  200. *
  201. * @return
  202. * Path to invalid OPML file.
  203. */
  204. function getInvalidOpml() {
  205. $opml = <<<EOF
  206. <opml>
  207. <invalid>
  208. </opml>
  209. EOF;
  210. $path = 'public://invalid-opml.xml';
  211. return file_unmanaged_save_data($opml, $path);
  212. }
  213. /**
  214. * Create a valid but empty OPML file.
  215. *
  216. * @return
  217. * Path to empty OPML file.
  218. */
  219. function getEmptyOpml() {
  220. $opml = <<<EOF
  221. <?xml version="1.0" encoding="utf-8"?>
  222. <opml version="1.0">
  223. <head></head>
  224. <body>
  225. <outline text="Sample text" />
  226. <outline text="Sample text" url="Sample URL" />
  227. </body>
  228. </opml>
  229. EOF;
  230. $path = 'public://empty-opml.xml';
  231. return file_unmanaged_save_data($opml, $path);
  232. }
  233. function getRSS091Sample() {
  234. return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/aggregator_test_rss091.xml';
  235. }
  236. function getAtomSample() {
  237. // The content of this sample ATOM feed is based directly off of the
  238. // example provided in RFC 4287.
  239. return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/aggregator_test_atom.xml';
  240. }
  241. function createSampleNodes() {
  242. $langcode = LANGUAGE_NONE;
  243. // Post 5 articles.
  244. for ($i = 0; $i < 5; $i++) {
  245. $edit = array();
  246. $edit['title'] = $this->randomName();
  247. $edit["body[$langcode][0][value]"] = $this->randomName();
  248. $this->drupalPost('node/add/article', $edit, t('Save'));
  249. }
  250. }
  251. }
  252. class AddFeedTestCase extends AggregatorTestCase {
  253. public static function getInfo() {
  254. return array(
  255. 'name' => 'Add feed functionality',
  256. 'description' => 'Add feed test.',
  257. 'group' => 'Aggregator'
  258. );
  259. }
  260. /**
  261. * Create a feed, ensure that it is unique, check the source, and delete the feed.
  262. */
  263. function testAddFeed() {
  264. $feed = $this->createFeed();
  265. // Check feed data.
  266. $this->assertEqual($this->getUrl(), url('admin/config/services/aggregator/add/feed', array('absolute' => TRUE)), t('Directed to correct url.'));
  267. $this->assertTrue($this->uniqueFeed($feed->title, $feed->url), t('The feed is unique.'));
  268. // Check feed source.
  269. $this->drupalGet('aggregator/sources/' . $feed->fid);
  270. $this->assertResponse(200, t('Feed source exists.'));
  271. $this->assertText($feed->title, t('Page title'));
  272. $this->drupalGet('aggregator/sources/' . $feed->fid . '/categorize');
  273. $this->assertResponse(200, t('Feed categorization page exists.'));
  274. // Delete feed.
  275. $this->deleteFeed($feed);
  276. }
  277. }
  278. class CategorizeFeedTestCase extends AggregatorTestCase {
  279. public static function getInfo() {
  280. return array(
  281. 'name' => 'Categorize feed functionality',
  282. 'description' => 'Categorize feed test.',
  283. 'group' => 'Aggregator'
  284. );
  285. }
  286. /**
  287. * Create a feed and make sure you can add more than one category to it.
  288. */
  289. function testCategorizeFeed() {
  290. // Create 2 categories.
  291. $category_1 = array('title' => $this->randomName(10), 'description' => '');
  292. $this->drupalPost('admin/config/services/aggregator/add/category', $category_1, t('Save'));
  293. $this->assertRaw(t('The category %title has been added.', array('%title' => $category_1['title'])), t('The category %title has been added.', array('%title' => $category_1['title'])));
  294. $category_2 = array('title' => $this->randomName(10), 'description' => '');
  295. $this->drupalPost('admin/config/services/aggregator/add/category', $category_2, t('Save'));
  296. $this->assertRaw(t('The category %title has been added.', array('%title' => $category_2['title'])), t('The category %title has been added.', array('%title' => $category_2['title'])));
  297. // Get categories from database.
  298. $categories = $this->getCategories();
  299. // Create a feed and assign 2 categories to it.
  300. $feed = $this->getFeedEditArray();
  301. $feed['block'] = 5;
  302. foreach ($categories as $cid => $category) {
  303. $feed['category'][$cid] = $cid;
  304. }
  305. // Use aggregator_save_feed() function to save the feed.
  306. aggregator_save_feed($feed);
  307. $db_feed = db_query("SELECT * FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed['title'], ':url' => $feed['url']))->fetch();
  308. // Assert the feed has two categories.
  309. $this->getFeedCategories($db_feed);
  310. $this->assertEqual(count($db_feed->categories), 2, t('Feed has 2 categories'));
  311. }
  312. }
  313. class UpdateFeedTestCase extends AggregatorTestCase {
  314. public static function getInfo() {
  315. return array(
  316. 'name' => 'Update feed functionality',
  317. 'description' => 'Update feed test.',
  318. 'group' => 'Aggregator'
  319. );
  320. }
  321. /**
  322. * Create a feed and attempt to update it.
  323. */
  324. function testUpdateFeed() {
  325. $remamining_fields = array('title', 'url', '');
  326. foreach ($remamining_fields as $same_field) {
  327. $feed = $this->createFeed();
  328. // Get new feed data array and modify newly created feed.
  329. $edit = $this->getFeedEditArray();
  330. $edit['refresh'] = 1800; // Change refresh value.
  331. if (isset($feed->{$same_field})) {
  332. $edit[$same_field] = $feed->{$same_field};
  333. }
  334. $this->drupalPost('admin/config/services/aggregator/edit/feed/' . $feed->fid, $edit, t('Save'));
  335. $this->assertRaw(t('The feed %name has been updated.', array('%name' => $edit['title'])), t('The feed %name has been updated.', array('%name' => $edit['title'])));
  336. // Check feed data.
  337. $this->assertEqual($this->getUrl(), url('admin/config/services/aggregator/', array('absolute' => TRUE)));
  338. $this->assertTrue($this->uniqueFeed($edit['title'], $edit['url']), t('The feed is unique.'));
  339. // Check feed source.
  340. $this->drupalGet('aggregator/sources/' . $feed->fid);
  341. $this->assertResponse(200, t('Feed source exists.'));
  342. $this->assertText($edit['title'], t('Page title'));
  343. // Delete feed.
  344. $feed->title = $edit['title']; // Set correct title so deleteFeed() will work.
  345. $this->deleteFeed($feed);
  346. }
  347. }
  348. }
  349. class RemoveFeedTestCase extends AggregatorTestCase {
  350. public static function getInfo() {
  351. return array(
  352. 'name' => 'Remove feed functionality',
  353. 'description' => 'Remove feed test.',
  354. 'group' => 'Aggregator'
  355. );
  356. }
  357. /**
  358. * Remove a feed and ensure that all it services are removed.
  359. */
  360. function testRemoveFeed() {
  361. $feed = $this->createFeed();
  362. // Delete feed.
  363. $this->deleteFeed($feed);
  364. // Check feed source.
  365. $this->drupalGet('aggregator/sources/' . $feed->fid);
  366. $this->assertResponse(404, t('Deleted feed source does not exists.'));
  367. // Check database for feed.
  368. $result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed->title, ':url' => $feed->url))->fetchField();
  369. $this->assertFalse($result, t('Feed not found in database'));
  370. }
  371. }
  372. class UpdateFeedItemTestCase extends AggregatorTestCase {
  373. public static function getInfo() {
  374. return array(
  375. 'name' => 'Update feed item functionality',
  376. 'description' => 'Update feed items from a feed.',
  377. 'group' => 'Aggregator'
  378. );
  379. }
  380. /**
  381. * Test running "update items" from the 'admin/config/services/aggregator' page.
  382. */
  383. function testUpdateFeedItem() {
  384. $this->createSampleNodes();
  385. // Create a feed and test updating feed items if possible.
  386. $feed = $this->createFeed();
  387. if (!empty($feed)) {
  388. $this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
  389. $this->removeFeedItems($feed);
  390. }
  391. // Delete feed.
  392. $this->deleteFeed($feed);
  393. // Test updating feed items without valid timestamp information.
  394. $edit = array(
  395. 'title' => "Feed without publish timestamp",
  396. 'url' => $this->getRSS091Sample(),
  397. );
  398. $this->drupalGet($edit['url']);
  399. $this->assertResponse(array(200), t('URL !url is accessible', array('!url' => $edit['url'])));
  400. $this->drupalPost('admin/config/services/aggregator/add/feed', $edit, t('Save'));
  401. $this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title'])));
  402. $feed = db_query("SELECT * FROM {aggregator_feed} WHERE url = :url", array(':url' => $edit['url']))->fetchObject();
  403. $this->drupalGet('admin/config/services/aggregator/update/' . $feed->fid);
  404. $before = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
  405. // Sleep for 3 second.
  406. sleep(3);
  407. db_update('aggregator_feed')
  408. ->condition('fid', $feed->fid)
  409. ->fields(array(
  410. 'checked' => 0,
  411. 'hash' => '',
  412. 'etag' => '',
  413. 'modified' => 0,
  414. ))
  415. ->execute();
  416. $this->drupalGet('admin/config/services/aggregator/update/' . $feed->fid);
  417. $after = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
  418. $this->assertTrue($before === $after, t('Publish timestamp of feed item was not updated (!before === !after)', array('!before' => $before, '!after' => $after)));
  419. }
  420. }
  421. class RemoveFeedItemTestCase extends AggregatorTestCase {
  422. public static function getInfo() {
  423. return array(
  424. 'name' => 'Remove feed item functionality',
  425. 'description' => 'Remove feed items from a feed.',
  426. 'group' => 'Aggregator'
  427. );
  428. }
  429. /**
  430. * Test running "remove items" from the 'admin/config/services/aggregator' page.
  431. */
  432. function testRemoveFeedItem() {
  433. // Create a bunch of test feeds.
  434. $feed_urls = array();
  435. // No last-modified, no etag.
  436. $feed_urls[] = url('aggregator/test-feed', array('absolute' => TRUE));
  437. // Last-modified, but no etag.
  438. $feed_urls[] = url('aggregator/test-feed/1', array('absolute' => TRUE));
  439. // No Last-modified, but etag.
  440. $feed_urls[] = url('aggregator/test-feed/0/1', array('absolute' => TRUE));
  441. // Last-modified and etag.
  442. $feed_urls[] = url('aggregator/test-feed/1/1', array('absolute' => TRUE));
  443. foreach ($feed_urls as $feed_url) {
  444. $feed = $this->createFeed($feed_url);
  445. // Update and remove items two times in a row to make sure that removal
  446. // resets all 'modified' information (modified, etag, hash) and allows for
  447. // immediate update.
  448. $this->updateAndRemove($feed, 2);
  449. $this->updateAndRemove($feed, 2);
  450. $this->updateAndRemove($feed, 2);
  451. // Delete feed.
  452. $this->deleteFeed($feed);
  453. }
  454. }
  455. }
  456. class CategorizeFeedItemTestCase extends AggregatorTestCase {
  457. public static function getInfo() {
  458. return array(
  459. 'name' => 'Categorize feed item functionality',
  460. 'description' => 'Test feed item categorization.',
  461. 'group' => 'Aggregator'
  462. );
  463. }
  464. /**
  465. * If a feed has a category, make sure that the children inherit that
  466. * categorization.
  467. */
  468. function testCategorizeFeedItem() {
  469. $this->createSampleNodes();
  470. // Simulate form submission on "admin/config/services/aggregator/add/category".
  471. $edit = array('title' => $this->randomName(10), 'description' => '');
  472. $this->drupalPost('admin/config/services/aggregator/add/category', $edit, t('Save'));
  473. $this->assertRaw(t('The category %title has been added.', array('%title' => $edit['title'])), t('The category %title has been added.', array('%title' => $edit['title'])));
  474. $category = db_query("SELECT * FROM {aggregator_category} WHERE title = :title", array(':title' => $edit['title']))->fetch();
  475. $this->assertTrue(!empty($category), t('The category found in database.'));
  476. $link_path = 'aggregator/categories/' . $category->cid;
  477. $menu_link = db_query("SELECT * FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $link_path))->fetch();
  478. $this->assertTrue(!empty($menu_link), t('The menu link associated with the category found in database.'));
  479. $feed = $this->createFeed();
  480. db_insert('aggregator_category_feed')
  481. ->fields(array(
  482. 'cid' => $category->cid,
  483. 'fid' => $feed->fid,
  484. ))
  485. ->execute();
  486. $this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
  487. $this->getFeedCategories($feed);
  488. $this->assertTrue(!empty($feed->categories), t('The category found in the feed.'));
  489. // For each category of a feed, ensure feed items have that category, too.
  490. if (!empty($feed->categories) && !empty($feed->items)) {
  491. foreach ($feed->categories as $category) {
  492. $categorized_count = db_select('aggregator_category_item')
  493. ->condition('iid', $feed->items, 'IN')
  494. ->countQuery()
  495. ->execute()
  496. ->fetchField();
  497. $this->assertEqual($feed->item_count, $categorized_count, t('Total items in feed equal to the total categorized feed items in database'));
  498. }
  499. }
  500. // Delete feed.
  501. $this->deleteFeed($feed);
  502. }
  503. }
  504. class ImportOPMLTestCase extends AggregatorTestCase {
  505. public static function getInfo() {
  506. return array(
  507. 'name' => 'Import feeds from OPML functionality',
  508. 'description' => 'Test OPML import.',
  509. 'group' => 'Aggregator',
  510. );
  511. }
  512. /**
  513. * Open OPML import form.
  514. */
  515. function openImportForm() {
  516. db_delete('aggregator_category')->execute();
  517. $category = $this->randomName(10);
  518. $cid = db_insert('aggregator_category')
  519. ->fields(array(
  520. 'title' => $category,
  521. 'description' => '',
  522. ))
  523. ->execute();
  524. $this->drupalGet('admin/config/services/aggregator/add/opml');
  525. $this->assertText('A single OPML document may contain a collection of many feeds.', t('Found OPML help text.'));
  526. $this->assertField('files[upload]', t('Found file upload field.'));
  527. $this->assertField('remote', t('Found Remote URL field.'));
  528. $this->assertField('refresh', '', t('Found Refresh field.'));
  529. $this->assertFieldByName("category[$cid]", $cid, t('Found category field.'));
  530. }
  531. /**
  532. * Submit form filled with invalid fields.
  533. */
  534. function validateImportFormFields() {
  535. $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
  536. $edit = array();
  537. $this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
  538. $this->assertRaw(t('You must <em>either</em> upload a file or enter a URL.'), t('Error if no fields are filled.'));
  539. $path = $this->getEmptyOpml();
  540. $edit = array(
  541. 'files[upload]' => $path,
  542. 'remote' => file_create_url($path),
  543. );
  544. $this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
  545. $this->assertRaw(t('You must <em>either</em> upload a file or enter a URL.'), t('Error if both fields are filled.'));
  546. $edit = array('remote' => 'invalidUrl://empty');
  547. $this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
  548. $this->assertText(t('This URL is not valid.'), t('Error if the URL is invalid.'));
  549. $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
  550. $this->assertEqual($before, $after, t('No feeds were added during the three last form submissions.'));
  551. }
  552. /**
  553. * Submit form with invalid, empty and valid OPML files.
  554. */
  555. function submitImportForm() {
  556. $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
  557. $form['files[upload]'] = $this->getInvalidOpml();
  558. $this->drupalPost('admin/config/services/aggregator/add/opml', $form, t('Import'));
  559. $this->assertText(t('No new feed has been added.'), t('Attempting to upload invalid XML.'));
  560. $edit = array('remote' => file_create_url($this->getEmptyOpml()));
  561. $this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
  562. $this->assertText(t('No new feed has been added.'), t('Attempting to load empty OPML from remote URL.'));
  563. $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
  564. $this->assertEqual($before, $after, t('No feeds were added during the two last form submissions.'));
  565. db_delete('aggregator_feed')->execute();
  566. db_delete('aggregator_category')->execute();
  567. db_delete('aggregator_category_feed')->execute();
  568. $category = $this->randomName(10);
  569. db_insert('aggregator_category')
  570. ->fields(array(
  571. 'cid' => 1,
  572. 'title' => $category,
  573. 'description' => '',
  574. ))
  575. ->execute();
  576. $feeds[0] = $this->getFeedEditArray();
  577. $feeds[1] = $this->getFeedEditArray();
  578. $feeds[2] = $this->getFeedEditArray();
  579. $edit = array(
  580. 'files[upload]' => $this->getValidOpml($feeds),
  581. 'refresh' => '900',
  582. 'category[1]' => $category,
  583. );
  584. $this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
  585. $this->assertRaw(t('A feed with the URL %url already exists.', array('%url' => $feeds[0]['url'])), t('Verifying that a duplicate URL was identified'));
  586. $this->assertRaw(t('A feed named %title already exists.', array('%title' => $feeds[1]['title'])), t('Verifying that a duplicate title was identified'));
  587. $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
  588. $this->assertEqual($after, 2, t('Verifying that two distinct feeds were added.'));
  589. $feeds_from_db = db_query("SELECT f.title, f.url, f.refresh, cf.cid FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} cf ON f.fid = cf.fid");
  590. $refresh = $category = TRUE;
  591. foreach ($feeds_from_db as $feed) {
  592. $title[$feed->url] = $feed->title;
  593. $url[$feed->title] = $feed->url;
  594. $category = $category && $feed->cid == 1;
  595. $refresh = $refresh && $feed->refresh == 900;
  596. }
  597. $this->assertEqual($title[$feeds[0]['url']], $feeds[0]['title'], t('First feed was added correctly.'));
  598. $this->assertEqual($url[$feeds[1]['title']], $feeds[1]['url'], t('Second feed was added correctly.'));
  599. $this->assertTrue($refresh, t('Refresh times are correct.'));
  600. $this->assertTrue($category, t('Categories are correct.'));
  601. }
  602. function testOPMLImport() {
  603. $this->openImportForm();
  604. $this->validateImportFormFields();
  605. $this->submitImportForm();
  606. }
  607. }
  608. class AggregatorCronTestCase extends AggregatorTestCase {
  609. public static function getInfo() {
  610. return array(
  611. 'name' => 'Update on cron functionality',
  612. 'description' => 'Update feeds on cron.',
  613. 'group' => 'Aggregator'
  614. );
  615. }
  616. /**
  617. * Add feeds update them on cron.
  618. */
  619. public function testCron() {
  620. // Create feed and test basic updating on cron.
  621. global $base_url;
  622. $key = variable_get('cron_key', 'drupal');
  623. $this->createSampleNodes();
  624. $feed = $this->createFeed();
  625. $this->drupalGet($base_url . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => $key)));
  626. $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
  627. $this->removeFeedItems($feed);
  628. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
  629. $this->drupalGet($base_url . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => $key)));
  630. $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
  631. // Test feed locking when queued for update.
  632. $this->removeFeedItems($feed);
  633. db_update('aggregator_feed')
  634. ->condition('fid', $feed->fid)
  635. ->fields(array(
  636. 'queued' => REQUEST_TIME,
  637. ))
  638. ->execute();
  639. $this->drupalGet($base_url . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => $key)));
  640. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
  641. db_update('aggregator_feed')
  642. ->condition('fid', $feed->fid)
  643. ->fields(array(
  644. 'queued' => 0,
  645. ))
  646. ->execute();
  647. $this->drupalGet($base_url . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => $key)));
  648. $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
  649. }
  650. }
  651. class AggregatorRenderingTestCase extends AggregatorTestCase {
  652. public static function getInfo() {
  653. return array(
  654. 'name' => 'Checks display of aggregator items',
  655. 'description' => 'Checks display of aggregator items on the page.',
  656. 'group' => 'Aggregator'
  657. );
  658. }
  659. /**
  660. * Add a feed block to the page and checks its links.
  661. *
  662. * TODO: Test the category block as well.
  663. */
  664. public function testBlockLinks() {
  665. // Create feed.
  666. $this->createSampleNodes();
  667. $feed = $this->createFeed();
  668. $this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
  669. // Place block on page (@see block.test:moveBlockToRegion())
  670. // Need admin user to be able to access block admin.
  671. $this->admin_user = $this->drupalCreateUser(array(
  672. 'administer blocks',
  673. 'access administration pages',
  674. 'administer news feeds',
  675. 'access news feeds',
  676. ));
  677. $this->drupalLogin($this->admin_user);
  678. // Prepare to use the block admin form.
  679. $block = array(
  680. 'module' => 'aggregator',
  681. 'delta' => 'feed-' . $feed->fid,
  682. 'title' => $feed->title,
  683. );
  684. $region = 'footer';
  685. $edit = array();
  686. $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $region;
  687. // Check the feed block is available in the block list form.
  688. $this->drupalGet('admin/structure/block');
  689. $this->assertFieldByName('blocks[' . $block['module'] . '_' . $block['delta'] . '][region]', '', 'Aggregator feed block is available for positioning.');
  690. // Position it.
  691. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  692. $this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
  693. // Confirm that the block is now being displayed on pages.
  694. $this->drupalGet('node');
  695. $this->assertText(t($block['title']), t('Feed block is displayed on the page.'));
  696. // Find the expected read_more link.
  697. $href = 'aggregator/sources/' . $feed->fid;
  698. $links = $this->xpath('//a[@href = :href]', array(':href' => url($href)));
  699. $this->assert(isset($links[0]), t('Link to href %href found.', array('%href' => $href)));
  700. // Visit that page.
  701. $this->drupalGet($href);
  702. $correct_titles = $this->xpath('//h1[normalize-space(text())=:title]', array(':title' => $feed->title));
  703. $this->assertFalse(empty($correct_titles), t('Aggregator feed page is available and has the correct title.'));
  704. }
  705. }
  706. /**
  707. * Tests for feed parsing.
  708. */
  709. class FeedParserTestCase extends AggregatorTestCase {
  710. public static function getInfo() {
  711. return array(
  712. 'name' => 'Feed parser functionality',
  713. 'description' => 'Test the built-in feed parser with valid feed samples.',
  714. 'group' => 'Aggregator',
  715. );
  716. }
  717. function setUp() {
  718. parent::setUp();
  719. // Do not remove old aggregator items during these tests, since our sample
  720. // feeds have hardcoded dates in them (which may be expired when this test
  721. // is run).
  722. variable_set('aggregator_clear', AGGREGATOR_CLEAR_NEVER);
  723. }
  724. /**
  725. * Test a feed that uses the RSS 0.91 format.
  726. */
  727. function testRSS091Sample() {
  728. $feed = $this->createFeed($this->getRSS091Sample());
  729. aggregator_refresh($feed);
  730. $this->drupalGet('aggregator/sources/' . $feed->fid);
  731. $this->assertResponse(200, t('Feed %name exists.', array('%name' => $feed->title)));
  732. $this->assertText('First example feed item title');
  733. $this->assertLinkByHref('http://example.com/example-turns-one');
  734. $this->assertText('First example feed item description.');
  735. }
  736. /**
  737. * Test a feed that uses the Atom format.
  738. */
  739. function testAtomSample() {
  740. $feed = $this->createFeed($this->getAtomSample());
  741. aggregator_refresh($feed);
  742. $this->drupalGet('aggregator/sources/' . $feed->fid);
  743. $this->assertResponse(200, t('Feed %name exists.', array('%name' => $feed->title)));
  744. $this->assertText('Atom-Powered Robots Run Amok');
  745. $this->assertLinkByHref('http://example.org/2003/12/13/atom03');
  746. $this->assertText('Some text.');
  747. $this->assertEqual('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', db_query('SELECT guid FROM {aggregator_item} WHERE link = :link', array(':link' => 'http://example.org/2003/12/13/atom03'))->fetchField(), 'Atom entry id element is parsed correctly.');
  748. }
  749. }