From 2449fe694ca26d2e5e5d200e15832f571e47f811 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 28 Apr 2016 08:33:51 +1200 Subject: [PATCH] More thorough cleanup on BAO_ContributionTest class. Use a teardown function for cleanup, remove all require onces, remove unrequired $ids params --- CRM/Contribute/BAO/Contribution.php | 7 +- .../CRM/Contribute/BAO/ContributionTest.php | 149 ++++++------------ 2 files changed, 52 insertions(+), 104 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index f65c46d050..93581b5130 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1207,7 +1207,12 @@ GROUP BY p.id } /** - * Get list of contribution In Honor of contact Ids. + * Get list of contributions which credit the passed in contact ID. + * + * The returned array provides details about the original contribution & donor. + * + * @todo - this is a confusing function called from one place. It has a test. It would be + * nice to deprecate it. * * @param int $honorId * In Honor of Contact ID. diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index b5f030aa0e..41315f6c36 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -25,9 +25,6 @@ +--------------------------------------------------------------------+ */ -require_once 'CiviTest/Contact.php'; -require_once 'CiviTest/Custom.php'; - /** * Class CRM_Contribute_BAO_ContributionTest * @group headless @@ -35,11 +32,19 @@ require_once 'CiviTest/Custom.php'; class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { /** - * Create() method (create and update modes). + * Clean up after tests. + */ + public function tearDown() { + $this->quickCleanUpFinancialEntities(); + $this->quickCleanUpFinancialEntities(array('civicrm_event')); + parent::tearDown(); + } + + /** + * Test create method (create and update modes). */ public function testCreate() { - $contactId = Contact::createIndividual(); - $ids = array('contribution' => NULL); + $contactId = $this->individualCreate(); $params = array( 'contact_id' => $contactId, @@ -59,9 +64,9 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'thankyou_date' => '20080522', ); - $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($params); - $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); + $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transaction id creation.'); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); //update contribution amount @@ -73,29 +78,25 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .'); $this->assertEquals($params['net_amount'], $contribution->net_amount, 'Check for Amount updation.'); - - //Delete Contribution - $this->contributionDelete($contribution->id); - - //Delete Contact - $this->contactDelete($contactId); } /** * Create() method with custom data. */ public function testCreateWithCustomData() { - $contactId = Contact::createIndividual(); - $ids = array('contribution' => NULL); + $contactId = $this->individualCreate(); //create custom data - $customGroup = Custom::createGroup(array(), 'Contribution'); + $customGroup = $this->customGroupCreate(array('extends' => 'Contribution')); + $customGroupID = $customGroup['id']; + $customGroup = $customGroup['values'][$customGroupID]; + $fields = array( 'label' => 'testFld', 'data_type' => 'String', 'html_type' => 'Text', 'is_active' => 1, - 'custom_group_id' => $customGroup->id, + 'custom_group_id' => $customGroupID, ); $customField = CRM_Core_BAO_CustomField::create($fields); @@ -124,15 +125,15 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'value' => 'Test custom value', 'type' => 'String', 'custom_field_id' => $customField->id, - 'custom_group_id' => $customGroup->id, - 'table_name' => $customGroup->table_name, + 'custom_group_id' => $customGroupID, + 'table_name' => $customGroup['table_name'], 'column_name' => $customField->column_name, 'file_id' => NULL, ), ), ); - $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($params); // Check that the custom field value is saved $customValueParams = array( @@ -144,19 +145,13 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.'); - - $this->contributionDelete($contribution->id); - Custom::deleteField($customField); - Custom::deleteGroup($customGroup); - $this->contactDelete($contactId); } /** * DeleteContribution() method */ public function testDeleteContribution() { - $contactId = Contact::createIndividual(); - $ids = array('contribution' => NULL); + $contactId = $this->individualCreate(); $params = array( 'contact_id' => $contactId, @@ -177,23 +172,22 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'thankyou_date' => '20080522', ); - $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($params); $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); - $contributiondelete = CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id); + CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id); $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id, 'id', 'trxn_id', 'Database check for deleted Contribution.' ); - $this->contactDelete($contactId); } /** - * Create honor-contact method + * Create honor-contact method. */ - public function testcreateAndGetHonorContact() { + public function testCreateAndGetHonorContact() { $firstName = 'John_' . substr(sha1(rand()), 0, 7); $lastName = 'Smith_' . substr(sha1(rand()), 0, 7); $email = "{$firstName}.{$lastName}@example.com"; @@ -223,10 +217,8 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { ); //create contribution on behalf of honary. - $contactId = Contact::createIndividual(); - $softParam['contact_id'] = $honoreeContactId; + $contactId = $this->individualCreate(array('first_name' => 'John', 'last_name' => 'Doe')); - $ids = array('contribution' => NULL); $param = array( 'contact_id' => $contactId, 'currency' => 'USD', @@ -236,8 +228,9 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'total_amount' => 66, ); - $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($param); $id = $contribution->id; + $softParam['contact_id'] = $honoreeContactId; $softParam['contribution_id'] = $id; $softParam['currency'] = $contribution->currency; $softParam['amount'] = $contribution->total_amount; @@ -253,8 +246,8 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $this->assertEquals(array( $id => array( 'honor_type' => 'In Honor of', - 'honorId' => $id, - 'display_name' => 'John Doe', + 'honorId' => $contactId, + 'display_name' => 'Mr. John Doe II', 'type' => 'Event Fee', 'type_id' => '4', 'amount' => '$ 66.00', @@ -276,15 +269,6 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount', 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution' ); - - //Delete honor contact - $this->contactDelete($honoreeContactId); - - //Delete Contribution record - $this->contributionDelete($contribution->id); - - //Delete contributor contact - $this->contactDelete($contactId); } /** @@ -336,11 +320,6 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id); $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.'); - - //Delete Contribution - $this->contributionDelete($contribution->id); - //Delete Contact - $this->contactDelete($contactId); } /** @@ -349,7 +328,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { * AddPremium(); */ public function testAddPremium() { - $contactId = Contact::createIndividual(); + $contactId = $this->individualCreate(); $ids = array( 'premium' => NULL, @@ -410,11 +389,6 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name, 'id', 'name', 'Database check for deleted Product.' ); - - //Delete Contribution - $this->contributionDelete($contribution->id); - //Delete Contact - $this->contactDelete($contactId); } /** @@ -423,9 +397,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { * checkDuplicateIds(); */ public function testcheckDuplicateIds() { - $contactId = Contact::createIndividual(); - - $ids = array('contribution' => NULL); + $contactId = $this->individualCreate(); $param = array( 'contact_id' => $contactId, @@ -446,7 +418,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'thankyou_date' => '20080522', ); - $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($param); $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); @@ -457,11 +429,6 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { ); $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data); $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .'); - - // Delete Contribution - $this->contributionDelete($contribution->id); - // Delete Contact - $this->contactDelete($contactId); } /** @@ -470,9 +437,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { * createCreditNoteId(); */ public function testCreateCreditNoteId() { - $contactId = Contact::createIndividual(); - - $ids = array('contribution' => NULL); + $contactId = $this->individualCreate(); $param = array( 'contact_id' => $contactId, @@ -494,22 +459,16 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { ); $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId(); - $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($param); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); $this->assertEquals($creditNoteId, $contribution->creditnote_id, 'Check if credit note id is created correctly.'); - - // Delete Contribution - $this->contributionDelete($contribution->id); - // Delete Contact - $this->contactDelete($contactId); } /** * Create() method (create and update modes). */ public function testIsPaymentFlag() { - $contactId = Contact::createIndividual(); - $ids = array('contribution' => NULL); + $contactId = $this->individualCreate(); $params = array( 'contact_id' => $contactId, @@ -529,7 +488,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'thankyou_date' => '20080522', ); - $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($params); $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); @@ -559,20 +518,13 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $trxnArray['is_payment'] = 0; $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.'); - - //Delete Contribution - $this->contributionDelete($contribution->id); - - //Delete Contact - $this->contactDelete($contactId); } /** * Create() method (create and update modes). */ public function testIsPaymentFlagForPending() { - $contactId = Contact::createIndividual(); - $ids = array('contribution' => NULL); + $contactId = $this->individualCreate(); $params = array( 'contact_id' => $contactId, @@ -593,7 +545,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { 'thankyou_date' => '20080522', ); - $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); + $contribution = CRM_Contribute_BAO_Contribution::create($params); $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); @@ -626,12 +578,6 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { $trxnArray['is_payment'] = 0; $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.'); - - //Delete Contribution - $this->contributionDelete($contribution->id); - - //Delete Contact - $this->contactDelete($contactId); } /** @@ -651,7 +597,7 @@ class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { public function checkItemValues($contribution) { $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType(4, $relationTypeId); - $query = "SELECT eft1.entity_id, ft.total_amount, eft1.amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') + $query = "SELECT eft1.entity_id, ft.total_amount, eft1.amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item') WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; @@ -664,9 +610,6 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.'); $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item'); } - - $this->contactDelete($this->_contactId); - Event::delete($this->_eventId); } /** @@ -693,9 +636,9 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; */ public function addParticipantWithContribution() { // creating price set, price field - require_once 'CiviTest/Event.php'; - $this->_contactId = Contact::createIndividual(); - $this->_eventId = Event::create($this->_contactId); + $this->_contactId = $this->individualCreate(); + $event = $this->eventCreate(); + $this->_eventId = $event['id']; $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4); $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']); $paramsSet['is_active'] = TRUE; @@ -819,7 +762,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; ), ); try { - $error = CRM_Contribute_BAO_Contribution::checkLineItems($params); + CRM_Contribute_BAO_Contribution::checkLineItems($params); $this->fail("Missed expected exception"); } catch (Exception $e) { -- 2.25.1