Skip to content

Commit

Permalink
Merge pull request #14814 from eileenmcnaughton/reg515
Browse files Browse the repository at this point in the history
dev/report/15 Add fix and tests for contact subtype report filter
  • Loading branch information
eileenmcnaughton authored Jul 14, 2019
2 parents ac7af20 + 5f13f94 commit 75c2638
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
26 changes: 22 additions & 4 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2129,12 +2129,30 @@ public function whereClause(&$field, $op, $value, $min, $max) {
* @return string
*/
public function whereSubtypeClause($field, $value, $op) {
// Get the correct SQL operator.
switch ($op) {
case 'notin':
$op = 'nhas';
$clauseSeparator = 'AND';
break;

case 'in':
$op = 'has';
$clauseSeparator = 'OR';
break;
}
$sqlOp = $this->getSQLOperator($op);
$clause = '( ';
$subtypeFilters = count($value);
for ($i = 0; $i < $subtypeFilters; $i++) {
$clause .= "{$field['dbAlias']} LIKE '%$value[$i]%'";
if ($i !== ($subtypeFilters - 1)) {
$clause .= " OR ";
if ($sqlOp == 'IS NULL' || $sqlOp == 'IS NOT NULL') {
$clause .= "{$field['dbAlias']} $sqlOp";
}
else {
for ($i = 0; $i < $subtypeFilters; $i++) {
$clause .= "{$field['dbAlias']} $sqlOp '%$value[$i]%'";
if ($i !== ($subtypeFilters - 1)) {
$clause .= " $clauseSeparator ";
}
}
}
$clause .= ' )';
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/api/v3/ReportTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,44 @@ public function testGrantReportSeparatedFilter() {
$this->assertEquals(1, $rows['count']);
}

/**
* Test contact subtype filter on summary report.
*
* @throws \CRM_Core_Exception
*/
public function testContactSubtypeNotNull() {
$this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]);
$this->individualCreate();

$rows = $this->callAPISuccess('report_template', 'getrows', [
'report_id' => 'contact/summary',
'contact_sub_type_op' => 'nnll',
'contact_sub_type_value' => [],
'contact_type_op' => 'eq',
'contact_type_value' => 'Individual',
]);
$this->assertEquals(1, $rows['count']);
}

/**
* Test contact subtype filter on summary report.
*
* @throws \CRM_Core_Exception
*/
public function testContactSubtypeNull() {
$this->individualCreate(['contact_sub_type' => ['Student', 'Parent']]);
$this->individualCreate();

$rows = $this->callAPISuccess('report_template', 'getrows', [
'report_id' => 'contact/summary',
'contact_sub_type_op' => 'nll',
'contact_sub_type_value' => [],
'contact_type_op' => 'eq',
'contact_type_value' => 'Individual',
]);
$this->assertEquals(1, $rows['count']);
}

/**
* Test PCP report to ensure total donors and total committed is accurate.
*/
Expand Down

0 comments on commit 75c2638

Please sign in to comment.