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

[REF] Cleanup up handling of dates for Recurring & Contribution date fields in query class #14825

Merged
merged 2 commits into from
Jul 15, 2019
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
38 changes: 21 additions & 17 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ class CRM_Contribute_BAO_Query extends CRM_Core_BAO_Query {
*/
public static function getFields($checkPermission = TRUE) {
if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['fields']) || !isset(\Civi::$statics[__CLASS__]['fields']['contribution'])) {
// Adding fields with some care as those without unique names could clobber others.
// Refer to CRM_Contribute_Form_SearchTest for existing tests ... and to add more!
$testedRecurFields = array_fill_keys(['contribution_recur_trxn_id', 'contribution_recur_processor_id', 'contribution_recur_payment_processor_id'], 1);
$recurFields = array_intersect_key(CRM_Contribute_DAO_ContributionRecur::fields(), $testedRecurFields);
$recurFields = CRM_Contribute_DAO_ContributionRecur::fields();
foreach ($recurFields as $fieldKey => $field) {
// We can only safely add in those with unique names as those without could clobber others.
// The array is keyed by unique names so if it doesn't match the key there is no unique name & we unset
// Refer to CRM_Contribute_Form_SearchTest for existing tests ... and to add more!
if ($field['name'] === $fieldKey) {
unset($recurFields[$fieldKey]);
}
}
$fields = array_merge($recurFields, CRM_Contribute_BAO_Contribution::exportableFields($checkPermission));
CRM_Contribute_BAO_Contribution::appendPseudoConstantsToFields($fields);
unset($fields['contribution_contact_id']);
Expand Down Expand Up @@ -179,6 +184,17 @@ public static function whereClauseSingle(&$values, &$query) {
$qillName = array_search($name, $fieldAliases);
}
$pseudoExtraParam = [];
$fieldName = str_replace(['_high', '_low'], '', $name);
$fieldSpec = CRM_Utils_Array::value($fieldName, $fields, []);
$tableName = CRM_Utils_Array::value('table_name', $fieldSpec, 'civicrm_contribution');
$dataType = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $fieldSpec));
if ($dataType === 'Timestamp' || $dataType === 'Date') {
$query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
$query->dateQueryBuilder($values,
$tableName, $fieldName, $fieldSpec['name'], $fieldSpec['title']
);
return;
}

switch ($name) {
case 'contribution_date':
Expand Down Expand Up @@ -230,17 +246,6 @@ public static function whereClauseSingle(&$values, &$query) {
$query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
return;

case 'contribution_cancel_date':
case 'contribution_cancel_date_low':
case 'contribution_cancel_date_low_time':
case 'contribution_cancel_date_high':
case 'contribution_cancel_date_high_time':
// process to / from date
$query->dateQueryBuilder($values,
'civicrm_contribution', 'contribution_cancel_date', 'cancel_date', ts('Cancelled / Refunded Date')
);
return;

case 'financial_type_id':
case 'invoice_id':
case 'invoice_number':
Expand All @@ -256,7 +261,6 @@ public static function whereClauseSingle(&$values, &$query) {
case 'contribution_check_number':
case 'contribution_contact_id':
case (strpos($name, '_amount') !== FALSE):
case (strpos($name, '_date') !== FALSE && $name != 'contribution_fulfilled_date'):
case 'contribution_campaign_id':

$fieldNamesNotToStripContributionFrom = [
Expand Down Expand Up @@ -516,7 +520,7 @@ public static function whereClauseSingle(&$values, &$query) {
$query->_qill[$grouping][] = "$whereTable[title] $op $quoteValue";
list($tableName) = explode('.', $whereTable['where'], 2);
$query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
if ($tableName == 'civicrm_contribution_product') {
if ($tableName === 'civicrm_contribution_product') {
$query->_tables['civicrm_product'] = $query->_whereTables['civicrm_product'] = 1;
$query->_tables['civicrm_contribution'] = $query->_whereTables['civicrm_contribution'] = 1;
}
Expand Down
104 changes: 99 additions & 5 deletions tests/phpunit/CRM/Contribute/Form/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,30 @@ public function testContributionSearchWithContactID() {
*
* @dataProvider getSearchData
*/
public function testContributionRecurSearchFilters($formValues, $expectedCount, $expectedContact, $expectedQill) {
public function testContributionRecurSearchFilters($formValues, $expectedCount, $expectedContact, $expectedQill, $expectedWhere = NULL) {
$this->setUpRecurringContributions();

$query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($formValues));
list($select, $from, $where, $having) = $query->query();

// get and assert contribution count
$contacts = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT contact_a.id, contact_a.display_name %s %s AND contact_a.id IS NOT NULL', $from, $where))->fetchAll();
foreach ($contacts as $key => $value) {
$this->assertEquals($expectedContact[$key], $value['display_name']);
if ($expectedCount) {
foreach ($contacts as $key => $value) {
$this->assertEquals($expectedContact[$key], $value['display_name']);
}
}
// assert the contribution count
$this->assertEquals($expectedCount, count($contacts));
// get and assert qill string
$qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
$qill = $query->qill();
$qillString = !empty($qill[1]) ? $qill[1] : CRM_Utils_Array::value(0, $qill);
$qill = trim(implode($query->getOperator(), $qillString));
$this->assertEquals($expectedQill, $qill);

if ($expectedWhere) {
$this->assertEquals($expectedWhere, $query->_where[1][0]);
}
}

/**
Expand Down Expand Up @@ -551,11 +559,12 @@ protected function setUpRecurringContributions() {
'financial_type_id' => 'Donation',
'trxn_id' => 'a transaction',
'processor_id' => 'a processor',
'start_date' => '20180101',
]);
$Contribution2 = $this->callAPISuccess('Contribution', 'create', [
'financial_type_id' => 'Donation',
'total_amount' => 22,
'receive_date' => date('Ymd'),
'receive_date' => '20180101',
'receive_date_time' => NULL,
'payment_instrument' => 1,
'contribution_status_id' => 1,
Expand Down Expand Up @@ -611,6 +620,91 @@ public function getSearchData() {
'expected_contact' => ['Mr. Terrence Smith II'],
'expected_qill' => "Recurring Contribution Processor ID = 'a processor'",
],
'receive_date_search' => [
'form_value' => [['receive_date_high', '=', 20180101, 1, 0]],
'expectedResult' => 1,
'expected_contact' => ['Mr. Terrence Smith II'],
'expected_qill' => 'Date Received - less than or equal to "January 1st, 2018 12:00 AM"',
'expected_where' => "civicrm_contribution.receive_date <= '20180101000000'",
],
'thankyou_date_search' => [
'form_value' => [['thankyou_date_high', '=', 20180101, 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Thank-you Date - less than or equal to "January 1st, 2018 12:00 AM"',
'expected_where' => "civicrm_contribution.thankyou_date <= '20180101000000'",
],
'cancel_date_search_low' => [
'form_value' => [['contribution_cancel_date_low', '=', 20180101, 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Cancelled / Refunded Date - greater than or equal to "January 1st, 2018 12:00 AM"',
'expected_where' => "civicrm_contribution.cancel_date >= '20180101000000'",
],
'cancel_date_search' => [
'form_value' => [['contribution_cancel_date', '=', 20180101, 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Cancelled / Refunded Date = January 1st, 2018 12:00 AM',
'expected_where' => "civicrm_contribution.cancel_date = '20180101000000'",
],
'cancel_date_relative' => [
'form_value' => [['contribution_cancel_date_relative', '=', 'this.year', 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Cancelled / Refunded Date is This calendar year (between January 1st, ' . date('Y') . ' 12:00 AM and December 31st, ' . date('Y') . ' 11:59 PM)',
'expected_where' => "civicrm_contribution.cancel_date BETWEEN '" . date('Y') . "0101000000' AND '" . date('Y') . "1231235959'",
],
'receipt_date_search_low' => [
'form_value' => [['receipt_date_low', '=', 20180101, 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Receipt Date - greater than or equal to "January 1st, 2018 12:00 AM"',
'expected_where' => "civicrm_contribution.receipt_date >= '20180101000000'",
],
'receipt_date_search' => [
'form_value' => [['receipt_date', '=', 20180101, 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Receipt Date = \'20180101\'',
'expected_where' => "civicrm_contribution.receipt_date = 20180101",
],
'revenue_recognition_search_high' => [
'form_value' => [['revenue_recognition_date_high', '=', 20180101, 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Revenue Recognition Date - less than or equal to "January 1st, 2018 12:00 AM"',
'expected_where' => "civicrm_contribution.revenue_recognition_date <= '20180101000000'",
],
'revenue_recognition_search' => [
'form_value' => [['revenue_recognition_date', '=', 20180101, 1, 0]],
'expectedResult' => 0,
'expected_contact' => [],
'expected_qill' => 'Revenue Recognition Date = \'20180101\'',
'expected_where' => "civicrm_contribution.revenue_recognition_date = 20180101",
],
'start_date_search' => [
'form_value' => [['contribution_recur_start_date', '=', 20180101, 1, 0]],
'expectedResult' => 1,
'expected_contact' => ['Mr. Terrence Smith II'],
'expected_qill' => 'Recurring Contribution Start Date = January 1st, 2018 12:00 AM',
'expected_where' => "civicrm_contribution_recur.start_date = '20180101000000'",
],
'start_date_search_high' => [
'form_value' => [['contribution_recur_start_date_high', '<=', 20180101, 1, 0]],
'expectedResult' => 1,
'expected_contact' => ['Mr. Terrence Smith II'],
'expected_qill' => 'Recurring Contribution Start Date - less than or equal to "January 1st, 2018 12:00 AM"',
'expected_where' => "civicrm_contribution_recur.start_date <= '20180101000000'",
],
'start_date_search_relative' => [
'form_value' => [['contribution_recur_start_date_relative', '=', 'this.year', 1, 0]],
'expectedResult' => 1,
'expected_contact' => ['Mr. Joe Miller II'],
'expected_qill' => 'Start Date is This calendar year (between January 1st, ' . date('Y') . ' 12:00 AM and December 31st, ' . date('Y') . ' 11:59 PM)',
'expected_where' => "civicrm_contribution_recur.start_date BETWEEN '" . date('Y') . "0101000000' AND '" . date('Y') . "1231235959'",
],

];
return $useCases;
}
Expand Down