minor refactors toward getting membership_recur test to pass
authorEileen McNaughton <eileen@fuzion.co.nz>
Sun, 12 Jul 2015 07:24:46 +0000 (19:24 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Sun, 12 Jul 2015 07:24:46 +0000 (19:24 +1200)
CRM/Contribute/Form/Contribution/Main.php
CRM/Member/BAO/Membership.php
CRM/Member/Form.php
CRM/Member/Form/MembershipRenewal.php
tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php

index ac7490095fa24e1aec85c4747f38e1f6d8acec13..8bed98d4dc3fd47d677a3049f91873497beec5ee 100644 (file)
@@ -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'];
index 7cec76f1cbf0946b7dde9da5f72c25c27b50dc8e..7289b36f4745781b167c41cc795cc2b79019f10f 100644 (file)
@@ -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 *
index 0c86ccfb6ff41fce837b200e27c9e577df92b887..06a3c8f8c6046a6be543a708b5a07f62ad8a2acb 100644 (file)
@@ -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;
index 1773369618c724cbb9b4a47c67f64b37873ddf89..507e0d20a82f45cf532f1b82bb7e077a7e085a91 100644 (file)
@@ -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);
+  }
+
 }
index 019ce0d8c004a0679877d08d05a515930669808e..5367dfc080d5c7a5f408b54ba72edd95ddb48e88 100644 (file)
@@ -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" <info@example.org>',
@@ -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,