You are here

class DatePHP4LibTest in Date 5.2

Same name and namespace in other branches
  1. 6 tests/date_test.test \DatePHP4LibTest

Test Date Library

Hierarchy

Expanded class hierarchy of DatePHP4LibTest

File

tests/date_test.test, line 5

View source
class DatePHP4LibTest extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => t('PHP 4 date library'),
      'desc' => t('Test ability to create valid dates for years from 100 to 4000 with PHP 4 date library. Tests only provide meaningful results when run in PHP 4 environment.'),
      'group' => 'Date tests',
    );
  }
  function testPHP4DateLibrary() {
    require_once './' . drupal_get_path('module', 'date_php4') . '/date_php4.inc';
    require_once './' . drupal_get_path('module', 'date_php4') . '/date_php4_lib.inc';
    @set_time_limit(0);
    $fail = false;
    $t = date_mktime(0, 0, 0);
    $this
      ->assertEqual(date_date('Y-m-d', $t), date('Y-m-d', $t), 'Input date_mktime(0, 0, 0), date() and date_date() should match');
    $t = date_mktime(0, 0, 0, 6, 1, 2102);
    $this
      ->assertEqual(date_date('Y-m-d', $t), '2102-06-01', 'Input   date_mktime(0, 0, 0, 6, 1, 2102), result ' . date_date('Y-m-d', $t));
    $t = date_mktime(0, 0, 0, 2, 1, 2102);
    $this
      ->assertEqual(date_date('Y-m-d', $t), '2102-02-01', 'Input   date_mktime(0, 0, 0, 2, 1, 2102), result ' . date_date('Y-m-d', $t));
    $t = date_mktime(0, 0, 0, 10, 11, 1492);
    $this
      ->assertEqual(date_date('D Y-m-d', $t), 'Fri 1492-10-11', 'Columbus landing Fri 1492-10-11 returned ' . date_date('D Y-m-d', $t));
    $t = date_mktime(0, 0, 0, 2, 29, 1500);
    $this
      ->assertEqual(date_date('Y-m-d', $t), '1500-02-29', 'julian leap years 1500-02-29 returned ' . date_date('Y-m-d', $t));
    $t = date_mktime(0, 0, 0, 2, 29, 1700);
    $this
      ->assertEqual(date_date('Y-m-d', $t), '1700-03-01', 'gregorian leap years 1700-03-01 returned ' . date_date('Y-m-d', $t));
    $diff = date_mktime(0, 0, 0, 10, 15, 1582) - date_mktime(0, 0, 0, 10, 4, 1582);
    $this
      ->assertEqual($diff, 3600 * 24, 'gregorian correction = ' . $diff / 3600 / 24 . ' days');
    $this
      ->assertEqual(date_dow(15, 10, 1582), 5, "15 Oct 1582, dow should be 5, was " . date_dow(15, 10, 1582));
    $this
      ->assertEqual(date_dow(4, 10, 1582), 4, "4 Oct 1582, dow should be 4, was " . date_dow(4, 10, 1582));

    //Testing overflow;
    $errors = array();
    $t = date_mktime(0, 0, 0, 3, 33, 1965);
    if (date_date('Y-m-d', $t) != '1965-04-02') {
      $errors[] = 'day overflow 1 ' . date_date('Y-m-d', $t);
    }
    $t = date_mktime(0, 0, 0, 4, 33, 1971);
    if (date_date('Y-m-d', $t) != '1971-05-03') {
      $errors[] = 'day overflow 2 ' . date_date('Y-m-d', $t);
    }
    $t = date_mktime(0, 0, 0, 1, 60, 1965);
    if (date_date('Y-m-d', $t) != '1965-03-01') {
      $errors[] = 'day overflow 3 ' . date_date('Y-m-d', $t);
    }
    $t = date_mktime(0, 0, 0, 12, 32, 1965);
    if (date_date('Y-m-d', $t) != '1966-01-01') {
      $errors[] = 'day overflow 4 ' . date_date('Y-m-d', $t);
    }
    $t = date_mktime(0, 0, 0, 12, 63, 1965);
    if (date_date('Y-m-d', $t) != '1966-02-01') {
      $errors[] = 'day overflow 5 ' . date_date('Y-m-d', $t);
    }
    $t = date_mktime(0, 0, 0, 13, 3, 1965);
    if (date_date('Y-m-d', $t) != '1966-01-03') {
      $errors[] = 'mth overflow 1 ' . date_date('Y-m-d', $t);
    }
    $this
      ->assertEqual(sizeof($errors), 0, 'Testing day overflow to following month, ' . sizeof($errors) . ' failed');

    //dsm($errors);

    //Testing 2-digit => 4-digit year conversion;
    $errors = array();
    if (date_year_digit_check(00) != 2000) {
      $errors[] = "2-digit 2000";
    }
    if (date_year_digit_check(10) != 2010) {
      $errors[] = "2-digit 2010";
    }
    if (date_year_digit_check(20) != 2020) {
      $errors[] = "2-digit 2020";
    }
    if (date_year_digit_check(30) != 2030) {
      $errors[] = "2-digit 2030";
    }
    if (date_year_digit_check(40) != 1940) {
      $errors[] = "2-digit 1940";
    }
    if (date_year_digit_check(50) != 1950) {
      $errors[] = "2-digit 1950";
    }
    if (date_year_digit_check(90) != 1990) {
      $errors[] = "2-digit 1990";
    }
    $this
      ->assertEqual(sizeof($errors), 0, "Testing 2-digit to 4 digit year conversions, " . sizeof($errors) . ' failed');

    // Test string formating
    $fmt = '\\d\\a\\t\\e T Y-m-d H:i:s a A d D F g G h H i j l L m M n O \\R\\F\\C822 r s t U w y Y z Z 2003';
    $s1 = date($fmt, 0);
    $s2 = date_date($fmt, 0);
    $this
      ->assertEqual($s1, $s2, "Comparing all format strings between date() and date_date() for a zero timestamp");
    flush();

    // Test generation of dates outside 1901-2038
    $errors = array();
    $error = date_test_date(100, 1);
    if (!empty($error)) {
      $errors[] = $error;
    }
    for ($i = 100; --$i >= 0;) {
      $y1 = 100 + rand(0, 1970 - 100);
      $m = rand(1, 12);
      $error = date_test_date($y1, $m);
      if (!empty($error)) {
        $errors[] = $error;
      }
      $y1 = 3000 - rand(0, 3000 - 1970);
      $error = date_test_date($y1, $m);
      if (!empty($error)) {
        $errors[] = $error;
      }
    }
    $passed = 201 - sizeof($errors);
    $this
      ->assertEqual(sizeof($errors), 0, 'Tested 201 random dates outside 1901-2038 between 100 and 4000, ' . $passed . ' passed, ' . sizeof($errors) . ' failed');

    //dsm($errors);
    $start = 1960 + rand(0, 10);
    $yrs = 12;
    $i = 365.25 * 86400 * ($start - 1970);
    $offset = 36000 + rand(10000, 60000);
    $max = 365 * $yrs * 86400;
    $lastyear = 0;

    // we generate a timestamp, convert it to a date, and convert it back to a timestamp
    // and check if the roundtrip broke the original timestamp value.
    $cnt = 0;
    $fail_roundtrip = 0;
    for ($max += $i; $i < $max; $i += $offset) {
      $ret = date_gmdate('m,d,Y,H,i,s', $i);
      $arr = explode(',', $ret);
      if ($lastyear != $arr[2]) {
        $lastyear = $arr[2];
        flush();
      }
      $newi = date_mktime($arr[3], $arr[4], $arr[5], $arr[0], $arr[1], $arr[2], 'UTC');
      if ($i != $newi) {
        $fail_roundtrip = 1;
        $except_roundtrip[] = "{$i} (" . date_date('m,d,Y,H,i,s', $newi) . "), returned {$newi} ({$ret})" . date_date('m,d,Y,H,i,s', $newi);
      }
      $cnt += 1;
    }
    $passed = $cnt - sizeof($except_roundtrip);

    //print_r($except_roundtrip);
    $this
      ->assertEqual($fail_roundtrip, 0, "Tested " . number_format($cnt) . " random timestamps from " . $start . " to " . ($start + $yrs) . " for roundtrip from timestamp to date and back, " . number_format($passed) . " passed, " . number_format(sizeof($except_roundtrip)) . " failed");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatePHP4LibTest::get_info function
DatePHP4LibTest::testPHP4DateLibrary function
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
DrupalTestCase::tearDown function tearDown implementation, setting back switched modules etc 1