From e0fa713325241c1c251bd03c2fb95ebf6816b181 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 14 Dec 2020 19:25:08 +1300 Subject: [PATCH] Cleanup on test classes to support more flexible use of price set work --- Civi/Test/ContactTestTrait.php | 8 +- .../CRM/Member/Form/MembershipTest.php | 96 +++++++------------ .../CRMTraits/Financial/PriceSetTrait.php | 78 ++++++++++++++- 3 files changed, 115 insertions(+), 67 deletions(-) diff --git a/Civi/Test/ContactTestTrait.php b/Civi/Test/ContactTestTrait.php index 065c6bccb7..4985acc70a 100644 --- a/Civi/Test/ContactTestTrait.php +++ b/Civi/Test/ContactTestTrait.php @@ -53,8 +53,10 @@ trait ContactTestTrait { * * @return int * id of Organisation created + * + * @throws \CiviCRM_API3_Exception */ - public function organizationCreate($params = [], $seq = 0) { + public function organizationCreate($params = [], $seq = 0): int { if (!$params) { $params = []; } @@ -76,7 +78,7 @@ trait ContactTestTrait { * * @throws \CiviCRM_API3_Exception */ - public function individualCreate($params = [], $seq = 0, $random = FALSE) { + public function individualCreate($params = [], $seq = 0, $random = FALSE): int { $params = array_merge($this->sampleContact('Individual', $seq, $random), $params); return $this->_contactCreate($params); } @@ -160,7 +162,7 @@ trait ContactTestTrait { * * @throws \CiviCRM_API3_Exception */ - private function _contactCreate($params) { + private function _contactCreate($params): int { $result = civicrm_api3('contact', 'create', $params); return (int) $result['id']; } diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index a70434be69..d461b49afd 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -26,7 +26,11 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { use CRMTraits_Financial_OrderTrait; + use CRMTraits_Financial_PriceSetTrait; + /** + * @var int + */ protected $_individualId; protected $_contribution; protected $_financialTypeId = 1; @@ -75,6 +79,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { * and redirect stdin to a temporary file. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public function setUp() { $this->_apiversion = 3; @@ -93,7 +98,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'contact_type_a' => 'Individual', 'contact_type_b' => 'Organization', ])['id']; - $membershipTypeAnnualFixed = $this->callAPISuccess('MembershipType', 'create', [ + $this->ids['membership_type']['AnnualFixed'] = $this->callAPISuccess('MembershipType', 'create', [ 'domain_id' => 1, 'name' => 'AnnualFixed', 'member_of_contact_id' => $this->ids['contact']['organization'], @@ -106,8 +111,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'relationship_type_id' => [$this->ids['relationship_type']['member']], 'relationship_direction' => ['b_a'], 'financial_type_id' => 2, - ]); - $this->membershipTypeAnnualFixedID = $membershipTypeAnnualFixed['id']; + ])['id']; $this->ids['membership_type']['AnnualRolling'] = $this->callAPISuccess('MembershipType', 'create', [ 'name' => 'AnnualRolling', @@ -288,7 +292,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { public function testFormRulePermanentOverrideWithNoStatus() { $params = [ 'join_date' => date('Y-m-d'), - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'is_override' => TRUE, ]; $files = []; @@ -301,7 +305,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { public function testFormRuleUntilDateOverrideWithValidOverrideEndDate() { $params = [ 'join_date' => date('Y-m-d'), - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'is_override' => TRUE, 'status_id' => 1, 'status_override_end_date' => date('Y-m-d'), @@ -315,7 +319,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { public function testFormRuleUntilDateOverrideWithNoOverrideEndDate() { $params = [ 'join_date' => date('Y-m-d'), - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'is_override' => CRM_Member_StatusOverrideTypes::UNTIL_DATE, 'status_id' => 1, ]; @@ -461,7 +465,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'join_date' => date('Y-m-d', $unix6MAgo), 'start_date' => '', 'end_date' => '', - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], ]; $files = []; $obj = new CRM_Member_Form_Membership(); @@ -495,7 +499,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'start_date' => '', 'end_date' => '', // This format reflects the organisation & then the type. - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'auto_renew' => '0', 'max_related' => '', 'num_terms' => '1', @@ -596,8 +600,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'join_date' => date('Y-m-d'), 'start_date' => '', 'end_date' => '', - // This format reflects the first being the organisation & the $this->membershipTypeAnnualFixedID being the type. - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + // This format reflects the first being the organisation & the $this->ids['membership_type']['AnnualFixed'] being the type. + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'record_contribution' => 1, 'total_amount' => 50, 'receive_date' => date('Y-m-d', time()) . ' 20:36:00', @@ -697,7 +701,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'start_date' => '', 'end_date' => '', // This format reflects the first number being the organisation & the second being the type. - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'receive_date' => date('Y-m-d', time()) . ' 20:36:00', 'record_contribution' => 1, 'total_amount' => $this->formatMoneyInput(50), @@ -752,7 +756,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - public function testSubmitRecur() { + public function testSubmitRecur(): void { CRM_Core_Session::singleton()->getStatus(TRUE); $pendingVal = $this->callAPISuccessGetValue('OptionValue', [ 'return' => 'id', @@ -768,7 +772,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { $form = $this->getForm(); $this->callAPISuccess('MembershipType', 'create', [ - 'id' => $this->membershipTypeAnnualFixedID, + 'id' => $this->ids['membership_type']['AnnualFixed'], 'duration_unit' => 'month', 'duration_interval' => 1, 'auto_renew' => TRUE, @@ -809,13 +813,19 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { ], CRM_Core_Session::singleton()->getStatus()); } + public function testSubmitRecurTwoRows() { + $this->createMembershipPriceSet(); + $form = $this->getForm(); + } + /** * CRM-20946: Test the financial entires especially the reversed amount, * after related Contribution is cancelled * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testFinancialEntiriesOnCancelledContribution() { + public function testFinancialEntriesOnCancelledContribution(): void { // Create two memberships for individual $this->_individualId, via a price set in the back end. $this->createTwoMembershipsViaPriceSetInBackEnd($this->_individualId); @@ -845,8 +855,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { ]); // compare the reversed amounts of respective memberships after cancelling contribution $cancelledMembershipAmounts = [ + -259.00, -20.00, - -10.00, ]; $count = 0; foreach ($result['values'] as $record) { @@ -870,7 +880,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'start_date' => '', 'end_date' => '', // This format reflects the first number being the organisation & the second being the type. - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'auto_renew' => '0', 'max_related' => '', 'num_terms' => '2', @@ -976,7 +986,7 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { ]); $processorDetail = $processor->getPaymentProcessor(); $this->callAPISuccess('MembershipType', 'create', [ - 'id' => $this->membershipTypeAnnualFixedID, + 'id' => $this->ids['membership_type']['AnnualFixed'], 'duration_unit' => 'month', 'duration_interval' => 1, 'auto_renew' => TRUE, @@ -1038,7 +1048,7 @@ Expires: ', * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - public function testTwoInheritedMembershipsViaPriceSetInBackend() { + public function testTwoInheritedMembershipsViaPriceSetInBackend(): void { // Create an organization and give it a "Member of" relationship to $this->_individualId. $orgID = $this->organizationCreate(); $relationship = $this->callAPISuccess('Relationship', 'create', [ @@ -1118,7 +1128,7 @@ Expires: ', * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - public function testTwoMembershipsViaPriceSetInBackendWithDiscount() { + public function testTwoMembershipsViaPriceSetInBackendWithDiscount(): void { // Register buildAmount hook to apply discount. $this->hookClass->setHook('civicrm_buildAmount', [$this, 'buildAmountMembershipDiscount']); @@ -1194,7 +1204,7 @@ Expires: ', 'end_date' => '', 'campaign_id' => '', // This format reflects the first number being the organisation & the second being the type. - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'auto_renew' => '1', 'is_recur' => 1, 'max_related' => 0, @@ -1236,47 +1246,11 @@ Expires: ', * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - protected function createTwoMembershipsViaPriceSetInBackEnd($contactId) { + protected function createTwoMembershipsViaPriceSetInBackEnd($contactId): void { $form = $this->getForm(); $form->preProcess(); $this->createLoggedInUser(); - - // create a price-set of price-field of type checkbox and each price-option corresponds to a membership type - $priceSet = $this->callAPISuccess('price_set', 'create', [ - 'is_quick_config' => 0, - 'extends' => 'CiviMember', - 'financial_type_id' => 1, - 'title' => 'my Page', - ]); - $priceSetID = $priceSet['id']; - // create respective checkbox price-field - $priceField = $this->callAPISuccess('price_field', 'create', [ - 'price_set_id' => $priceSetID, - 'label' => 'Memberships', - 'html_type' => 'Checkbox', - ]); - $priceFieldID = $priceField['id']; - // create two price options, each represent a membership type of amount 20 and 10 respectively - $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [ - 'price_set_id' => $priceSetID, - 'price_field_id' => $priceField['id'], - 'label' => 'Long Haired Goat', - 'amount' => 20, - 'financial_type_id' => 'Donation', - 'membership_type_id' => $this->ids['membership_type']['AnnualRolling'], - 'membership_num_terms' => 1, - ]); - $pfvIDs = [$priceFieldValue['id'] => 1]; - $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [ - 'price_set_id' => $priceSetID, - 'price_field_id' => $priceField['id'], - 'label' => 'Shoe-eating Goat', - 'amount' => 10, - 'financial_type_id' => 'Donation', - 'membership_type_id' => $this->ids['membership_type']['AnnualRollingOrg2'], - 'membership_num_terms' => 2, - ]); - $pfvIDs[$priceFieldValue['id']] = 1; + $pfvIDs = $this->createMembershipPriceSet(); // register for both of these memberships via backoffice membership form submission $params = [ @@ -1285,8 +1259,8 @@ Expires: ', 'start_date' => '', 'end_date' => '', // This format reflects the 23 being the organisation & the 25 being the type. - "price_$priceFieldID" => $pfvIDs, - 'price_set_id' => $priceSetID, + "price_" . $this->getPriceFieldID() => $pfvIDs, + 'price_set_id' => $this->getPriceSetID(), 'membership_type_id' => [1 => 0], 'auto_renew' => '0', 'max_related' => '', @@ -1381,7 +1355,7 @@ Expires: ', 'start_date' => '', 'end_date' => '', // This format reflects the first number being the organisation & the second being the type. - 'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID], + 'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']], 'record_contribution' => 1, 'total_amount' => 55, 'receive_date' => date('Y-m-d') . ' 20:36:00', diff --git a/tests/phpunit/CRMTraits/Financial/PriceSetTrait.php b/tests/phpunit/CRMTraits/Financial/PriceSetTrait.php index f783e5275c..4e22e2ff2a 100644 --- a/tests/phpunit/CRMTraits/Financial/PriceSetTrait.php +++ b/tests/phpunit/CRMTraits/Financial/PriceSetTrait.php @@ -16,6 +16,28 @@ */ trait CRMTraits_Financial_PriceSetTrait { + /** + * Get the created price set id. + * + * @param string $key + * + * @return int + */ + protected function getPriceSetID($key = 'membership'):int { + return $this->ids['PriceSet'][$key]; + } + + /** + * Get the created price field id. + * + * @param string $key + * + * @return int + */ + protected function getPriceFieldID($key = 'membership'):int { + return $this->ids['PriceField'][$key]; + } + /** * Create a contribution with 2 line items. * @@ -25,8 +47,12 @@ trait CRMTraits_Financial_PriceSetTrait { * @param array $lineItemFinancialTypes * Financial Types, if an override is intended. */ - protected function createContributionWithTwoLineItemsAgainstPriceSet($params, $lineItemFinancialTypes = []) { - $params = array_merge(['total_amount' => 300, 'financial_type_id' => 'Donation', 'contribution_status_id' => 'Pending'], $params); + protected function createContributionWithTwoLineItemsAgainstPriceSet($params, $lineItemFinancialTypes = []): void { + $params = array_merge([ + 'total_amount' => 300, + 'financial_type_id' => 'Donation', + 'contribution_status_id' => 'Pending', + ], $params); $priceFields = $this->createPriceSet('contribution'); foreach ($priceFields['values'] as $key => $priceField) { $financialTypeID = (!empty($lineItemFinancialTypes) ? array_shift($lineItemFinancialTypes) : $priceField['financial_type_id']); @@ -43,7 +69,53 @@ trait CRMTraits_Financial_PriceSetTrait { ]; } $order = $this->callAPISuccess('Order', 'create', $params); - $this->callAPISuccess('Payment', 'create', ['contribution_id' => $order['id'], 'total_amount' => $params['total_amount']]); + $this->callAPISuccess('Payment', 'create', [ + 'contribution_id' => $order['id'], + 'total_amount' => $params['total_amount'], + ]); + } + + /** + * Create a non-quick-config price set with all memberships in it. + * + * The price field is of type checkbox and each price-option + * corresponds to a membership type + * + * @return array + */ + protected function createMembershipPriceSet(): array { + $this->ids['PriceSet']['membership'] = (int) $this->callAPISuccess('PriceSet', 'create', [ + 'is_quick_config' => 0, + 'extends' => 'CiviMember', + 'financial_type_id' => 1, + 'title' => 'my Page', + ])['id']; + + $this->ids['PriceField']['membership'] = (int) $this->callAPISuccess('PriceField', 'create', [ + 'price_set_id' => $this->getPriceSetID(), + 'label' => 'Memberships', + 'html_type' => 'Checkbox', + ])['id']; + + // Add a few variants to assign. + $labels = ['Shoe eating goat', 'Long Haired Goat', 'Pesky rabbit', 'Rabbits are goats too', 'Runaway rabbit']; + $amounts = [10, 20, 259, 88, 133]; + $membershipNumTerms = [1, 1, 2, 1, 1, 1]; + foreach ($this->ids['membership_type'] as $membershipKey => $membershipTypeID) { + $this->ids['PriceFieldValue'][$membershipKey] = $this->callAPISuccess('price_field_value', 'create', [ + 'price_set_id' => $this->ids['PriceSet'], + 'price_field_id' => $this->ids['PriceField']['membership'], + 'label' => array_shift($labels), + 'amount' => array_shift($amounts), + 'financial_type_id' => 'Donation', + 'membership_type_id' => $membershipTypeID, + 'membership_num_terms' => array_shift($membershipNumTerms), + ])['id']; + } + return [ + $this->ids['PriceFieldValue']['AnnualRollingOrg2'] => 1, + $this->ids['PriceFieldValue']['AnnualRolling'] => 1, + ]; } } -- 2.25.1