Skip to content

Commit

Permalink
Merge pull request #19881 from eileenmcnaughton/task1
Browse files Browse the repository at this point in the history
[REF] Store the getSearchQueryResults so it can be re-accessed
  • Loading branch information
colemanw authored Mar 24, 2021
2 parents 01528ed + 787a5af commit 1214798
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
56 changes: 33 additions & 23 deletions CRM/Contribute/Form/Task/TaskTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
trait CRM_Contribute_Form_Task_TaskTrait {

/**
* Query result object.
*
* @var \CRM_Core_DAO
*/
protected $queryBAO;

/**
* Get the results from the BAO_Query object based search.
*
Expand All @@ -28,31 +35,34 @@ trait CRM_Contribute_Form_Task_TaskTrait {
* @throws \CRM_Core_Exception
*/
public function getSearchQueryResults(): CRM_Core_DAO {
$form = $this;
$queryParams = $this->getQueryParams();
$returnProperties = ['contribution_id' => 1];
$sortOrder = $sortCol = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
$sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
//Include sort column in select clause.
$sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
$returnProperties[$sortCol] = 1;
}
if (!$this->queryBAO) {
$form = $this;
$queryParams = $this->getQueryParams();
$returnProperties = ['contribution_id' => 1];
$sortOrder = $sortCol = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
$sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
//Include sort column in select clause.
$sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
$returnProperties[$sortCol] = 1;
}

$query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
CRM_Contact_BAO_Query::MODE_CONTRIBUTE
);
// @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
// can we remove? if not why not?
if ($this->isQueryIncludesSoftCredits()) {
$query->_rowCountClause = ' count(civicrm_contribution.id)';
$query->_groupByComponentClause = ' GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ';
}
else {
$query->_distinctComponentClause = ' civicrm_contribution.id';
$query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
$query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
CRM_Contact_BAO_Query::MODE_CONTRIBUTE
);
// @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
// can we remove? if not why not?
if ($this->isQueryIncludesSoftCredits()) {
$query->_rowCountClause = ' count(civicrm_contribution.id)';
$query->_groupByComponentClause = ' GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ';
}
else {
$query->_distinctComponentClause = ' civicrm_contribution.id';
$query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
}
$this->queryBAO = $query->searchQuery(0, 0, $sortOrder);
}
return $query->searchQuery(0, 0, $sortOrder);
return $this->queryBAO;
}

/**
Expand Down
17 changes: 11 additions & 6 deletions tests/phpunit/CRM/Contribute/Form/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@
*/

/**
* Class CRM_Contribute_Form_Tasktest
* Class CRM_Contribute_Form_TaskTest
*/
class CRM_Contribute_Form_TaskTest extends CiviUnitTestCase {

protected $_individualId;

/**
* Clean up after each test.
*
* @throws \CRM_Core_Exception
*/
public function tearDown() {
public function tearDown(): void {
$this->quickCleanUpFinancialEntities();
CRM_Utils_Hook::singleton()->reset();
parent::tearDown();
}

/**
* CRM-19722 - Check CRM_Contribute_Form_Task::preProcessCommon()
* executes without any error after sorting the search result.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testPreProcessCommonAfterSorting() {
public function testPreProcessCommonAfterSorting(): void {
$fields = [
'source' => 'contribution_source',
'status' => 'contribution_status',
'financialTypes' => 'financial_type',
];
$contributionIds = [];
$financialTypes = ['Member Dues', 'Event Fee', 'Donation'];
$status = ['Completed', 'Partially paid', 'Pending'];
$source = ['test source text', 'check source text', 'source text'];
Expand Down Expand Up @@ -65,9 +69,10 @@ public function testPreProcessCommonAfterSorting() {
$expectedValues[$fld] = $sortedFields;
}

// Assert contribIds are returned in a sorted order.
$form = $this->getFormObject('CRM_Contribute_Form_Task', ['radio_ts' => 'ts_all'], 'Search');
foreach ($fields as $val) {
// Assert contribIds are returned in a sorted order.
/* @var CRM_Contribute_Form_Task $form */
$form = $this->getFormObject('CRM_Contribute_Form_Task', ['radio_ts' => 'ts_all'], 'Search');
$form->set(CRM_Utils_Sort::SORT_ORDER, "`{$val}` asc");
CRM_Contribute_Form_Task::preProcessCommon($form);

Expand Down

0 comments on commit 1214798

Please sign in to comment.