From fb93db643086f9ac39354915150c2cc66b4a95b3 Mon Sep 17 00:00:00 2001
From: Bradley M. Froehle <bfroehle@math.berkeley.edu>
Date: Tue, 4 Jan 2011 18:29:29 -0800
Subject: [PATCH] 5 - Begin work on new date/time unit tests.

---
 modules/system/system.test |  159 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 159 insertions(+), 0 deletions(-)

diff --git modules/system/system.test modules/system/system.test
index 549df44..fdac76b 100644
--- modules/system/system.test
+++ modules/system/system.test
@@ -923,6 +923,165 @@ class SiteMaintenanceTestCase extends DrupalWebTestCase {
 }
 
 /**
+ * Tests generic date format and date type functions and hooks
+ */
+class DateTimeUnitTest extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Date and time unit tests',
+      'description' => 'Performs tests on date format and date type functions and hooks.',
+      'group' => 'System',
+    );
+  }
+
+  /**
+   * Test that system_get_date_formats returns system-defined date formats.
+   */
+  function testSystemGetDateFormats() {
+    // Get a list of all known date formats. There are very many of these, we
+    // only check that a few exist and are provided in the proper format.
+    $formats = system_get_date_formats();
+
+    // @todo Remove these verbose calls once the test routine is finalized.
+    $this->verbose(print_r($formats, TRUE));
+    $this->verbose(print_r(system_get_date_formats('short'), TRUE));
+
+    // In testing each date format, we unset the date format ID since we cannot
+    // know this value in advance.
+    $short_format_1 = $formats['short']['Y-m-d H:i'];
+    unset($short_format_1['dfid']);
+    $expected_short_format_1 = array(
+      'format' => 'Y-m-d H:i',
+      'type' => 'short',
+      'locales' => array(),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $this->assertEqual($short_format_1, $expected_short_format_1, t('Information about the %type date format %format was provided.', array('%type' => 'short', '%format' => 'Y-m-d H:i')));
+
+    $short_format_2 = $formats['short']['m/d/Y - H:i'];
+    unset($short_format_2['dfid']);
+    $expected_short_format_2 = array(
+      'format' => 'm/d/Y - H:i',
+      'type' => 'short',
+      'locales' => array('en-us'),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $this->assertEqual($short_format_2, $expected_short_format_2, t('Information about the %type date format %format was provided.', array('%type' => 'short', '%format' => 'm/d/Y - H:i')));
+
+    // Now test medium formats.
+    $medium_format_1 = $formats['medium']['D, Y-m-d H:i'];
+    unset($medium_format_1['dfid']);
+    $expected_medium_format_1 = array(
+      'format' => 'D, Y-m-d H:i',
+      'type' => 'medium',
+      'locales' => array(),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $this->assertEqual($medium_format_1, $expected_medium_format_1, t('Information about the %type date format %format was provided.', array('%type' => 'medium', '%format' => 'D, Y-m-d H:i')));
+
+    $medium_format_2 = $formats['medium']['D, m/d/Y - H:i'];
+    unset($medium_format_2['dfid']);
+    $expected_medium_format_2 = array(
+      'format' => 'D, m/d/Y - H:i',
+      'type' => 'medium',
+      'locales' => array('en-us'),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $this->assertEqual($medium_format_2, $expected_medium_format_2, t('Information about the %type date format %format was provided.', array('%type' => 'medium', '%format' => 'D, m/d/Y - H:i')));
+
+    // Finally test long formats.
+    $long_format_1 = $formats['long']['l, F j, Y - H:i'];
+    unset($long_format_1['dfid']);
+    $expected_long_format_1 = array(
+      'format' => 'l, F j, Y - H:i',
+      'type' => 'long',
+      'locales' => array(),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $this->assertEqual($long_format_1, $expected_long_format_1, t('Information about the %type date format %format was provided.', array('%type' => 'long', '%format' => 'l, F j, Y - H:i')));
+
+    $long_format_2 = $formats['long']['l, j F, Y - H:i'];
+    unset($long_format_2['dfid']);
+    $expected_long_format_2 = array(
+      'format' => 'l, j F, Y - H:i',
+      'type' => 'long',
+      'locales' => array(),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $this->assertEqual($long_format_2, $expected_long_format_2, t('Information about the %type date format %format was provided.', array('%type' => 'long', '%format' => 'l, j F, Y - H:i')));
+
+    // Test that system_get_date_formats can be called with an argument.
+    $short_formats = system_get_date_formats('short');
+    $this->assertIdentical($formats['short'], $short_formats, t('The date formats associated with the %type date type are returned.', array('%type' => 'short')));
+
+    // Test that system_get_date_formats returns no information about date
+    // types which do not exist.
+    $does_not_exist_formats = system_get_date_formats('does_not_exist');
+    $this->assertFalse($does_not_exist_formats, t('No information was date format information provided for a non-existent date type.'));
+  }
+
+  /**
+   * Test that system_get_date_types returns system-defined date types.
+   */
+  function testSystemGetDateTypes() {
+    // Create the expected responses from system_get_date_types().
+    $expected_type_long = array(
+      'type' => 'long',
+      'title' => t('Long'),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $expected_type_medium = array(
+      'type' => 'medium',
+      'title' => t('Medium'),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+    $expected_type_short = array(
+      'type' => 'short',
+      'title' => t('Short'),
+      'locked' => TRUE,
+      'module' => 'system',
+      'is_new' => FALSE,
+    );
+
+    // Get a list of all date types, and verify the system-defined date types,
+    // are amongst those returned.
+    $types = system_get_date_types();
+
+    $this->assertEqual($types['long'], $expected_type_long, t('System provided %type date type found.', array('%type' => 'long')));
+    $this->assertEqual($types['medium'], $expected_type_medium, t('System provided %type date type found.', array('%type' => 'medium')));
+    $this->assertEqual($types['short'], $expected_type_short, t('System provided %type date type found.', array('%type' => 'short')));
+
+    // Get a specific date type and verify that the response is correct.
+    $type_long = system_get_date_types('long');
+    $this->assertEqual($type_long, $expected_type_long, t('System provided %type date type found.', array('%type' => 'long')));
+    $type_medium = system_get_date_types('medium');
+    $this->assertEqual($type_medium, $expected_type_medium, t('System provided %type date type found.', array('%type' => 'medium')));
+    $type_short = system_get_date_types('short');
+    $this->assertEqual($type_short, $expected_type_short, t('System provided %type date type found.', array('%type' => 'short')));
+
+    // This date type does not exist so the response should be FALSE.
+    $type_does_not_exist = system_get_date_types('does_not_exist');
+    $this->assertFalse($type_does_not_exist, t('No date type information provided for a non-existent date type.'));
+  }
+}
+
+/**
  * Tests generic date and time handling capabilities of Drupal.
  */
 class DateTimeFunctionalTest extends DrupalWebTestCase {
-- 
1.7.3.4

