From b4d97fa31a919eac5ab362d60aba4df8993049aa Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 10 May 2022 19:41:12 +1200 Subject: [PATCH] Add test for NO_MATCH flow --- .../individual_invalid_contact_sub_type.csv | 2 + .../CRM/Contact/Import/Parser/ContactTest.php | 78 +++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 2 + 3 files changed, 82 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_contact_sub_type.csv diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_contact_sub_type.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_contact_sub_type.csv new file mode 100644 index 0000000000..a26814f46c --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_invalid_contact_sub_type.csv @@ -0,0 +1,2 @@ +first_name,Last Name,Contact Subtype +Joe,Green,Team diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index f6a6ad253c..2dce16a130 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -736,6 +736,11 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { } } + /** + * Get combinations to test for validation. + * + * @return array[] + */ public function validateDataProvider(): array { return [ 'individual_required' => [ @@ -756,6 +761,41 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { ]; } + /** + * Test the import. + * + * @dataProvider importDataProvider + * + * @throws \API_Exception + */ + public function testImport($csv, $mapper, $expectedError): void { + try { + $this->importCSV($csv, $mapper); + } + catch (CRM_Core_Exception $e) { + $this->assertSame($expectedError, $e->getMessage()); + return; + } + if ($expectedError) { + $this->fail('expected error :' . $expectedError); + } + } + + /** + * Get combinations to test for validation. + * + * @return array[] + */ + public function importDataProvider(): array { + return [ + 'individual_invalid_sub_type' => [ + 'csv' => 'individual_invalid_contact_sub_type.csv', + 'mapper' => [['first_name'], ['last_name'], ['contact_sub_type']], + 'expected_error' => 'Invalid value for field(s) : type', + ], + ]; + } + /** * Test that setting duplicate action to fill doesn't blow away data * that exists, but does fill in where it's empty. @@ -1149,4 +1189,42 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $parser->validateValues(array_values($dataSource->getRow())); } + /** + * Import the csv file values. + * + * This function uses a flow that mimics the UI flow. + * + * @param string $csv Name of csv file. + * @param array $mapper Mapping as entered on MapField form. + * e.g [['first_name']['email', 1]]. + * @param array $submittedValues + */ + protected function importCSV(string $csv, array $mapper, array $submittedValues = []): void { + $submittedValues = array_merge([ + 'uploadFile' => ['name' => __DIR__ . '/../Form/data/' . $csv], + 'skipColumnHeader' => TRUE, + 'fieldSeparator' => ',', + 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, + 'mapper' => $mapper, + 'dataSource' => 'CRM_Import_DataSource_CSV', + 'file' => ['name' => $csv], + 'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE, + 'groups' => [], + ], $submittedValues); + $form = $this->getFormObject('CRM_Contact_Import_Form_DataSource', $submittedValues); + $form->buildForm(); + $form->postProcess(); + $userJobID = $form->getUserJobID(); + /* @var CRM_Contact_Import_Form_MapField $form */ + $form = $this->getFormObject('CRM_Contact_Import_Form_MapField', $submittedValues); + $form->setUserJobID($userJobID); + $form->buildForm(); + $form->postProcess(); + /* @var CRM_Contact_Import_Form_MapField $form */ + $form = $this->getFormObject('CRM_Contact_Import_Form_Preview', $submittedValues); + $form->setUserJobID($userJobID); + $form->buildForm(); + $form->postProcess(); + } + } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index ecac2cd93d..4a29682d3d 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3221,12 +3221,14 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { case 'CRM_Contact_Import_Form_DataSource': case 'CRM_Contact_Import_Form_MapField': + case 'CRM_Contact_Import_Form_Preview': $form->controller = new CRM_Contact_Import_Controller(); $form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller)); // The submitted values should be set on one or the other of the forms in the flow. // For test simplicity we set on all rather than figuring out which ones go where.... $_SESSION['_' . $form->controller->_name . '_container']['values']['DataSource'] = $formValues; $_SESSION['_' . $form->controller->_name . '_container']['values']['MapField'] = $formValues; + $_SESSION['_' . $form->controller->_name . '_container']['values']['Preview'] = $formValues; return $form; case strpos($class, '_Form_') !== FALSE: -- 2.25.1