From 112e13faf669eb664d8d36d158591fa44bd4e977 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 9 Jun 2022 12:27:04 +1200 Subject: [PATCH] Fix test to validate forms & remove some unused code --- .toxic.json | 1 - CRM/Contribute/Import/Form/DataSource.php | 14 -- CRM/Contribute/Import/Parser/Contribution.php | 232 ------------------ CRM/Export/BAO/Export.php | 1 + .../Import/Parser/ContributionTest.php | 38 +-- .../CRM/Custom/Import/Parser/ApiTest.php | 35 +++ .../phpunit/CRMTraits/Import/ParserTrait.php | 107 ++++++++ 7 files changed, 144 insertions(+), 284 deletions(-) create mode 100644 tests/phpunit/CRM/Custom/Import/Parser/ApiTest.php create mode 100644 tests/phpunit/CRMTraits/Import/ParserTrait.php diff --git a/.toxic.json b/.toxic.json index 3886591fd1..399c2407ac 100644 --- a/.toxic.json +++ b/.toxic.json @@ -32,7 +32,6 @@ "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", diff --git a/CRM/Contribute/Import/Form/DataSource.php b/CRM/Contribute/Import/Form/DataSource.php index 36ef3c9884..6de2b35a8d 100644 --- a/CRM/Contribute/Import/Form/DataSource.php +++ b/CRM/Contribute/Import/Form/DataSource.php @@ -54,20 +54,6 @@ class CRM_Contribute_Import_Form_DataSource extends CRM_Import_Form_DataSource { $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 */ diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index c69cd37002..f98f1bc2d1 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -110,83 +110,6 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { */ 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. * @@ -274,161 +197,6 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { } } - /** - * 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 */ diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index c713eb8ade..6ff9506f45 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -251,6 +251,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c 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)) { diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index ab39c9a5c5..19c11d37dc 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -17,6 +17,7 @@ use Civi\Api4\UserJob; */ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { use CRMTraits_Custom_CustomDataTrait; + use CRMTraits_Import_ParserTrait; /** * Default entity for class. @@ -198,43 +199,6 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { $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. * diff --git a/tests/phpunit/CRM/Custom/Import/Parser/ApiTest.php b/tests/phpunit/CRM/Custom/Import/Parser/ApiTest.php new file mode 100644 index 0000000000..f051157b01 --- /dev/null +++ b/tests/phpunit/CRM/Custom/Import/Parser/ApiTest.php @@ -0,0 +1,35 @@ +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']); + } + +} diff --git a/tests/phpunit/CRMTraits/Import/ParserTrait.php b/tests/phpunit/CRMTraits/Import/ParserTrait.php new file mode 100644 index 0000000000..4531d74ccb --- /dev/null +++ b/tests/phpunit/CRMTraits/Import/ParserTrait.php @@ -0,0 +1,107 @@ +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; + } + +} -- 2.25.1