Skip to content

Commit

Permalink
Merge pull request #13801 from eileenmcnaughton/5.12
Browse files Browse the repository at this point in the history
Fix unrelased regression where activity date relative filters are ingnored in the rc
  • Loading branch information
seamuslee001 authored Mar 13, 2019
2 parents a78ec51 + 8fc9f99 commit fb617dc
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 7 deletions.
63 changes: 63 additions & 0 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,11 @@ public static function fixWhereValues($id, &$values, $wildcard = 0, $useEquals =
* @param string $apiEntity
*/
public function whereClauseSingle(&$values, $apiEntity = NULL) {
if ($this->isARelativeDateField($values[0])) {
$this->buildRelativeDateQuery($values);
return;
}

// do not process custom fields or prefixed contact ids or component params
if (CRM_Core_BAO_CustomField::getKeyID($values[0]) ||
(substr($values[0], 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) ||
Expand Down Expand Up @@ -6886,4 +6891,62 @@ protected function isPseudoFieldAnFK($fieldSpec) {
return TRUE;
}

/**
* Is the field a relative date field.
*
* @param string $fieldName
*
* @return bool
*/
protected function isARelativeDateField($fieldName) {
if (substr($fieldName, -9, 9) !== '_relative') {
return FALSE;
}
$realField = substr($fieldName, 0, strlen($fieldName) - 9);
return isset($this->_fields[$realField]);
}

/**
* @param $values
*/
protected function buildRelativeDateQuery(&$values) {
$value = CRM_Utils_Array::value(2, $values);
if (empty($value)) {
return;
}
$fieldName = substr($values[0], 0, strlen($values[0]) - 9);
$fieldSpec = $this->_fields[$fieldName];
$tableName = $fieldSpec['table_name'];
$filters = CRM_Core_OptionGroup::values('relative_date_filters');
$grouping = CRM_Utils_Array::value(3, $values);
$this->_tables[$tableName] = $this->_whereTables[$tableName] = 1;

$dates = CRM_Utils_Date::getFromTo($value, NULL, NULL);
if (empty($dates[0])) {
// ie. no start date we only have end date
$this->_where[$grouping][] = $fieldSpec['where'] . " <= '{$dates[1]}'";

$this->_qill[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("to %1", [
CRM_Utils_Date::customFormat($dates[1]),
]) . ')';
}
elseif (empty($dates[1])) {
// ie. no end date we only have start date
$this->_where[$grouping][] = $fieldSpec['where'] . " >= '{$dates[1]}'";

$this->_qill[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("from %1", [
CRM_Utils_Date::customFormat($dates[0]),
]) . ')';
}
else {
// we have start and end dates.
$this->_where[$grouping][] = $fieldSpec['where'] . " BETWEEN '{$dates[0]}' AND '{$dates[1]}'";

$this->_qill[$grouping][] = ts('%1 is ', [$fieldSpec['title']]) . $filters[$value] . ' (' . ts("between %1 and %2", [
CRM_Utils_Date::customFormat($dates[0]),
CRM_Utils_Date::customFormat($dates[1]),
]) . ')';
}
}

}
78 changes: 71 additions & 7 deletions tests/phpunit/CRM/Activity/Form/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ class CRM_Activity_Form_SearchTest extends CiviUnitTestCase {
public function setUp() {
parent::setUp();
$this->individualID = $this->individualCreate();
$this->contributionCreate(array('contact_id' => $this->individualID, 'receive_date' => '2017-01-30'));
$this->contributionCreate([
'contact_id' => $this->individualID,
'receive_date' => '2017-01-30',
]);
}

public function tearDown() {
$tablesToTruncate = array(
$tablesToTruncate = [
'civicrm_activity',
'civicrm_activity_contact',
);
];
$this->quickCleanup($tablesToTruncate);
}

Expand All @@ -32,7 +35,8 @@ public function testSearch() {
$form->postProcess();
$qfKey = $form->controller->_key;
$rows = $form->controller->get('rows');
$this->assertEquals(array(array(
$this->assertEquals([
[
'contact_id' => '3',
'contact_type' => '<a href="/index.php?q=civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4" class="crm-summary-link"><div class="icon crm-icon Individual-icon"></div></a>',
'sort_name' => 'Anderson, Anthony',
Expand All @@ -46,8 +50,8 @@ public function testSearch() {
'activity_type_id' => '6',
'activity_type' => 'Contribution',
'activity_is_test' => '0',
'target_contact_name' => array(),
'assignee_contact_name' => array(),
'target_contact_name' => [],
'assignee_contact_name' => [],
'source_contact_id' => '3',
'source_contact_name' => 'Anderson, Anthony',
'checkbox' => 'mark_x_1',
Expand All @@ -56,7 +60,67 @@ public function testSearch() {
'campaign' => NULL,
'campaign_id' => NULL,
'repeat' => '',
)), $rows);
],
], $rows);
}

/**
* Test the Qill for activity Date time.
*
* @dataProvider getSearchCriteria
*
* @param array $searchCriteria
* @param array $expectedQill
*/
public function testQill($searchCriteria, $expectedQill) {
$selector = new CRM_Activity_Selector_Search($searchCriteria);
$this->assertEquals($expectedQill, $selector->getQILL());
}

/**
* Get criteria for activity testing.
*/
public function getSearchCriteria() {

// We have to define format because tests crash trying to access the config param from the dataProvider
// perhaps because there is no property on config?
$format = '%B %E%f, %Y %l:%M %P';
$dates['ending_60.day'] = CRM_Utils_Date::getFromTo('ending_60.day', NULL, NULL);
$dates['earlier.year'] = CRM_Utils_Date::getFromTo('earlier.year', NULL, NULL);
$dates['greater.year'] = CRM_Utils_Date::getFromTo('greater.year', NULL, NULL);
return [
[
'search_criteria' => [
['activity_date_time_relative', '=', 'ending_60.day', 0, 0],
],
'expected_qill' => [['Activity Date is Last 60 days including today (between ' . CRM_Utils_Date::customFormat($dates['ending_60.day'][0], $format) . ' and ' . CRM_Utils_Date::customFormat($dates['ending_60.day'][1], $format) . ')']],
],
[
'search_criteria' => [
['activity_date_time_relative', '=', 'earlier.year', 0, 0],
],
'expected_qill' => [['Activity Date is To end of previous calendar year (to ' . CRM_Utils_Date::customFormat($dates['earlier.year'][1], $format) . ')']],
],
[
'search_criteria' => [
['activity_date_time_relative', '=', 'greater.year', 0, 0],
],
'expected_qill' => [['Activity Date is From start of current calendar year (from ' . CRM_Utils_Date::customFormat($dates['greater.year'][0], $format) . ')']],
],
[
'search_criteria' => [
['activity_date_time_low', '=', '2019-03-05', 0, 0],
['activity_date_time_high', '=', '2019-03-27', 0, 0],
],
'expected_qill' => [['Activity Date - greater than or equal to "March 5th, 2019 12:00 AM" AND less than or equal to "March 27th, 2019 11:59 PM"']],
],
[
'search_criteria' => [
['activity_status_id', '=', ['IN' => ['1', '2']], 0, 0]
],
'expected_qill' => [['Activity Status In Scheduled, Completed']],
],
];
}

}

0 comments on commit fb617dc

Please sign in to comment.