Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further join tests & fixes
Browse files Browse the repository at this point in the history
Turns out the carefully constructed join was being whomped - but only sometimes
eileenmcnaughton committed Nov 4, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 79d1de8 commit 6aa9454
Showing 2 changed files with 45 additions and 4 deletions.
12 changes: 8 additions & 4 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
@@ -5298,7 +5298,10 @@ public function dateQueryBuilder(
) {
// @todo - remove dateFormat - pretty sure it's never passed in...
list($name, $op, $value, $grouping, $wildcard) = $values;

if ($name !== $fieldName && $name !== "{$fieldName}_low" && $name !== "{$fieldName}_high") {
CRM_Core_Error::deprecatedFunctionWarning('Date query builder called unexpectedly');
return;
}
if ($tableName === 'civicrm_contact') {
// Special handling for contact table as it has a known alias in advanced search.
$tableName = 'contact_a';
@@ -5360,7 +5363,6 @@ public function dateQueryBuilder(
$secondDateFormat = CRM_Utils_Date::customFormat($secondDate);
}

$this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;
if ($secondDate) {
$highDBFieldName = $highDBFieldName ?? $dbFieldName;
$this->_where[$grouping][] = "
@@ -5411,11 +5413,13 @@ public function dateQueryBuilder(
$this->_where[$grouping][] = self::buildClause("{$tableName}.{$dbFieldName}", $op);
}

$this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;

$op = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
$this->_qill[$grouping][] = "$fieldTitle $op $format";
}

// Ensure the tables are set, but don't whomp anything.
$this->_tables[$tableName] = $this->_tables[$tableName] ?? 1;
$this->_whereTables[$tableName] = $this->_whereTables[$tableName] ?? 1;
}

/**
37 changes: 37 additions & 0 deletions tests/phpunit/CRM/Core/BAO/CustomQueryTest.php
Original file line number Diff line number Diff line change
@@ -121,6 +121,43 @@ public function testSearchCustomDataDateHighLow() {
$this->assertEquals('LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id', trim($queryObject->_whereTables[$this->getCustomGroupTable()]));
}

/**
* Test filtering by the renamed custom date fields.
*
* The conversion to date picker will result int these fields
* being renamed _high & _low and needing to return correctly.
*
* @throws \CRM_Core_Exception
*/
public function testSearchCustomDataDateLowWithPermsInPlay() {
$this->createLoggedInUser();
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access all custom data'];
$this->createCustomGroupWithFieldOfType([], 'date');
$dateCustomFieldName = $this->getCustomFieldName('date');
// Assigning the relevant form value to be within a custom key is normally done in
// build field params. It would be better if it were all done in convertFormValues
// but for now we just imitate it.
$formValues = [
$dateCustomFieldName . '_low' => '2014-06-06',
];

$params = CRM_Contact_BAO_Query::convertFormValues($formValues);
$queryObject = new CRM_Contact_BAO_Query($params);
$queryObject->query();
$this->assertEquals(
'civicrm_value_group_with_fi_1.' . $this->getCustomFieldColumnName('date') . ' >= \'20140606000000\'',
trim($queryObject->_where[0][0])
);
$this->assertEquals(
'FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id ) LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1) LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id
LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id',
trim($queryObject->_fromClause)
);
$this->assertEquals('Test Date - greater than or equal to "June 6th, 2014 12:00 AM"', $queryObject->_qill[0][0]);
$this->assertEquals(1, $queryObject->_whereTables['civicrm_contact']);
$this->assertEquals('LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id', trim($queryObject->_whereTables[$this->getCustomGroupTable()]));
}

/**
* Test filtering by relative custom data dates.
*

0 comments on commit 6aa9454

Please sign in to comment.