You are here

class DateApiUnitTestCase in Date 7.2

Same name and namespace in other branches
  1. 7.3 date_api/tests/DateApiUnitTestCase.test \DateApiUnitTestCase

Unit tests for Date API functions.

Hierarchy

Expanded class hierarchy of DateApiUnitTestCase

File

date_api/tests/DateApiUnitTestCase.test, line 11
Unit tests for Date API functions.

View source
class DateApiUnitTestCase extends DrupalUnitTestCase {

  /**
   * Date API unit tests.
   */
  public static function getInfo() {
    return array(
      'name' => t('Date API unit tests'),
      'description' => t('Unit test coverage for Date API functions.'),
      'group' => t('Date'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    drupal_load('module', 'date_api');
    parent::setUp();
  }

  /**
   * Tests for date_is_all_day().
   */
  public function testDateIsAllDay() {

    // Two empty strings, this should always return FALSE.
    $string1 = '';
    $string2 = '';
    $response = date_is_all_day($string1, $string2);
    $this
      ->assertFalse($response, 'Two empty strings cannot represent an all-day date pair.');

    // Two random date strings do not make an all-day pair.
    $string1 = '2021-02-25 01:01:01';
    $string2 = '2021-02-25 23:00:00';
    $response = date_is_all_day($string1, $string2);
    $this
      ->assertFalse($response, 'Two random date strings do not make an "all day" pair.');

    // It is not a valid "all day" date pair to start one second after midnight
    // and end on midnight the next day.
    $string1 = '2021-02-25 00:00:01';
    $string2 = '2021-02-26 00:00:00';
    $response = date_is_all_day($string1, $string2);
    $this
      ->assertFalse($response, 'Does not mark dates as "all day" if the first is 1 second after midnight on day 1 and the other is midnight on day 2.');

    // A valid "all day" date pair when the granularity is 1, regardless of the
    // unit.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:59:59';

    // The granularity option must be one of a limited set of strings.
    $response = date_is_all_day($string1, $string2, 'mango');
    $this
      ->assertFalse($response, 'The granularity argument must be one of a limited set of strings, so "mango" will not work.');

    // A valid "all day" date pair starts at midnight and ends one second before
    // midnight, both on the same day.
    $response = date_is_all_day($string1, $string2);
    $this
      ->assertTrue($response, 'An "all day" date pair must start at midnight and end one time unit before midnight of the next day.');

    // Confirm the granularity option works with seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to "second".');

    // Confirm the granularity option works with minutes.
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to "minute".');

    // Confirm the granularity option works with hours.
    $response = date_is_all_day($string1, $string2, 'hour');
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to "hour".');

    // Test the "increment" argument with a unit of 1.
    $response = date_is_all_day($string1, $string2, 'second', 1);
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to 1 "second".');
    $response = date_is_all_day($string1, $string2, 'minute', 1);
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to 1 "minute".');
    $response = date_is_all_day($string1, $string2, 'hour', 1);
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to 1 "hour".');

    // Test the "increment" argument with a unit of 15.
    $response = date_is_all_day($string1, $string2, 'second', 15);
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to 15 "second".');
    $response = date_is_all_day($string1, $string2, 'minute', 15);
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to 15 "minute".');
    $response = date_is_all_day($string1, $string2, 'hour', 15);
    $this
      ->assertTrue($response, 'End time is 23:59:59 and granularity is set to 15 "hour".');

    // A datetime pair ending at 23:59:00.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:59:00';

    // This pair will only be valid when the granularity is "minute" or "hour".
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, 'Not valid with an end time of 23:59:00 and granularity is set to "second".');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertTrue($response, '23:59:00 is a valid end time if the granularity is set to "minute".');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertTrue($response, '23:59:00 is a valid end time if the granularity is set to "hour".');

    // A datetime pair ending at 23:59:24. This is to confirm that segments of
    // the time, after the granularity option, are ignored.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:59:24';

    // This pair will only be valid when the granularity is "minute" or "hour".
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, 'Not valid with an end time of 23:59:24 and granularity is set to "second".');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertTrue($response, '23:59:24 is a valid end time if the granularity is set to "minute".');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertTrue($response, '23:59:24 is a valid end time if the granularity is set to "hour".');

    // A datetime pair ending at 23:00:00.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:00:00';

    // This pair will only be valid when the granularity is "minute" or "hour".
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, 'Not valid with an end time of 23:00:00 and granularity is set to "second".');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertFalse($response, 'Not valid with an end time of 23:00:00 and granularity is set to "minute".');
    $response = date_is_all_day($string1, $string2, 'hour');
    $this
      ->assertTrue($response, '23:00:00 is a valid end time if the granularity is set to "hour".');

    // A datetime pair ending at 23:13:00.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:13:00';

    // This pair will only be valid when the granularity is "minute" or "hour".
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, 'Not valid with an end time of 23:13:00 and granularity is set to "second".');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertFalse($response, 'Not valid with an end time of 23:13:00 and granularity is set to "minute".');
    $response = date_is_all_day($string1, $string2, 'hour');
    $this
      ->assertTrue($response, '23:00:00 is a valid end time if the granularity is set to "hour".');

    // A datetime pair ending at 23:59:55.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:59:55';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:59:55 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 5);
    $this
      ->assertTrue($response, '23:59:55 is a valid end time when the increment is set to 5 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute', 5);
    $this
      ->assertTrue($response, '23:59:55 is a valid end time when the increment is set to 5 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 5);
    $this
      ->assertTrue($response, '23:59:55 is a valid end time when the increment is set to 5 hours.');

    // A datetime pair ending at 23:55:00.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:55:00';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:55:00 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 5);
    $this
      ->assertFalse($response, '23:55:00 is not a valid end time when the increment is set to 5 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertFalse($response, '23:55:00 is not a valid end time when the increment is set to 1 minute.');
    $response = date_is_all_day($string1, $string2, 'minute', 5);
    $this
      ->assertTrue($response, '23:55:00 is a valid end time when the increment is set to 5 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 5);
    $this
      ->assertTrue($response, '23:55:00 is a valid end time when the increment is set to 5 hours.');

    // A datetime pair ending at 23:59:50.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:59:50';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:59:50 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 10);
    $this
      ->assertTrue($response, '23:59:50 is a valid end time when the increment is set to 10 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute', 10);
    $this
      ->assertTrue($response, '23:59:50 is a valid end time when the increment is set to 10 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 10);
    $this
      ->assertTrue($response, '23:59:50 is a valid end time when the increment is set to 10 hours.');

    // A datetime pair ending at 23:50:00.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:50:00';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:50:00 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 10);
    $this
      ->assertFalse($response, '23:50:00 is not a valid end time when the increment is set to 10 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertFalse($response, '23:50:00 is not a valid end time when the increment is set to 1 minute.');
    $response = date_is_all_day($string1, $string2, 'minute', 10);
    $this
      ->assertTrue($response, '23:50:00 is a valid end time when the increment is set to 10 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 10);
    $this
      ->assertTrue($response, '23:50:00 is a valid end time when the increment is set to 10 hours.');

    // A datetime pair ending at 23:59:45.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:59:45';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:59:45 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 15);
    $this
      ->assertTrue($response, '23:59:45 is a valid end time when the increment is set to 15 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute', 15);
    $this
      ->assertTrue($response, '23:59:45 is a valid end time when the increment is set to 15 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 15);
    $this
      ->assertTrue($response, '23:59:45 is a valid end time when the increment is set to 15 hours.');

    // A datetime pair ending at 23:45:00.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:45:00';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:45:00 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 15);
    $this
      ->assertFalse($response, '23:45:00 is not a valid end time when the increment is set to 15 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertFalse($response, '23:45:00 is not a valid end time when the increment is set to 1 minute.');
    $response = date_is_all_day($string1, $string2, 'minute', 15);
    $this
      ->assertTrue($response, '23:45:00 is a valid end time when the increment is set to 15 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 15);
    $this
      ->assertTrue($response, '23:45:00 is a valid end time when the increment is set to 15 hours.');

    // A datetime pair ending at 23:59:30.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:59:30';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:59:30 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 30);
    $this
      ->assertTrue($response, '23:59:30 is a valid end time when the increment is set to 30 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute', 30);
    $this
      ->assertTrue($response, '23:59:30 is a valid end time when the increment is set to 30 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 30);
    $this
      ->assertTrue($response, '23:59:30 is a valid end time when the increment is set to 30 hours.');

    // A datetime pair ending at 23:30:00.
    $string1 = '2021-02-25 00:00:00';
    $string2 = '2021-02-25 23:30:00';

    // Test the "increment" argument with 5 seconds.
    $response = date_is_all_day($string1, $string2, 'second');
    $this
      ->assertFalse($response, '23:30:00 is not a valid end time when the increment is set to 1 second.');
    $response = date_is_all_day($string1, $string2, 'second', 30);
    $this
      ->assertFalse($response, '23:30:00 is not a valid end time when the increment is set to 30 seconds.');
    $response = date_is_all_day($string1, $string2, 'minute');
    $this
      ->assertFalse($response, '23:30:00 is not a valid end time when the increment is set to 1 minute.');
    $response = date_is_all_day($string1, $string2, 'minute', 30);
    $this
      ->assertTrue($response, '23:30:00 is a valid end time when the increment is set to 30 minutes.');
    $response = date_is_all_day($string1, $string2, 'hour', 30);
    $this
      ->assertTrue($response, '23:30:00 is a valid end time when the increment is set to 30 hours.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateApiUnitTestCase::getInfo public static function Date API unit tests.
DateApiUnitTestCase::setUp public function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
DateApiUnitTestCase::testDateIsAllDay public function Tests for date_is_all_day().
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct