From 5384a9787bd640fda3e3dd1396cf23d03a7725da Mon Sep 17 00:00:00 2001 From: Vedant Rathore Date: Sat, 28 Jan 2017 13:43:41 +0530 Subject: [PATCH] Unit tests for issue CRM-19752 --- CRM/Contact/BAO/Query.php | 38 +++++++++++++++------ tests/phpunit/CRM/Contact/BAO/QueryTest.php | 23 +++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index d7e47b6520..9d687aac8f 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4895,17 +4895,10 @@ SELECT COUNT( conts.total_amount ) as total_count, if ($context == 'search') { $where .= " AND contact_a.is_deleted = 0 "; } - CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); - if (!empty($financialTypes)) { - $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL"; - $from .= " LEFT JOIN civicrm_line_item li - ON civicrm_contribution.id = li.contribution_id AND - li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") "; - } - else { - $where .= " AND civicrm_contribution.financial_type_id IN (0)"; - } + $query = $this->getSummaryQuery($where, $from); + $where = $query[0]; + $from = $query[1]; // make sure contribution is completed - CRM-4989 $completedWhere = $where . " AND civicrm_contribution.contribution_status_id = 1 "; @@ -5035,6 +5028,31 @@ SELECT COUNT( conts.total_amount ) as cancel_count, return $summary; } + /** + * Function for getting the SummaryQuery of the respective financial type + * + * @param $where + * @param $from + * + * @return array + */ + public function getSummaryQuery($where, $from) { + if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) { + $financialTypes = CRM_Contribute_PseudoConstant::financialType(); + } + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); + if (!empty($financialTypes)) { + $where .= " AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($financialTypes)) . ") AND li.id IS NULL"; + $from .= " LEFT JOIN civicrm_line_item li + ON civicrm_contribution.id = li.contribution_id AND + li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") "; + } + else { + $where .= " AND civicrm_contribution.financial_type_id IN (0)"; + } + return array($where, $from, $financialTypes); + } + /** * Getter for the qill object. * diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 8111339be4..0757ee8fc5 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -365,4 +365,27 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { $this->fail('Test failed for some reason which is not good'); } + public function testGetSummaryQueryWithFinancialACLDisabled() { + $where = NULL; + $from = NULL; + $CRM_Contact_BAO_Query = new CRM_Contact_BAO_Query(); + $query = $CRM_Contact_BAO_Query->getSummaryQuery($where, $from); + $this->assertEquals(" AND civicrm_contribution.financial_type_id IN (" . implode(',', array_keys($query[2])) . ") AND li.id IS NULL", $query[0]); + } + + public function testGetSummaryQueryWithFinancialACLEnabled() { + $where = NULL; + $from = NULL; + $cid = $this->createLoggedInUser(); + CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'CiviCRM: view contributions of type Donation'); + $CRM_Contact_BAO_Query = new CRM_Contact_BAO_Query(); + $query = $CRM_Contact_BAO_Query->getSummaryQuery($where, $from); + $from = $query[1]; + $financialTypes = $query[2]; + $this->assertEquals( + " LEFT JOIN civicrm_line_item li + ON civicrm_contribution.id = li.contribution_id AND + li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN (" . implode(',', array_keys($financialTypes)) . ") ", $from); + } + } -- 2.25.1