Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.7
diff -u -p -r1.7 common.test
--- modules/simpletest/tests/common.test	17 Sep 2008 07:01:31 -0000	1.7
+++ modules/simpletest/tests/common.test	20 Sep 2008 19:13:33 -0000
@@ -251,3 +251,286 @@ class DrupalSetContentTestCase extends D
     }
   }
 }
+
+/**
+ * Test for the format_date() function.
+ */
+class DrupalFormatDateTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Drupal date formatting'),
+      'description' => t("Performs tests on Drupal's date format."),
+      'group' => t('System'),
+    );
+  }
+
+  /**
+   * Implementation of setUp().
+   */
+  function setUp() {
+    // Create some arbitrary date formats other than the default values.
+    $this->date_formats = array(
+      'short' => 'm -- d -- Y ---- H/i',
+      'medium' => 'D, m d Y - H/i',
+      'long' => 'l, F_j, Y - H::i',
+    );
+
+    // Get the current time on the server.
+    $this->current_time = $_SERVER['REQUEST_TIME'];
+
+    // Define an array of possible date formats to cycle through later.
+    $this->date_format_types = array('small', 'large', 'medium');
+
+    parent::setUp();
+  }
+
+  /**
+   * Tests for the format_date() function.
+   */
+  function testFormatDate() {
+    // Get our 'real' user - it's used by format_date() directly.
+    global $user;
+
+    // Make sure we don't save the session as the fake user.
+    drupal_save_session(FALSE);
+
+    // Store the real user so that we can go back to being that user later.
+    $real_user = $user;
+
+    $test_user = $this->drupalCreateUser(array('administer site configuration'));
+    $this->drupalLogin($test_user);
+
+    // Make the test user the global $user so that tests use the local time
+    // zone settings.
+    $user = $test_user;
+
+    // Set the date and time settings to custom formats.
+    $edit = array();
+    $edit['date_format_short'] = 'custom';
+    $edit['date_format_medium'] = 'custom';
+    $edit['date_format_long'] = 'custom';
+    $edit['date_format_short_custom'] = $this->date_formats['short'];
+    $edit['date_format_medium_custom'] = $this->date_formats['medium'];
+    $edit['date_format_long_custom'] = $this->date_formats['long'];
+
+    $this->drupalPost('admin/settings/date-time', $edit, t('Save configuration'));
+    $this->refreshVariables();
+
+    // Set the local time zone of the test user to an arbitrary time zone.
+    $edit = array();
+    $edit['timezone'] = '-21600';
+    $this->drupalPost('user/'. $user->uid .'/edit', $edit, t('Save'));
+
+    // Reload the user so the user will have the update timezone.
+    $user = user_load($user->uid);
+
+    $this->assertFormatDate(-21600);
+
+    // Become another user who does not have a local time zone set.
+    $user = $this->drupalCreateUser(array('administer site configuration'));
+
+    // Remove user ability to configure time zone and use the system default.
+    $edit = array();
+    $edit['configurable_timezones'] = 0;
+    $edit['date_default_timezone'] = -36000;
+    $this->drupalPost('admin/settings/date-time', $edit, t('Save configuration'));
+    $this->refreshVariables();
+
+    $this->assertFormatDate(-36000);
+
+    // Restore the real user to power and allow session saving to continue.
+    $user = $real_user;
+    drupal_save_session(TRUE);
+  }
+
+  /**
+   * Helper function: asserts that format_date() creates the same formated
+   * string as gmdate() does.
+   */
+  function assertFormatDate($time_zone) {
+    // Get the current system time and add the time zone.
+    $localized_time = $this->current_time + $time_zone;
+
+    foreach ($this->date_format_types as $date_format) {
+      // Use gmdate() to create the date string based on the local time and
+      // corresponding formats. 
+      switch ($date_format) {
+        case 'small':
+          $date = gmdate($this->date_formats['short'], $localized_time);
+          break;
+        case 'large':
+          $date = gmdate($this->date_formats['long'], $localized_time);
+          break;
+        case 'medium':
+          $date = gmdate($this->date_formats['medium'], $localized_time);
+          break;
+      }
+
+      // Use format_date() to create the date string based on the server time,
+      // time zone set by drupal, and corresponding formats.
+      $drupal_date = format_date($this->current_time, $date_format);
+
+      // The return value of format_date() should have the same value as the 
+      // return value of gmdate().
+      $this->assertEqual($date, $drupal_date);
+    }
+  }
+}
+
+/**
+ * Test for setting a custom page not found page.
+ */
+class DrupalCustomPageNotFoundTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Custom page not found'),
+      'description' => t("Performs tests on Drupal's custom page not found setting."),
+      'group' => t('System'),
+    );
+  }
+
+  /**
+   * Tests custom page not found pages.
+   */
+  function testDrupalCustomPageNotFound() {
+    $user = $this->drupalCreateUser(array('administer site configuration'));
+    $this->drupalLogin($user);
+
+    // Create the custom 404 page.
+    $node = $this->drupalCreateNode();
+
+    // Set the drupal setting for the custom 404 page.
+    $edit = array();
+    $edit['site_404'] = 'node/'. $node->nid;
+    $this->drupalPost('admin/settings/error-reporting', $edit, t('Save configuration'));
+
+    // Log the admin user out.
+    $this->drupalLogout();
+
+    // Try to go to an arbitrary page that should respond in a 404 error.
+    $this->drupalGet('example_test_page_404');
+
+    // We should get our custom 404 page.
+    $this->assertResponse(404, t('Access denied code was received.'));
+    $this->assertText($node->title, t('The custom page not found title was displayed.'));
+    $this->assertText($node->body, t('The custom page not found body was displayed.'));
+  }
+}
+
+/**
+ * Test for setting a custom access denied page.
+ */
+class DrupalCustomAccessDeniedTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Custom access denied'),
+      'description' => t("Performs tests on Drupal's custom access denied page."),
+      'group' => t('System')
+    );
+  }
+
+  /**
+   * Tests custom access denied pages.
+   */
+  function testDrupalCustomAccessDenied() {
+    $user = $this->drupalCreateUser(array('administer site configuration'));
+    $this->drupalLogin($user);
+
+    // Create the custom 403 page.
+    $node = $this->drupalCreateNode();
+
+    // Set the drupal setting for the custom 403 page.
+    $edit = array('site_403' => 'node/'. $node->nid);
+    $this->drupalPost('admin/settings/error-reporting', $edit, t('Save configuration'));
+
+    // Log the admin user out.
+    $this->drupalLogout();
+
+    // Try to go to arbitrary page that should respond in a 403 error.
+    $this->drupalGet('node/add/page');
+
+    // We should get our custom 404 page.
+    $this->assertResponse(403, 'Access denied code.');
+    $this->assertText($node->title, t('The custom access denied title was displayed.'));
+    $this->assertText($node->body, t('The custom access denied body was displayed.'));
+  }
+}
+
+/**
+ * Test for the drupal_get_headers() function.
+ */
+class DrupalGetHeadersTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Drupal get headers'),
+      'description' => t("Performs tests on Drupal's get headers function."),
+      'group' => t('System'),
+    );
+  }
+
+  /**
+   * Tests the drupal_get_headers() function indirectly by calling a cached
+   * page with non-standard headers.
+   */
+  function testDrupalGetHeaders() {
+    $user = $this->drupalCreateUser(array('administer site configuration'));
+    $this->drupalLogin($user);
+
+    // Create a page so that the rss.xml will be generated.
+    $this->drupalCreateNode(array(
+      'promote' => 1
+    ));
+
+    // Turn page caching on because page caching uses drupal_get_headers() to
+    // send out the proper headers.
+    $edit = array();
+    $edit['cache'] = CACHE_NORMAL;
+    $this->drupalPost('admin/settings/performance', $edit, t('Save configuration'));
+    $this->refreshVariables();
+
+    // Log the admin user out, so that we can have cached pages
+    $this->drupalLogout();
+
+    // Clear out any already existing caches.
+    cache_clear_all();
+
+    $this->drupalGet('node');
+    $content_type = curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);
+    $this->drupalGet('node');
+    $cached_content_type = curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);
+
+    // Make sure that the node page content type header and the cached node
+    // page content type header are the same.
+    $this->assertEqual($content_type, $cached_content_type);
+
+    $this->drupalGet('rss.xml');
+    $content_type = curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);
+    $this->drupalGet('rss.xml');
+    $cached_content_type = curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);
+
+    // Make sure that the rss.xml page content type header and the cached
+    // rss.xml page content type header are the same.
+    $this->assertEqual($content_type, $cached_content_type);
+
+    $this->drupalGet('rss.xml');
+    $rss_content_type = curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);
+    $this->drupalGet('node');
+    $node_content_type = curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);
+
+    // Make sure that the cached node page content type header and the
+    // cached rss.xml page content type header are the same.
+    $this->assertNotEqual($rss_content_type, $node_content_type);
+  }
+}
\ No newline at end of file
