Skip to content

Commit

Permalink
dev/core#3850 Fix checkbox import
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 28, 2022
1 parent 4fbddb3 commit 586cf18
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
42 changes: 2 additions & 40 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,46 +845,8 @@ private function formatProfileContactParams(
}
}
else {
if (($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key))) {
// for autocomplete transfer hidden value instead of label
if ($params[$key] && isset($params[$key . '_id'])) {
$value = $params[$key . '_id'];
}

// we need to append time with date
if ($params[$key] && isset($params[$key . '_time'])) {
$value .= ' ' . $params[$key . '_time'];
}

// if auth source is not checksum / login && $value is blank, do not proceed - CRM-10128
if (($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 &&
($value == '' || !isset($value))
) {
continue;
}

$valueId = NULL;

//CRM-13596 - check for contact_sub_type_hidden first
if (array_key_exists('contact_sub_type_hidden', $params)) {
$type = $params['contact_sub_type_hidden'];
}
else {
$type = $data['contact_type'];
if (!empty($data['contact_sub_type'])) {
$type = CRM_Utils_Array::explodePadded($data['contact_sub_type']);
}
}

CRM_Core_BAO_CustomField::formatCustomField($customFieldId,
$data['custom'],
$value,
$type,
$valueId,
$contactID,
FALSE,
FALSE
);
if ((CRM_Core_BAO_CustomField::getKeyID($key))) {
$data[$key] = $value;
}
elseif ($key === 'edit') {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
First Name,Last name,Colour
Bob,Smith,"Red,Yellow,Blue"
Mary,Smith,"1,2,3"
48 changes: 48 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,52 @@ public function testCustomDataName(): void {
$this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]);
}

/**
* Test values import correctly when they are numbers.
*
* https://lab.civicrm.org/dev/core/-/issues/3850
* @throws \CRM_Core_Exception
*/
public function testCustomCheckboxNumericValues(): void {
$this->createCustomGroupWithFieldOfType([], 'checkbox', '', [
'option_values' => [
[
'label' => 'Red',
'value' => '1',
'weight' => 1,
'is_active' => 1,
],
[
'label' => 'Yellow',
'value' => '2',
'weight' => 2,
'is_active' => 1,
],
[
'label' => 'Blue',
'value' => '3',
'weight' => 3,
'is_active' => 1,
],
],
]);
$this->importCSV('individual_with_custom_checkbox_field.csv', [
[0 => 'first_name'],
[0 => 'last_name'],
[0 => $this->getCustomFieldName('checkbox')],
]);
$contacts = Contact::get()->addWhere('last_name', '=', 'Smith')
->addSelect($this->getCustomFieldName('checkbox', 4))
->execute();
$this->assertCount(2, $contacts);
foreach ($contacts as $contact) {
$this->assertEquals([1,2,3], $contact[$this->getCustomFieldName('checkbox', 4)]);
}
}

public function testCustomAutoComplete() {
$this->createCustomGroupWithFieldOfType([], 'autocomplete');
}
/**
* Test importing in the Preferred Language Field
*
Expand Down Expand Up @@ -2095,6 +2141,8 @@ protected function validateCSV(string $csv, array $mapper, array $submittedValue
* e.g [['first_name']['email', 1]].
* {@see \CRM_Contact_Import_Parser_Contact::getMappingFieldFromMapperInput}
* @param array $submittedValues
*
* @throws \CRM_Core_Exception
*/
protected function importCSV(string $csv, array $mapper, array $submittedValues = []): void {
$submittedValues = array_merge([
Expand Down

0 comments on commit 586cf18

Please sign in to comment.