SearchKit - Fix php error when doing math equations
authorColeman Watts <coleman@civicrm.org>
Thu, 27 Oct 2022 02:03:56 +0000 (22:03 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 27 Oct 2022 02:16:03 +0000 (22:16 -0400)
Fixes dev/core#3938

Civi/Api4/Query/SqlEquation.php
ext/search_kit/Civi/Api4/Action/SearchDisplay/GetDefault.php
ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php

index 0b2f22334fc7721b501104f96757b2754f8e14e3..dec56e5e148bf0aadfa39050b461fc88eefb75a4 100644 (file)
@@ -61,6 +61,17 @@ class SqlEquation extends SqlExpression {
     }
   }
 
+  /**
+   * Get the arguments and operators passed to this sql expression.
+   *
+   * For each item in the returned array, if it's an array, it's a value; if it's a string, it's an operator.
+   *
+   * @return array
+   */
+  public function getArgs(): array {
+    return $this->args;
+  }
+
   /**
    * Render the expression for insertion into the sql query
    *
index 937041bd19ef88e50b23bd161791ac5826ea91bf..61546674a7add0d651ebb94dbfb8c89fc84271d5 100644 (file)
@@ -149,7 +149,9 @@ class GetDefault extends \Civi\Api4\Generic\AbstractAction {
     if ($expr instanceof SqlEquation) {
       $args = [];
       foreach ($expr->getArgs() as $arg) {
-        $args[] = $this->getColumnLabel($arg['expr']);
+        if (is_array($arg) && !empty($arg['expr'])) {
+          $args[] = $this->getColumnLabel(SqlExpression::convert($arg['expr']));
+        }
       }
       return '(' . implode(',', array_filter($args)) . ')';
     }
index 18e5a49a8b21944a8732f47f1baf88bdcee33717..50556cafac83f6b3555e127ee4b51d31eb38c8dd 100644 (file)
@@ -1421,4 +1421,35 @@ class SearchRunTest extends Api4TestBase implements TransactionalInterface {
     $this->assertEquals('¥500', $result[2]['columns'][0]['val']);
   }
 
+  public function testSelectEquations() {
+    $activities = $this->saveTestRecords('Activity', [
+      'records' => [
+        ['duration' => 60],
+        ['duration' => 120],
+        ['duration' => 180],
+      ],
+    ]);
+    $params = [
+      'checkPermissions' => FALSE,
+      'return' => 'page:1',
+      'savedSearch' => [
+        'api_entity' => 'Activity',
+        'api_params' => [
+          'version' => 4,
+          'select' => ['id', '(duration / 60)'],
+          'where' => [['id', 'IN', $activities->column('id')]],
+        ],
+      ],
+      'display' => NULL,
+      'sort' => [['id', 'ASC']],
+    ];
+
+    $result = civicrm_api4('SearchDisplay', 'run', $params);
+    $this->assertCount(3, $result);
+
+    $this->assertEquals(1, $result[0]['columns'][1]['val']);
+    $this->assertEquals(2, $result[1]['columns'][1]['val']);
+    $this->assertEquals(3, $result[2]['columns'][1]['val']);
+  }
+
 }