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-19060 preliminary tidy up #11389

Merged
merged 3 commits into from
Dec 8, 2017
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
70 changes: 55 additions & 15 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,9 @@ public function preProcess() {
}
}
}
if (!isset($this->_columns[$tableName]['metadata'][$fieldName])) {
$this->_columns[$tableName]['metadata'][$fieldName] = $this->_columns[$tableName][$fieldGrp][$fieldName];
}
}
}
}
Expand Down Expand Up @@ -2281,6 +2284,7 @@ public function buildChart(&$rows) {
*/
public function select() {
$select = $this->_selectAliases = array();
$this->storeGroupByArray();

foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
Expand Down Expand Up @@ -2311,7 +2315,7 @@ public function select() {
}

// include statistics columns only if set
if (!empty($field['statistics'])) {
if (!empty($field['statistics']) && !empty($this->_groupByArray)) {
$select = $this->addStatisticsToSelect($field, $tableName, $fieldName, $select);
}
else {
Expand Down Expand Up @@ -2645,19 +2649,7 @@ public function buildQuery($applyLimit = TRUE) {
* Build group by clause.
*/
public function groupBy() {
if (!empty($this->_params['group_bys']) &&
is_array($this->_params['group_bys'])
) {
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('group_bys', $table)) {
foreach ($table['group_bys'] as $fieldName => $field) {
if (!empty($this->_params['group_bys'][$fieldName])) {
$this->_groupByArray[] = $field['dbAlias'];
}
}
}
}
}
$this->storeGroupByArray();

if (!empty($this->_groupByArray)) {
$this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $this->_groupByArray);
Expand Down Expand Up @@ -3370,7 +3362,7 @@ public function legacySlowGroupFilterClause($field, $value, $op) {
$group->find();
$smartGroups = array();
while ($group->fetch()) {
if (in_array($group->id, $this->_params['gid_value']) &&
if (in_array($group->id, (array) $this->_params['gid_value']) &&
$group->saved_search_id
) {
$smartGroups[] = $group->id;
Expand Down Expand Up @@ -5194,6 +5186,54 @@ protected function buildColumns($specs, $tableName, $daoName = NULL, $tableAlias
return $columns;
}

/**
* Store group bys into array - so we can check elsewhere what is grouped.
*/
protected function storeGroupByArray() {

if (CRM_Utils_Array::value('group_bys', $this->_params) &&
is_array($this->_params['group_bys']) &&
!empty($this->_params['group_bys'])
) {
foreach ($this->_columns as $tableName => $table) {
$table = $this->_columns[$tableName];
if (array_key_exists('group_bys', $table)) {
foreach ($table['group_bys'] as $fieldName => $fieldData) {
$field = $this->_columns[$tableName]['metadata'][$fieldName];
if (!empty($this->_params['group_bys'][$fieldName])) {
if (!empty($field['chart'])) {
$this->assign('chartSupported', TRUE);
}

if (!empty($table['group_bys'][$fieldName]['frequency']) &&
!empty($this->_params['group_bys_freq'][$fieldName])
) {

switch ($this->_params['group_bys_freq'][$fieldName]) {
case 'FISCALYEAR':
$this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = self::fiscalYearOffset($field['dbAlias']);

case 'YEAR':
$this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = " {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";

default:
$this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_{$this->_params['group_bys_freq'][$fieldName]} FROM {$field['dbAlias']})";

}
}
else {
if (!in_array($field['dbAlias'], $this->_groupByArray)) {
$this->_groupByArray[$tableName . '_' . $fieldName] = $field['dbAlias'];
}
}
}
}

}
}
}
}

/**
* @param $options
*
Expand Down
56 changes: 8 additions & 48 deletions CRM/Report/Form/Member/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,9 @@
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
* $Id$
*
*/
class CRM_Report_Form_Member_Detail extends CRM_Report_Form {

protected $_addressField = FALSE;

protected $_emailField = FALSE;

protected $_phoneField = FALSE;

protected $_contribField = FALSE;

protected $_summary = NULL;

protected $_customGroupExtends = array(
Expand All @@ -66,7 +56,7 @@ class CRM_Report_Form_Member_Detail extends CRM_Report_Form {
*
* @var bool
*/
protected $groupFilterNotOptimised = TRUE;
protected $groupFilterNotOptimised = FALSE;

/**
* Class constructor.
Expand Down Expand Up @@ -283,39 +273,10 @@ public function preProcess() {
parent::preProcess();
}

public function select() {
$select = $this->_columnHeaders = array();

foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
if ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}
elseif ($tableName == 'civicrm_phone') {
$this->_phoneField = TRUE;
}
elseif ($tableName == 'civicrm_contribution') {
$this->_contribField = TRUE;
}
$select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
if (array_key_exists('title', $field)) {
$this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
}
$this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
}
}
}
}

$this->_selectClauses = $select;
$this->_select = "SELECT " . implode(', ', $select) . " ";
}

public function from() {
$this->_from = "
FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
$this->setFromBase('civicrm_contact');
$this->_from .= "
{$this->_aclFrom}
INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
ON {$this->_aliases['civicrm_contact']}.id =
{$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
Expand All @@ -331,24 +292,23 @@ public function from() {
{$this->_aliases['civicrm_address']}.is_primary = 1\n";
}

//used when email field is selected
if ($this->_emailField) {
if ($this->isTableSelected('civicrm_email')) {
$this->_from .= "
LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
ON {$this->_aliases['civicrm_contact']}.id =
{$this->_aliases['civicrm_email']}.contact_id AND
{$this->_aliases['civicrm_email']}.is_primary = 1\n";
}
//used when phone field is selected
if ($this->_phoneField) {
if ($this->isTableSelected('civicrm_phone')) {
$this->_from .= "
LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
ON {$this->_aliases['civicrm_contact']}.id =
{$this->_aliases['civicrm_phone']}.contact_id AND
{$this->_aliases['civicrm_phone']}.is_primary = 1\n";
}
//used when contribution field is selected
if ($this->_contribField) {
//used when contribution field is selected.
if ($this->isTableSelected('civicrm_contribution')) {
$this->_from .= "
LEFT JOIN civicrm_membership_payment cmp
ON {$this->_aliases['civicrm_membership']}.id = cmp.membership_id
Expand Down
59 changes: 44 additions & 15 deletions tests/phpunit/api/v3/ReportTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,28 @@ public static function getReportTemplates() {
* These templates require minimal data config.
*/
public static function getContributionReportTemplates() {
return array(array('contribute/summary'), array('contribute/detail'), array('contribute/repeat'), array('contribute/topDonor'));
return array(array('contribute/summary'), array('contribute/detail'), array('contribute/repeat'), array('topDonor' => 'contribute/topDonor'));
}

/**
* Get contribution templates that work with basic filter tests.
*
* These templates require minimal data config.
*/
public static function getMembershipReportTemplates() {
return array(array('member/detail'));
}

public static function getMembershipAndContributionReportTemplatesForGroupTests() {
$templates = array_merge(self::getContributionReportTemplates(), self::getMembershipReportTemplates());
foreach ($templates as $key => $value) {
if (array_key_exists('topDonor', $value)) {
// Report is not standard enough to test here.
unset($templates[$key]);
}

}
return $templates;
}

/**
Expand Down Expand Up @@ -312,23 +333,32 @@ public function testLybuntReportWithFYDataOrderByLastYearAmount() {

/**
* Test the group filter works on the contribution summary (with a smart group).
*
* @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
*
* @param string $template
* Name of the template to test.
*/
public function testContributionSummaryWithSmartGroupFilter() {
public function testContributionSummaryWithSmartGroupFilter($template) {
$groupID = $this->setUpPopulatedSmartGroup();
$rows = $this->callAPISuccess('report_template', 'getrows', array(
'report_id' => 'contribute/summary',
'report_id' => $template,
'gid_value' => $groupID,
'gid_op' => 'in',
'options' => array('metadata' => array('sql')),
));
$this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);

$this->assertNumberOfContactsInResult(3, $rows, $template);
if ($template === 'contribute/summary') {
$this->assertEquals(3, $rows['values'][0]['civicrm_contribution_total_amount_count']);
}
}

/**
* Test the group filter works on the contribution summary (with a smart group).
* Test the group filter works on the contribution summary.
*
* @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
*/
public function testContributionSummaryWithNotINSmartGroupFilter() {
public function testContributionSummaryWithNotINSmartGroupFilter($template) {
$groupID = $this->setUpPopulatedSmartGroup();
$rows = $this->callAPISuccess('report_template', 'getrows', array(
'report_id' => 'contribute/summary',
Expand All @@ -341,14 +371,14 @@ public function testContributionSummaryWithNotINSmartGroupFilter() {
}

/**
* Test the group filter works on the contribution summary (with a smart group).
* Test the group filter works on the various reports.
*
* @dataProvider getContributionReportTemplates
* @dataProvider getMembershipAndContributionReportTemplatesForGroupTests
*
* @param string $template
* Report template unique identifier.
*/
public function testContributionSummaryWithNonSmartGroupFilter($template) {
public function testReportsWithNonSmartGroupFilter($template) {
$groupID = $this->setUpPopulatedGroup();
$rows = $this->callAPISuccess('report_template', 'getrows', array(
'report_id' => $template,
Expand Down Expand Up @@ -467,6 +497,7 @@ public function setUpPopulatedSmartGroup() {
));
foreach (array($household1ID, $individual1ID, $householdID, $individualID, $individualIDRemoved) as $contactID) {
$this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
$this->contactMembershipCreate(array('contact_id' => $contactID));
}

// Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
Expand All @@ -475,12 +506,9 @@ public function setUpPopulatedSmartGroup() {
}

/**
* Set up a smart group for testing.
*
* The smart group includes all Households by filter. In addition an individual
* is created and hard-added and an individual is created that is not added.
* Set up a static group for testing.
*
* One household is hard-added as well as being in the filter.
* An individual is created and hard-added and an individual is created that is not added.
*
* This gives us a range of scenarios for testing contacts are included only once
* whenever they are hard-added or in the criteria.
Expand All @@ -507,6 +535,7 @@ public function setUpPopulatedGroup($returnAddedContact = FALSE) {

foreach (array($individual1ID, $individualID, $individualIDRemoved) as $contactID) {
$this->contributionCreate(array('contact_id' => $contactID, 'invoice_id' => '', 'trxn_id' => ''));
$this->contactMembershipCreate(array('contact_id' => $contactID));
}

// Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
Expand Down