From d215c0e1d07ee942730b3cd87e3125ac7db2f995 Mon Sep 17 00:00:00 2001 From: Mark Hanna Date: Fri, 12 Oct 2018 10:21:42 -0500 Subject: [PATCH] do not strtolower() string values while building where clause for custom field queries remove empty row test updates for dev issue 436 fixes for test updates for dev issue 436 fixes for test updates for dev issue 436 another fix for new multi-valued custom field test additions another fix for new multi-valued custom field test additions fix to testCustomDataGet to remove false positive add exception for note entity in testCustomDataGet --- CRM/Core/BAO/CustomQuery.php | 4 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 39 +++++++++++++++++++ .../phpunit/api/v3/SyntaxConformanceTest.php | 24 ++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/CRM/Core/BAO/CustomQuery.php b/CRM/Core/BAO/CustomQuery.php index a923bb53c1..d47e095758 100644 --- a/CRM/Core/BAO/CustomQuery.php +++ b/CRM/Core/BAO/CustomQuery.php @@ -295,8 +295,6 @@ SELECT f.id, f.label, f.data_type, continue; } - $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; - foreach ($values as $tuple) { list($name, $op, $value, $grouping, $wildcard) = $tuple; @@ -335,7 +333,7 @@ SELECT f.id, f.label, f.data_type, // fix $value here to escape sql injection attacks if (!is_array($value)) { if ($field['data_type'] == 'String') { - $value = CRM_Utils_Type::escape($strtolower($value), 'String'); + $value = CRM_Utils_Type::escape($value, 'String'); } elseif ($value) { $value = CRM_Utils_Type::escape($value, 'Integer'); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 30d0ff18fe..986a89169f 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1963,6 +1963,45 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { return array('custom_group_id' => $customGroup['id'], 'custom_field_id' => $customField['id']); } + /** + * Create a custom group with a single text custom field, multi-select widget, with a variety of option values including upper and lower case. + * See api_v3_SyntaxConformanceTest:testCustomDataGet for how to use this + * + * @param string $function + * __FUNCTION__. + * @param string $filename + * $file __FILE__. + * + * @return array + * ids of created objects + */ + public function entityCustomGroupWithSingleStringMultiSelectFieldCreate($function, $filename) { + $params = array('title' => $function); + $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8); + $params['extends'] = $entity ? $entity : 'Contact'; + $customGroup = $this->CustomGroupCreate($params); + $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'], 'label' => $function, 'html_type' => 'Multi-Select', 'default_value' => 1)); + CRM_Core_PseudoConstant::flush(); + $options = [ + 'defaultValue' => 'Default Value', + 'lowercasevalue' => 'Lowercase Value', + 1 => 'Integer Value', + ]; + $custom_field_params = ['sequential' => 1, 'id' => $customField['id']]; + $custom_field_api_result = $this->callAPISuccess('custom_field', 'get', $custom_field_params); + $this->assertNotEmpty($custom_field_api_result['values'][0]['option_group_id']); + $option_group_params = ['sequential' => 1, 'id' => $custom_field_api_result['values'][0]['option_group_id']]; + $option_group_result = $this->callAPISuccess('OptionGroup', 'get', $option_group_params); + $this->assertNotEmpty($option_group_result['values'][0]['name']); + foreach ($options as $option_value => $option_label) { + $option_group_params = ['option_group_id' => $option_group_result['values'][0]['name'], 'value' => $option_value, 'label' => $option_label]; + $option_value_result = $this->callAPISuccess('OptionValue', 'create', $option_group_params); + } + + return array('custom_group_id' => $customGroup['id'], 'custom_field_id' => $customField['id'], 'custom_field_option_group_id' => $custom_field_api_result['values'][0]['option_group_id'], 'custom_field_group_options' => $options); + } + + /** * Delete custom group. * diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index fa0dbe649c..76a6d99576 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -838,6 +838,9 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { * @param $entityName */ public function testCustomDataGet($entityName) { + if ($entityName === 'Note') { + $this->markTestIncomplete('Note can not be processed here because of a vagary in the note api, it adds entity_table=contact to the get params when id is not present - which makes sense almost always but kills this test'); + } $this->quickCleanup(array('civicrm_uf_match')); $this->createLoggedInUser();// so subsidiary activities are created @@ -861,20 +864,35 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { // We are not passing 'check_permissions' so the the more limited permissions *should* be // ignored but per CRM-17700 there is a history of custom data applying permissions when it shouldn't. CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'view my contact'); + $objects = $this->getMockableBAOObjects($entityName, 1); + + // simple custom field $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, $usableName . 'Test.php'); $customFieldName = 'custom_' . $ids['custom_field_id']; - $objects = $this->getMockableBAOObjects($entityName, 1); $params = array('id' => $objects[0]->id, 'custom_' . $ids['custom_field_id'] => "custom string"); $result = $this->callAPISuccess($entityName, 'create', $params); - $this->assertTrue(isset($result['id']), 'no id on ' . $entityName); $getParams = array('id' => $result['id'], 'return' => array($customFieldName)); $check = $this->callAPISuccess($entityName, 'get', $getParams); $this->assertTrue(!empty($check['values'][$check['id']][$customFieldName]), 'Custom data not present for ' . $entityName); $this->assertEquals("custom string", $check['values'][$check['id']][$customFieldName], 'Custom data not present for ' . $entityName); - $this->customFieldDelete($ids['custom_field_id']); $this->customGroupDelete($ids['custom_group_id']); + + $ids2 = $this->entityCustomGroupWithSingleStringMultiSelectFieldCreate(__FUNCTION__, $usableName . 'Test.php'); + $customFieldNameMultiSelect = 'custom_' . $ids2['custom_field_id']; + // String custom field, Multi-Select html type + foreach ($ids2['custom_field_group_options'] as $option_value => $option_label) { + $params = ['id' => $objects[0]->id, 'custom_' . $ids2['custom_field_id'] => $option_value]; + $result = $this->callAPISuccess($entityName, 'create', $params); + $getParams = [$customFieldNameMultiSelect => $option_value, 'return' => [$customFieldNameMultiSelect]]; + $this->callAPISuccessGetCount($entityName, $getParams, 1); + } + + // cleanup + $this->customFieldDelete($ids2['custom_field_id']); + $this->customGroupDelete($ids2['custom_group_id']); + $this->callAPISuccess($entityName, 'delete', array('id' => $result['id'])); $this->quickCleanup(array('civicrm_uf_match')); if (!empty($createdValue)) { -- 2.25.1