From: eileen Date: Tue, 19 Jan 2016 21:33:18 +0000 (+1300) Subject: CRM-17837 Report class, various minor tweaks to support increasing test coverage X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=55f71fa785c6a5d2cd756074e89d69b15e372ed4;p=civicrm-core.git CRM-17837 Report class, various minor tweaks to support increasing test coverage --- diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index 6241e62a9e..0864946101 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -414,6 +414,16 @@ class CRM_Report_Form extends CRM_Core_Form { */ protected $sql; + + /** + * SQL being run in this report as an array. + * + * The sql in the report is stored in this variable in order to be returned to api & test calls. + * + * @var string + */ + + protected $sqlArray; /** * Class constructor. */ @@ -1274,6 +1284,7 @@ class CRM_Report_Form extends CRM_Core_Form { ); $this->assignTabs(); + $this->sqlArray[] = $sql; foreach (array('LEFT JOIN') as $term) { $sql = str_replace($term, '
  ' . $term, $sql); } @@ -2701,7 +2712,7 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND } if (!empty($orderByField)) { - $this->_orderByFields[] = $orderByField; + $this->_orderByFields[$orderByField['tplField']] = $orderByField; $orderBys[] = "{$orderByField['dbAlias']} {$orderBy['order']}"; // Record any section headers for assignment to the template @@ -2724,22 +2735,8 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND * @return array */ public function unselectedSectionColumns() { - $selectColumns = 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]) - ) { - - $selectColumns["{$tableName}_{$fieldName}"] = 1; - } - } - } - } - if (is_array($this->_sections)) { - return array_diff_key($this->_sections, $selectColumns); + return array_diff_key($this->_sections, $this->getSelectColumns()); } else { return array(); @@ -3178,6 +3175,15 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND return $this->_resultSet; } + /** + * Get the sql used to generate the report. + * + * @return string + */ + public function getReportSql() { + return $this->sqlArray; + } + /** * Use the form name to create the tpl file name. * @@ -3614,11 +3620,10 @@ ORDER BY cg.weight, cf.weight"; return; } $mapper = CRM_Core_BAO_CustomQuery::$extendsMap; + $customTables = explode(',', CRM_Core_DAO::singleValueQuery("SELECT GROUP_CONCAT(table_name) FROM civicrm_custom_group")); foreach ($this->_columns as $table => $prop) { - if (substr($table, 0, 13) == 'civicrm_value' || - substr($table, 0, 12) == 'custom_value' - ) { + if (in_array($table, $customTables)) { $extendsTable = $mapper[$prop['extends']]; // check field is in params @@ -4511,4 +4516,26 @@ LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_a $this->assign('sections', $this->_sections); } + /** + * Get an array of the columns that have been selected for display. + * + * @return array + */ + public function getSelectColumns() { + $selectColumns = 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]) + ) { + + $selectColumns["{$tableName}_{$fieldName}"] = 1; + } + } + } + } + return $selectColumns; + } + } diff --git a/CRM/Report/Form/Contact/LoggingSummary.php b/CRM/Report/Form/Contact/LoggingSummary.php index ada4a90d18..5b984acf04 100644 --- a/CRM/Report/Form/Contact/LoggingSummary.php +++ b/CRM/Report/Form/Contact/LoggingSummary.php @@ -29,7 +29,6 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ */ class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary { /** diff --git a/CRM/Report/Form/Contact/Relationship.php b/CRM/Report/Form/Contact/Relationship.php index fc506efdeb..92ef86431e 100644 --- a/CRM/Report/Form/Contact/Relationship.php +++ b/CRM/Report/Form/Contact/Relationship.php @@ -44,6 +44,13 @@ class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form { ); public $_drilldownReport = array('contact/detail' => 'Link to Detail Report'); + /** + * This will be a_b or b_a. + * + * @var string + */ + protected $relationType; + /** * Class constructor. */ @@ -557,7 +564,6 @@ class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form { public function postProcess() { $this->beginPostProcess(); - $this->relationType = NULL; $originalRelationshipTypeIdValue = $this->_params['relationship_type_id_value']; if (!empty($this->_params['relationship_type_id_value'])) { $relationshipTypes = array(); diff --git a/CRM/Report/Form/Contribute/Lybunt.php b/CRM/Report/Form/Contribute/Lybunt.php index 746f35c982..ecd99c0f12 100644 --- a/CRM/Report/Form/Contribute/Lybunt.php +++ b/CRM/Report/Form/Contribute/Lybunt.php @@ -29,8 +29,6 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ - * */ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { @@ -48,6 +46,7 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { /** */ public function __construct() { + $this->_rollup = 'WITH ROLLUP'; $this->_autoIncludeIndexedFieldsAsOrderBys = 1; $yearsInPast = 10; $yearsInFuture = 1; @@ -215,6 +214,7 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => $optionYear, 'default' => date('Y'), + 'type' => CRM_Utils_Type::T_INT, ), 'financial_type_id' => array( 'title' => ts('Financial Type'), @@ -249,10 +249,6 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { parent::__construct(); } - public function preProcess() { - parent::preProcess(); - } - public function select() { $this->_columnHeaders = $select = array(); @@ -382,7 +378,7 @@ class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { public function groupBy() { $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self::fiscalYearOffset($this->_aliases['civicrm_contribution'] . - '.receive_date') . " WITH ROLLUP"; + '.receive_date') . " " . $this->_rollup; $this->assign('chartSupported', TRUE); } diff --git a/CRM/Report/Form/Contribute/Sybunt.php b/CRM/Report/Form/Contribute/Sybunt.php index b9727aaadc..b8a664b589 100644 --- a/CRM/Report/Form/Contribute/Sybunt.php +++ b/CRM/Report/Form/Contribute/Sybunt.php @@ -29,8 +29,6 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ - * */ class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form { @@ -51,6 +49,7 @@ class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form { /** */ public function __construct() { + $this->_rollup = 'WITH ROLLUP'; $this->_autoIncludeIndexedFieldsAsOrderBys = 1; $yearsInPast = 10; $yearsInFuture = 1; @@ -221,6 +220,7 @@ class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form { 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => $optionYear, 'default' => date('Y'), + 'type' => CRM_Utils_Type::T_INT, ), 'financial_type_id' => array( 'title' => ts('Financial Type'), @@ -395,7 +395,7 @@ class CRM_Report_Form_Contribute_Sybunt extends CRM_Report_Form { $this->assign('chartSupported', TRUE); $this->_groupBy = "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self::fiscalYearOffset($this->_aliases['civicrm_contribution'] . - '.receive_date') . " WITH ROLLUP "; + '.receive_date') . " " . " " . $this->_rollup; } /** diff --git a/CRM/Report/Form/Walklist/Walklist.php b/CRM/Report/Form/Walklist/Walklist.php index 1c60343111..c8c3dd41fc 100644 --- a/CRM/Report/Form/Walklist/Walklist.php +++ b/CRM/Report/Form/Walklist/Walklist.php @@ -97,6 +97,7 @@ class CRM_Report_Form_Walklist_Walklist extends CRM_Report_Form { 'state_province_id' => array( 'title' => ts('State/Province'), 'default' => TRUE, + 'type' => CRM_Utils_Type::T_INT, ), 'country_id' => array( 'title' => ts('Country'), diff --git a/api/v3/ReportTemplate.php b/api/v3/ReportTemplate.php index 0a71b12128..aa502885fb 100644 --- a/api/v3/ReportTemplate.php +++ b/api/v3/ReportTemplate.php @@ -154,7 +154,8 @@ function _civicrm_api3_report_template_getrows($params) { $sql = $reportInstance->buildQuery(); $rows = $metadata = $requiredMetadata = array(); $reportInstance->buildRows($sql, $rows); - $requiredMetadata = array(); + $reportInstance->formatDisplay($rows); + if (isset($params['options']) && !empty($params['options']['metadata'])) { $requiredMetadata = $params['options']['metadata']; if (in_array('title', $requiredMetadata)) { @@ -162,11 +163,14 @@ function _civicrm_api3_report_template_getrows($params) { } if (in_array('labels', $requiredMetadata)) { foreach ($reportInstance->_columnHeaders as $key => $header) { - //would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty + // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty //NB I think these are already translated $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : ''; } } + if (in_array('sql', $requiredMetadata)) { + $metadata['metadata']['sql'] = $reportInstance->getReportSql(); + } } return array($rows, $reportInstance, $metadata); } diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php index 14f8b56106..50aa20fed7 100644 --- a/tests/phpunit/api/v3/ReportTemplateTest.php +++ b/tests/phpunit/api/v3/ReportTemplateTest.php @@ -184,18 +184,13 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { public static function getReportTemplates() { $reportsToSkip = array( 'activity' => 'does not respect function signature on from clause', - 'walklist' => 'Notice: Undefined index: type in CRM_Report_Form_Walklist_Walklist line 155.(suspect the select function should be removed in favour of the parent (state province field) also, type should be added to state province & others? & potentially getAddressColumns fn should be used per other reports', 'contribute/repeat' => 'Reports with important functionality in postProcess are not callable via the api. For variable setting recommend beginPostProcessCommon, for temp table creation recommend From fn', 'contribute/topDonor' => 'construction of query in postProcess makes inaccessible ', - 'contribute/sybunt' => 'e notice - (ui gives fatal error at civicrm/report/contribute/sybunt&reset=1&force=1 e-notice is on yid_valueContribute/Sybunt.php(214) because at the force url "yid_relative" not "yid_value" is defined', - 'contribute/lybunt' => 'same as sybunt - fatals on force url & test identifies why', 'event/income' => 'I do no understand why but error is Call to undefined method CRM_Report_Form_Event_Income::from() in CRM/Report/Form.php on line 2120', - 'contact/relationship' => '(see contribute/repeat), property declaration issue, Undefined property: CRM_Report_Form_Contact_Relationship::$relationType in /Contact/Relationship.php(486):', 'logging/contact/summary' => '(likely to be test related) probably logging off Undefined index: Form/Contact/LoggingSummary.php(231): PHP', 'logging/contact/detail' => '(likely to be test related) probably logging off DB Error: no such table', 'logging/contribute/summary' => '(likely to be test related) probably logging off DB Error: no such table', 'logging/contribute/detail' => '(likely to be test related) probably logging off DB Error: no such table', - 'survey/detail' => '(likely to be test related) Undefined index: CiviCampaign civicrm CRM/Core/Component.php(196)', 'contribute/history' => 'Declaration of CRM_Report_Form_Contribute_History::buildRows() should be compatible with CRM_Report_Form::buildRows($sql, &$rows)', 'activitySummary' => 'We use temp tables for the main query generation and name are dynamic. These names are not available in stats() when called directly.', );