Skip to content

Commit

Permalink
Fix customField legacy handling in APIv3
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Sep 4, 2020
1 parent e418ac0 commit 5e17e88
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 22 deletions.
8 changes: 5 additions & 3 deletions CRM/Core/DAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/CustomField.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:7bdf73d78bf9965895218895184e40a9)
* (GenCodeChecksum:1acb9b3538bd3005b99e6af6d9ec062f)
*/

/**
Expand Down Expand Up @@ -225,7 +225,7 @@ class CRM_Core_DAO_CustomField extends CRM_Core_DAO {
public $option_group_id;

/**
* Serialization method - a non-null value indicates a multi-valued field.
* Serialization method - a non-zero value indicates a multi-valued field.
*
* @var int
*/
Expand Down Expand Up @@ -686,8 +686,10 @@ public static function &fields() {
'name' => 'serialize',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Serialize'),
'description' => ts('Serialization method - a non-null value indicates a multi-valued field.'),
'description' => ts('Serialization method - a non-zero value indicates a multi-valued field.'),
'required' => TRUE,
'where' => 'civicrm_custom_field.serialize',
'default' => '0',
'table_name' => 'civicrm_custom_field',
'entity' => 'CustomField',
'bao' => 'CRM_Core_BAO_CustomField',
Expand Down
7 changes: 7 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.29.1.mysql.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{* file to handle db changes in 5.29.1 during upgrade *}

UPDATE `civicrm_custom_field` SET `serialize` = 0 WHERE `serialize` IS NULL;

ALTER TABLE `civicrm_custom_field`
CHANGE COLUMN `serialize`
`serialize` int unsigned NOT NULL DEFAULT 0 COMMENT 'Serialization method - a non-zero value indicates a multi-valued field.';
37 changes: 32 additions & 5 deletions api/v3/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,45 @@ function civicrm_api3_custom_field_delete($params) {
* @return array
*/
function civicrm_api3_custom_field_get($params) {
if (CRM_Core_BAO_Domain::isDBVersionAtLeast('5.27.alpha1') && ($params['legacy_html_type'] ?? TRUE) && !empty($params['return'])) {
if (is_array($params['return'])) {
// Legacy handling for serialize property
$handleLegacy = (($params['legacy_html_type'] ?? !isset($params['serialize'])) && CRM_Core_BAO_Domain::isDBVersionAtLeast('5.27.alpha1'));
if ($handleLegacy && !empty($params['return'])) {
if (!is_array($params['return'])) {
$params['return'] = explode(',', str_replace(' ', '', $params['return']));
}
if (!in_array('serialize', $params['return'])) {
$params['return'][] = 'serialize';
}
elseif (is_string($params['return'])) {
$params['return'] .= ',serialize';
}
if ($handleLegacy && !empty($params['html_type'])) {
$serializedTypes = ['CheckBox', 'Multi-Select', 'Multi-Select Country', 'Multi-Select State/Province'];
if (is_string($params['html_type'])) {
if (strpos($params['html_type'], 'Multi-Select') === 0) {
$params['html_type'] = str_replace('Multi-Select', 'Select', $params['html_type']);
$params['serialize'] = 1;
}
elseif (!in_array($params['html_type'], $serializedTypes)) {
$params['serialize'] = 0;
}
}
elseif (is_array($params['html_type']) && !empty($params['html_type']['IN'])) {
$excludeNonSerialized = !array_diff($params['html_type']['IN'], $serializedTypes);
$onlyNonSerialized = !array_intersect($params['html_type']['IN'], $serializedTypes);
$params['html_type']['IN'] = array_map(function($val) {
return str_replace('Multi-Select', 'Select', $val);
}, $params['html_type']['IN']);
if ($excludeNonSerialized) {
$params['serialize'] = 1;
}
if ($onlyNonSerialized) {
$params['serialize'] = 0;
}
}
}

$results = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);

if (($params['legacy_html_type'] ?? TRUE) && !empty($results['values']) && is_array($results['values'])) {
if ($handleLegacy && !empty($results['values']) && is_array($results['values']) && !isset($params['serialize'])) {
foreach ($results['values'] as $id => $result) {
if (!empty($result['serialize']) && !empty($result['html_type'])) {
$results['values'][$id]['html_type'] = str_replace('Select', 'Multi-Select', $result['html_type']);
Expand Down
20 changes: 10 additions & 10 deletions tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('country'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
'pseudoconstant' => [
'table' => 'civicrm_country',
'keyColumn' => 'id',
Expand Down Expand Up @@ -559,7 +559,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.my_file_' . $this->getCustomFieldID('file'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
],
$this->getCustomFieldName('text') => [
'name' => $this->getCustomFieldName('text'),
Expand Down Expand Up @@ -593,7 +593,7 @@ public function testGetFieldsForImport() {
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'maxlength' => 300,
'serialize' => NULL,
'serialize' => 0,
],
$this->getCustomFieldName('select_string') => [
'name' => $this->getCustomFieldName('select_string'),
Expand Down Expand Up @@ -626,7 +626,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
'pseudoconstant' => [
'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
Expand Down Expand Up @@ -663,7 +663,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
],
$this->getCustomFieldName('link') => [
'name' => $this->getCustomFieldName('link'),
Expand Down Expand Up @@ -696,7 +696,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
],
$this->getCustomFieldName('int') => [
'name' => $this->getCustomFieldName('int'),
Expand Down Expand Up @@ -729,7 +729,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('int'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
],
$this->getCustomFieldName('contact_reference') => [
'name' => $this->getCustomFieldName('contact_reference'),
Expand Down Expand Up @@ -762,7 +762,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('contact_reference'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
],
$this->getCustomFieldName('state') => [
'name' => $this->getCustomFieldName('state'),
Expand All @@ -788,7 +788,7 @@ public function testGetFieldsForImport() {
'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('state'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
'serialize' => NULL,
'serialize' => 0,
'pseudoconstant' => [
'table' => 'civicrm_state_province',
'keyColumn' => 'id',
Expand Down Expand Up @@ -871,7 +871,7 @@ public function testGetFieldsForImport() {
'text_length' => NULL,
'options_per_line' => NULL,
'is_search_range' => '0',
'serialize' => NULL,
'serialize' => 0,
'pseudoconstant' => [
'callback' => 'CRM_Core_SelectValues::boolean',
],
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Core/BAO/CustomQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testSearchCustomDataDateRelative() {
'type' => 4,
'where' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'] . '.date_field_' . $dateCustomField['id'],
'import' => 1,
'serialize' => NULL,
'serialize' => 0,
], $queryObj->getFieldSpec('custom_' . $dateCustomField['id']));

}
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/CRM/Utils/Migrate/fixtures/Activity-text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<is_active>1</is_active>
<is_view>0</is_view>
<column_name>name1_1</column_name>
<serialize>0</serialize>
<in_selector>0</in_selector>
<custom_group_name>example</custom_group_name>
</CustomField>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<is_active>1</is_active>
<is_view>0</is_view>
<column_name>name1_1</column_name>
<serialize>0</serialize>
<in_selector>0</in_selector>
<custom_group_name>example</custom_group_name>
</CustomField>
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/CRM/Utils/Migrate/fixtures/Contact-text.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<is_active>1</is_active>
<is_view>0</is_view>
<column_name>name1_1</column_name>
<serialize>0</serialize>
<in_selector>0</in_selector>
<custom_group_name>example</custom_group_name>
</CustomField>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<is_active>1</is_active>
<is_view>0</is_view>
<column_name>name1_1</column_name>
<serialize>0</serialize>
<in_selector>0</in_selector>
<custom_group_name>example</custom_group_name>
</CustomField>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<is_active>1</is_active>
<is_view>0</is_view>
<column_name>name1_1</column_name>
<serialize>0</serialize>
<in_selector>0</in_selector>
<custom_group_name>example</custom_group_name>
</CustomField>
Expand Down
60 changes: 60 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,64 @@ public function testDisableSearchableContactReferenceField() {
$this->callAPISuccess('CustomField', 'create', $params);
}

public function testLegacyHtmlType() {
$customGroup = $this->customGroupCreate([
'name' => 'testCustomGroup',
'title' => 'testCustomGroup',
'extends' => 'Individual',
]);
$f1 = $this->callAPISuccess('CustomField', 'create', [
'label' => 'SingleSelect',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'String',
'html_type' => 'Select',
'option_values' => [1 => 'One', 2 => 'Two'],
]);
$f2 = $this->callAPISuccess('CustomField', 'create', [
'label' => 'CheckBoxes',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'String',
'html_type' => 'CheckBox',
'option_values' => [1 => 'One', 2 => 'Two'],
]);
$f3 = $this->callAPISuccess('CustomField', 'create', [
'label' => 'MultiSelect',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'String',
'html_type' => 'Multi-Select',
'option_values' => [1 => 'One', 2 => 'Two'],
]);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => 'Multi-Select',
'sequential' => 1,
]);
$this->assertCount(1, $result['values']);
$this->assertEquals('MultiSelect', $result['values'][0]['label']);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => ['IN' => ['Multi-Select', 'CheckBox']],
'sequential' => 1,
]);
$this->assertCount(2, $result['values']);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => 'Select',
'sequential' => 1,
]);
$this->assertCount(1, $result['values']);
$this->assertEquals('SingleSelect', $result['values'][0]['label']);

$result = $this->callAPISuccess('CustomField', 'get', [
'custom_group_id' => 'testCustomGroup',
'html_type' => ['IN' => ['Select']],
'sequential' => 1,
]);
$this->assertCount(1, $result['values']);
$this->assertEquals('SingleSelect', $result['values'][0]['label']);
}

}
4 changes: 2 additions & 2 deletions tests/phpunit/api/v4/Action/CustomValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testCRUD() {
'entity' => "Custom_$group",
'data_type' => 'String',
'fk_entity' => NULL,
'serialize' => NULL,
'serialize' => 0,
'options' => $optionValues,
],
[
Expand All @@ -109,7 +109,7 @@ public function testCRUD() {
'entity' => "Custom_$group",
'data_type' => 'String',
'fk_entity' => NULL,
'serialize' => NULL,
'serialize' => 0,
],
[
'name' => 'id',
Expand Down
4 changes: 3 additions & 1 deletion xml/schema/Core/CustomField.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,13 @@
<type>int unsigned</type>
<title>Serialize</title>
<length>255</length>
<comment>Serialization method - a non-null value indicates a multi-valued field.</comment>
<comment>Serialization method - a non-zero value indicates a multi-valued field.</comment>
<pseudoconstant>
<callback>CRM_Core_SelectValues::fieldSerialization</callback>
</pseudoconstant>
<add>5.27</add>
<required>true</required>
<default>0</default>
</field>
<field>
<name>filter</name>
Expand Down

0 comments on commit 5e17e88

Please sign in to comment.