Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21776: Ensure from clause adds custom table when search query is … #11679

Merged
merged 1 commit into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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