[REF] extract member form batch update & add test
authoreileen <emcnaughton@wikimedia.org>
Thu, 26 Dec 2019 04:02:27 +0000 (17:02 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 26 Dec 2019 04:08:00 +0000 (17:08 +1300)
CRM/Member/Form/Task/Batch.php
tests/phpunit/CRM/Member/Form/Task/BatchTest.php [new file with mode: 0644]

index 814ced3cbb8d77c8a21ddd6739484f819df3ce4e..04c7bbb0c7b9f7a08c564c1db82e14499df5500d 100644 (file)
@@ -187,78 +187,86 @@ class CRM_Member_Form_Task_Batch extends CRM_Member_Form_Task {
   /**
    * Process the form after the input has been submitted and validated.
    *
-   *
-   * @return void
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public function postProcess() {
     $params = $this->exportValues();
-    // @todo extract submit functions &
-    // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
-    // handling of custom data, specifically checkbox fields.
+    if (isset($params['field'])) {
+      $this->submit($params);
+      CRM_Core_Session::setStatus(ts('Your updates have been saved.'), ts('Saved'), 'success');
+    }
+    else {
+      CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
+    }
+  }
+
+  /**
+   * @param array $params
+   *
+   * @return mixed
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function submit(array $params) {
     $dates = [
       'membership_join_date',
       'membership_start_date',
       'membership_end_date',
     ];
-    if (isset($params['field'])) {
-      $customFields = [];
-      foreach ($params['field'] as $key => $value) {
-        $params['id'] = $key;
-        if (!empty($value['membership_source'])) {
-          $value['source'] = $value['membership_source'];
-        }
+    $customFields = [];
+    foreach ($params['field'] as $key => $value) {
+      $value['id'] = $key;
+      if (!empty($value['membership_source'])) {
+        $value['source'] = $value['membership_source'];
+      }
 
-        if (!empty($value['membership_type'])) {
-          $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
-        }
+      if (!empty($value['membership_type'])) {
+        $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
+      }
 
-        unset($value['membership_source']);
-        unset($value['membership_type']);
+      unset($value['membership_source']);
+      unset($value['membership_type']);
 
-        //Get the membership status
-        $value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
-        unset($value['membership_status']);
-        foreach ($dates as $val) {
-          if (isset($value[$val])) {
-            $value[$val] = CRM_Utils_Date::processDate($value[$val]);
-          }
+      //Get the membership status
+      $value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
+      unset($value['membership_status']);
+      foreach ($dates as $val) {
+        if (isset($value[$val])) {
+          $value[$val] = CRM_Utils_Date::processDate($value[$val]);
+        }
+      }
+      if (empty($customFields)) {
+        if (empty($value['membership_type_id'])) {
+          $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
         }
-        if (empty($customFields)) {
-          if (empty($value['membership_type_id'])) {
-            $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
-          }
 
-          // membership type custom data
-          $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId);
+        // membership type custom data
+        $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId);
 
-          $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
-            CRM_Core_BAO_CustomField::getFields('Membership',
-              FALSE, FALSE, NULL, NULL, TRUE
-            )
-          );
-        }
-        //check for custom data
-        $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
-          $key,
-          'Membership',
-          $membershipTypeId
+        $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
+          CRM_Core_BAO_CustomField::getFields('Membership',
+            FALSE, FALSE, NULL, NULL, TRUE
+          )
         );
+      }
+      //check for custom data
+      $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
+        $key,
+        'Membership',
+        $membershipTypeId
+      );
 
-        $membership = CRM_Member_BAO_Membership::add($value);
+      $membership = CRM_Member_BAO_Membership::add($value);
 
-        // add custom field values
-        if (!empty($value['custom']) &&
-          is_array($value['custom'])
-        ) {
-          CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id);
-        }
+      // add custom field values
+      if (!empty($value['custom']) &&
+        is_array($value['custom'])
+      ) {
+        CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id);
       }
-
-      CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
-    }
-    else {
-      CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
     }
+    return $value;
   }
 
 }
diff --git a/tests/phpunit/CRM/Member/Form/Task/BatchTest.php b/tests/phpunit/CRM/Member/Form/Task/BatchTest.php
new file mode 100644 (file)
index 0000000..ce464ce
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * @package CiviCRM_APIv3
+ * @subpackage API_Contribution
+ * @group headless
+ */
+class CRM_Member_Form_Task_BatchTest extends CiviUnitTestCase {
+
+  use CRMTraits_Custom_CustomDataTrait;
+
+  /**
+   * Clean up after each test.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function tearDown() {
+    $this->validateAllContributions();
+    $this->validateAllPayments();
+    $this->quickCleanUpFinancialEntities();
+  }
+
+  /**
+   * Test batch submission.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testBatchSubmit() {
+    $form = $this->getFormObject('CRM_Member_Form_Task_Batch');
+    $membership1 = $this->contactMembershipCreate(['contact_id' => $this->individualCreate()]);
+    $membership2 = $this->contactMembershipCreate(['contact_id' => $this->individualCreate()]);
+    $this->createCustomGroupWithFieldOfType(['extends' => 'Membership'], 'text');
+    $form->submit([
+      'field' => [
+        $membership1 => [
+          $this->getCustomFieldName('text') => '80',
+          'membership_join_date' => '2019-12-26',
+        ],
+        $membership2 => [
+          $this->getCustomFieldName('text') => '100',
+          'membership_join_date' => '2019-11-26',
+          'membership_source' => 'form',
+        ],
+      ],
+    ]);
+    $memberships = $this->callAPISuccess('Membership', 'get', [])['values'];
+    $this->assertEquals('2019-12-26', $memberships[$membership1]['join_date']);
+    $this->assertEquals('2019-11-26', $memberships[$membership2]['join_date']);
+    $this->assertEquals('form', $memberships[$membership2]['source']);
+    $this->assertEquals(80, $memberships[$membership1][$this->getCustomFieldName('text')]);
+    $this->assertEquals(100, $memberships[$membership2][$this->getCustomFieldName('text')]);
+  }
+
+}