From 2e70c1a1ca0bd572ed698ccd0a03975543a40c13 Mon Sep 17 00:00:00 2001 From: colemanw Date: Mon, 13 Nov 2023 15:42:09 -0500 Subject: [PATCH] dev/core#4773 - Fix APIv4 query regression --- Civi/Api4/Query/Api4SelectQuery.php | 2 +- .../api/v4/Entity/ContributionTest.php | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/api/v4/Entity/ContributionTest.php diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index 44833e9c6d..8887bc1834 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -349,7 +349,7 @@ class Api4SelectQuery extends Api4Query { [$fieldExpr, $operator, $valueExpr, $isExpr] = array_pad((array) $condition, 4, NULL); if (in_array($operator, ['=', 'IN'], TRUE)) { // If flag is set then value must be parsed as an expression - if ($isExpr) { + if ($isExpr && is_string($valueExpr)) { $expr = SqlExpression::convert($valueExpr); $valueExpr = in_array($expr->getType(), ['SqlString', 'SqlNumber'], TRUE) ? $expr->getExpr() : NULL; } diff --git a/tests/phpunit/api/v4/Entity/ContributionTest.php b/tests/phpunit/api/v4/Entity/ContributionTest.php new file mode 100644 index 0000000000..04bb354d61 --- /dev/null +++ b/tests/phpunit/api/v4/Entity/ContributionTest.php @@ -0,0 +1,69 @@ +createTestRecord('Individual')['id']; + $fid = $this->createTestRecord('FinancialType')['id']; + $this->saveTestRecords('Contribution', [ + 'records' => [ + ['financial_type_id' => $fid], + ['financial_type_id' => $fid], + ['financial_type_id' => $fid], + ['financial_type_id' => 1], + ], + 'defaults' => [ + 'contact_id' => $cid, + ], + ]); + + $apiParams = [ + 'select' => [ + 'COUNT(id) AS COUNT_id', + 'GROUP_CONCAT(DISTINCT financial.name) AS financial_name', + 'SUM(net_amount) AS SUM_net_amount', + ], + 'where' => [ + ['contact_id', 'IN', [$cid]], + ], + 'groupBy' => [ + 'financial_type_id', + ], + 'join' => [ + [ + 'FinancialType AS financial', + 'INNER', + ['financial_type_id', '=', 'financial.id'], + ['financial.id', 'IN', [$fid]], + ], + ], + ]; + $result = civicrm_api4('Contribution', 'get', $apiParams); + $this->assertEquals(3, $result[0]['COUNT_id']); + } + +} -- 2.25.1