From be7c33c674d20b10b8843b33995d462829ee4635 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 4 Jun 2022 21:13:05 +1200 Subject: [PATCH] Add test for tab-separated file Missed out the csv files --- CRM/Import/Form/DataSource.php | 2 +- .../Member/Import/Parser/MembershipTest.php | 25 +++++++++++++++++-- .../Member/Import/Parser/data/memberships.csv | 2 ++ .../Import/Parser/data/memberships_valid.csv | 2 ++ .../Import/Parser/data/memberships_valid.tsv | 2 ++ 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv create mode 100644 tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.csv create mode 100644 tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.tsv diff --git a/CRM/Import/Form/DataSource.php b/CRM/Import/Form/DataSource.php index a5e53ad43f..306fe2d755 100644 --- a/CRM/Import/Form/DataSource.php +++ b/CRM/Import/Form/DataSource.php @@ -158,7 +158,6 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { * Entity to set for paraser currently only for custom import */ protected function submitFileForMapping($parserClassName, $entity = NULL) { - $this->controller->resetPage('MapField'); CRM_Core_Session::singleton()->set('dateTypes', $this->getSubmittedValue('dateFormats')); $this->processDatasource(); @@ -181,6 +180,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { // add all the necessary variables to the form $parser->set($this); + $this->controller->resetPage('MapField'); } /** diff --git a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php index 91ae68307d..7e07799609 100644 --- a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php @@ -70,6 +70,7 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { $this->_relationshipTypeId = $this->relationshipTypeCreate($params); $this->_orgContactID = $this->organizationCreate(); $this->_financialTypeId = 1; + $this->restoreMembershipTypes(); $this->_membershipTypeName = 'Mickey Mouse Club Member'; $params = [ 'name' => $this->_membershipTypeName, @@ -91,8 +92,6 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { $this->_membershipTypeID = $membershipType->id; $this->_mebershipStatusID = $this->membershipStatusCreate('test status'); - $session = CRM_Core_Session::singleton(); - $session->set('dateTypes', 1); } /** @@ -407,6 +406,24 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { $this->assertEquals('Invalid value for field(s) : Membership Type', $row['_status_message']); } + /** + * Test the full form-flow import. + */ + public function testImportTSV() :void { + $this->individualCreate(['email' => 'member@example.com']); + $this->importCSV('memberships_valid.tsv', [ + ['name' => 'email'], + ['name' => 'membership_source'], + ['name' => 'membership_type_id'], + ['name' => 'membership_start_date'], + ['name' => 'do_not_import'], + ], ['fieldSeparator' => 'tab']); + $dataSource = new CRM_Import_DataSource_CSV($this->userJobID); + $row = $dataSource->getRow(); + $this->assertEquals('IMPORTED', $row['_status']); + $this->callAPISuccessGetSingle('Membership', []); + } + /** * Import the csv file values. * @@ -430,8 +447,12 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { 'groups' => [], ], $submittedValues); $form = $this->getFormObject('CRM_Member_Import_Form_DataSource', $submittedValues); + $values = $_SESSION['_' . $form->controller->_name . '_container']['values']; $form->buildForm(); $form->postProcess(); + // This gets reset in DataSource so re-do.... + $_SESSION['_' . $form->controller->_name . '_container']['values'] = $values; + $this->userJobID = $form->getUserJobID(); $form = $this->getFormObject('CRM_Member_Import_Form_MapField', $submittedValues); $form->setUserJobID($this->userJobID); diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv new file mode 100644 index 0000000000..dfdf1524fb --- /dev/null +++ b/tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv @@ -0,0 +1,2 @@ +Contact ID,Source,Membership Type,Start Date,Ignore me +1,Import,General,2019-03-23,Just some cruft diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.csv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.csv new file mode 100644 index 0000000000..86c7037dac --- /dev/null +++ b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.csv @@ -0,0 +1,2 @@ +email,Source,Membership Type,Start Date,Ignore me +member@example.com,Import,General,2019-03-23,Just some cruft diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.tsv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.tsv new file mode 100644 index 0000000000..925aec034b --- /dev/null +++ b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_valid.tsv @@ -0,0 +1,2 @@ +email Source Membership Type Start Date Ignore me +member@example.com Import General 2019-03-23 Just some cruft -- 2.25.1