From: larssandergreen Date: Sun, 10 Sep 2023 00:48:39 +0000 (-0600) Subject: Don't include test, template, deleted entities in searchdisplay totals X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=01dd6e55f61dfaa89d371b50a1aa456ead67dc99;p=civicrm-core.git Don't include test, template, deleted entities in searchdisplay totals --- diff --git a/Civi/Api4/Generic/AbstractGetAction.php b/Civi/Api4/Generic/AbstractGetAction.php index 41b9c2d818..06af935336 100644 --- a/Civi/Api4/Generic/AbstractGetAction.php +++ b/Civi/Api4/Generic/AbstractGetAction.php @@ -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) { diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php index 9eadab8c5e..3a66af498d 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php @@ -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(); diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index ecb890792a..82f33532cc 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -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' => [