Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Import] Check subtype validity in validate rather than wait for 'import', test #23491

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ abstract class CRM_Import_Parser {
*/
const CONTACT_INDIVIDUAL = 1, CONTACT_HOUSEHOLD = 2, CONTACT_ORGANIZATION = 4;


/**
* User job id.
*
Expand Down
22 changes: 5 additions & 17 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,22 +1014,7 @@ public function testImportGenders(): void {
['do_not_import'],
];
$csv = 'individual_genders.csv';
/* @var CRM_Import_DataSource_CSV $dataSource */
/* @var \CRM_Contact_Import_Parser_Contact $parser */
[$dataSource, $parser] = $this->getDataSourceAndParser($csv, $mapper, []);
while ($values = $dataSource->getRow()) {
try {
$parser->validateValues(array_values($values));
if ($values['expected'] !== 'Valid') {
$this->fail($values['gender'] . ' should not have been valid');
}
}
catch (CRM_Core_Exception $e) {
if ($values['expected'] !== 'Invalid') {
$this->fail($values['gender'] . ' should have been valid');
}
}
}
$this->validateMultiRowCsv($csv, $mapper, 'gender');

$this->importCSV($csv, $mapper);
$contacts = Contact::get()
Expand Down Expand Up @@ -1562,18 +1547,21 @@ protected function importCSV(string $csv, array $mapper, array $submittedValues
}

/**
* Validate a csv with multiple rows in it.
*
* @param string $csv
* @param array $mapper Mapping as entered on MapField form.
* e.g [['first_name']['email', 1]].
* @param string $field
* Name of the field whose data should be output in the error message.
* @param array $submittedValues
* Values submitted in the form process.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
private function validateMultiRowCsv(string $csv, array $mapper, string $field, $submittedValues): void {
private function validateMultiRowCsv(string $csv, array $mapper, string $field, $submittedValues = []): void {
/* @var CRM_Import_DataSource_CSV $dataSource */
/* @var \CRM_Contact_Import_Parser_Contact $parser */
[$dataSource, $parser] = $this->getDataSourceAndParser($csv, $mapper, $submittedValues);
Expand Down