From 35ee17006571db12407e7cd4d5974a764911c07b Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 23 Mar 2023 13:25:27 +1300 Subject: [PATCH] dev/core#3937 Remove legacy code causing custom boolean import bug --- CRM/Contact/Import/Parser/Contact.php | 21 ------------ CRM/Import/Parser.php | 2 +- .../Import/Form/data/individual_boolean.csv | 11 +++++++ .../CRM/Contact/Import/Parser/ContactTest.php | 32 +++++++++++++++++++ .../CRMTraits/Custom/CustomDataTrait.php | 5 ++- 5 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Import/Form/data/individual_boolean.csv diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 079ecf9b82..b9c3f95bdd 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -311,9 +311,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { //retain earlier value when Import mode is `Fill` unset($params[$key]); } - else { - $params[$key] = CRM_Utils_String::strtoboolstr($val); - } } } } @@ -1029,24 +1026,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params); return TRUE; } - - // Check for custom field values - $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), - FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE - ); - - foreach ($values as $key => $value) { - if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { - // check if it's a valid custom field id - - if (!array_key_exists($customFieldID, $customFields)) { - return civicrm_api3_create_error('Invalid custom field ID'); - } - else { - $params[$key] = $value; - } - } - } return TRUE; } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 7ba284499b..baac46be14 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1603,7 +1603,7 @@ abstract class CRM_Import_Parser implements UserJobInterface { if ($fieldMetadata['type'] === CRM_Utils_Type::T_BOOLEAN) { $value = CRM_Utils_String::strtoboolstr($importedValue); if ($value !== FALSE) { - return (bool) $value; + return (int) $value; } return 'invalid_import_value'; } diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_boolean.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_boolean.csv new file mode 100644 index 0000000000..ebf940c7a9 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_boolean.csv @@ -0,0 +1,11 @@ +first_name,Last Name,Street Address,Do Not Email,Contact Custom Field, Address Custom Field +Joe,1,Main Street,0,0,0 +Joe,2,Main Street,FALSE,FALSE,FALSE +Joe,3,Main Street,false,false,false +Joe,4,Main st,no,no,no +Joe,5,Main Street,NO,NO,NO +Betty,1,Main Street,1,1,1 +Betty,2,Main Street,TRUE,TRUE,TRUE +Betty,3,Main Street,true,true,true +Betty,4,Main Street,yes,yes,yes +Betty,5,Main Street,YES,YES,YES diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index a085d08817..9f3b2d5475 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -1402,6 +1402,38 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { } } + /** + * Test boolean field handling. + * + * @throws \CRM_Core_Exception + */ + public function testImportBooleanFields(): void { + $this->createCustomGroupWithFieldOfType(['extends' => 'Address', 'name' => 'Address'], 'boolean', 'address_'); + $this->createCustomGroupWithFieldOfType(['extends' => 'Contact', 'name' => 'Contact'], 'boolean', 'contact_'); + $this->importCSV('individual_boolean.csv', [ + ['first_name'], + ['last_name'], + ['street_address', 1], + ['do_not_email'], + [$this->getCustomFieldName('address_boolean'), 1], + [$this->getCustomFieldName('contact_boolean')], + ]); + $contacts = Address::get()->addWhere('contact_id.first_name', 'IN', ['Joe', 'Betty'])->setSelect([ + 'contact_id.first_name', + 'contact_id.do_not_email', + $this->getCustomFieldName('address_boolean', 4), + 'contact_id.' . $this->getCustomFieldName('contact_boolean', 4), + ])->execute(); + + foreach ($contacts as $contact) { + $boolean = !($contact['contact_id.first_name'] === 'Joe'); + $this->assertSame($boolean, $contact['contact_id.do_not_email']); + $this->assertSame($boolean, $contact[$this->getCustomFieldName('address_boolean', 4)]); + $this->assertSame($boolean, $contact['contact_id.' . $this->getCustomFieldName('contact_boolean', 4)]); + } + + } + /** * @throws \CRM_Core_Exception */ diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 479b825b95..1cee45b503 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -94,7 +94,7 @@ trait CRMTraits_Custom_CustomDataTrait { * */ public function createCustomGroupWithFieldOfType(array $groupParams = [], string $customFieldType = 'text', ?string $identifier = NULL, array $fieldParams = []): void { - $supported = ['text', 'select', 'date', 'checkbox', 'int', 'contact_reference', 'radio', 'multi_country']; + $supported = ['text', 'select', 'date', 'checkbox', 'int', 'contact_reference', 'radio', 'multi_country', 'boolean']; if (!in_array($customFieldType, $supported, TRUE)) { $this->fail('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do'); } @@ -136,6 +136,9 @@ trait CRMTraits_Custom_CustomDataTrait { $reference = $this->createMultiCountryCustomField($fieldParams)['id']; return; + case 'boolean': + $reference = $this->createBooleanCustomField($fieldParams)['id']; + return; } } -- 2.25.1