Import - fix regression on matching by contribution ID
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 24 Aug 2022 03:52:20 +0000 (15:52 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 24 Aug 2022 04:04:13 +0000 (16:04 +1200)
CRM/Contribute/Import/Parser/Contribution.php
tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
tests/phpunit/CRM/Contribute/Import/Parser/data/contributions_update.csv [new file with mode: 0644]

index 9c4a4bbd249d23e866c604cdf9fe98eddee8910b..eed146f4cb0d6cf9baa04b183a79b9ac2b781e20 100644 (file)
@@ -306,7 +306,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
         $paramValues['contact_type'] = $this->getContactType();
       }
       elseif ($this->isUpdateExisting() &&
-        (!empty($paramValues['contribution_id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
+        (!empty($paramValues['id']) || !empty($values['trxn_id']) || !empty($paramValues['invoice_id']))
       ) {
         $paramValues['contact_type'] = $this->getContactType();
       }
@@ -329,9 +329,9 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
       if ($this->isUpdateExisting()) {
         //fix for CRM-2219 - Update Contribution
         // onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
-        if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['contribution_id'])) {
+        if (!empty($paramValues['invoice_id']) || !empty($paramValues['trxn_id']) || !empty($paramValues['id'])) {
           $dupeIds = [
-            'id' => $paramValues['contribution_id'] ?? NULL,
+            'id' => $paramValues['id'] ?? NULL,
             'trxn_id' => $paramValues['trxn_id'] ?? NULL,
             'invoice_id' => $paramValues['invoice_id'] ?? NULL,
           ];
@@ -684,12 +684,14 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser {
               $params['contribution_contact_id'] = $matchingContactIds[0];
             }
           }
-          elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) {
+          elseif (!empty($params['id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) {
             // when update mode check contribution id or trxn id or
             // invoice id
+            // @todo - this check is obsolete. It survives for now
+            // in order to keep the rc patch small & non-conflicty.
             $contactId = new CRM_Contribute_DAO_Contribution();
-            if (!empty($params['contribution_id'])) {
-              $contactId->id = $params['contribution_id'];
+            if (!empty($params['id'])) {
+              $contactId->id = $params['id'];
             }
             elseif (!empty($params['trxn_id'])) {
               $contactId->trxn_id = $params['trxn_id'];
index e1463439fabdc93a7d4379381c8dc9b392d258aa..b5d934781bcac8eb589b2bcd7558872354d9ffb8 100644 (file)
@@ -277,6 +277,9 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     $this->assertEquals('No matching Contact found for (mum@example.com )', $row['_status_message']);
   }
 
+  /**
+   * @throws \CRM_Core_Exception
+   */
   public function testImportWithMatchByExternalIdentifier() :void {
     CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_contact AUTO_INCREMENT = 1000000");
 
@@ -387,6 +390,44 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     return $userJobID;
   }
 
+  /**
+   * Test that existing contributions are found and updated.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testImportUpdateExisting(): void {
+    $this->contributionCreate([
+      'contact_id' => $this->individualCreate(),
+      'trxn_id' => 'abc',
+      'invoice_id' => '65',
+      'total_amount' => 8,
+      'financial_type_id:name' => 'Event Fee',
+    ]);
+    $mapping = [
+      ['name' => 'contribution_id'],
+      ['name' => 'invoice_id'],
+      ['name' => 'trxn_id'],
+      ['name' => ''],
+      ['name' => 'total_amount'],
+      ['name' => 'receive_date'],
+      ['name' => 'financial_type_id'],
+      ['name' => 'contribution_source'],
+      ['name' => ''],
+    ];
+    $this->importCSV('contributions_update.csv', $mapping, ['onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE]);
+    $rows = $this->getDataSource()->getRows();
+    foreach ($rows as $row) {
+      if ($row[8] === 'valid') {
+        // Note that as the valid value is 'IMPORTED' - but the fix to make that correct
+        // is not in 5.53 so temporary support for pledge_payment_imported
+        $this->assertContains($row[10], ['pledge_payment_imported', 'IMPORTED'], $row[11]);
+      }
+      else {
+        $this->assertEquals('ERROR', $row[10], $row[11]);
+      }
+    }
+  }
+
   /**
    * Add a random extra option value
    *
diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/data/contributions_update.csv b/tests/phpunit/CRM/Contribute/Import/Parser/data/contributions_update.csv
new file mode 100644 (file)
index 0000000..9e3ee80
--- /dev/null
@@ -0,0 +1,7 @@
+Contribution ID,Invoice ID,Trxn ID,Contact ID,Total Amount,Receive Date,Financial Type,Reason,Row Status
+1,67,abc,8,999,2008-09-20,Donation,Invoice ID mismatch,invalid
+1,67,abcd,8,999,2008-09-20,Donation,Trxn ID mismatch,invalid
+1,65,abc,3,999,2008-09-20,Donation,All fields OK,valid
+,65,abc,3,999,2008-09-20,Donation,Only Invoice ID OK,valid
+1,,"",3,99,2008-09-20,Donation,Only Contribution ID OK,valid
+