function DateTimePlusTest::testInvalidDates

Test invalid date handling.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Datetime/DateTimePlusTest.php, line 318
Definition of Drupal\system\Tests\Datetime\DateTimePlusTest.

Class

DateTimePlusTest

Namespace

Drupal\system\Tests\Datetime

Code

function testInvalidDates() {

  // Test for invalid month names when we are using a short version
  // of the month.
  $input = '23 abc 2012';
  $timezone = NULL;
  $format = 'd M Y';
  $date = new DateTimePlus($input, $timezone, $format);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "{$input} contains an invalid month name and produces errors.");

  // Test for invalid hour.
  $input = '0000-00-00T45:30:00';
  $timezone = NULL;
  $format = 'Y-m-d\\TH:i:s';
  $date = new DateTimePlus($input, $timezone, $format);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "{$input} contains an invalid hour and produces errors.");

  // Test for invalid day.
  $input = '0000-00-99T05:30:00';
  $timezone = NULL;
  $format = 'Y-m-d\\TH:i:s';
  $date = new DateTimePlus($input, $timezone, $format);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "{$input} contains an invalid day and produces errors.");

  // Test for invalid month.
  $input = '0000-75-00T15:30:00';
  $timezone = NULL;
  $format = 'Y-m-d\\TH:i:s';
  $date = new DateTimePlus($input, $timezone, $format);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "{$input} contains an invalid month and produces errors.");

  // Test for invalid year.
  $input = '11-08-01T15:30:00';
  $timezone = NULL;
  $format = 'Y-m-d\\TH:i:s';
  $date = new DateTimePlus($input, $timezone, $format);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "{$input} contains an invalid year and produces errors.");

  // Test for invalid year from date array. 10000 as a year will
  // create an exception error in the PHP DateTime object.
  $input = array(
    'year' => 10000,
    'month' => 7,
    'day' => 8,
    'hour' => 8,
    'minute' => 0,
    'second' => 0,
  );
  $timezone = 'America/Chicago';
  $date = new DateTimePlus($input, $timezone);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "array('year' => 10000, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0) contains an invalid year and produces errors.");

  // Test for invalid month from date array.
  $input = array(
    'year' => 2010,
    'month' => 27,
    'day' => 8,
    'hour' => 8,
    'minute' => 0,
    'second' => 0,
  );
  $timezone = 'America/Chicago';
  $date = new DateTimePlus($input, $timezone);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "array('year' => 2010, 'month' => 27, 'day' => 8, 'hour' => 8, 'minute' => 0, 'second' => 0) contains an invalid month and produces errors.");

  // Test for invalid hour from date array.
  $input = array(
    'year' => 2010,
    'month' => 2,
    'day' => 28,
    'hour' => 80,
    'minute' => 0,
    'second' => 0,
  );
  $timezone = 'America/Chicago';
  $date = new DateTimePlus($input, $timezone);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 80, 'minute' => 0, 'second' => 0) contains an invalid hour and produces errors.");

  // Test for invalid minute from date array.
  $input = array(
    'year' => 2010,
    'month' => 7,
    'day' => 8,
    'hour' => 8,
    'minute' => 88,
    'second' => 0,
  );
  $timezone = 'America/Chicago';
  $date = new DateTimePlus($input, $timezone);
  $this
    ->assertNotEqual(count($date
    ->getErrors()), 0, "array('year' => 2010, 'month' => 7, 'day' => 8, 'hour' => 8, 'minute' => 88, 'second' => 0) contains an invalid minute and produces errors.");
}