Skip to content

Commit

Permalink
Merge pull request #11554 from eileenmcnaughton/test_fin
Browse files Browse the repository at this point in the history
CRM-17647 test fixes to avoid calling Contribution::create without sk…
  • Loading branch information
eileenmcnaughton authored Jan 19, 2018
2 parents 95fe600 + 307dc80 commit 730ed80
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions tests/phpunit/CRM/Financial/BAO/FinancialItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,26 @@ public function tearDown() {
* Check method add()
*/
public function testAdd() {
$firstName = 'Shane';
$lastName = 'Whatson';
$params = array(
'first_name' => $firstName,
'last_name' => $lastName,
$params = [
'first_name' => 'Shane',
'last_name' => 'Whatson',
'contact_type' => 'Individual',
);
];

$contact = CRM_Contact_BAO_Contact::add($params);
$contact = $this->callAPISuccess('Contact', 'create', $params);

$price = 100;
$cParams = array(
'contact_id' => $contact->id,
$cParams = [
'contact_id' => $contact['id'],
'total_amount' => $price,
'financial_type_id' => 1,
'is_active' => 1,
'skipLineItem' => 1,
);
];

$defaults = array();
$contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
$contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
$lParams = array(
'entity_id' => $contribution->id,
'entity_id' => $contribution['id'],
'entity_table' => 'civicrm_contribution',
'price_field_id' => 1,
'qty' => 1,
Expand All @@ -81,7 +78,9 @@ public function testAdd() {
);

$lineItem = CRM_Price_BAO_LineItem::create($lParams);
CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
$contributionObj = $this->getContributionObject($contribution['id']);

CRM_Financial_BAO_FinancialItem::add($lineItem, $contributionObj);
$result = $this->assertDBNotNull(
'CRM_Financial_DAO_FinancialItem',
$lineItem->id,
Expand All @@ -96,28 +95,25 @@ public function testAdd() {
* Check method retrive()
*/
public function testRetrieve() {
$firstName = 'Shane';
$lastName = 'Whatson';
$params = array(
'first_name' => $firstName,
'last_name' => $lastName,
$params = [
'first_name' => 'Shane',
'last_name' => 'Whatson',
'contact_type' => 'Individual',
);
];

$contact = CRM_Contact_BAO_Contact::add($params);
$contact = $this->callAPISuccess('Contact', 'create', $params);
$price = 100.00;
$cParams = array(
'contact_id' => $contact->id,
$cParams = [
'contact_id' => $contact['id'],
'total_amount' => $price,
'financial_type_id' => 1,
'is_active' => 1,
'skipLineItem' => 1,
);
];

$defaults = array();
$contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
$contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
$lParams = array(
'entity_id' => $contribution->id,
'entity_id' => $contribution['id'],
'entity_table' => 'civicrm_contribution',
'price_field_id' => 1,
'qty' => 1,
Expand All @@ -128,8 +124,9 @@ public function testRetrieve() {
'financial_type_id' => 1,
);

$contributionObj = $this->getContributionObject($contribution['id']);
$lineItem = CRM_Price_BAO_LineItem::create($lParams);
CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
CRM_Financial_BAO_FinancialItem::add($lineItem, $contributionObj);
$values = array();
$fParams = array(
'entity_id' => $lineItem->id,
Expand All @@ -143,28 +140,25 @@ public function testRetrieve() {
* Check method create()
*/
public function testCreate() {
$firstName = 'Shane';
$lastName = 'Whatson';
$params = array(
'first_name' => $firstName,
'last_name' => $lastName,
$params = [
'first_name' => 'Shane',
'last_name' => 'Whatson',
'contact_type' => 'Individual',
);
];

$contact = CRM_Contact_BAO_Contact::add($params);
$contact = $this->callAPISuccess('Contact', 'create', $params);
$price = 100.00;
$cParams = array(
'contact_id' => $contact->id,
'contact_id' => $contact['id'],
'total_amount' => $price,
'financial_type_id' => 1,
'is_active' => 1,
'skipLineItem' => 1,
);

$defaults = array();
$contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
$contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
$lParams = array(
'entity_id' => $contribution->id,
'entity_id' => $contribution['id'],
'entity_table' => 'civicrm_contribution',
'price_field_id' => 1,
'qty' => 1,
Expand All @@ -177,7 +171,7 @@ public function testCreate() {

$lineItem = CRM_Price_BAO_LineItem::create($lParams);
$fParams = array(
'contact_id' => $contact->id,
'contact_id' => $contact['id'],
'description' => 'Contribution Amount',
'amount' => $price,
'financial_account_id' => 1,
Expand All @@ -190,7 +184,7 @@ public function testCreate() {
CRM_Financial_BAO_FinancialItem::create($fParams);
$entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
$entityTrxn->entity_table = 'civicrm_contribution';
$entityTrxn->entity_id = $contribution->id;
$entityTrxn->entity_id = $contribution['id'];
$entityTrxn->amount = $price;
if ($entityTrxn->find(TRUE)) {
$entityId = $entityTrxn->entity_id;
Expand Down Expand Up @@ -358,4 +352,18 @@ public function testGetPreviousFinancialItemHavingTax($thousandSeparator) {
$this->callAPISuccessGetSingle('FinancialItem', $params, $checkAgainst);
}

/**
* Get the contribution object.
*
* @param int $contributionID
*
* @return \CRM_Contribute_BAO_Contribution
*/
protected function getContributionObject($contributionID) {
$contributionObj = new CRM_Contribute_BAO_Contribution();
$contributionObj->id = $contributionID;
$contributionObj->find(TRUE);
return $contributionObj;
}

}

0 comments on commit 730ed80

Please sign in to comment.