From 9281d89e70612abc9bb2fc4a972d2526c845367a Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Sun, 14 Mar 2021 21:25:19 -0400 Subject: [PATCH] core#2461 - allow booleans to be of type boolean --- CRM/Utils/Rule.php | 4 ++++ tests/phpunit/CRM/Utils/RuleTest.php | 22 ++++++++++++++++++++++ tests/phpunit/CRM/Utils/TypeTest.php | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 9341e173f922..9c5707925cb1 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -625,6 +625,10 @@ public static function string($value, $maxLength = 0) { * @return bool */ public static function boolean($value) { + if ($value === TRUE || $value === FALSE) { + return TRUE; + } + // This is intentionally not using === comparison - but will fail on FALSE. return preg_match( '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value ) ? TRUE : FALSE; diff --git a/tests/phpunit/CRM/Utils/RuleTest.php b/tests/phpunit/CRM/Utils/RuleTest.php index 52b5253ce4cb..afa6a753cc22 100644 --- a/tests/phpunit/CRM/Utils/RuleTest.php +++ b/tests/phpunit/CRM/Utils/RuleTest.php @@ -79,6 +79,28 @@ public function numericDataProvider() { ]; } + /** + * @dataProvider booleanDataProvider + * @param $inputData + * @param $expectedResult + */ + public function testBoolean($inputData, $expectedResult) { + $this->assertEquals($expectedResult, CRM_Utils_Rule::boolean($inputData)); + } + + /** + * @return array + */ + public function booleanDataProvider() { + return [ + [TRUE, TRUE], + ['TRUE', TRUE], + [FALSE, TRUE], + ['false', TRUE], + ['banana', FALSE], + ]; + } + /** * @dataProvider moneyDataProvider * @param $inputData diff --git a/tests/phpunit/CRM/Utils/TypeTest.php b/tests/phpunit/CRM/Utils/TypeTest.php index 21a47fa35b33..48fd76ee4df7 100644 --- a/tests/phpunit/CRM/Utils/TypeTest.php +++ b/tests/phpunit/CRM/Utils/TypeTest.php @@ -131,6 +131,11 @@ public function escapeDataProvider() { ['field(contribution_status_id,4,5,6) asc, contact_id asc', 'MysqlOrderBy', 'field(`contribution_status_id`,4,5,6) asc, `contact_id` asc'], ['table.civicrm_column_name desc,other_column,another_column desc', 'MysqlOrderBy', '`table`.`civicrm_column_name` desc, `other_column`, `another_column` desc'], ['table.`Home-street_address` asc, `table-alias`.`Home-street_address` desc,`table-alias`.column', 'MysqlOrderBy', '`table`.`Home-street_address` asc, `table-alias`.`Home-street_address` desc, `table-alias`.`column`'], + [TRUE, 'Boolean', TRUE], + [FALSE, 'Boolean', FALSE], + ['TRUE', 'Boolean', 'TRUE'], + ['false', 'Boolean', 'false'], + ['banana', 'Boolean', NULL], ]; }