From 1a00ea3c4ef690d432a9a6e26df21a59ac472e86 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 12 Jul 2015 19:24:46 +1200 Subject: [PATCH] minor refactors toward getting membership_recur test to pass --- CRM/Contribute/Form/Contribution/Main.php | 2 +- CRM/Member/BAO/Membership.php | 14 ++--- CRM/Member/Form.php | 2 +- CRM/Member/Form/MembershipRenewal.php | 57 ++++++++++--------- .../CRM/Member/Form/MembershipRenewalTest.php | 13 +---- 5 files changed, 40 insertions(+), 48 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index ac7490095f..8bed98d4dc 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -1198,7 +1198,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } else { $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, - $params['selectMembership'] + (array) $params['selectMembership'] ); } $memFee = $membershipTypeValues['minimum_fee']; diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 7cec76f1cb..7289b36f47 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -1747,7 +1747,7 @@ WHERE civicrm_membership.contact_id = civicrm_contact.id * Build an array of available membership types. * * @param CRM_Core_Form $form - * @param int $membershipTypeID + * @param array $membershipTypeID * @param bool $activeOnly * Do we only want active ones? * (probably this should default to TRUE but as a newly added parameter we are leaving default b @@ -1755,21 +1755,17 @@ WHERE civicrm_membership.contact_id = civicrm_contact.id * * @return array */ - public static function buildMembershipTypeValues(&$form, $membershipTypeID = NULL, $activeOnly = FALSE) { + public static function buildMembershipTypeValues(&$form, $membershipTypeID = array(), $activeOnly = FALSE) { $whereClause = " WHERE domain_id = " . CRM_Core_Config::domainID(); + $membershipTypeIDS = (array) $membershipTypeID; if ($activeOnly) { $whereClause .= " AND is_active = 1 "; } - if (is_array($membershipTypeID)) { - $allIDs = implode(',', $membershipTypeID); + if (!empty($membershipTypeIDS)) { + $allIDs = implode(',', $membershipTypeIDS); $whereClause .= " AND id IN ( $allIDs )"; } - elseif (is_numeric($membershipTypeID) && - $membershipTypeID > 0 - ) { - $whereClause .= " AND id = $membershipTypeID"; - } $query = " SELECT * diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 0c86ccfb6f..06a3c8f8c6 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -87,7 +87,7 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { $this->assign('context', $this->_context); $this->assign('membershipMode', $this->_mode); - $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, NULL, TRUE); + $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, array(), TRUE); foreach ($this->allMembershipTypeDetails as $index => $membershipType) { if ($membershipType['auto_renew']) { $this->_recurMembershipTypes[$index] = $membershipType; diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 1773369618..507e0d20a8 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -431,8 +431,17 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { public function postProcess() { // get the submitted form values. $this->_params = $formValues = $this->controller->exportValues($this->_name); + $this->assignBillingName(); - $statusMsg = $this->submit($formValues); + try { + $statusMsg = $this->submit($formValues); + } + catch (\Civi\Payment\Exception\PaymentProcessorException $e) { + CRM_Core_Error::displaySessionError($e->getMessage()); + CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', + "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}" + )); + } CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success'); } @@ -528,8 +537,6 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $fields["email-{$this->_bltID}"] = 1; - $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type'); - $nameFields = array('first_name', 'middle_name', 'last_name'); foreach ($nameFields as $name) { @@ -539,11 +546,12 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $formValues['preserveDBName'] = TRUE; } } - +dpm($fields); //here we are setting up the billing contact - if different from the member they are already created // but they will get billing details assigned CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields, - $this->_contributorContactID, NULL, NULL, $ctype + $this->_contributorContactID, NULL, NULL, + CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') ); // add all the additional payment params we need @@ -574,18 +582,11 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); - if (!empty($paymentParams['auto_renew'])) { + if (!empty($formValues['auto_renew'])) { $contributionRecurParams = $this->processRecurringContribution($paymentParams); $paymentParams = array_merge($paymentParams, $contributionRecurParams); } - $result = $payment->doDirectPayment($paymentParams); - - if (is_a($result, 'CRM_Core_Error')) { - CRM_Core_Error::displaySessionError($result); - CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', - "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}" - )); - } + $result = $payment->doPayment($paymentParams); if ($result) { $this->_params = array_merge($this->_params, $result); @@ -747,19 +748,6 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { )); $this->assign('customValues', $customValues); if ($this->_mode) { - if (!empty($this->_params['billing_first_name'])) { - $name = $this->_params['billing_first_name']; - } - - if (!empty($this->_params['billing_middle_name'])) { - $name .= " {$this->_params['billing_middle_name']}"; - } - - if (!empty($this->_params['billing_last_name'])) { - $name .= " {$this->_params['billing_last_name']}"; - } - $this->assign('billingName', $name); - // assign the address formatted up for display $addressParts = array( "street_address-{$this->_bltID}", @@ -829,4 +817,19 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $this->submit($formValues); } + protected function assignBillingName() { + if (!empty($this->_params['billing_first_name'])) { + $name = $this->_params['billing_first_name']; + } + + if (!empty($this->_params['billing_middle_name'])) { + $name .= " {$this->_params['billing_middle_name']}"; + } + + if (!empty($this->_params['billing_last_name'])) { + $name .= " {$this->_params['billing_last_name']}"; + } + $this->assign('billingName', $name); + } + } diff --git a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php index 019ce0d8c0..5367dfc080 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php @@ -148,14 +148,6 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { $this->callAPISuccess('relationship_type', 'delete', array('id' => 20)); } - /** - * Test CRM_Member_Form_Membership::buildQuickForm() - */ - //function testCRMMemberFormMembershipBuildQuickForm() - //{ - // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" ); - //} - /** * Test the submit function of the membership form. */ @@ -174,7 +166,8 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { 'num_terms' => '1', 'source' => '', 'total_amount' => '50.00', - 'financial_type_id' => '2', //Member dues, see data.xml + //Member dues, see data.xml + 'financial_type_id' => '2', 'soft_credit_type_id' => '', 'soft_credit_contact_id' => '', 'from_email_address' => '"Demonstrators Anonymous" ', @@ -267,7 +260,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase { $form->testSubmit($params); $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId)); - //$this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 1); + $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 1); $contribution = $this->callAPISuccess('Contribution', 'get', array( 'contact_id' => $this->_individualId, 'is_test' => TRUE, -- 2.25.1