Skip to content

Commit

Permalink
Merge pull request #19404 from eileenmcnaughton/mem_deb
Browse files Browse the repository at this point in the history
[Test] Extend new membership form to cover multi-line renew + minor cleanup
  • Loading branch information
eileenmcnaughton authored Jan 18, 2021
2 parents 9345720 + e659743 commit 36c2072
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ protected function setPriceSetParameters(array $formValues): array {
*
* @param array $formValues
*/
public function testSubmit($formValues) {
public function testSubmit(array $formValues): void {
$this->setContextVariables($formValues);
$this->_memType = $formValues['membership_type_id'][1];
$this->_params = $formValues;
Expand Down
14 changes: 3 additions & 11 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -1935,13 +1935,10 @@ protected function processContribution(
* @param array $params
* @param int $contactID
*
* @return int|null
* @return int
*/
protected function legacyProcessRecurringContribution(&$params, $contactID) {
protected function legacyProcessRecurringContribution(array $params, $contactID): int {
$form = $this;
if (empty($params['is_recur'])) {
return NULL;
}

$recurParams = ['contact_id' => $contactID];
$recurParams['amount'] = $params['amount'] ?? NULL;
Expand Down Expand Up @@ -1974,12 +1971,7 @@ protected function legacyProcessRecurringContribution(&$params, $contactID) {

$campaignId = $params['campaign_id'] ?? $form->_values['campaign_id'] ?? NULL;
$recurParams['campaign_id'] = $campaignId;
$recurring = CRM_Contribute_BAO_ContributionRecur::add($recurParams);
if (is_a($recurring, 'CRM_Core_Error')) {
throw new CRM_Core_Exception(CRM_Core_Error::getMessages($recurring));
}

return $recurring->id;
return CRM_Contribute_BAO_ContributionRecur::add($recurParams)->id;
}

}
39 changes: 32 additions & 7 deletions tests/phpunit/CRM/Member/Form/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,17 +769,15 @@ public function testSubmitRecur(): void {
'label' => 'PendingEdited',
]);

$form = $this->getForm();

$this->callAPISuccess('MembershipType', 'create', [
'id' => $this->ids['membership_type']['AnnualFixed'],
'duration_unit' => 'month',
'duration_interval' => 1,
'auto_renew' => TRUE,
]);
$form->preProcess();
$this->createLoggedInUser();
$params = $this->getBaseSubmitParams();
$form = $this->getForm();
$this->createLoggedInUser();
$form->_mode = 'test';
$form->_contactID = $this->_individualId;
$form->testSubmit($params);
Expand Down Expand Up @@ -813,9 +811,33 @@ public function testSubmitRecur(): void {
], CRM_Core_Session::singleton()->getStatus());
}

public function testSubmitRecurTwoRows() {
$this->createMembershipPriceSet();
/**
* Test submit recurring with two line items.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testSubmitRecurTwoRows(): void {
$pfvIDs = $this->createMembershipPriceSet();
$form = $this->getForm();
$form->_mode = 'live';
$priceParams = [
'price_' . $this->getPriceFieldID() => $pfvIDs,
'price_set_id' => $this->getPriceSetID(),
'frequency_interval' => 1,
'frequency_unit' => 'month',
'membership_type_id' => NULL,
];
$form->testSubmit(array_merge($this->getBaseSubmitParams(), $priceParams));
$memberships = $this->callAPISuccess('Membership', 'get')['values'];
$this->assertCount(2, $memberships);
$this->callAPISuccessGetSingle('Contribution', []);
$this->callAPISuccessGetCount('MembershipPayment', [], 2);
$lines = $this->callAPISuccess('LineItem', 'get', ['sequential' => 1])['values'];
$this->assertCount(2, $lines);
$this->assertEquals('civicrm_membership', $lines[0]['entity_table']);
$this->assertEquals('civicrm_membership', $lines[1]['entity_table']);

}

/**
Expand Down Expand Up @@ -1177,10 +1199,12 @@ public function buildAmountMembershipDiscount($pageType, &$form, &$amount) {
/**
* Get a membership form object.
*
* We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
* We need to instantiate the form to run preprocess, which means we have to
* trick it about the request method.
*
* @return \CRM_Member_Form_Membership
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected function getForm() {
if (isset($_REQUEST['cid'])) {
Expand All @@ -1189,6 +1213,7 @@ protected function getForm() {
$form = new CRM_Member_Form_Membership();
$_SERVER['REQUEST_METHOD'] = 'GET';
$form->controller = new CRM_Core_Controller();
$form->preProcess();
return $form;
}

Expand Down

0 comments on commit 36c2072

Please sign in to comment.