Add test to check update-with-id works and date handling works
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 4 Jun 2022 12:04:53 +0000 (00:04 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 6 Jun 2022 04:46:38 +0000 (16:46 +1200)
Fix test to test the matching csv (changed by mistake)

CRM/Member/Import/Parser/Membership.php
tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php
tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv
tests/phpunit/CRM/Member/Import/Parser/data/memberships_invalid.csv [new file with mode: 0644]
tests/phpunit/CRM/Member/Import/Parser/data/memberships_update_custom_date.csv [new file with mode: 0644]

index 67fa939784cdf26d3a2b65f57d943805d1cd85b9..56d97dba3a94ced3c3fa31c8be965b73ad126838 100644 (file)
@@ -316,36 +316,9 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
         $params['join_date'] = $params['start_date'];
       }
 
-      $session = CRM_Core_Session::singleton();
-      $dateType = CRM_Core_Session::singleton()->get('dateTypes');
       $formatted = $params;
-
-      $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Membership';
-      $customFields = CRM_Core_BAO_CustomField::getFields($customDataType);
-
       // don't add to recent items, CRM-4399
       $formatted['skipRecentView'] = TRUE;
-      foreach ($params as $key => $val) {
-        if ($val) {
-          switch ($key) {
-            case 'status_id':
-              // @todo - we can do this based on the presence of 'pseudoconstant' in the metadata rather than field specific.
-              $params[$key] = $this->parsePseudoConstantField($val, $this->fieldMetadata[$key]);
-              break;
-
-          }
-          if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
-            if ($customFields[$customFieldID]['data_type'] == 'Date') {
-              $this->formatCustomDate($params, $formatted, $dateType, $key);
-              unset($params[$key]);
-            }
-            elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
-              $params[$key] = CRM_Utils_String::strtoboolstr($val);
-            }
-          }
-        }
-      }
-      //date-Format part ends
 
       $formatValues = [];
       foreach ($params as $key => $field) {
@@ -371,7 +344,6 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
         //fix for CRM-2219 Update Membership
         // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
         if (!empty($formatted['is_override']) && empty($formatted['status_id'])) {
-          array_unshift($values, 'Required parameter missing: Status');
           $this->setImportStatus($rowNumber, 'ERROR', 'Required parameter missing: Status');
           return CRM_Import_Parser::ERROR;
         }
@@ -409,17 +381,16 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
       }
 
       //Format dates
-      $startDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $formatted), '%Y-%m-%d');
-      $endDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $formatted), '%Y-%m-%d');
-      $joinDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('join_date', $formatted), '%Y-%m-%d');
+      $startDate = $formatted['start_date'];
+      $endDate = $formatted['end_date'] ?? NULL;
+      $joinDate = $formatted['join_date'];
 
-      if (empty($formatValues['contact_id'])) {
+      if (empty($formatValues['id']) && empty($formatValues['contact_id'])) {
         $error = $this->checkContactDuplicate($formatValues);
 
         if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
           $matchedIDs = explode(',', $error['error_message']['params'][0]);
           if (count($matchedIDs) > 1) {
-            array_unshift($values, 'Multiple matching contact records detected for this row. The membership was not imported');
             $this->setImportStatus($rowNumber, 'ERROR', 'Multiple matching contact records detected for this row. The membership was not imported');
             return CRM_Import_Parser::ERROR;
           }
@@ -511,7 +482,6 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
           $checkCid->external_identifier = $formatValues['external_identifier'];
           $checkCid->find(TRUE);
           if ($checkCid->id != $formatted['contact_id']) {
-            array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
             $this->setImportStatus($rowNumber, 'ERROR', 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
             return CRM_Import_Parser::ERROR;
           }
@@ -545,7 +515,6 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
         }
         elseif (empty($formatted['is_override'])) {
           if (empty($calcStatus)) {
-            array_unshift($values, 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.');
             $this->setImportStatus($rowNumber, 'ERROR', 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.');
             return CRM_Import_Parser::ERROR;
           }
@@ -635,15 +604,6 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
 
     foreach ($params as $key => $value) {
 
-      //Handling Custom Data
-      if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
-        $values[$key] = $value;
-        $type = $customFields[$customFieldID]['html_type'];
-        if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) {
-          $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type);
-        }
-      }
-
       switch ($key) {
         case 'contact_id':
           if (!CRM_Utils_Rule::integer($value)) {
@@ -660,13 +620,6 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser {
           $values[$key] = $value;
           break;
 
-        case 'membership_type_id':
-          if (!array_key_exists($value, CRM_Member_PseudoConstant::membershipType())) {
-            throw new Exception('Invalid Membership Type Id');
-          }
-          $values[$key] = $value;
-          break;
-
         default:
           break;
       }
index 7e07799609657d7137b7610ca31742f6d8bce000..4561ef893aa6c18e22c0083dfdff63f085761dec 100644 (file)
@@ -50,7 +50,9 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
    *
    * @var string
    */
-  protected $_membershipTypeID = NULL;
+  protected $_membershipTypeID;
+
+  protected $entity = 'Membership';
 
   /**
    * Set up for test.
@@ -393,7 +395,7 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
    * Test the full form-flow import.
    */
   public function testImportCSV() :void {
-    $this->importCSV('memberships.csv', [
+    $this->importCSV('memberships_invalid.csv', [
       ['name' => 'membership_contact_id'],
       ['name' => 'membership_source'],
       ['name' => 'membership_type_id'],
@@ -424,6 +426,30 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
     $this->callAPISuccessGetSingle('Membership', []);
   }
 
+  /**
+   * Test dates are parsed.
+   */
+  public function testUpdateWithCustomDates(): void {
+    $this->createCustomGroupWithFieldOfType([], 'date');
+    $contactID = $this->individualCreate(['external_identifier' => 'ext-1']);
+    $this->callAPISuccess('Membership', 'create', [
+      'contact_id' => $contactID,
+      'membership_type_id' => 'General',
+      'start_date' => '2020-10-01',
+    ]);
+    $mapping = [
+      ['name' => 'membership_id'],
+      ['name' => 'membership_source'],
+      ['name' => 'membership_type_id'],
+      ['name' => 'membership_start_date'],
+      ['name' => $this->getCustomFieldName('date')],
+    ];
+    $this->importCSV('memberships_update_custom_date.csv', $mapping, ['dateFormats' => 32]);
+    $membership = $this->callAPISuccessGetSingle('Membership', []);
+    $this->assertEquals('2021-03-23', $membership['start_date']);
+    $this->assertEquals('2019-03-23 00:00:00', $membership[$this->getCustomFieldName('date')]);
+  }
+
   /**
    * Import the csv file values.
    *
index dfdf1524fbcdcd0bd9b1b8f5c17346af8638bd97..56acafa693754c5f416d976d01de6d58ecd0fc3f 100644 (file)
@@ -1,2 +1,2 @@
 Contact ID,Source,Membership Type,Start Date,Ignore me
-1,Import,General,2019-03-23,Just some cruft
+1,Import,Made Up,2019-03-23,Just some cruft
diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships_invalid.csv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_invalid.csv
new file mode 100644 (file)
index 0000000..56acafa
--- /dev/null
@@ -0,0 +1,2 @@
+Contact ID,Source,Membership Type,Start Date,Ignore me
+1,Import,Made Up,2019-03-23,Just some cruft
diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships_update_custom_date.csv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_update_custom_date.csv
new file mode 100644 (file)
index 0000000..f49273c
--- /dev/null
@@ -0,0 +1,2 @@
+Membership ID,Source,Membership Type,Start Date,Custom Date
+1,Import,General,23/03/2021,23/03/2019