dev/core#4773 - Fix APIv4 query regression
authorcolemanw <coleman@civicrm.org>
Mon, 13 Nov 2023 20:42:09 +0000 (15:42 -0500)
committercolemanw <coleman@civicrm.org>
Mon, 13 Nov 2023 20:42:09 +0000 (15:42 -0500)
Civi/Api4/Query/Api4SelectQuery.php
tests/phpunit/api/v4/Entity/ContributionTest.php [new file with mode: 0644]

index 44833e9c6ddf4851039b5cac5642dd22b38c0a2f..8887bc18348fee24e977d6d8037deee7d6d380cf 100644 (file)
@@ -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 (file)
index 0000000..04bb354
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+namespace api\v4\Entity;
+
+use api\v4\Api4TestBase;
+use Civi\Test\TransactionalInterface;
+
+/**
+ * @group headless
+ */
+class ContributionTest extends Api4TestBase implements TransactionalInterface {
+
+  public function testGetWithJoinOnFinancialType(): void {
+    $cid = $this->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']);
+  }
+
+}