Fix some a couple of places where form variables were passed, remove a couple more...
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 9 Jun 2022 00:43:48 +0000 (12:43 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 9 Jun 2022 01:03:51 +0000 (13:03 +1200)
CRM/Contribute/Import/Form/MapField.php
CRM/Contribute/Import/Form/Preview.php
CRM/Import/Forms.php
tests/phpunit/CRM/Custom/Import/Parser/ApiTest.php [deleted file]

index 9026f66c5c6c344d39852f27682b279ae3575d2d..ccfcdff34317347aaa8072fb7311a0c3e0b7e91f 100644 (file)
@@ -48,11 +48,11 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
         }
         if ($field == $contactORContributionId) {
           if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
-            $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
+            !$self->isUpdateExisting()
           ) {
             $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', [1 => $threshold]) . '<br />';
           }
-          elseif ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
+          elseif ($self->isUpdateExisting() &&
             !(in_array('invoice_id', $importKeys) || in_array('trxn_id', $importKeys) ||
               in_array('contribution_id', $importKeys)
             )
@@ -74,15 +74,10 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
   public function preProcess() {
     parent::preProcess();
 
-    $this->_columnCount = $this->get('columnCount');
-    $skipColumnHeader = $this->getSubmittedValue('skipColumnHeader');
-    $this->_onDuplicate = $this->getSubmittedValue('onDuplicate');
-    $this->assign('skipColumnHeader', $skipColumnHeader);
-
     $highlightedFields = ['financial_type_id', 'total_amount'];
     //CRM-2219 removing other required fields since for updation only
     //invoice id or trxn id or contribution id is required.
-    if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
+    if ($this->isUpdateExisting()) {
       $remove = [
         'contribution_contact_id',
         'email',
@@ -104,7 +99,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
         $highlightedFields[] = $key;
       }
     }
-    elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) {
+    elseif ($this->isSkipExisting()) {
       unset($this->_mapperFields['contribution_id']);
       $highlightedFieldsArray = [
         'contribution_contact_id',
@@ -155,7 +150,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
     }
     $sel1 = $this->_mapperFields;
 
-    if (!$this->get('onDuplicate')) {
+    if (!$this->isUpdateExisting()) {
       unset($sel1['id']);
       unset($sel1['contribution_id']);
     }
@@ -178,7 +173,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
     foreach ($columnHeaders as $i => $columnHeader) {
       $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
       $jsSet = FALSE;
-      if ($this->get('savedMapping')) {
+      if ($this->getSubmittedValue('savedMapping')) {
         [$mappingName, $mappingContactType] = CRM_Core_BAO_Mapping::getMappingFields($savedMappingID);
 
         $mappingName = $mappingName[1];
@@ -265,7 +260,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
         $warning++;
       }
     }
-    if ($warning != 0 && $this->get('savedMapping')) {
+    if ($warning != 0 && $this->getSubmittedValue('savedMapping')) {
       $session = CRM_Core_Session::singleton();
       $session->setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.'));
     }
@@ -294,7 +289,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
   public static function formRule($fields, $files, $self) {
     $errors = [];
     $fieldMessage = NULL;
-    $contactORContributionId = $self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE ? 'contribution_id' : 'contribution_contact_id';
+    $contactORContributionId = $self->isUpdateExisting() ? 'contribution_id' : 'contribution_contact_id';
     if (!array_key_exists('savedMapping', $fields)) {
       $importKeys = [];
       foreach ($fields['mapper'] as $mapperPart) {
@@ -327,7 +322,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
       $errors = self::checkRequiredFields($self, $contactORContributionId, $importKeys, $errors, $weightSum, $threshold, $fieldMessage);
 
       //at least one field should be mapped during update.
-      if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
+      if ($self->isUpdateExisting()) {
         $atleastOne = FALSE;
         foreach ($self->_mapperFields as $key => $field) {
           if (in_array($key, $importKeys) &&
index 3bfc113f7b4ad277c869d6aacb95b86966f36147..85d9bcdf3c1e07b0d3c5213092f13021a5668f4e 100644 (file)
  */
 class CRM_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
 
-  /**
-   * Set variables up before form is built.
-   */
-  public function preProcess() {
-    parent::preProcess();
-    $invalidRowCount = $this->getRowCount(CRM_Import_Parser::VALID);
-
-    $downloadURL = '';
-    if ($invalidRowCount) {
-      $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
-      $downloadURL = CRM_Utils_System::url('civicrm/export', $urlParams);
-    }
-
-    $this->setStatusUrl();
-    $this->assign('downloadErrorRecordsUrl', $downloadURL);
-  }
-
-  /**
-   * Get the mapped fields as an array of labels.
-   *
-   * e.g
-   * ['First Name', 'Employee Of - First Name', 'Home - Street Address']
-   *
-   * @return array
-   * @throws \API_Exception
-   * @throws \CRM_Core_Exception
-   */
-  protected function getMappedFieldLabels(): array {
-    $mapper = [];
-    $parser = $this->getParser();
-    foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) {
-      $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber));
-    }
-    return $mapper;
-  }
-
   /**
    * @return \CRM_Contribute_Import_Parser_Contribution
    */
index 571ef0f87e8d694acc5c1916769cb547be830d61..0b37ea76b221e8e4ffae146d797c176a319a77a0 100644 (file)
@@ -575,7 +575,6 @@ class CRM_Import_Forms extends CRM_Core_Form {
    *
    * @return array
    * @throws \API_Exception
-   * @throws \CRM_Core_Exception
    */
   protected function getMappedFieldLabels(): array {
     $mapper = [];
@@ -643,4 +642,12 @@ class CRM_Import_Forms extends CRM_Core_Form {
     return ((int) $this->getSubmittedValue('onDuplicate')) === CRM_Import_Parser::DUPLICATE_UPDATE;
   }
 
+  /**
+   * Has the user chosen to update existing records.
+   * @return bool
+   */
+  protected function isSkipExisting(): bool {
+    return ((int) $this->getSubmittedValue('onDuplicate')) === CRM_Import_Parser::DUPLICATE_SKIP;
+  }
+
 }
diff --git a/tests/phpunit/CRM/Custom/Import/Parser/ApiTest.php b/tests/phpunit/CRM/Custom/Import/Parser/ApiTest.php
deleted file mode 100644 (file)
index f051157..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-<?php
-/**
- * @file
- * File for the CRM_Contribute_Import_Parser_ContributionTest class.
- */
-
-/**
- *  Test Contribution import parser.
- *
- * @package CiviCRM
- * @group headless
- */
-class CRM_Custom_Import_Parser_ApiTest extends CiviUnitTestCase {
-
-  use CRMTraits_Custom_CustomDataTrait;
-  use CRMTraits_Import_ParserTrait;
-
-  /**
-   * Test the full form-flow import.
-   */
-  public function testImport(): void {
-    $this->importCSV('contributions.csv', [
-      ['name' => 'first_name'],
-      ['name' => 'total_amount'],
-      ['name' => 'receive_date'],
-      ['name' => 'financial_type_id'],
-      ['name' => 'email'],
-    ]);
-    $dataSource = new CRM_Import_DataSource_CSV($this->userJobID);
-    $row = $dataSource->getRow();
-    $this->assertEquals('ERROR', $row['_status']);
-    $this->assertEquals('No matching Contact found for (mum@example.com )', $row['_status_message']);
-  }
-
-}