Skip to content

Commit

Permalink
Fixed issue #18401: Unable to use checkdate function if debug set (#3214
Browse files Browse the repository at this point in the history
)

Co-authored-by: Lapiu Dev <devgit@lapiu.biz>
  • Loading branch information
gabrieljenik and lapiudevgit committed Jun 13, 2023
1 parent de071e0 commit b80eb91
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
24 changes: 23 additions & 1 deletion application/helpers/expressions/em_core_helper.php
Expand Up @@ -202,7 +202,7 @@ function __construct()
'atan' => array('atan', 'Decimal.asNum.atan', gT('Arc tangent'), 'number atan(number)', 'http://php.net/atan', 1),
'atan2' => array('atan2', 'Decimal.asNum.atan2', gT('Arc tangent of two variables'), 'number atan2(number, number)', 'http://php.net/atan2', 2),
'ceil' => array('ceil', 'Decimal.asNum.ceil', gT('Round fractions up'), 'number ceil(number)', 'http://php.net/ceil', 1),
'checkdate' => array('checkdate', 'checkdate', gT('Returns true(1) if it is a valid date in gregorian calendar'), 'bool checkdate(month,day,year)', 'http://php.net/checkdate', 3),
'checkdate' => array('exprmgr_checkdate', 'checkdate', gT('Returns true(1) if it is a valid date in gregorian calendar'), 'bool checkdate(month,day,year)', 'http://php.net/checkdate', 3),
'cos' => array('cos', 'Decimal.asNum.cos', gT('Cosine'), 'number cos(number)', 'http://php.net/cos', 1),
'count' => array('exprmgr_count', 'LEMcount', gT('Count the number of answered questions in the list'), 'number count(arg1, arg2, ... argN)', '', -1),
'countif' => array('exprmgr_countif', 'LEMcountif', gT('Count the number of answered questions in the list equal the first argument'), 'number countif(matches, arg1, arg2, ... argN)', '', -2),
Expand Down Expand Up @@ -2884,6 +2884,28 @@ function exprmgr_sumifop($args)
return $result;
}

/**
* Validate a Gregorian date
* @see https://www.php.net/checkdate
* Check if all params are valid before send it to PHP checkdate to avoid PHP Warning
*
* @param mixed $month
* @param mixed $day
* @param mixed $year
* @return boolean
*/
function exprmgr_checkdate($month, $day, $year)
{
if (
(!ctype_digit($month) && !is_int($month))
|| (!ctype_digit($day) && !is_int($day))
|| (!ctype_digit($year) && !is_int($year))
) {
return false;
}
return checkdate(intval($month), intval($day), intval($year));
}

/**
* Find the closest matching Numerical input values in a list an replace it by the
* corresponding value within another list
Expand Down
78 changes: 78 additions & 0 deletions tests/unit/helpers/CheckDateWrapTest.php
@@ -0,0 +1,78 @@
<?php

namespace ls\tests;

class CheckDateWrapTest extends TestBaseClass
{
/**
* Testing that exprmgr_checkdate evaluates
* the month parameter correctly.
*
* The month parameter must be an integer value
* or an integer numeric string.
*/
public function testMonthCheck()
{
$check_string = exprmgr_checkdate('June', 07, 2023);
$this->assertFalse($check_string, 'Unexpected evaluation result, the month parameter should not be a string.');

$check_numeric = exprmgr_checkdate('06', 07, 2023);
$this->assertTrue($check_numeric, 'Unexpected evaluation result, the month parameter can be a numeric value.');

$check_int = exprmgr_checkdate(6, 07, 2023);
$this->assertTrue($check_int, 'Unexpected evaluation result, the month parameter can be an integer.');
}

/**
* Testing that exprmgr_checkdate evaluates
* the day parameter correctly.
*
* The day parameter must be an integer value
* or an integer numeric string.
*/
public function testDayCheck()
{
$check_string = exprmgr_checkdate(06, '7th.', 2023);
$this->assertFalse($check_string, 'Unexpected evaluation result, the day parameter should not be a string.');

$check_numeric = exprmgr_checkdate(06, '7', 2023);
$this->assertTrue($check_numeric, 'Unexpected evaluation result, the day parameter can be a numeric value.');

$check_int = exprmgr_checkdate(06, 07, 2023);
$this->assertTrue($check_int, 'Unexpected evaluation result, the day parameter can be an integer.');
}

/**
* Testing that exprmgr_checkdate evaluates
* the year parameter correctly.
*
* The year parameter must be an integer value
* or an integer numeric string.
*/
public function testYearCheck()
{
$check_string = exprmgr_checkdate(06, 07, 'Twenty Twenty-three');
$this->assertFalse($check_string, 'Unexpected evaluation result, the year parameter should not be a string.');

$check_numeric = exprmgr_checkdate(06, 07, '2023');
$this->assertTrue($check_numeric, 'Unexpected evaluation result, the year parameter can be a numeric value.');

$check_int = exprmgr_checkdate(06, 07, 2023);
$this->assertTrue($check_int, 'Unexpected evaluation result, the year parameter can be an integer.');
}

/**
* Testing the result of checkdate.
*/
public function testCheckdate()
{
$check_numercic = exprmgr_checkdate('06', '07', '2023');
$this->assertTrue($check_numercic, 'Unexpected evaluation result, all parameters are correct.');

$check_int = exprmgr_checkdate(06, 07, 2023);
$this->assertTrue($check_int, 'Unexpected evaluation result, all paramaters are correct.');

$check_wrong_order = exprmgr_checkdate(15, 07, 2023);
$this->assertFalse($check_wrong_order, 'Unexpected evaluation result, the parameter order is incorrect.');
}
}

0 comments on commit b80eb91

Please sign in to comment.