Add handling & test for translated contribution status
authoreileen <emcnaughton@wikimedia.org>
Thu, 25 Jul 2019 05:49:18 +0000 (17:49 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 29 Jul 2019 00:54:30 +0000 (12:54 +1200)
CRM/Contribute/Import/Parser/Contribution.php
tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php

index 2b8b3f32823d85eabd3a0aad24e1a1e36b61b17a..b6c801431f57115bf89d3670ebbff7b0c8c56725 100644 (file)
@@ -806,7 +806,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
         case 'total_amount':
         case 'fee_amount':
         case 'net_amount':
-        // @todo add test like testPaymentTypeLabel & remove these lines as we can anticipate error will still be caught & handled.
+          // @todo add test like testPaymentTypeLabel & remove these lines as we can anticipate error will still be caught & handled.
           if (!CRM_Utils_Rule::money($value)) {
             return civicrm_api3_create_error("$key not a valid amount: $value");
           }
@@ -833,14 +833,6 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
           }
           break;
 
-        case 'contribution_status_id':
-          // @todo add test like testPaymentTypeLabel & remove these lines in favour of 'default' part of switch.
-          require_once 'CRM/Core/PseudoConstant.php';
-          if (!$values['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $value)) {
-            return civicrm_api3_create_error("Contribution Status is not valid: $value");
-          }
-          break;
-
         case 'soft_credit':
           // import contribution record according to select contact type
           // validate contact id and external identifier.
@@ -1011,8 +1003,13 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Contribute_Import_Pa
           break;
 
         default:
-          if  (isset($fields[$key]) && !empty($fields[$key]['is_pseudofield_for'])) {
-            $realField = $fields[$key]['is_pseudofield_for'];
+          // Hande name or label for fields with options.
+          if (isset($fields[$key]) &&
+            // Yay - just for a surprise we are inconsistent on whether we pass the pseudofield (payment_instrument)
+            // or the field name (contribution_status_id)
+            (!empty($fields[$key]['is_pseudofield_for']) || !empty($fields[$key]['pseudoconstant']))
+          ) {
+            $realField = $fields[$key]['is_pseudofield_for'] ?? $key;
             $realFieldSpec = $fields[$realField];
             /* @var \CRM_Core_DAO $bao */
             $bao = $realFieldSpec['bao'];
index 3078ca4c1703b49ab12662cda79ddd6af2bd61f5..44a6161911e77718727ee688dccfda7909123f60 100644 (file)
@@ -15,8 +15,9 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
   /**
    * Setup function.
    */
-  public function setUp() {
-    parent::setUp();
+  public function tearDown() {
+    $this->quickCleanUpFinancialEntities();
+    parent::tearDown();
   }
 
   /**
@@ -55,9 +56,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     $mapperSoftCredit = [NULL, NULL, NULL, "external_identifier"];
     $mapperSoftCreditType = [NULL, NULL, NULL, "1"];
     $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Contribute_Import_Parser::SOFT_CREDIT, $mapperSoftCredit, NULL, $mapperSoftCreditType);
-    $params = [
-      "contact_id" => $contact1Id,
-    ];
+    $params = ['contact_id' => $contact1Id];
     $values = [];
     $contributionsOfMainContact = CRM_Contribute_BAO_Contribution::retrieve($params, $values, $values);
     $this->assertEquals(1230.99, $contributionsOfMainContact->total_amount);
@@ -110,10 +109,39 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     ]);
     $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'not at all random'];
     $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL);
-    $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'payment_instrument_id'  => 'random']);
+    $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'payment_instrument_id' => 'random']);
     $this->assertEquals('not at all random', $contribution['payment_instrument']);
   }
 
+  /**
+   * Test handling of contribution statuses.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testContributionStatusLabel() {
+    $contactID = $this->individualCreate();
+    $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check', 'contribution_status_id' => 'Pending'];
+    // Note that the expected result should logically be CRM_Import_Parser::valid but writing test to reflect not fix here
+    $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL);
+    $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID]);
+    $this->assertEquals('Pending', $contribution['contribution_status']);
+
+    $this->callAPISuccess('OptionValue', 'create', [
+      'option_group_id' => 'contribution_status',
+      'value' => 777,
+      'name' => 'random',
+      'label' => 'not at all random',
+    ]);
+    $values['contribution_status_id'] = 'not at all random';
+    $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL);
+    $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'contribution_status_id' => 'random']);
+    $this->assertEquals('not at all random', $contribution['contribution_status']);
+
+    $values['contribution_status_id'] = 'just say no';
+    $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::ERROR);
+    $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2);
+  }
+
   /**
    * Run the import parser.
    *