Fix for issue when optional contact_id is blank
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 22 Mar 2023 09:22:37 +0000 (22:22 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 22 Mar 2023 10:57:28 +0000 (23:57 +1300)
CRM/Contribute/Import/Parser/Contribution.php
tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
tests/phpunit/CRM/Contribute/Import/Parser/data/contributions.csv

index a379e26bff48ad0cb671ccfea1aefc3d4acfd866..6f47c715d30ad638eb303a30d32ea2835b0f0ca7 100644 (file)
@@ -232,7 +232,19 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
         $params[$entity][$this->getFieldMetadata($mappedField['name'])['name']] = $this->getTransformedFieldValue($mappedField['name'], $fieldValue);
       }
     }
-    return $params;
+    return $this->removeEmptyValues($params);
+  }
+
+  protected function removeEmptyValues($array) {
+    foreach ($array as $key => $value) {
+      if (is_array($value)) {
+        $array[$key] = $this->removeEmptyValues($value);
+      }
+      elseif ($value === '') {
+        unset($array[$key]);
+      }
+    }
+    return $array;
   }
 
   /**
index 9f46ee1330be4141f426d4dc6e55fe676c791107..caaa6240322a0d0f8a29a4af5c6b5cfa6ca7c3e6 100644 (file)
@@ -597,6 +597,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
       ['name' => ''],
       ['name' => 'trxn_id'],
       ['name' => 'contribution_campaign_id'],
+      ['name' => 'contribution_contact_id'],
     ];
     // First we try to create without total_amount mapped.
     // It will fail in create mode as total_amount is required for create.
@@ -616,7 +617,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     $this->importCSV('contributions.csv', $fieldMappings, ['onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP]);
 
     $row = $this->getDataSource()->getRows()[0];
-    $this->assertEquals('IMPORTED', $row[10]);
+    $this->assertEquals('IMPORTED', $row[11]);
     $contribution = Contribution::get()->addSelect('source', 'id')->execute()->first();
     $this->assertEmpty($contribution['source']);
 
@@ -635,7 +636,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     $this->importCSV('contributions.csv', $fieldMappings, ['onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE]);
 
     $row = $this->getDataSource()->getRows()[0];
-    $this->assertEquals('IMPORTED', $row[10]);
+    $this->assertEquals('IMPORTED', $row[11]);
     $contribution = Contribution::get()->addSelect('source', 'id')->execute()->first();
     $this->assertEquals('Call him back', $contribution['source']);
   }
index a16d213fb7ac66fcdecdcc67776dd0efbf624cf2..7080b8ef977f7cf83e9a18fc78a23ede6d349517 100644 (file)
@@ -1,2 +1,2 @@
-External Identifier,Total Amount,Receive Date,Financial Type,Soft Credit to,Source,Note,Transaction ID,Campaign ID
-bob,65,2008-09-20,Donation,mum@example.com,Word of mouth,Call him back,999,1
+External Identifier,Total Amount,Receive Date,Financial Type,Soft Credit to,Source,Note,Transaction ID,Campaign ID,Blank column
+bob,65,2008-09-20,Donation,mum@example.com,Word of mouth,Call him back,999,1,