Index: modules/aggregator/aggregator.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.module,v
retrieving revision 1.404
diff -u -r1.404 aggregator.module
--- modules/aggregator/aggregator.module	26 Jan 2009 14:08:42 -0000	1.404
+++ modules/aggregator/aggregator.module	28 Jan 2009 22:12:51 -0000
@@ -515,6 +515,7 @@
     ->fields(array(
       'checked' => 0,
       'hash' => '',
+      'etag' => '',
       'modified' => 0,
       'description' => $feed->description,
       'image' => $feed->image,
Index: modules/aggregator/aggregator.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.test,v
retrieving revision 1.18
diff -u -r1.18 aggregator.test
--- modules/aggregator/aggregator.test	22 Jan 2009 12:59:32 -0000	1.18
+++ modules/aggregator/aggregator.test	28 Jan 2009 22:12:51 -0000
@@ -5,7 +5,7 @@
   private static $prefix = 'simpletest_aggregator_';
 
   function setUp() {
-    parent::setUp('aggregator');
+    parent::setUp('aggregator', 'aggregator_test');
     $web_user = $this->drupalCreateUser(array('administer news feeds', 'access news feeds', 'create article content'));
     $this->drupalLogin($web_user);
   }
@@ -13,10 +13,13 @@
   /**
    * Create an aggregator feed (simulate form submission on admin/content/aggregator/add/feed).
    *
+   * @param URL URL 
+   *   If given, feed will be created with this URL, otherwise /rss.xml will be used.
+   *   @see getFeedEditArray()
    * @return $feed Full feed object if possible.
    */
-  function createFeed() {
-    $edit = $this->getFeedEditArray();
+  function createFeed($feed_url = NULL) {
+    $edit = $this->getFeedEditArray($feed_url);
     $this->drupalPost('admin/content/aggregator/add/feed', $edit, t('Save'));
     $this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title'])));
 
@@ -38,11 +41,15 @@
   /**
    * Return a randomly generated feed edit array.
    *
+   * @param URL $feed_url 
+   *   If given, feed will be created with this URL, otherwise /rss.xml will be used.
    * @return array Feed array.
    */
-  function getFeedEditArray() {
+  function getFeedEditArray($feed_url = NULL) {
     $feed_name = $this->randomName(10, self::$prefix);
-    $feed_url = url(NULL, array('absolute' => TRUE)) . 'rss.xml?feed=' . $feed_name;
+    if (!$feed_url) {
+      $feed_url = url(NULL, array('absolute' => TRUE)) . 'rss.xml?feed=' . $feed_name;
+    }
     $edit = array(
       'title' => $feed_name,
       'url' => $feed_url,
@@ -52,19 +59,27 @@
   }
 
   /**
+   * Return the count of the randomly created feed array.
+   * 
+   * @return int Number of feed items on default feed created by createFeed().
+   */
+  function getDefaultFeedItemCount() {
+    // Our tests are based off of rss.xml, so let's find out how many elements should be related.
+    // @todo: remove db_rewrite_sql() when possible
+    $feed_count = db_query_range(db_rewrite_sql('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1'), 0, variable_get('feed_default_items', 10))->fetchField();
+    return $feed_count > 10 ? 10 : $feed_count;
+  }
+
+  /**
    * Update feed items (simulate click to admin/content/aggregator/update/$fid).
    *
    * @param object $feed Feed object representing the feed.
+   * @param int $expected_count Expected number of feed items.
    */
-  function updateFeedItems(&$feed) {
+  function updateFeedItems(&$feed, $expected_count) {
     // First, let's ensure we can get to the rss xml.
-    $this->drupalGet('rss.xml');
-    $this->assertResponse(200, t('rss.xml is reachable.'));
-
-    // Our tests are based off of rss.xml, so let's find out how many elements should be related.
-    // @todo: remove db_rewrite_sql() when possible
-    $feed_count = db_query_range(db_rewrite_sql('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1'), 0, variable_get('feed_default_items', 10))->fetchField();
-    $feed_count = $feed_count > 10 ? 10 : $feed_count;
+    $this->drupalGet($feed->url);
+    $this->assertResponse(200, t('!url is reachable.', array('!url' => $feed->url)));
 
     // Refresh the feed (simulated link click).
     $this->drupalGet('admin/content/aggregator/update/' . $feed->fid);
@@ -77,7 +92,7 @@
       $feed->items[] = $item->iid;
     }
     $feed->item_count = count($feed->items);
-    $this->assertEqual($feed_count, $feed->item_count, t('Total items in feed equal to the total items in database (!val1 != !val2)', array('!val1' => $feed_count, '!val2' => $feed->item_count)));
+    $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)));
   }
 
   /**
@@ -91,6 +106,20 @@
   }
 
   /**
+   * Add and remove feed items and ensure that the count is zero.
+   * 
+   * @param object $feed Feed object representing the feed.
+   * @param int $expected_count Expected number of feed items.
+   */
+  function updateAndRemove($feed, $expected_count) {
+    $this->updateFeedItems($feed, $expected_count);
+    $this->assertText('There is new syndicated content from');
+    $this->removeFeedItems($feed);
+    $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
+    $this->assertTrue($count == 0);
+  }
+
+  /**
    * Pull feed categories from aggregator_category_feed table.
    *
    * @param object $feed Feed object representing the feed.
@@ -192,36 +221,7 @@
   }
 
   function getRSS091Sample() {
-    $feed = <<<EOT
-<?xml version="1.0" encoding="UTF-8"?>
-<rss version="0.91">
-  <channel>
-    <title>Example</title>
-    <link>http://example.com</link>
-    <description>Example updates</description>
-    <language>en-us</language>
-    <copyright>Copyright 2000, Example team.</copyright>
-    <managingEditor>editor@example.com</managingEditor>
-    <webMaster>webmaster@example.com</webMaster>
-    <image>
-      <title>Example</title>
-      <url>http://example.com/images/druplicon.png</url>
-      <link>http://example.com</link>
-      <width>88</width>
-      <height>100</height>
-      <description>Example updates</description>
-    </image>
-    <item>
-      <title>Example turns one</title>
-      <link>http://example.com/example-turns-one</link>
-      <description>Example turns one.</description>
-    </item>
-  </channel>
-</rss>
-EOT;
-
-    $path = file_directory_path() . '/rss091.xml';
-    return file_unmanaged_save_data($feed, $path);
+    return url(drupal_get_path('module', 'aggregator') . '/aggregator_test_rss091.xml', array('absolute' => TRUE));
   }
 
   function createSampleNodes() {
@@ -352,7 +352,7 @@
     // Create a feed and test updating feed items if possible.
     $feed = $this->createFeed();
     if (!empty($feed)) {
-      $this->updateFeedItems($feed);
+      $this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
       $this->removeFeedItems($feed);
     }
 
@@ -362,7 +362,7 @@
     // Test updating feed items without valid timestamp information.
     $edit = array(
       'title' => "Feed without publish timestamp",
-      'url' => file_create_url($this->getRSS091Sample()),
+      'url' => $this->getRSS091Sample(),
     );
     $this->drupalGet($edit['url']);
     $this->assertResponse(array(200), t('URL !url is accessible', array('!url' => $edit['url'])));
@@ -407,18 +407,28 @@
    * Test running "remove items" from the 'admin/content/aggregator' page.
    */
   function testRemoveFeedItem() {
-    $this->createSampleNodes();
-
-    $feed = $this->createFeed();
-
-    // Add and remove feed items and ensure that the count is zero.
-    $this->updateFeedItems($feed);
-    $this->removeFeedItems($feed);
-    $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
-    $this->assertTrue($count == 0);
-
-    // Delete feed.
-    $this->deleteFeed($feed);
+    // Create a bunch of test feeds.
+    $feed_urls = array();
+    // No last-modified, no etag.
+    $feed_urls[0] = url('aggregator/test-feed', array('absolute' => TRUE));
+    // Last-modified, but no etag.
+    $feed_urls[1] = url('aggregator/test-feed/1', array('absolute' => TRUE));
+    // No Last-modified, but etag.
+    $feed_urls[2] = url('aggregator/test-feed/0/1', array('absolute' => TRUE));
+    // Last-modified and etag.
+    $feed_urls[2] = url('aggregator/test-feed/1/1', array('absolute' => TRUE));
+
+    foreach ($feed_urls as $feed_url) {
+      $feed = $this->createFeed($feed_url);
+      // Update and remove items two times in a row to make sure that removal
+      // resets all 'modified' information (modified, etag, hash) and allows for 
+      // immediate update.
+      $this->updateAndRemove($feed, 2);
+      $this->updateAndRemove($feed, 2);
+      $this->updateAndRemove($feed, 2);
+      // Delete feed.
+      $this->deleteFeed($feed);
+    }
   }
 }
 
@@ -459,7 +469,7 @@
         'fid' => $feed->fid,
       ))
       ->execute();
-    $this->updateFeedItems($feed);
+    $this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
     $this->getFeedCategories($feed);
     $this->assertTrue(!empty($feed->categories), t('The category found in the feed.'));
 
Index: modules/aggregator/aggregator_test.info
===================================================================
RCS file: modules/aggregator/aggregator_test.info
diff -N modules/aggregator/aggregator_test.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/aggregator/aggregator_test.info	28 Jan 2009 22:12:51 -0000
@@ -0,0 +1,7 @@
+name = "Aggregator module tests"
+description = "Support module for aggregator related testing."
+package = Testing
+version = VERSION
+core = 7.x
+files[] = aggregator_test.module
+hidden = TRUE
Index: modules/aggregator/aggregator_test.module
===================================================================
RCS file: modules/aggregator/aggregator_test.module
diff -N modules/aggregator/aggregator_test.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/aggregator/aggregator_test.module	28 Jan 2009 22:12:51 -0000
@@ -0,0 +1,58 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_menu().
+ */
+function aggregator_test_menu() {
+  $items['aggregator/test-feed'] = array(
+    'title' => 'Test feed static last modified date',
+    'description' => "A cached test feed with a static last modified date.",
+    'page callback' => 'aggregator_test_feed',
+    'access arguments' => array('access content'),
+  );
+  return $items;
+}
+
+/**
+ * Page callback. Generates a test feed and simulates last-modified and etags.
+ *
+ * @param $use_last_modified 
+ *   Set TRUE to send a last modified header.
+ * @param $use_etag
+ *   Set TRUE to send an etag.
+ */
+function aggregator_test_feed($use_last_modified = FALSE, $use_etag = FALSE) {
+  $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT');
+  $etag = md5($last_modified);
+
+  $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
+  $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
+
+  // Send appropriate response. We respond with a 304 not modified on either
+  // etag or on last modified.
+  if ($use_last_modified) {
+    drupal_set_header("Last-Modified: " . gmdate(DATE_RFC1123, $last_modified));
+  }
+  if ($use_etag) {
+    drupal_set_header("ETag: " .$etag);
+  }
+  // Return 304 not modified if either last modified or etag match.
+  if ($last_modified == $if_modified_since || $etag == $if_none_match) {
+    drupal_set_header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
+    return;
+  }
+
+  // The following headers force validation of cache:
+  drupal_set_header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
+  drupal_set_header("Cache-Control: must-revalidate");
+  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
+
+  // Read actual feed from file.
+  $file_name = DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator_test_rss091.xml';
+  $handle = fopen($file_name, 'r');
+  $feed = fread($handle, filesize($file_name));
+  fclose($handle);
+
+  print $feed;
+}
Index: modules/aggregator/aggregator_test_rss091.xml
===================================================================
RCS file: modules/aggregator/aggregator_test_rss091.xml
diff -N modules/aggregator/aggregator_test_rss091.xml
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/aggregator/aggregator_test_rss091.xml	28 Jan 2009 22:12:51 -0000
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<rss version="0.91">
+  <channel>
+    <title>Example</title>
+    <link>http://example.com</link>
+    <description>Example updates</description>
+    <language>en-us</language>
+    <copyright>Copyright 2000, Example team.</copyright>
+    <managingEditor>editor@example.com</managingEditor>
+    <webMaster>webmaster@example.com</webMaster>
+    <image>
+      <title>Example</title>
+      <url>http://example.com/images/druplicon.png</url>
+      <link>http://example.com</link>
+      <width>88</width>
+      <height>100</height>
+      <description>Example updates</description>
+    </image>
+    <item>
+      <title>Example turns one</title>
+      <link>http://example.com/example-turns-one</link>
+      <description>Example turns one.</description>
+    </item>
+    <item>
+      <title>Example turns two</title>
+      <link>http://example.com/example-turns-two</link>
+      <description>Example turns two.</description>
+    </item>
+  </channel>
+</rss>
\ No newline at end of file
