Skip to content

Commit

Permalink
WIP - membership tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaphoneJon committed Feb 25, 2022
1 parent e08e41c commit 02449cc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 29 deletions.
11 changes: 3 additions & 8 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,21 +1157,16 @@ protected function setRecurringMembershipParams() {
if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required'])
|| (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) &&
!empty($this->_params['is_recur']))
&& !empty($this->_paymentProcessor['is_recur'])
) {
$this->_params['auto_renew'] = TRUE;
}
if (!empty($this->_paymentProcessor['is_recur'])
&& !empty($this->_params['auto_renew'])
&& empty($this->_params['is_recur']) && empty($this->_params['frequency_interval'])
) {

$this->_params['is_recur'] = $this->_values['is_recur'] = 1;
$membershipTypeDetails = \Civi\Api4\MembershipType::get()
->addWhere('id', '=', $selectedMembershipTypeID)
->execute()
->first();
$this->_params['frequency_interval'] = $this->_values['fee'][$priceFieldId]['options'][$priceFieldValue]['membership_num_terms'];
$this->_params['frequency_unit'] = $membershipTypeDetails['duration_unit'];
$this->_params['frequency_interval'] = $this->_params['frequency_interval'] ?? $this->_values['fee'][$priceFieldId]['options'][$priceFieldValue]['membership_num_terms'];
$this->_params['frequency_unit'] = $this->_params['frequency_unit'] ?? $membershipTypeDetails['duration_unit'];
}
}

Expand Down
96 changes: 75 additions & 21 deletions tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
*/
class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {

/**
* The id of the contribution page.
* @var int
*/
private int $contributionPageId;

/**
* The id of the contribution page's payment processor.
* @var int
*/
private int $paymentProcessorId;

/**
* The price set of the contribution page.
* @var int
*/
private int $priceSetId;

/**
* Clean up DB.
*/
Expand All @@ -26,53 +44,84 @@ public function tearDown(): void {
parent::tearDown();
}

/**
* Given a membership type ID, return the price field value.
*/
private function getPriceFieldValue($membershipTypeId) {
return $this->callAPISuccessGetValue('PriceFieldValue', ['membership_type_id' => $membershipTypeId, 'return' => 'id']);
}

/**
* Establish a standard list of submit params to more accurately test the submission.
*/
private function getSubmitParams() {
return [
'id' => $this->contributionPageId,
'amount' => 80,
'first_name' => 'Billy',
'last_name' => 'Gruff',
'email' => 'billy@goat.gruff',
'payment_processor_id' => $this->paymentProcessorId,
'credit_card_number' => '4111111111111111',
'credit_card_type' => 'Visa',
'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
'cvv2' => 123,
'auto_renew' => 1,
'priceSetId' => $this->priceSetId,
];
}

/**
* Test that the membership is set to recurring if the membership type is always autorenew.
*/
public function testSetRecurFunction() {
$membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
$form = $this->getContributionForm();
$form->testSubmit([
'selectMembership' => $membershipTypeID,
]);
$priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
$form->testSubmit(array_merge($this->getSubmitParams(), [
'price_' . $this->priceSetId => $priceFieldValueId,
]));
$this->assertEquals(1, $form->_params['is_recur']);
}

/**
* Test that the membership is set to recurring if the membership type is always autorenew.
* Test that the membership is set to recurring if the membership type is optionally autorenew and is_recur is true.
*/
public function testSetRecurFunctionOptionalYes() {
$membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
$form = $this->getContributionForm();
$form->testSubmit([
'selectMembership' => $membershipTypeID,
$priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
$form->testSubmit(array_merge($this->getSubmitParams(), [
'price_' . $this->priceSetId => $priceFieldValueId,
'is_recur' => 1,
]);
]));
$this->assertEquals(1, $form->_params['is_recur']);
}

/**
* Test that the membership is set to recurring if the membership type is always autorenew.
* Test that the membership is not set to recurring if the membership type is optionally autorenew and is_recur is false.
*/
public function testSetRecurFunctionOptionalNo() {
$membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
$form = $this->getContributionForm();
$form->testSubmit([
'selectMembership' => $membershipTypeID,
$priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
$form->testSubmit(array_merge($this->getSubmitParams(), [
'price_' . $this->priceSetId => $priceFieldValueId,
'is_recur' => 0,
]);
]));
$this->assertEquals(0, $form->_params['is_recur']);
}

/**
* Test that the membership is set to recurring if the membership type is always autorenew.
* Test that the membership doesn't have an "is_recur" key if the membership type can never autorenew.
*/
public function testSetRecurFunctionNotAvailable() {
$membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 0, 'minimum_fee' => 80]);
$form = $this->getContributionForm();
$form->testSubmit([
'selectMembership' => $membershipTypeID,
]);
$priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
$form->testSubmit(array_merge($this->getSubmitParams(), [
'price_' . $this->priceSetId => $priceFieldValueId,
]));
$this->assertArrayNotHasKey('is_recur', $form->_params);
}

Expand All @@ -82,11 +131,16 @@ public function testSetRecurFunctionNotAvailable() {
* @return \CRM_Contribute_Form_Contribution_Main
*/
protected function getContributionForm($params = []) {
$params['priceSetID'] = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
$this->priceSetId = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
'name' => 'default_membership_type_amount',
'return' => 'id',
]);

$paymentProcessor = $this->paymentProcessorCreate([
'payment_processor_type_id' => 'Dummy',
'is_test' => 0,
]);

$contributionPageParams = (array_merge($params, [
'currency' => 'NZD',
'goal_amount' => 6000,
Expand All @@ -95,20 +149,20 @@ protected function getContributionForm($params = []) {
'pay_later_text' => 'Front up',
'pay_later_receipt' => 'Ta',
'is_email_receipt' => 1,
'payment_processor' => $this->paymentProcessorCreate([
'payment_processor_type_id' => 'Dummy',
'is_test' => 0,
]),
'payment_processor' => $paymentProcessor,
'amount_block_is_active' => 1,
]));

/** @var \CRM_Contribute_Form_Contribution_Main $form */
$form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main');
$contributionPage = reset($this->contributionPageCreate($contributionPageParams)['values']);
$form->set('id', $contributionPage['id']);
CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $params['priceSetID']);
CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $this->priceSetId);
$form->preProcess();
$form->buildQuickForm();
// Need these values to create more realistic submit params (in getSubmitParams).
$this->paymentProcessorId = $paymentProcessor;
$this->contributionPageId = (int) $contributionPage['id'];
return $form;
}

Expand Down

0 comments on commit 02449cc

Please sign in to comment.