From 1254d04c3620cabfd40172a973b0fe14bdd36acd Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 19 May 2022 18:23:37 +1200 Subject: [PATCH] Add unit tests to cover date field imports --- .../Form/data/individual_dates_type16.csv | 3 ++ .../Form/data/individual_dates_type2.csv | 4 ++ .../Form/data/individual_dates_type32.csv | 3 ++ .../Form/data/individual_dates_type4.csv | 5 +++ .../Form/data/individual_dates_type8.csv | 2 + .../CRM/Contact/Import/Parser/ContactTest.php | 44 ++++++++++++++----- 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type16.csv create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type2.csv create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type32.csv create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type4.csv create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type8.csv diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type16.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type16.csv new file mode 100644 index 0000000000..b457c3b960 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type16.csv @@ -0,0 +1,3 @@ +first_name,Last Name,Birth Date,Deceased Date,Custom Date One,Custom Date Two,Street Address,Type,Expected +Joe,13,01-Sep-08,01-Sep-08,01-Sep-08,01-Sep-08,22 Downing St,16,Valid +Joe,14,01/09/08,01/09/08,01/09/08,01/09/08,23 Downing St,16,Valid diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type2.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type2.csv new file mode 100644 index 0000000000..cdade829f6 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type2.csv @@ -0,0 +1,4 @@ +first_name,Last Name,Birth Date,Deceased Date,Custom Date One,Custom Date Two,Street Address,Type,expected +Joe,4,09/01/08,09/01/08,09/01/08,09/01/08,13 Downing St,2,Valid +Joe,5,09-01-08,09-01-08,09-01-08,09-01-08,14 Downing St,2,Valid +Joe,6,9/1/08,9/1/08,9/1/08,9/1/08,15 Downing St,2,Valid diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type32.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type32.csv new file mode 100644 index 0000000000..808152ca42 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type32.csv @@ -0,0 +1,3 @@ +first_name,Last Name,Birth Date,Deceased Date,Custom Date One,Custom Date Two,Street Address,Type,Expected +Joe,15,01/09/2008,01/09/2008,01/09/2008,01/09/2008,24 Downing St,32,Valid +Joe,16,1/9/2008,1/9/2008,1/9/2008,1/9/2008,25 Downing St,32,Valid diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type4.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type4.csv new file mode 100644 index 0000000000..f456e8534b --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type4.csv @@ -0,0 +1,5 @@ +first_name,Last Name,Birth Date,Deceased Date,Custom Date One,Custom Date Two,Street Address,Type,expected +Joe,8,09/01/2008,09/01/2008,09/01/2008,09/01/2008,17 Downing St,4,Valid +Joe,9,09-01-2008,09-01-2008,09-01-2008,09-01-2008,18 Downing St,4,Valid +Joe,10,09/01/2008,09/01/2008,09/01/2008,09/01/2008,19 Downing St,4,Valid +Joe,11,09-01-2008,09-01-2008,09-01-2008,09-01-2008,20 Downing St,4,Valid diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type8.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type8.csv new file mode 100644 index 0000000000..36badcbf35 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_dates_type8.csv @@ -0,0 +1,2 @@ +first_name,Last Name,Birth Date,Deceased Date,Custom Date One,Custom Date Two,Street Address,Type,expected +Joe,12,"September 12, 2008","September 12, 2008","September 12, 2008","September 12, 2008",21 Downing St,8,Valid diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index a99ed59a5f..c8cc450b60 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -1050,17 +1050,21 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { } /** - * @throws \Civi\API\Exception\UnauthorizedException + * Test date validation. + * + * @dataProvider dateDataProvider + * + * @param string $csv + * @param int $dateType + * * @throws \API_Exception * @throws \CRM_Core_Exception */ - public function testValidateDateData(): void { + public function testValidateDateData($csv, $dateType): void { $addressCustomGroupID = $this->createCustomGroup(['extends' => 'Address', 'name' => 'Address']); $contactCustomGroupID = $this->createCustomGroup(['extends' => 'Contact', 'name' => 'Contact']); $addressCustomFieldID = $this->createDateCustomField(['custom_group_id' => $addressCustomGroupID])['id']; $contactCustomFieldID = $this->createDateCustomField(['custom_group_id' => $contactCustomGroupID])['id']; - $csv = 'individual_dates_type1.csv'; - $dateType = CRM_Core_Form_Date::DATE_yyyy_mm_dd; $mapper = [ ['first_name'], ['last_name'], @@ -1073,20 +1077,23 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { ]; // Date types should be picked up from submitted values but still some clean up to do. CRM_Core_Session::singleton()->set('dateTypes', $dateType); - $this->validateMultiRowCsv($csv, $mapper, 'custom_' . $addressCustomFieldID, ['dateFormats' => $dateType]); + $this->validateMultiRowCsv($csv, $mapper, 'custom_date_one', ['dateFormats' => $dateType]); $fields = [ 'contact_id.birth_date', 'contact_id.deceased_date', + 'contact_id.is_deceased', 'contact_id.custom_' . $contactCustomFieldID, $addressCustomFieldID, ]; - $contacts = Address::get() - ->addWhere('contact_id.first_name', '=', 'Joe') - ->setSelect($fields) - ->execute(); + $contacts = Address::get()->addWhere('contact_id.first_name', '=', 'Joe')->setSelect($fields)->execute(); foreach ($contacts as $contact) { foreach ($fields as $field) { - $this->assertEquals('2008-09-01', $contact[$field]); + if ($field === 'contact_is_deceased') { + $this->assertTrue($contact[$field]); + } + else { + $this->assertEquals('2008-09-01', $contact[$field]); + } } } } @@ -1121,6 +1128,22 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $this->assertCount(3, $contacts); } + /** + * Data provider for date tests. + * + * @return array[] + */ + public function dateDataProvider(): array { + return [ + 'type_1' => ['csv' => 'individual_dates_type1.csv', 'dateType' => CRM_Core_Form_Date::DATE_yyyy_mm_dd], + 'type_2' => ['csv' => 'individual_dates_type2.csv', 'dateType' => CRM_Core_Form_Date::DATE_mm_dd_yy], + 'type_4' => ['csv' => 'individual_dates_type4.csv', 'dateType' => CRM_Core_Form_Date::DATE_mm_dd_yyyy], + 'type_8' => ['csv' => 'individual_dates_type8.csv', 'dateType' => CRM_Core_Form_Date::DATE_Month_dd_yyyy], + 'type_16' => ['csv' => 'individual_dates_type16.csv', 'dateType' => CRM_Core_Form_Date::DATE_dd_mon_yy], + 'type_32' => ['csv' => 'individual_dates_type32.csv', 'dateType' => CRM_Core_Form_Date::DATE_dd_mm_yyyy], + ]; + } + /** * Test that setting duplicate action to fill doesn't blow away data * that exists, but does fill in where it's empty. @@ -1535,6 +1558,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { 'sqlQuery' => 'SELECT first_name FROM civicrm_contact', 'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP, 'dedupe_rule_id' => NULL, + 'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd, ], $submittedValues), ], 'status_id:name' => 'draft', -- 2.25.1