Don't include test, template, deleted entities in searchdisplay totals
authorlarssandergreen <lars@wildsight.ca>
Sun, 10 Sep 2023 00:48:39 +0000 (18:48 -0600)
committerlarssandergreen <lars@wildsight.ca>
Sun, 10 Sep 2023 00:48:39 +0000 (18:48 -0600)
Civi/Api4/Generic/AbstractGetAction.php
ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php
ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php

index 41b9c2d818cc8dc031609cd368492b41895d4d9f..06af9353362d0d495a11b45b53a87877e0d0ff7f 100644 (file)
@@ -39,7 +39,7 @@ abstract class AbstractGetAction extends AbstractQueryAction {
    *
    * @throws \CRM_Core_Exception
    */
-  protected function setDefaultWhereClause() {
+  public function setDefaultWhereClause() {
     if (!$this->_itemsToGet('id')) {
       $fields = $this->entityFields();
       foreach ($fields as $field) {
index 9eadab8c5e3d29e81dde443b129d291b82f49f15..3a66af498d7ef38f8f04f3dd5291d30c50bba643 100644 (file)
@@ -63,6 +63,7 @@ class Run extends AbstractRunAction {
       case 'tally':
         unset($apiParams['orderBy'], $apiParams['limit']);
         $api = Request::create($entityName, 'get', $apiParams);
+        $api->setDefaultWhereClause();
         $query = new Api4SelectQuery($api);
         $query->forceSelectId = FALSE;
         $sql = $query->getSql();
index ecb890792abe0c8e9d16ca174dc85bc84225bb63..82f33532cce5517a6d5932e0c71f34e1320981a4 100644 (file)
@@ -1697,6 +1697,48 @@ class SearchRunTest extends Api4TestBase implements TransactionalInterface {
     $this->assertEquals('$250.00', $result[3]['columns'][0]['val']);
   }
 
+  public function testContributionTotalCountWithTestAndTemplateContributions():void {
+    // Add a source here for the where below, as if we use id, we get the test and template contributions
+    $contributions = $this->saveTestRecords('Contribution', [
+      'records' => [
+        ['is_test' => TRUE, 'source' => 'TestTemplate'],
+        ['is_template' => TRUE, 'source' => 'TestTemplate'],
+        ['source' => 'TestTemplate'],
+      ],
+    ]);
+
+    $params = [
+      'checkPermissions' => FALSE,
+      'return' => 'page:1',
+      'savedSearch' => [
+        'api_entity' => 'Contribution',
+        'api_params' => [
+          'version' => 4,
+          'select' => ['id'],
+          'where' => [['source', '=', 'TestTemplate']],
+        ],
+      ],
+      'display' => [
+        'settings' => [
+          'columns' => [
+            [
+              'type' => 'field',
+              'key' => 'id',
+              'tally' => [
+                'fn' => 'COUNT',
+              ],
+            ],
+          ],
+        ],
+      ],
+    ];
+
+    $return = civicrm_api4('SearchDisplay', 'run', $params);
+    $params['return'] = 'tally';
+    $total = civicrm_api4('SearchDisplay', 'run', $params);
+    $this->assertEquals($return->rowCount, $total[0]['id']);
+  }
+
   public function testSelectEquations() {
     $activities = $this->saveTestRecords('Activity', [
       'records' => [