APIv4 - Fix passing '0' as a function argument
authorcolemanw <coleman@civicrm.org>
Sun, 11 Jun 2023 19:20:45 +0000 (15:20 -0400)
committercolemanw <coleman@civicrm.org>
Sun, 11 Jun 2023 19:20:45 +0000 (15:20 -0400)
Before: "CRM_Core_Exception : Too few arguments to param 3 for SQL function IF"

After: Function works with '0' as an argument.

Civi/Api4/Query/SqlExpression.php
tests/phpunit/api/v4/Action/SqlFunctionTest.php

index f4192477d39858d2aa138cc9b8ab8635850a9546..6fce01092c1d1ab1682de459a9039ee2af1ddcad 100644 (file)
@@ -224,7 +224,7 @@ abstract class SqlExpression {
   protected function captureExpressions(string &$arg, array $mustBe, int $max) {
     $captured = [];
     $arg = ltrim($arg);
-    while ($arg) {
+    while (strlen($arg)) {
       $item = $this->captureExpression($arg);
       $arg = ltrim(substr($arg, strlen($item)));
       $expr = self::convert($item, FALSE, $mustBe);
index 329c4fefc75e779e528350aa7686884ebcfcd91b..5f868900781dfd2ec7908b70792cb2e992c97868 100644 (file)
@@ -167,7 +167,7 @@ class SqlFunctionTest extends Api4TestBase implements TransactionalInterface {
 
     $result = Activity::get(FALSE)
       ->addWhere('id', 'IN', $aids)
-      ->addSelect('IF(is_deleted, "Trash", "No Trash") AS trashed')
+      ->addSelect('IF(is_deleted, 1, 0) AS trashed')
       ->addSelect('NULLIF(subject, location) AS nullif_subject_is_location')
       ->addSelect('NULLIF(duration, 456) AS nullif_duration_is_456')
       ->addSelect('COALESCE(duration, location) AS coalesce_duration_location')
@@ -179,9 +179,9 @@ class SqlFunctionTest extends Api4TestBase implements TransactionalInterface {
       ->execute()->indexBy('id');
 
     $this->assertCount(3, $result);
-    $this->assertEquals('No Trash', $result[$aids[0]]['trashed']);
-    $this->assertEquals('Trash', $result[$aids[1]]['trashed']);
-    $this->assertEquals('No Trash', $result[$aids[2]]['trashed']);
+    $this->assertEquals(0, $result[$aids[0]]['trashed']);
+    $this->assertEquals(1, $result[$aids[1]]['trashed']);
+    $this->assertEquals(0, $result[$aids[2]]['trashed']);
 
     $this->assertEquals(NULL, $result[$aids[0]]['nullif_subject_is_location']);
     $this->assertEquals('xyz', $result[$aids[1]]['nullif_subject_is_location']);