[Ref] Unit test attempt to create reported bugs , minor refactor
authoreileen <emcnaughton@wikimedia.org>
Tue, 9 Jun 2020 07:50:02 +0000 (19:50 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 20 Jul 2020 02:39:11 +0000 (14:39 +1200)
This lays the ground work to test a couple of reported bugs. Minor cleanup of code

CRM/Member/BAO/Membership.php
CRM/Member/Import/Parser.php
CRM/Member/Import/Parser/Membership.php
tests/phpunit/CRM/Member/Form/MembershipTest.php
tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php

index 42582dac5cdc760452c6d9d4aa770fd76c015122..18cffb95a5a694196d3712cf5fa2671bd23eb8e1 100644 (file)
@@ -897,13 +897,11 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    *
    * @return array
    *   array of importable Fields
+   * @throws \CRM_Core_Exception
    */
-  public static function &importableFields($contactType = 'Individual', $status = TRUE) {
-    if (!self::$_importableFields) {
-      if (!self::$_importableFields) {
-        self::$_importableFields = [];
-      }
-
+  public static function importableFields($contactType = 'Individual', $status = TRUE) {
+    $fields = Civi::cache('fields')->get('membership_importable_fields' . $contactType . $status);
+    if (!$fields) {
       if (!$status) {
         $fields = ['' => ['title' => '- ' . ts('do not import') . ' -']];
       }
@@ -941,16 +939,16 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
         }
       }
       $tmpContactField['external_identifier'] = $contactFields['external_identifier'];
-      $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " " . ts('(match to contact)');
+      $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . ' ' . ts('(match to contact)');
 
-      $tmpFields['membership_contact_id']['title'] = $tmpFields['membership_contact_id']['title'] . " " . ts('(match to contact)');
+      $tmpFields['membership_contact_id']['title'] .= ' ' . ts('(match to contact)');
 
       $fields = array_merge($fields, $tmpContactField);
       $fields = array_merge($fields, $tmpFields);
       $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Membership'));
-      self::$_importableFields = $fields;
+      Civi::cache('fields')->set('membership_importable_fields' . $contactType . $status, $fields);
     }
-    return self::$_importableFields;
+    return $fields;
   }
 
   /**
index fb47d1f458a297542361a92eff46b38942243921..045d336a847ebc3d59406fd19a9a7d8ebf010343 100644 (file)
@@ -13,6 +13,8 @@
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
+ *
+ * Class CRM_Member_Import_Parser
  */
 abstract class CRM_Member_Import_Parser extends CRM_Import_Parser {
 
index b43b507705f78bd47e16913f6ec49990093e043b..50e75e19ee31a7f0fb1525daab4d05bac0ad5872 100644 (file)
@@ -125,7 +125,7 @@ class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser {
    */
   public function summary(&$values) {
     $erroneousField = NULL;
-    $response = $this->setActiveFieldValues($values, $erroneousField);
+    $this->setActiveFieldValues($values, $erroneousField);
 
     $errorRequired = FALSE;
 
@@ -268,7 +268,7 @@ class CRM_Member_Import_Parser_Membership extends CRM_Member_Import_Parser {
       }
 
       $session = CRM_Core_Session::singleton();
-      $dateType = $session->get('dateTypes');
+      $dateType = CRM_Core_Session::singleton()->get('dateTypes');
       $formatted = [];
       $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Membership';
       $customFields = CRM_Core_BAO_CustomField::getFields($customDataType);
index ec0cee5f8f1f5cd771a17b66dae4c12a9695ce0f..ef0d0e99bda8a0b0c14ca9583844f1e9c8f1a361 100644 (file)
@@ -1217,6 +1217,7 @@ Expires: ',
    * @param int $contactId Id of contact on which the memberships will be created.
    *
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   protected function createTwoMembershipsViaPriceSetInBackEnd($contactId) {
     $form = $this->getForm();
index efc8748ce0c045085c96666dc0515d4bfaf5e7c8..59de09c1ce9f2f2847ed6f25939f4cc4d51d0536 100644 (file)
@@ -31,6 +31,8 @@
  * @group headless
  */
 class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
+  use CRMTraits_Custom_CustomDataTrait;
+
   /**
    * Membership type name used in test function.
    *
@@ -79,8 +81,8 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
       'fixed_period_start_day' => 101,
       'fixed_period_rollover_day' => 1231,
     ];
-    $ids = [];
-    $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
+
+    $membershipType = CRM_Member_BAO_MembershipType::add($params);
     $this->_membershipTypeID = $membershipType->id;
 
     $this->_mebershipStatusID = $this->membershipStatusCreate('test status');
@@ -102,10 +104,10 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
       'civicrm_membership_payment',
       'civicrm_contact',
     ];
-    $this->quickCleanup($tablesToTruncate, TRUE);
     $this->relationshipTypeDelete($this->_relationshipTypeId);
     $this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
     $this->membershipStatusDelete($this->_mebershipStatusID);
+    $this->quickCleanup($tablesToTruncate, TRUE);
   }
 
   /**
@@ -328,4 +330,35 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
     return $membershipImporter;
   }
 
+  /**
+   * Test importing to a custom field.
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  public function testImportCustomData() {
+    $donaldDuckID = $this->individualCreate(['first_name' => 'Donald', 'last_name' => 'Duck']);
+    $this->createCustomGroupWithFieldsOfAllTypes(['extends' => 'Membership']);
+    $membershipImporter = $this->createImportObject([
+      'membership_contact_id',
+      'membership_type_id',
+      'membership_start_date',
+      $this->getCustomFieldName('text'),
+      $this->getCustomFieldName('select_string'),
+    ]);
+    $importValues = [
+      $donaldDuckID,
+      $this->_membershipTypeID,
+      date('Y-m-d'),
+      'blah',
+      'Red',
+    ];
+
+    $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
+    $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
+    $membership = $this->callAPISuccessGetSingle('Membership', []);
+    $this->assertEquals('blah', $membership[$this->getCustomFieldName('text')]);
+    $this->assertEquals('R', $membership[$this->getCustomFieldName('select_string')]);
+  }
+
 }