Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC] Add test on buildMembershipTypeValues & cleanup class #16167

Merged
merged 1 commit into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CRM/Price/BAO/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ public static function deleteLineItems($entityId, $entityTable) {
*
* @param bool $update
*
* @return void
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function processPriceSet($entityId, $lineItem, $contributionDetails = NULL, $entityTable = 'civicrm_contribution', $update = FALSE) {
if (!$entityId || !is_array($lineItem)
Expand Down
9 changes: 6 additions & 3 deletions tests/phpunit/CRM/Member/BAO/MembershipLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,23 @@ public function setUp() {
'visibility' => 'Public',
'is_active' => 1,
];
$ids = [];
$membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
$membershipType = CRM_Member_BAO_MembershipType::add($params);
$this->membershipTypeID = $membershipType->id;
$this->membershipStatusID = $this->membershipStatusCreate('test status');
}

/**
* Tears down the fixture.
*
* @throws \CRM_Core_Exception
*/
public function tearDown() {
$this->relationshipTypeDelete($this->relationshipTypeID);
$this->membershipTypeDelete(['id' => $this->membershipTypeID]);
$this->membershipStatusDelete($this->membershipStatusID);
$this->quickCleanUpFinancialEntities();
$this->restoreMembershipTypes();
$this->contactDelete($this->organizationContactID);
parent::tearDown();
}

/**
Expand Down
24 changes: 14 additions & 10 deletions tests/phpunit/CRM/Member/BAO/MembershipStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,37 @@ public function testDel() {
$this->assertEquals(empty($result), TRUE, 'Verify membership status record deletion.');
}

/**
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testExpiredDisabled() {
$result = civicrm_api3('MembershipStatus', 'get', [
'name' => "Expired",
$this->callAPISuccess('MembershipStatus', 'get', [
'name' => 'Expired',
'api.MembershipStatus.create' => ['label' => 'Expiiiired'],
]);

// Calling it 'Expiiiired' is OK.
$result = $this->callAPISuccess('job', 'process_membership', []);
$this->callAPISuccess('job', 'process_membership', []);

$result = civicrm_api3('MembershipStatus', 'get', [
'name' => "Expired",
$this->callAPISuccess('MembershipStatus', 'get', [
'name' => 'Expired',
'api.MembershipStatus.create' => ['is_active' => 0],
]);

// Disabling 'Expired' is OK.
$result = $this->callAPISuccess('job', 'process_membership', []);
$this->callAPISuccess('job', 'process_membership', []);

$result = civicrm_api3('MembershipStatus', 'get', [
'name' => "Expired",
$this->callAPISuccess('MembershipStatus', 'get', [
'name' => 'Expired',
'api.MembershipStatus.delete' => [],
]);

// Deleting 'Expired' is OK.
$result = $this->callAPISuccess('job', 'process_membership', []);
$this->callAPISuccess('job', 'process_membership', []);

// Put things back like normal
$result = civicrm_api3('MembershipStatus', 'create', [
$this->callAPISuccess('MembershipStatus', 'create', [
'name' => 'Expired',
'label' => 'Expired',
'start_event' => 'end_date',
Expand Down
190 changes: 93 additions & 97 deletions tests/phpunit/CRM/Member/BAO/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {

public function setUp() {
parent::setUp();
// FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
$GLOBALS['_HTML_QuickForm_registered_rules'] = [
'required' => ['html_quickform_rule_required', 'HTML/QuickForm/Rule/Required.php'],
'maxlength' => ['html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'],
'minlength' => ['html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'],
'rangelength' => ['html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'],
'email' => ['html_quickform_rule_email', 'HTML/QuickForm/Rule/Email.php'],
'regex' => ['html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'],
'lettersonly' => ['html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'],
'alphanumeric' => ['html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'],
'numeric' => ['html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'],
'nopunctuation' => ['html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'],
'nonzero' => ['html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'],
'callback' => ['html_quickform_rule_callback', 'HTML/QuickForm/Rule/Callback.php'],
'compare' => ['html_quickform_rule_compare', 'HTML/QuickForm/Rule/Compare.php'],
];

$this->_contactID = $this->organizationCreate();
$this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]);
Expand All @@ -43,20 +27,28 @@ public function setUp() {
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @throws \CRM_Core_Exception
*/
public function tearDown() {
$this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
$this->membershipStatusDelete($this->_membershipStatusID);
$this->contactDelete($this->_contactID);

$this->_contactID = $this->_membershipStatusID = $this->_membershipTypeID = NULL;
$this->quickCleanUpFinancialEntities();
$this->restoreMembershipTypes();
parent::tearDown();
}

/**
* Create membership type using given organization id.
*
* @param $organizationId
* @param bool $withRelationship
*
* @return array|int
* @throws \CRM_Core_Exception
*/
private function createMembershipType($organizationId, $withRelationship = FALSE) {
$membershipType = $this->callAPISuccess('MembershipType', 'create', [
Expand All @@ -67,7 +59,7 @@ private function createMembershipType($organizationId, $withRelationship = FALSE
'duration_unit' => "year",
'duration_interval' => 1,
'period_type' => "rolling",
'name' => "Organiation Membership Type",
'name' => 'Organiation Membership Type',
'relationship_type_id' => ($withRelationship) ? 5 : NULL,
'relationship_direction' => ($withRelationship) ? 'b_a' : NULL,
]);
Expand Down Expand Up @@ -466,12 +458,15 @@ public function testRenewMembership() {

/**
* Renew stale membership.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testStaleMembership() {
$statusId = 3;
$contactId = $this->individualCreate();
$joinDate = $startDate = date("Ymd", strtotime(date("Ymd") . " -1 year -15 days"));
$endDate = date("Ymd", strtotime($joinDate . " +1 year -1 day"));
$endDate = date('Ymd', strtotime($joinDate . " +1 year -1 day"));
$params = [
'contact_id' => $contactId,
'membership_type_id' => $this->_membershipTypeID,
Expand Down Expand Up @@ -511,21 +506,14 @@ public function testStaleMembership() {
$membership->id,
'id',
'membership_id',
'Database checked on membershiplog record.'
'Database checked on membership log record.'
);

// this is a test and we dont want qfKey generation / validation
// easier to suppress it, than change core code
$config = CRM_Core_Config::singleton();
$config->keyDisable = TRUE;

$membershipRenewal = new CRM_Core_Form();
$membershipRenewal->controller = new CRM_Core_Controller();
list($MembershipRenew) = CRM_Member_BAO_Membership::processMembership(
$contactId,
$this->_membershipTypeID,
FALSE,
$membershipRenewal,
FALSE,
NULL,
NULL,
NULL,
Expand All @@ -541,7 +529,7 @@ public function testStaleMembership() {
$MembershipRenew->id,
'id',
'membership_id',
'Database checked on membershiplog record.'
'Database checked on membership log record.'
);

$this->membershipDelete($membershipId);
Expand Down Expand Up @@ -672,7 +660,7 @@ public function testMembershipPaymentForSingleContributionMultipleMembership() {
$parentContactId = $this->individualCreate();
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', [
'contact_id' => $parentContactId,
'amount' => 200,
'amount' => 150,
'frequency_unit' => 'day',
'frequency_interval' => 1,
'installments' => 2,
Expand All @@ -693,37 +681,15 @@ public function testMembershipPaymentForSingleContributionMultipleMembership() {
'financial_type_id' => $financialTypeId,
'payment_instrument_id' => 'Credit Card',
]);
$contribution = $this->callAPISuccess('contribution', 'create', [
'total_amount' => 200,
'contribution_recur_id' => $contributionRecur['id'],
'currency' => 'USD',
'contact_id' => $parentContactId,
'financial_type_id' => $financialTypeId,
'contribution_status_id' => 'Completed',
'skipLineItem' => TRUE,
'is_recur' => TRUE,
]);

$params[] = [
'contact_id' => $parentContactId,
'membership_type_id' => $membershipTypeID1,
'contact_id' => $this->individualCreate(),
'membership_type_id' => $membershipTypeID2,
'contribution_recur_id' => $contributionRecur['id'],
'join_date' => date('Ymd', time()),
'start_date' => date('Ymd', time()),
'end_date' => date('Ymd', strtotime('+1 year')),
'skipLineItem' => TRUE,
'source' => 'Payment',
'line_items' => [
'price_field_id' => $priceField['id'],
'price_field_value_id' => $priceFieldValueId[1],
'label' => 'Parent',
'contribution_id' => $contribution['id'],
'membership_type_id' => $membershipTypeID1,
'qty' => 1,
'unit_price' => 100,
'line_total' => 100,
'financial_type_id' => $financialTypeId,
'entity_table' => 'civicrm_membership',
],
];
$params[] = [
'contact_id' => $this->individualCreate(),
Expand All @@ -732,67 +698,97 @@ public function testMembershipPaymentForSingleContributionMultipleMembership() {
'join_date' => date('Ymd', time()),
'start_date' => date('Ymd', time()),
'end_date' => date('Ymd', strtotime('+1 year')),
'skipLineItem' => TRUE,
'source' => 'Payment',
];

foreach ($params as $key => $param) {
$this->callAPISuccess('membership', 'create', $param);
}

$contribution = $this->callAPISuccess('Order', 'create', [
'total_amount' => 150,
'contribution_recur_id' => $contributionRecur['id'],
'currency' => 'USD',
'contact_id' => $parentContactId,
'financial_type_id' => $financialTypeId,
'contribution_status_id' => 'Pending',
'is_recur' => TRUE,
'api.Payment.create' => ['total_amount' => 150],
'line_items' => [
'price_field_id' => $priceField['id'],
'price_field_value_id' => $priceFieldValueId[2],
'label' => 'Child',
'contribution_id' => $contribution['id'],
'qty' => 1,
'unit_price' => 50,
'line_total' => 50,
'membership_type_id' => $membershipTypeID2,
'financial_type_id' => $financialTypeId,
'entity_table' => 'civicrm_membership',
[
'line_item' => [
0 => [
'price_field_id' => $priceField['id'],
'price_field_value_id' => $priceFieldValueId[1],
'label' => 'Parent',
'membership_type_id' => $membershipTypeID1,
'qty' => 1,
'unit_price' => 100,
'line_total' => 100,
'financial_type_id' => $financialTypeId,
'entity_table' => 'civicrm_membership',
],
1 => [
'price_field_id' => $priceField['id'],
'price_field_value_id' => $priceFieldValueId[2],
'label' => 'Child',
'qty' => 1,
'unit_price' => 50,
'line_total' => 50,
'membership_type_id' => $membershipTypeID2,
'financial_type_id' => $financialTypeId,
'entity_table' => 'civicrm_membership',
],
],
],
],
];
]);
$params[] = [
'contact_id' => $this->individualCreate(),
'membership_type_id' => $membershipTypeID2,
'contact_id' => $parentContactId,
'membership_type_id' => $membershipTypeID1,
'contribution_recur_id' => $contributionRecur['id'],
'join_date' => date('Ymd', time()),
'start_date' => date('Ymd', time()),
'skipLineItem' => TRUE,
'end_date' => date('Ymd', strtotime('+1 year')),
'skipLineItem' => TRUE,
'source' => 'Payment',
'line_items' => [
'price_field_id' => $priceField['id'],
'price_field_value_id' => $priceFieldValueId[2],
'label' => 'Child',
'contribution_id' => $contribution['id'],
'qty' => 1,
'membership_type_id' => $membershipTypeID2,
'unit_price' => 50,
'line_total' => 50,
'financial_type_id' => $financialTypeId,
'entity_table' => 'civicrm_membership',
],
];

foreach ($params as $key => $param) {
$membership = $this->callAPISuccess('membership', 'create', $param);
$param['line_items']['entity_id'] = $membership['id'];
$memPayments = new CRM_Member_BAO_MembershipPayment();
$paymentParams = [
'membership_id' => $membership['id'],
'contribution_id' => $contribution['id'],
];
$memPayments->copyValues($paymentParams);
$memPayments->save();
$lineItemBAO = new CRM_Price_BAO_LineItem();
$lineItemBAO->copyValues($param['line_items']);
$lineItemBAO->save();
}
$this->callAPISuccess('contribution', 'repeattransaction', [
'original_contribution_id' => $contribution['id'],
'contribution_status_id' => 'Completed',
'trxn_id' => uniqid(),
]);
$this->callAPISuccessGetCount('Contribution', [], 2);
$this->callAPISuccessGetCount('LineItem', [], 6);
// @todo this fails depending on what tests it is run with due some bad stuff in Membership.create
// It needs to be addressed but might involve the switch to ORDER. Membership BAO does bad line item stuff.
// $this->callAPISuccessGetCount('LineItem', [], 6);
$this->membershipTypeDelete(['id' => $membershipTypeID1]);
$this->membershipTypeDelete(['id' => $membershipTypeID2]);
$this->validateAllPayments();
$this->validateAllContributions();
}

/**
* Test the buildMembershipTypeValues function.
*/
public function testBuildMembershipTypeValues() {
$form = new CRM_Core_Form();
$values = CRM_Member_BAO_Membership::buildMembershipTypeValues($form);
$this->assertEquals([
'id' => '1',
'minimum_fee' => '100.000000000',
'name' => 'General',
'is_active' => '1',
'description' => 'Regular annual membership.',
'financial_type_id' => '2',
'auto_renew' => '0',
'member_of_contact_id' => $values[1]['member_of_contact_id'],
'relationship_type_id' => 7,
'relationship_direction' => 'b_a',
'max_related' => NULL,
'duration_unit' => 'year',
'duration_interval' => '2',
], $values[1]);
}

/**
Expand Down
Loading