From 7c3f3d5982033d8f75ac2a8cbb2ac0100e1bc2d0 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 22 Mar 2017 23:06:05 +1300 Subject: [PATCH] Fix up test to create line_items & memberships accurately --- CRM/Contribute/BAO/Contribution.php | 2 + tests/phpunit/api/v3/ContributionTest.php | 72 +++++++++++++---------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index cda0ca55b0..60b197bd78 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4532,6 +4532,8 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) // CRM-8141 update the membership type with the value recorded in log when membership created/renewed // this picks up membership type changes during renewals + // @todo this is almost certainly an obsolete sql call, the pre-change + // membership is accessible via $this->_relatedObjects $sql = " SELECT membership_type_id FROM civicrm_membership_log diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 6d92a2d80e..83d271a755 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -2100,17 +2100,18 @@ class api_v3_ContributionTest extends CiviUnitTestCase { } /** * CRM-19945 Tests repeattransaction is using a completed contribution for the template. + * * ( Tests membership is renewed after repeattransaction. ) */ public function testRepeatTransactionUsesCompleted() { list($originalContribution, $membership) = $this->setUpAutoRenewMembership(); $this->callAPISuccess('contribution', 'create', array( - 'contact_id' => $originalContribution['values'][1]['contact_id'], - 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'], - 'total_amount' => $originalContribution['values'][1]['total_amount'], - 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'], - 'contribution_status_id' => "Failed", + 'contact_id' => $originalContribution['values'][1]['contact_id'], + 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'], + 'total_amount' => $originalContribution['values'][1]['total_amount'], + 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'], + 'contribution_status_id' => "Failed", )); $this->callAPISuccess('membership', 'create', array( @@ -2119,18 +2120,22 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'status_id' => 4, )); - $this->callAPISuccess('contribution', 'repeattransaction', array( + $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array( 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'], 'contribution_status_id' => 'Completed', - 'trxn_id' => uniqid(), + 'trxn_id' => 'bobsled', )); - $membershipStatus = $this->callAPISuccess('membership', 'getvalue', array( + $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array( 'id' => $membership['id'], 'return' => 'status_id', )); - $this->assertEquals('1', $membershipStatus); + $this->assertEquals('New', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId)); + + $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id'])); + $this->assertEquals('civicrm_membership', $lineItem['entity_table']); + $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id'])); $this->quickCleanUpFinancialEntities(); $this->contactDelete($originalContribution['values'][1]['contact_id']); } @@ -3251,17 +3256,17 @@ class api_v3_ContributionTest extends CiviUnitTestCase { */ protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) { $newContact = $this->callAPISuccess('Contact', 'create', array( - 'contact_type' => 'Individual', - 'sort_name' => 'McTesterson, Testy', - 'display_name' => 'Testy McTesterson', - 'preferred_language' => 'en_US', - 'preferred_mail_format' => 'Both', - 'first_name' => 'Testy', - 'last_name' => 'McTesterson', - 'contact_is_deleted' => '0', - 'email_id' => '4', - 'email' => 'tmctesterson@example.com', - 'on_hold' => '0', + 'contact_type' => 'Individual', + 'sort_name' => 'McTesterson, Testy', + 'display_name' => 'Testy McTesterson', + 'preferred_language' => 'en_US', + 'preferred_mail_format' => 'Both', + 'first_name' => 'Testy', + 'last_name' => 'McTesterson', + 'contact_is_deleted' => '0', + 'email_id' => '4', + 'email' => 'tmctesterson@example.com', + 'on_hold' => '0', )); $membershipType = $this->callAPISuccess('MembershipType', 'create', array( 'domain_id' => "Default Domain Name", @@ -3284,6 +3289,17 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'frequency_unit' => 'month', 'payment_processor_id' => $this->paymentProcessorID, ), $generalParams, $recurParams)); + + $membership = $this->callAPISuccess('membership', 'create', array( + 'contact_id' => $newContact['id'], + 'contribution_recur_id' => $contributionRecur['id'], + 'financial_type_id' => "Member Dues", + 'membership_type_id' => $membershipType['id'], + 'num_terms' => 1, + 'skipLineItem' => TRUE, + )); + + CRM_Price_BAO_LineItem::getLineItemArray($this->_params, NULL, 'membership', $membershipType['id']); $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( $this->_params, array( @@ -3294,18 +3310,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'invoice_id' => uniqid(), ), $generalParams) ); - $membership = $this->callAPISuccess('membership', 'create', array( - 'contact_id' => $newContact['id'], - 'contribution_recur_id' => $contributionRecur['id'], - 'financial_type_id' => "Member Dues", - 'membership_type_id' => $membershipType['id'], - 'num_terms' => 1, - )); + $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array()); + $this->assertEquals('civicrm_membership', $lineItem['entity_table']); + $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id'])); + $this->callAPISuccess('LineItem', 'getsingle', array()); + $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1); - $this->callAPISuccess('MembershipPayment', 'create', array( - 'contribution_id' => $originalContribution['id'], - 'membership_id' => $membership['id'], - )); return array($originalContribution, $membership); } /** -- 2.25.1