Skip to content

Commit

Permalink
Cleanup on test classes to support more flexible use of price set work
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Dec 14, 2020
1 parent 11a8311 commit 945d659
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 66 deletions.
8 changes: 5 additions & 3 deletions Civi/Test/ContactTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public function createLoggedInUser() {
*
* @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 = [];
}
Expand All @@ -76,7 +78,7 @@ public function organizationCreate($params = [], $seq = 0) {
*
* @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);
}
Expand Down Expand Up @@ -160,7 +162,7 @@ public function sampleContact($contact_type, $seq = 0, $random = FALSE) {
*
* @throws \CiviCRM_API3_Exception
*/
private function _contactCreate($params) {
private function _contactCreate($params): int {
$result = civicrm_api3('contact', 'create', $params);
return (int) $result['id'];
}
Expand Down
94 changes: 34 additions & 60 deletions tests/phpunit/CRM/Member/Form/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -93,7 +98,7 @@ public function setUp() {
'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'],
Expand All @@ -106,8 +111,7 @@ public function setUp() {
'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',
Expand Down Expand Up @@ -288,7 +292,7 @@ public function testFormRuleRollingLifetimeEnd() {
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 = [];
Expand All @@ -301,7 +305,7 @@ public function testFormRulePermanentOverrideWithNoStatus() {
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'),
Expand All @@ -315,7 +319,7 @@ public function testFormRuleUntilDateOverrideWithValidOverrideEndDate() {
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,
];
Expand Down Expand Up @@ -461,7 +465,7 @@ public function testFormRuleFixedJoin6MonthsAgo() {
'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();
Expand Down Expand Up @@ -495,7 +499,7 @@ public function testSubmit(string $thousandSeparator) {
'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',
Expand Down Expand Up @@ -596,8 +600,8 @@ public function testContributionUpdateOnMembershipTypeChange(): void {
'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',
Expand Down Expand Up @@ -697,7 +701,7 @@ public function testSubmitPartialPayment(string $thousandSeparator): void {
'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),
Expand Down Expand Up @@ -752,7 +756,7 @@ public function testSubmitPartialPayment(string $thousandSeparator): void {
* @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',
Expand All @@ -768,7 +772,7 @@ public function testSubmitRecur() {
$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,
Expand Down Expand Up @@ -809,13 +813,19 @@ public function testSubmitRecur() {
], 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 testFinancialEntiriesOnCancelledContribution(): void {
// Create two memberships for individual $this->_individualId, via a price set in the back end.
$this->createTwoMembershipsViaPriceSetInBackEnd($this->_individualId);

Expand Down Expand Up @@ -870,7 +880,7 @@ public function testSubmitPayLaterWithBilling() {
'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',
Expand Down Expand Up @@ -976,7 +986,7 @@ public function testSubmitRecurCompleteInstant() {
]);
$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,
Expand Down Expand Up @@ -1038,7 +1048,7 @@ public function testSubmitRecurCompleteInstant() {
* @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', [
Expand Down Expand Up @@ -1118,7 +1128,7 @@ public function testTwoInheritedMembershipsViaPriceSetInBackend() {
* @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']);

Expand Down Expand Up @@ -1194,7 +1204,7 @@ protected function getBaseSubmitParams() {
'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,
Expand Down Expand Up @@ -1236,47 +1246,11 @@ protected function getBaseSubmitParams() {
* @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 = [
Expand All @@ -1285,8 +1259,8 @@ protected function createTwoMembershipsViaPriceSetInBackEnd($contactId) {
'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' => '',
Expand Down Expand Up @@ -1381,7 +1355,7 @@ public function testLineItemAmountOnSalesTax() {
'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',
Expand Down
78 changes: 75 additions & 3 deletions tests/phpunit/CRMTraits/Financial/PriceSetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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']);
Expand All @@ -43,7 +69,53 @@ protected function createContributionWithTwoLineItemsAgainstPriceSet($params, $l
];
}
$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,
];
}

}

0 comments on commit 945d659

Please sign in to comment.