From 3107f56b33479a1d4ad4b29c8f9a4bffd0af99a5 Mon Sep 17 00:00:00 2001 From: Darrick Servis Date: Mon, 6 Jun 2022 12:32:24 +1200 Subject: [PATCH] Improve codes & testing --- CRM/Contact/Import/Parser/Contact.php | 5 ++-- ...lid_external_identifier_email_mismatch.csv | 4 +++ .../CRM/Contact/Import/Parser/ContactTest.php | 30 ++++++++++--------- 3 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_external_identifier_email_mismatch.csv diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index a3b1f96063..c1ef99603a 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -568,7 +568,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { if (!empty($value['relationship_type_id'])) { $requiredSubType = $this->getRelatedContactSubType($value['relationship_type_id'], $value['relationship_direction']); if ($requiredSubType && $value['contact_sub_type'] && $requiredSubType !== $value['contact_sub_type']) { - throw new CRM_Core_Exception($prefixString . ts('Mismatched or Invalid contact subtype found for this related contact.')); + // Tested in CRM_Contact_Import_Parser_ContactTest::testImportContactSubTypes + throw new CRM_Core_Exception($prefixString . ts('Mismatched or Invalid contact subtype found for this related contact.'), CRM_Import_Parser::ERROR); } } } @@ -1138,7 +1139,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { if (array_key_exists($extIDMatch, $possibleMatches['values'])) { return $extIDMatch; } - throw new CRM_Core_Exception(ts('Matching this contact based on the de-dupe rule would cause an external ID conflict')); + throw new CRM_Core_Exception(ts('Matching this contact based on the de-dupe rule would cause an external ID conflict'), CRM_Import_Parser::ERROR); } return $extIDMatch; } diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_external_identifier_email_mismatch.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_external_identifier_email_mismatch.csv new file mode 100644 index 0000000000..8b736e103b --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_external_identifier_email_mismatch.csv @@ -0,0 +1,4 @@ +first,last,email,external_identifier +bob,dobbs,bob@null.com,123 +sue,davis,other@null.com,456 +mis,match,other@null.com,123 diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 285b2b0b0a..a918062cb1 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -1060,19 +1060,8 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { * @throws \API_Exception * @throws \CRM_Core_Exception */ - public function testImport($csv, $mapper, $expectedError, $expectedOutcomes = [], $contactType = NULL): void { - try { - $this->importCSV($csv, $mapper, [ - 'contactType' => $contactType ?? CRM_Import_Parser::CONTACT_INDIVIDUAL, - ]); - } - catch (CRM_Core_Exception $e) { - $this->assertSame($expectedError, $e->getMessage()); - return; - } - if ($expectedError) { - $this->fail('expected error :' . $expectedError); - } + public function testImport($csv, $mapper, $expectedError, $expectedOutcomes = [], $submittedValues = []): void { + $this->importCSV($csv, $mapper, $submittedValues); $dataSource = new CRM_Import_DataSource_CSV(UserJob::get(FALSE)->setSelect(['id'])->execute()->first()['id']); foreach ($expectedOutcomes as $outcome => $count) { $this->assertEquals($dataSource->getRowCount([$outcome]), $count); @@ -1092,6 +1081,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { 'expected_error' => '', 'expected_outcomes' => [CRM_Import_Parser::ERROR => 1], ], + //Record duplicates multiple contacts 'organization_multiple_duplicates_invalid' => [ 'csv' => 'organization_multiple_duplicates_invalid.csv', 'mapper' => [['organization_name'], ['email']], @@ -1100,7 +1090,19 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { CRM_Import_Parser::VALID => 2, CRM_Import_Parser::ERROR => 1, ], - 'contact_type' => CRM_Import_Parser::CONTACT_ORGANIZATION, + 'submitted_values' => [ + 'contactType' => CRM_Import_Parser::CONTACT_ORGANIZATION, + ], + ], + //Matching this contact based on the de-dupe rule would cause an external ID conflict + 'individual_invalid_external_identifier_email_mismatch' => [ + 'csv' => 'individual_invalid_external_identifier_email_mismatch.csv', + 'mapper' => [['first_name'], ['last_name'], ['email'], ['external_identifier']], + 'expected_error' => '', + 'expected_outcomes' => [ + CRM_Import_Parser::VALID => 2, + CRM_Import_Parser::ERROR => 1, + ], ], ]; } -- 2.25.1