Skip to content

Commit

Permalink
Merge pull request #19902 from eileenmcnaughton/mem_cont
Browse files Browse the repository at this point in the history
[REF] [Towards membership api] Simplify determination on contribution recur contact id
  • Loading branch information
colemanw authored Apr 8, 2021
2 parents 008958d + 23f0a3f commit 3f979ab
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 73 deletions.
22 changes: 18 additions & 4 deletions CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,29 @@ public function storeContactFields($formValues) {
}

[$this->_memberDisplayName, $this->_memberEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);

$this->_contributorContactID = $this->getContributionContactID();
//CRM-10375 Where the payer differs to the member the payer should get the email.
// here we store details in order to do that
if (!empty($formValues['soft_credit_contact_id'])) {
$this->_receiptContactId = $this->_contributorContactID = $formValues['soft_credit_contact_id'];
$this->_receiptContactId = $formValues['soft_credit_contact_id'];
[$this->_contributorDisplayName, $this->_contributorEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contributorContactID);
}
else {
$this->_receiptContactId = $this->_contributorContactID = $this->_contactID;
$this->_receiptContactId = $this->_contactID;
$this->_contributorDisplayName = $this->_memberDisplayName;
$this->_contributorEmail = $this->_memberEmail;
}
}

/**
* Get the contact id for the contribution.
*
* @return int
*/
protected function getContributionContactID(): int {
return (int) ($this->getSubmittedValue('soft_credit_contact_id') ?: $this->getSubmittedValue('contact_id'));
}

/**
* Set variables in a way that can be accessed from different places.
*
Expand Down Expand Up @@ -494,7 +503,12 @@ protected function setPriceSetParameters(array $formValues): array {
*
* @param array $formValues
*/
public function testSubmit(array $formValues): void {
public function testSubmit(array $formValues = []): void {
if (empty($formValues)) {
// If getForm is used these will be set - this is now
// preferred.
$formValues = $this->controller->exportValues($this->_name);
}
$this->exportedValues = $formValues;
$this->setContextVariables($formValues);
$this->_memType = !empty($formValues['membership_type_id']) ? $formValues['membership_type_id'][1] : NULL;
Expand Down
8 changes: 3 additions & 5 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,7 @@ protected function processContribution(
$params,
$contributionParams
) {
$contactID = $contributionParams['contact_id'];
$contributionParams['contribution_recur_id'] = $this->legacyProcessRecurringContribution($params, $contactID);
$contributionParams['contribution_recur_id'] = $this->legacyProcessRecurringContribution($params);
return CRM_Contribute_BAO_Contribution::add($contributionParams);
}

Expand All @@ -1693,16 +1692,15 @@ protected function processContribution(
* This function was copied from another form & needs cleanup.
*
* @param array $params
* @param int $contactID
*
* @return int|null
* @throws \CiviCRM_API3_Exception
*/
protected function legacyProcessRecurringContribution(array $params, $contactID): ?int {
protected function legacyProcessRecurringContribution(array $params): ?int {
if (!$this->isCreateRecurringContribution()) {
return NULL;
}
$recurParams = ['contact_id' => $contactID];
$recurParams = ['contact_id' => $this->getContributionContactID()];
$recurParams['amount'] = $this->order->getTotalAmount();
// for the legacyProcessRecurringContribution function to be reached auto_renew must be true
$recurParams['auto_renew'] = TRUE;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Form/MembershipRenewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public static function formRule($params, $files, $self) {
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function postProcess() {
public function postProcess(): void {
// get the submitted form values.
$this->_params = $this->controller->exportValues($this->_name);
$this->assignBillingName();
Expand Down
81 changes: 37 additions & 44 deletions tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ public function tearDown(): void {
* @throws \CiviCRM_API3_Exception
*/
public function testSubmit(): void {
$form = $this->getForm();
$loggedInUserID = $this->createLoggedInUser();
$loggedInUserDisplayName = Contact::get()->addWhere('id', '=', $loggedInUserID)->addSelect('display_name')->execute()->first()['display_name'];
$params = $this->getBaseSubmitParams();
$form = $this->getForm(array_merge($params, ['total_amount' => 50]));
$form->_contactID = $this->_individualId;

$form->testSubmit(array_merge($params, ['total_amount' => 50]));
Expand Down Expand Up @@ -190,10 +190,10 @@ public function testSubmit(): void {
public function testSubmitWithTax(): void {
$this->enableTaxAndInvoicing();
$this->addTaxAccountToFinancialType($this->financialTypeID);
$form = $this->getForm();
$form->testSubmit(array_merge($this->getBaseSubmitParams(), [
$form = $this->getForm(array_merge($this->getBaseSubmitParams(), [
'total_amount' => '50.00',
]));
$form->testSubmit();
$contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualId, 'is_test' => TRUE, 'return' => ['total_amount', 'tax_amount']]);
$this->assertEquals(50, $contribution['total_amount']);
$this->assertEquals(4.55, $contribution['tax_amount']);
Expand Down Expand Up @@ -257,18 +257,15 @@ public function testSubmitChangeType(): void {
* @throws \CiviCRM_API3_Exception
*/
public function testSubmitRecur(): void {
$form = $this->getForm();

$this->callAPISuccess('MembershipType', 'create', [
'id' => $this->membershipTypeAnnualFixedID,
'duration_unit' => 'month',
'duration_interval' => 1,
'auto_renew' => TRUE,
]);
$form->preProcess();
$this->createLoggedInUser();
$params = [
'cid' => $this->_individualId,
$form = $this->getForm([
'contact_id' => $this->_individualId,
'price_set_id' => 0,
'join_date' => CRM_Utils_Time::date('m/d/Y'),
'start_date' => '',
Expand All @@ -290,7 +287,7 @@ public function testSubmitRecur(): void {
'cvv2' => '123',
'credit_card_exp_date' => [
'M' => '9',
'Y' => CRM_Utils_Time::date('Y') + 1,
'Y' => (int) CRM_Utils_Time::date('Y') + 1,
],
'credit_card_type' => 'Visa',
'billing_first_name' => 'Test',
Expand All @@ -301,11 +298,12 @@ public function testSubmitRecur(): void {
'billing_postal_code-5' => '90210',
'billing_country_id-5' => '1228',
'send_receipt' => 1,
];
]);
$this->createLoggedInUser();
$form->_mode = 'test';
$form->_contactID = $this->_individualId;

$form->testSubmit($params);
$form->testSubmit();
$membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
$contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
$this->assertEquals(1, $contributionRecur['is_email_receipt']);
Expand Down Expand Up @@ -349,7 +347,6 @@ public function testSubmitRecur(): void {
* @throws \CiviCRM_API3_Exception
*/
public function testSubmitRecurCompleteInstant(): void {
$form = $this->getForm();
/** @var \CRM_Core_Payment_Dummy $processor */
$processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID);
$processor->setDoDirectPaymentResult([
Expand All @@ -365,13 +362,12 @@ public function testSubmitRecurCompleteInstant(): void {
'auto_renew' => TRUE,
]);
$this->createLoggedInUser();
$form->preProcess();
$form = $this->getForm(array_merge($this->getBaseSubmitParams(), ['is_recur' => 1, 'auto_renew' => '1']));

$form->_contactID = $this->_individualId;
$params = array_merge($this->getBaseSubmitParams(), ['is_recur' => 1, 'auto_renew' => '1']);
$form->_mode = 'test';

$form->testSubmit($params);
$form->testSubmit();
$membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
$this->assertEquals('2020-04-13', $membership['join_date']);
$this->assertEquals(CRM_Utils_Time::date('Y-01-01'), $membership['start_date']);
Expand Down Expand Up @@ -430,15 +426,13 @@ public function testSubmitRecurCompleteInstant(): void {
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
* @dataProvider getThousandSeparators
*/
public function testSubmitRecurCompleteInstantWithMail(string $thousandSeparator): void {
$this->setCurrencySeparators($thousandSeparator);
// Visibility is 'Public Pages and Listings' in order to try to get to a specific line
// of code to ensure it's tested - it might not be a 'real' use case.
$this->createCustomGroupWithFieldOfType(['extends' => 'Membership'], 'multi_country', NULL, ['visibility' => 'Public Pages and Listings']);
$form = $this->getForm();
$this->mut = new CiviMailUtils($this, TRUE);
/** @var \CRM_Core_Payment_Dummy $processor */
$processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID);
Expand All @@ -455,17 +449,17 @@ public function testSubmitRecurCompleteInstantWithMail(string $thousandSeparator
'auto_renew' => TRUE,
]);
$this->createLoggedInUser();
$form->preProcess();

$form->_contactID = $this->_individualId;
$form->_mode = 'live';

$form->testSubmit(array_merge($this->getBaseSubmitParams(), [
$form = $this->getForm(array_merge($this->getBaseSubmitParams(), [
'is_recur' => 1,
'send_receipt' => 1,
'auto_renew' => 1,
$this->getCustomFieldName('multi_country') => [1006, 1007],
]));

$form->_contactID = $this->_individualId;
$form->_mode = 'live';

$form->testSubmit();
$contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
$this->assertEquals(1, $contributionRecur['is_email_receipt']);
$this->mut->checkMailLog([
Expand All @@ -483,11 +477,10 @@ public function testSubmitRecurCompleteInstantWithMail(string $thousandSeparator
* @throws \CiviCRM_API3_Exception
*/
public function testSubmitPayLater(): void {
$form = $this->getForm(NULL);
$this->createLoggedInUser();
$originalMembership = $this->callAPISuccessGetSingle('membership', []);
$params = [
'cid' => $this->_individualId,
$form = $this->getForm([
'contact_id' => $this->_individualId,
'join_date' => CRM_Utils_Time::date('m/d/Y'),
'start_date' => '',
'end_date' => '',
Expand All @@ -505,10 +498,10 @@ public function testSubmitPayLater(): void {
'record_contribution' => TRUE,
'trxn_id' => 777,
'contribution_status_id' => 2,
];
], NULL);
$form->_contactID = $this->_individualId;

$form->testSubmit($params);
$form->testSubmit();
$membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
$this->assertEquals(strtotime($membership['end_date']), strtotime($originalMembership['end_date']));
$contribution = $this->callAPISuccessGetSingle('Contribution', [
Expand All @@ -533,11 +526,10 @@ public function testSubmitPayLater(): void {
* @throws \CiviCRM_API3_Exception
*/
public function testSubmitPayLaterWithBilling(): void {
$form = $this->getForm(NULL);
$this->createLoggedInUser();
$originalMembership = $this->callAPISuccessGetSingle('membership', []);
$params = [
'cid' => $this->_individualId,
$form = $this->getForm([
'contact_id' => $this->_individualId,
'start_date' => '',
'end_date' => '',
// This format reflects the first value being the organisation & the second being the type.
Expand All @@ -561,10 +553,10 @@ public function testSubmitPayLaterWithBilling(): void {
'billing_state_province_id-5' => '1003',
'billing_postal_code-5' => '90210',
'billing_country_id-5' => '1228',
];
], NULL);
$form->_contactID = $this->_individualId;

$form->testSubmit($params);
$form->testSubmit();
$membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
$this->assertEquals(strtotime($membership['end_date']), strtotime($originalMembership['end_date']));
$this->assertEquals(10, $membership['max_related']);
Expand Down Expand Up @@ -594,11 +586,10 @@ public function testSubmitPayLaterWithBilling(): void {
* @throws \CiviCRM_API3_Exception
*/
public function testSubmitComplete(): void {
$form = $this->getForm(NULL);
$this->createLoggedInUser();
$originalMembership = $this->callAPISuccessGetSingle('membership', []);
$params = [
'cid' => $this->_individualId,
$form = $this->getForm([
'contact_id' => $this->_individualId,
'join_date' => CRM_Utils_Time::date('m/d/Y'),
'start_date' => '',
'end_date' => '',
Expand All @@ -617,10 +608,10 @@ public function testSubmitComplete(): void {
'trxn_id' => 777,
'contribution_status_id' => 1,
'fee_amount' => .5,
];
], NULL);
$form->_contactID = $this->_individualId;

$form->testSubmit($params);
$form->testSubmit();
$membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
$this->assertEquals(strtotime($membership['end_date']), strtotime('+ 2 years',
strtotime($originalMembership['end_date'])));
Expand All @@ -643,17 +634,18 @@ public function testSubmitComplete(): void {
*
* We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
*
* @param array $formValues
* @param string $mode
*
* @return \CRM_Member_Form_MembershipRenewal
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected function getForm($mode = 'test'): CRM_Member_Form_MembershipRenewal {
$form = new CRM_Member_Form_MembershipRenewal();
$_SERVER['REQUEST_METHOD'] = 'GET';
$form->controller = new CRM_Core_Controller();
protected function getForm($formValues = [], $mode = 'test'): CRM_Member_Form_MembershipRenewal {
/* @var CRM_Member_Form_MembershipRenewal $form */
$form = $this->getFormObject('CRM_Member_Form_MembershipRenewal', $formValues);

$form->_bltID = 5;
$form->_mode = $mode;
$form->setEntityId($this->_membershipID);
Expand All @@ -669,6 +661,7 @@ protected function getForm($mode = 'test'): CRM_Member_Form_MembershipRenewal {
protected function getBaseSubmitParams(): array {
return [
'cid' => $this->_individualId,
'contact_id' => $this->_individualId,
// This format reflects the key being the organisation & the value being the type.
'membership_type_id' => [$this->ids['contact']['organization'], $this->membershipTypeAnnualFixedID],
'num_terms' => '1',
Expand All @@ -682,7 +675,7 @@ protected function getBaseSubmitParams(): array {
'cvv2' => '123',
'credit_card_exp_date' => [
'M' => '9',
'Y' => CRM_Utils_Time::date('Y') + 1,
'Y' => (int) CRM_Utils_Time::date('Y') + 1,
],
'credit_card_type' => 'Visa',
'billing_first_name' => 'Test',
Expand All @@ -702,7 +695,7 @@ protected function getBaseSubmitParams(): array {
* @throws \CiviCRM_API3_Exception
*/
public function testSubmitRenewExpired(): void {
$form = $this->getForm(NULL);
$form = $this->getForm([], NULL);
$this->createLoggedInUser();
$originalMembership = $this->callAPISuccessGetSingle('membership', []);
$this->callAPISuccess('Membership', 'create', [
Expand Down
Loading

0 comments on commit 3f979ab

Please sign in to comment.