[APIv4] Permit using other SQL functions such as CONCAT within a GROUP_CONCAT
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 22 Dec 2020 01:19:38 +0000 (12:19 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Tue, 22 Dec 2020 01:19:38 +0000 (12:19 +1100)
Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php
tests/phpunit/api/v4/Action/SqlFunctionTest.php

index b683f15a72362e68dc250a296a6f5200031afcb4..fb618e0ac7c02eab3b56e865a960847635007fcb 100644 (file)
@@ -24,7 +24,7 @@ class SqlFunctionGROUP_CONCAT extends SqlFunction {
     [
       'prefix' => ['', 'DISTINCT', 'ALL'],
       'expr' => 1,
-      'must_be' => ['SqlField'],
+      'must_be' => ['SqlField', 'sqlFunction'],
       'optional' => FALSE,
     ],
     [
index 0ff7f2cec13a2f4cef5c26ca77814a25a0266b70..27815be6c3b96aa76bf5e6daa84876792c7b7cbe 100644 (file)
@@ -76,6 +76,18 @@ class SqlFunctionTest extends UnitTestCase {
 
     $this->assertTrue(4 === $agg['count']);
     $this->assertContains('Donation', $agg['GROUP_CONCAT:financial_type_id:name']);
+
+    // Test GROUP_CONCAT with a CONCAT as well
+    $agg = Contribution::get(FALSE)
+      ->addGroupBy('contact_id')
+      ->addWhere('contact_id', '=', $cid)
+      ->addSelect("GROUP_CONCAT(CONCAT(financial_type_id, ', ', contact_id, ', ', total_amount))")
+      ->addSelect('COUNT(*) AS count')
+      ->execute()
+      ->first();
+
+    $this->assertTrue(4 === $agg['count']);
+    $this->assertContains('1, ' . $cid . ', 100.00', $agg['GROUP_CONCAT:financial_type_id_contact_id_total_amount']);
   }
 
   public function testGroupHaving() {