Skip to content

Commit

Permalink
Add Unit tests for the annual query.
Browse files Browse the repository at this point in the history
Note that in my experiments the test fails when more than one line item is in play -
for all the right reasons
  • Loading branch information
eileenmcnaughton committed Jan 15, 2019
1 parent f72d5f5 commit 236f183
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
43 changes: 43 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {

use CRMTraits_Financial_FinancialACLTrait;
use CRMTraits_Financial_PriceSetTrait;

/**
* Clean up after tests.
Expand Down Expand Up @@ -324,6 +325,22 @@ public function testAnnualQueryWithFinancialACLsEnabled() {
$this->disableFinancialACLs();
}

/**
* Test the annual query returns a correct result when multiple line items are present.
*/
public function testAnnualWithMultipleLineItems() {
$contactID = $this->createLoggedInUserWithFinancialACL();
$this->createContributionWithTwoLineItemsAgainstPriceSet([
'contact_id' => $contactID]
);
$this->enableFinancialACLs();
$sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([$contactID]);
$result = CRM_Core_DAO::executeQuery($sql);
$result->fetch();
$this->assertEquals(300, $result->amount);
$this->assertEquals(1, $result->count);
}

/**
* Test that financial type data is not added to the annual query if acls not enabled.
*/
Expand All @@ -337,6 +354,32 @@ public function testAnnualQueryWithFinancialACLsDisabled() {
CRM_Core_DAO::executeQuery($sql);
}

/**
* Test that financial type data is not added to the annual query if acls not enabled.
*/
public function testAnnualQueryWithFinancialHook() {
$this->hookClass->setHook('civicrm_selectWhereClause', array($this, 'aclIdNoZero'));
$sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
$this->assertContains('SUM(total_amount) as amount,', $sql);
$this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql);
$this->assertContains('b.id NOT IN (0)', $sql);
$this->assertNotContains('b.financial_type_id', $sql);
CRM_Core_DAO::executeQuery($sql);
}

/**
* Add ACL denying values LIKE '0'.
*
* @param string $entity
* @param string $clauses
*/
public function aclIdNoZero($entity, &$clauses) {
if ($entity != 'Contribution') {
return;
}
$clauses['id'] = "NOT IN (0)";
}

/**
* Display sort name during.
* Update multiple contributions
Expand Down
5 changes: 4 additions & 1 deletion tests/phpunit/CRMTraits/Financial/FinancialACLTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ protected function disableFinancialACLs() {
* @param array $aclPermissions
* Array of ACL permissions in the format
* [[$action, $financialType], [$action, $financialType])
*
* @return int Contact ID
*/
protected function createLoggedInUserWithFinancialACL($aclPermissions = [['view', 'Donation']]) {
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
$this->createLoggedInUser();
$contactID = $this->createLoggedInUser();
$this->addFinancialAclPermissions($aclPermissions);
return $contactID;
}

/**
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/CRMTraits/Financial/PriceSetTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Trait PriceSetTrait
*
* Trait for working with Price Sets in tests
*/
trait CRMTraits_Financial_PriceSetTrait {

/**
* Create a contribution with 2 line items.
*
* This also involves creating t
*
* @param $params
*/
protected function createContributionWithTwoLineItemsAgainstPriceSet($params) {
$params = array_merge(['total_amount' => 300, 'financial_type_id' => 'Donation'], $params);
$priceFields = $this->createPriceSet('contribution');
foreach ($priceFields['values'] as $key => $priceField) {
$params['line_items'][]['line_item'][$key] = [
'price_field_id' => $priceField['price_field_id'],
'price_field_value_id' => $priceField['id'],
'label' => $priceField['label'],
'field_title' => $priceField['label'],
'qty' => 1,
'unit_price' => $priceField['amount'],
'line_total' => $priceField['amount'],
'financial_type_id' => $priceField['financial_type_id'],
'entity_table' => 'civicrm_contribution',
];
}
$order = $this->callAPISuccess('order', 'create', $params);
}


}

0 comments on commit 236f183

Please sign in to comment.