Unverified Commit 22f99645 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3241299 Properly return FALSE for invalid strings in...

fix: #3241299 Properly return FALSE for invalid strings in DateTimePlus::checkArray() so that the correct exception is thrown

By: hayashi
By: smustgrave
By: akram khan
By: nitin shrivastava
By: dcam
By: xjm
parent acaf0b1c
Loading
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -568,14 +568,20 @@ public static function prepareArray($array, $force_valid_date = FALSE) {
   *   TRUE if the datetime parts contain valid values, otherwise FALSE.
   */
  public static function checkArray($array) {
    $valid_date = FALSE;
    $valid_time = TRUE;
    // Check for a valid date using checkdate(). Only values that
    // meet that test are valid. An empty value, either a string or a 0, is not
    // a valid value.
    if (!empty($array['year']) && !empty($array['month']) && !empty($array['day'])) {
      $valid_date = checkdate($array['month'], $array['day'], $array['year']);
    foreach (['year', 'month', 'day'] as $key) {
      if (
        empty($array[$key])
        || filter_var($array[$key], FILTER_VALIDATE_INT) === FALSE
      ) {
        return FALSE;
      }
    }
    $valid_date = checkdate($array['month'], $array['day'], $array['year']);

    // Testing for valid time is reversed. Missing time is OK,
    // but incorrect values are not.
    foreach (['hour', 'minute', 'second'] as $key) {
+12 −0
Original line number Diff line number Diff line
@@ -631,6 +631,18 @@ public static function providerTestCheckArray(): array {
      ],
      'Missing day' => [['year' => 2059, 'month' => 1, 'second' => 1], FALSE],
      'Zero day' => [['year' => 2059, 'month' => 1, 'day' => 0], FALSE],
      'Non-numeric year' => [
        ['year' => 'invalid', 'month' => 11, 'day' => 2],
        FALSE,
      ],
      'Non-numeric month' => [
        ['year' => 2025, 'month' => 'invalid', 'day' => 2],
        FALSE,
      ],
      'Non-numeric day' => [
        ['year' => 2025, 'month' => 11, 'day' => 'invalid'],
        FALSE,
      ],
    ];
  }