From f2f65d3348eaf2a70a32030e4bdbed5554679c6a Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 2 May 2018 11:07:16 +1200 Subject: [PATCH] Fix fatal error on logging tab when hook alters logging tables. When a custom table is removed from logged tables by hook (e.g a calculated table like a summary table) the logging report still tries to include it but fails with a fatal error. This patch excludes it from the list of customDataEnabled logging tables --- CRM/Logging/ReportSummary.php | 20 -------------- CRM/Logging/Schema.php | 6 ++++- tests/phpunit/api/v3/ReportTemplateTest.php | 30 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/CRM/Logging/ReportSummary.php b/CRM/Logging/ReportSummary.php index 5d3f98d853..03b70dac27 100644 --- a/CRM/Logging/ReportSummary.php +++ b/CRM/Logging/ReportSummary.php @@ -232,26 +232,6 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form { $this->_where .= " AND (entity_log_civireport.log_action != 'Initialization')"; } - public function postProcess() { - $this->beginPostProcess(); - $rows = array(); - - $this->buildTemporaryTables(); - $sql = $this->buildQuery(); - - $this->buildRows($sql, $rows); - $this->addToDeveloperTab($sql); - - // format result set. - $this->formatDisplay($rows); - - // assign variables to templates - $this->doTemplateAssignment($rows); - - // do print / pdf / instance stuff if needed - $this->endPostProcess($rows); - } - /** * Get log type. * diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index 2bfd97dca7..996653ce6c 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -200,7 +200,11 @@ AND (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString ) $customGroupDAO = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); $customGroupDAO->find(); while ($customGroupDAO->fetch()) { - $customGroupTables[$customGroupDAO->table_name] = $this->logs[$customGroupDAO->table_name]; + // logging is disabled for the table (e.g by hook) then $this->logs[$customGroupDAO->table_name] + // will be empty. + if (!empty($this->logs[$customGroupDAO->table_name])) { + $customGroupTables[$customGroupDAO->table_name] = $this->logs[$customGroupDAO->table_name]; + } } return $customGroupTables; } diff --git a/tests/phpunit/api/v3/ReportTemplateTest.php b/tests/phpunit/api/v3/ReportTemplateTest.php index 96718a185f..8737c7763c 100644 --- a/tests/phpunit/api/v3/ReportTemplateTest.php +++ b/tests/phpunit/api/v3/ReportTemplateTest.php @@ -165,6 +165,36 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase { } } + /** + * Test logging report when a custom data table has a table removed by hook. + * + * Here we are checking that no fatal is triggered. + */ + public function testLoggingReportWithHookRemovalOfCustomDataTable() { + Civi::settings()->set('logging', 1); + $group1 = $this->customGroupCreate(); + $group2 = $this->customGroupCreate(['name' => 'second_one', 'title' => 'second one', 'table_name' => 'civicrm_value_second_one']); + $this->customFieldCreate(array('custom_group_id' => $group1['id'], 'label' => 'field one')); + $this->customFieldCreate(array('custom_group_id' => $group2['id'], 'label' => 'field two')); + $this->hookClass->setHook('civicrm_alterLogTables', array($this, 'alterLogTablesRemoveCustom')); + + $this->callAPISuccess('report_template', 'getrows', array( + 'report_id' => 'logging/contact/summary', + )); + Civi::settings()->set('logging', 0); + $this->customGroupDelete($group1['id']); + $this->customGroupDelete($group2['id']); + } + + /** + * Remove one log table from the logging spec. + * + * @param array $logTableSpec + */ + public function alterLogTablesRemoveCustom(&$logTableSpec) { + unset($logTableSpec['civicrm_value_second_one']); + } + /** * Test get statistics. * -- 2.25.1