Skip to content
Snippets Groups Projects
Commit 1254d04c authored by Eileen McNaughton's avatar Eileen McNaughton
Browse files

Add unit tests to cover date field imports

parent 9b30054f
Branches
Tags
No related merge requests found
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
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
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
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
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
......@@ -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',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment