"CRM_Contribute_Form_Contribution_Main::formRule()": "toxicAlert",
"CRM_Contribute_Form_Contribution_Main::submit()": "toxicAlert",
"CRM_Contribute_Form_Task_Invoice::printPDF()": "toxicAlert",
- "CRM_Contribute_Import_Parser_Contribution::run()": "toxicAlert",
"CRM_Core_BAO_ActionScheduleTest::setUp()": "toxicAlert",
"CRM_Core_BAO_CustomField::formatCustomField()": "toxicAlert",
"CRM_Core_BAO_CustomGroup::getTree()": "toxicAlert",
$this->addContactTypeSelector();
}
- /**
- * Process the uploaded file.
- */
- public function postProcess() {
- $this->storeFormValues([
- 'onDuplicate',
- 'contactType',
- 'dateFormats',
- 'savedMapping',
- ]);
-
- $this->submitFileForMapping('CRM_Contribute_Import_Parser_Contribution');
- }
-
/**
* @return \CRM_Contribute_Import_Parser_Contribution
*/
*/
protected $_softCreditErrorsFileName;
- /**
- * @param string $fileName
- * @param string $separator
- * @param $mapper
- * @param bool $skipColumnHeader
- * @param int $mode
- * @param int $contactType
- * @param int $onDuplicate
- * @param int $statusID
- *
- * @return mixed
- * @throws Exception
- */
- public function run(
- $fileName,
- $separator,
- $mapper,
- $skipColumnHeader = FALSE,
- $mode = self::MODE_PREVIEW,
- $contactType = self::CONTACT_INDIVIDUAL,
- $onDuplicate = self::DUPLICATE_SKIP,
- $statusID = NULL
- ) {
- // Since $this->_contactType is still being called directly do a get call
- // here to make sure it is instantiated.
- $this->getContactType();
-
- $this->init();
-
- $this->_lineCount = $this->_validSoftCreditRowCount = $this->_validPledgePaymentRowCount = 0;
- $this->_invalidRowCount = $this->_validCount = $this->_invalidSoftCreditRowCount = $this->_invalidPledgePaymentRowCount = 0;
- $this->_totalCount = 0;
-
- $this->_errors = [];
- $this->_warnings = [];
- $this->_pledgePaymentErrors = [];
- $this->_softCreditErrors = [];
- if ($statusID) {
- $this->progressImport($statusID);
- $startTimestamp = $currTimestamp = $prevTimestamp = time();
- }
-
- if ($mode == self::MODE_MAPFIELD) {
- $this->_rows = [];
- }
- else {
- $this->_activeFieldCount = count($this->_activeFields);
- }
-
- $dataSource = $this->getDataSourceObject();
- $totalRowCount = $dataSource->getRowCount(['new']);
- $dataSource->setStatuses(['new']);
-
- while ($row = $dataSource->getRow()) {
- $values = array_values($row);
- $this->_lineCount++;
-
- $this->_totalCount++;
-
- if ($mode == self::MODE_MAPFIELD) {
- $returnCode = CRM_Import_Parser::VALID;
- }
- // Note that import summary appears to be unused
- elseif ($mode == self::MODE_PREVIEW || $mode == self::MODE_SUMMARY) {
- $returnCode = $this->summary($values);
- }
- elseif ($mode == self::MODE_IMPORT) {
- $this->import($values);
- if ($statusID && (($this->_lineCount % 50) == 0)) {
- $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
- }
- }
-
- }
-
- }
-
/**
* Get the field mappings for the import.
*
}
}
- /**
- * Store parser values.
- *
- * @param CRM_Core_Session $store
- *
- * @param int $mode
- */
- public function set($store, $mode = self::MODE_SUMMARY) {
- $store->set('totalRowCount', $this->_totalCount);
- $store->set('validRowCount', $this->_validCount);
- $store->set('invalidRowCount', $this->_invalidRowCount);
-
- $store->set('invalidSoftCreditRowCount', $this->_invalidSoftCreditRowCount);
- $store->set('validSoftCreditRowCount', $this->_validSoftCreditRowCount);
- $store->set('invalidPledgePaymentRowCount', $this->_invalidPledgePaymentRowCount);
- $store->set('validPledgePaymentRowCount', $this->_validPledgePaymentRowCount);
-
- switch ($this->_contactType) {
- case 'Individual':
- $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
- break;
-
- case 'Household':
- $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
- break;
-
- case 'Organization':
- $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
- }
-
- if ($this->_invalidRowCount) {
- $store->set('errorsFileName', $this->_errorFileName);
- }
- if (isset($this->_rows) && !empty($this->_rows)) {
- $store->set('dataValues', $this->_rows);
- }
-
- if ($this->_invalidPledgePaymentRowCount) {
- $store->set('pledgePaymentErrorsFileName', $this->_pledgePaymentErrorsFileName);
- }
-
- if ($this->_invalidSoftCreditRowCount) {
- $store->set('softCreditErrorsFileName', $this->_softCreditErrorsFileName);
- }
-
- if ($mode == self::MODE_IMPORT) {
- $store->set('duplicateRowCount', $this->_duplicateCount);
- if ($this->_duplicateCount) {
- $store->set('duplicatesFileName', $this->_duplicateFileName);
- }
- }
- }
-
- /**
- * Export data to a CSV file.
- *
- * @param string $fileName
- * @param array $header
- * @param array $data
- */
- public static function exportCSV($fileName, $header, $data) {
- $output = [];
- $fd = fopen($fileName, 'w');
-
- foreach ($header as $key => $value) {
- $header[$key] = "\"$value\"";
- }
- $config = CRM_Core_Config::singleton();
- $output[] = implode($config->fieldSeparator, $header);
-
- foreach ($data as $datum) {
- foreach ($datum as $key => $value) {
- if (isset($value[0]) && is_array($value)) {
- foreach ($value[0] as $k1 => $v1) {
- if ($k1 == 'location_type_id') {
- continue;
- }
- $datum[$k1] = $v1;
- }
- }
- else {
- $datum[$key] = "\"$value\"";
- }
- }
- $output[] = implode($config->fieldSeparator, $datum);
- }
- fwrite($fd, implode("\n", $output));
- fclose($fd);
- }
-
- /**
- * Determines the file extension based on error code.
- *
- * @param int $type
- * Error code constant.
- *
- * @return string
- */
- public static function errorFileName($type) {
- $fileName = NULL;
- if (empty($type)) {
- return $fileName;
- }
-
- $config = CRM_Core_Config::singleton();
- $fileName = $config->uploadDir . "sqlImport";
-
- switch ($type) {
- case self::SOFT_CREDIT_ERROR:
- $fileName .= '.softCreditErrors';
- break;
-
- case self::PLEDGE_PAYMENT_ERROR:
- $fileName .= '.pledgePaymentErrors';
- break;
-
- default:
- $fileName = parent::errorFileName($type);
- break;
- }
-
- return $fileName;
- }
-
- /**
- * Determines the file name based on error code.
- *
- * @param int $type
- * Error code constant.
- *
- * @return string
- */
- public static function saveFileName($type) {
- $fileName = NULL;
- if (empty($type)) {
- return $fileName;
- }
-
- switch ($type) {
- case self::SOFT_CREDIT_ERROR:
- $fileName = 'Import_Soft_Credit_Errors.csv';
- break;
-
- case self::PLEDGE_PAYMENT_ERROR:
- $fileName = 'Import_Pledge_Payment_Errors.csv';
- break;
-
- default:
- $fileName = parent::saveFileName($type);
- break;
- }
-
- return $fileName;
- }
-
/**
* The initializer code, called before the processing
*/
if (method_exists($parserName, 'errorFileName') &&
method_exists($parserName, 'saveFileName')
) {
+ CRM_Core_Error::deprecatedWarning('unused code');
$errorFileName = $parserName::errorFileName($type);
$saveFileName = $parserName::saveFileName($type);
if (!empty($errorFileName) && !empty($saveFileName)) {
*/
class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
use CRMTraits_Custom_CustomDataTrait;
+ use CRMTraits_Import_ParserTrait;
/**
* Default entity for class.
$this->callAPISuccessGetSingle('PledgePayment', ['pledge_id' => $pledgeID, 'contribution_id' => $contribution['id']]);
}
- /**
- * Import the csv file values.
- *
- * This function uses a flow that mimics the UI flow.
- *
- * @param string $csv Name of csv file.
- * @param array $fieldMappings
- * @param array $submittedValues
- */
- protected function importCSV(string $csv, array $fieldMappings, array $submittedValues = []): void {
- $submittedValues = array_merge([
- 'uploadFile' => ['name' => __DIR__ . '/data/' . $csv],
- 'skipColumnHeader' => TRUE,
- 'fieldSeparator' => ',',
- 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
- 'mapper' => $this->getMapperFromFieldMappings($fieldMappings),
- 'dataSource' => 'CRM_Import_DataSource_CSV',
- 'file' => ['name' => $csv],
- 'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd,
- 'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE,
- 'groups' => [],
- ], $submittedValues);
- $form = $this->getFormObject('CRM_Contribute_Import_Form_DataSource', $submittedValues);
- $form->buildForm();
- $form->postProcess();
- $this->userJobID = $form->getUserJobID();
- $form = $this->getFormObject('CRM_Contribute_Import_Form_MapField', $submittedValues);
- $form->setUserJobID($this->userJobID);
- $form->buildForm();
- $form->postProcess();
- /* @var CRM_Contribute_Import_Form_MapField $form */
- $form = $this->getFormObject('CRM_Contribute_Import_Form_Preview', $submittedValues);
- $form->setUserJobID($this->userJobID);
- $form->buildForm();
- $form->postProcess();
- }
-
/**
* Test phone is included if it is part of dedupe rule.
*
--- /dev/null
+<?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']);
+ }
+
+}
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved. |
+ | |
+ | This work is published under the GNU AGPLv3 license with some |
+ | permitted exceptions and without any warranty. For full license |
+ | and copyright information, see https://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Trait ParserTrait
+ *
+ * Trait for testing imports.
+ */
+trait CRMTraits_Import_ParserTrait {
+
+ /**
+ * Import the csv file values.
+ *
+ * This function uses a flow that mimics the UI flow.
+ *
+ * @param string $csv Name of csv file.
+ * @param array $fieldMappings
+ * @param array $submittedValues
+ */
+ protected function importCSV(string $csv, array $fieldMappings, array $submittedValues = []): void {
+ $reflector = new ReflectionClass(get_class($this));
+ $directory = dirname($reflector->getFileName());
+ $submittedValues = array_merge([
+ 'uploadFile' => ['name' => $directory . '/data/' . $csv],
+ 'skipColumnHeader' => TRUE,
+ 'fieldSeparator' => ',',
+ 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
+ 'mapper' => $this->getMapperFromFieldMappings($fieldMappings),
+ 'dataSource' => 'CRM_Import_DataSource_CSV',
+ 'file' => ['name' => $csv],
+ 'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd,
+ 'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
+ 'groups' => [],
+ ], $submittedValues);
+ $form = $this->getDataSourceForm($submittedValues);
+ $form->buildForm();
+ $form->postProcess();
+ $this->userJobID = $form->getUserJobID();
+ $form = $this->getMapFieldForm($submittedValues);
+ $form->setUserJobID($this->userJobID);
+ $form->buildForm();
+ $this->assertTrue($form->validate());
+ $form->postProcess();
+ $form = $this->getPreviewForm($submittedValues);
+ $form->setUserJobID($this->userJobID);
+ $form->buildForm();
+ $this->assertTrue($form->validate());
+ $form->postProcess();
+ }
+
+ /**
+ * Get the import's datasource form.
+ *
+ * Defaults to contribution - other classes should override.
+ *
+ * @param array $submittedValues
+ *
+ * @return \CRM_Contribute_Import_Form_DataSource
+ * @noinspection PhpUnnecessaryLocalVariableInspection
+ */
+ protected function getDataSourceForm(array $submittedValues): CRM_Contribute_Import_Form_DataSource {
+ /* @var \CRM_Contribute_Import_Form_DataSource $form */
+ $form = $this->getFormObject('CRM_Contribute_Import_Form_DataSource', $submittedValues);
+ return $form;
+ }
+
+ /**
+ * Get the import's mapField form.
+ *
+ * Defaults to contribution - other classes should override.
+ *
+ * @param array $submittedValues
+ *
+ * @return \CRM_Contribute_Import_Form_MapField
+ * @noinspection PhpUnnecessaryLocalVariableInspection
+ */
+ protected function getMapFieldForm(array $submittedValues): CRM_Contribute_Import_Form_MapField {
+ /* @var \CRM_Contribute_Import_Form_MapField $form */
+ $form = $this->getFormObject('CRM_Contribute_Import_Form_MapField', $submittedValues);
+ return $form;
+ }
+
+ /**
+ * Get the import's preview form.
+ *
+ * Defaults to contribution - other classes should override.
+ *
+ * @param array $submittedValues
+ *
+ * @return \CRM_Contribute_Import_Form_Preview
+ * @noinspection PhpUnnecessaryLocalVariableInspection
+ */
+ protected function getPreviewForm(array $submittedValues): CRM_Contribute_Import_Form_Preview {
+ /* @var CRM_Contribute_Import_Form_Preview $form */
+ $form = $this->getFormObject('CRM_Contribute_Import_Form_Preview', $submittedValues);
+ return $form;
+ }
+
+}