Skip to content

Commit

Permalink
CRM-21776: Ensure from clause adds custom table when search query is …
Browse files Browse the repository at this point in the history
…ordered by a custom field
  • Loading branch information
Jitendra Purohit committed Feb 16, 2018
1 parent c9dfdcd commit 744050b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6418,6 +6418,15 @@ protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFro
break;

default:
$cfID = CRM_Core_BAO_CustomField::getKeyID($field);
// add to cfIDs array if not present
if (!empty($cfID) && !array_key_exists($cfID, $this->_cfIDs)) {
$this->_cfIDs[$cfID] = array();
$this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE, $this->_locationSpecificCustomFields);
$this->_customQuery->query();
$this->_select = array_merge($this->_select, $this->_customQuery->_select);
$this->_tables = array_merge($this->_tables, $this->_customQuery->_tables);
}
foreach ($this->_pseudoConstantsSelect as $key => $pseudoConstantMetadata) {
// By replacing the join to the option value table with the mysql construct
// ORDER BY field('contribution_status_id', 2,1,4)
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/CRM/Contact/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,59 @@ public function testContactIDQuery() {
$searchOBJ->contactIDQuery($params, '1_u');
}

/**
* Test if custom table is added in from clause when
* search results are ordered by a custom field.
*/
public function testSelectorQueryOrderByCustomField() {
//Search for any params.
$params = [[
0 => 'country-1',
1 => '=',
2 => '1228',
3 => 1,
4 => 0,
]];

//Create a test custom group and field.
$customGroup = $this->callAPISuccess('CustomGroup', 'create', array(
'title' => "test custom group",
'extends' => "Individual",
));
$cgTableName = $customGroup['values'][$customGroup['id']]['table_name'];
$customField = $this->callAPISuccess('CustomField', 'create', array(
'custom_group_id' => $customGroup['id'],
'label' => "test field",
'html_type' => "Text",
));
$customFieldId = $customField['id'];

//Sort by the custom field created above.
$sortParams = array(
1 => array(
'name' => 'test field',
'sort' => "custom_{$customFieldId}",
),
);
$sort = new CRM_Utils_Sort($sortParams, '1_d');

//Form a query to order by a custom field.
$query = new CRM_Contact_BAO_Query($params,
CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES,
NULL, FALSE, FALSE, 1,
FALSE, TRUE, TRUE, NULL,
'AND'
);
$query->searchQuery(0, 0, $sort,
FALSE, FALSE, FALSE,
FALSE, FALSE
);
//Check if custom table is included in $query->_tables.
$this->assertTrue(in_array($cgTableName, array_keys($query->_tables)));
//Assert if from clause joins the custom table.
$this->assertTrue(strpos($query->_fromClause, $cgTableName) !== FALSE);
}

/**
* Get the default select string since this is generally consistent.
*/
Expand Down

0 comments on commit 744050b

Please sign in to comment.