Skip to content

Commit

Permalink
do not strtolower() string values while building where clause for cus…
Browse files Browse the repository at this point in the history
…tom 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
  • Loading branch information
jackrabbithanna committed Oct 23, 2018
1 parent 3f7c098 commit d215c0e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
4 changes: 1 addition & 3 deletions CRM/Core/BAO/CustomQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ public function where() {
continue;
}

$strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';

foreach ($values as $tuple) {
list($name, $op, $value, $grouping, $wildcard) = $tuple;

Expand Down Expand Up @@ -335,7 +333,7 @@ public function where() {
// 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');
Expand Down
39 changes: 39 additions & 0 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,45 @@ public function entityCustomGroupWithSingleFieldCreate($function, $filename) {
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.
*
Expand Down
24 changes: 21 additions & 3 deletions tests/phpunit/api/v3/SyntaxConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,9 @@ public function testSimple_get($Entity) {
* @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

Expand All @@ -861,20 +864,35 @@ public function testCustomDataGet($entityName) {
// 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)) {
Expand Down

0 comments on commit d215c0e

Please sign in to comment.