From 8dc07829c0c52cd0f3ec99d35cfb310a1c497ecf Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Mon, 13 Dec 2021 14:14:55 -0500 Subject: [PATCH] fixes reporting#85 - Don't crash Contact Logging Detail report when viewing a contribution --- CRM/Logging/ReportDetail.php | 2 +- .../Report/Form/Contact/LoggingDetailTest.php | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/CRM/Report/Form/Contact/LoggingDetailTest.php diff --git a/CRM/Logging/ReportDetail.php b/CRM/Logging/ReportDetail.php index 36404cf1d5..e026f3d4ad 100644 --- a/CRM/Logging/ReportDetail.php +++ b/CRM/Logging/ReportDetail.php @@ -477,7 +477,7 @@ class CRM_Logging_ReportDetail extends CRM_Report_Form { * @return string */ private function convertForeignKeyValuesToLabels(string $fkClassName, string $field, int $keyval): string { - if (property_exists($fkClassName, '_labelField')) { + if ($fkClassName::$_labelField) { $labelValue = CRM_Core_DAO::getFieldValue($fkClassName, $keyval, $fkClassName::$_labelField); // Not sure if this should use ts - there's not a lot of context (`%1 (id: %2)`) - and also the similar field labels above don't use ts. return "{$labelValue} (id: {$keyval})"; diff --git a/tests/phpunit/CRM/Report/Form/Contact/LoggingDetailTest.php b/tests/phpunit/CRM/Report/Form/Contact/LoggingDetailTest.php new file mode 100644 index 0000000000..0f812befb9 --- /dev/null +++ b/tests/phpunit/CRM/Report/Form/Contact/LoggingDetailTest.php @@ -0,0 +1,55 @@ +callAPISuccess('Setting', 'create', ['logging' => TRUE]); + $this->quickCleanup($this->_tablesToTruncate); + } + + public function tearDown(): void { + $this->callAPISuccess('Setting', 'create', ['logging' => FALSE]); + parent::tearDown(); + $log = new CRM_Logging_Schema(); + $log->dropAllLogTables(); + } + + /** + * Ensure a missing label name on a DAO won't crash the Logging Detail Report. + */ + public function testLabelFieldIsntRequired() { + // Create an individual and a contribution in the same database connection (as if a new contact submitted a contribution online). + $cid = $this->individualCreate(); + $this->contributionCreate(['contact_id' => $cid]); + $logConnId = CRM_Core_DAO::singleValueQuery('SELECT log_conn_id FROM log_civicrm_contribution ORDER BY log_date DESC LIMIT 1'); + + // Logging Details report builds rows in the constructor so we have to pass the log_conn_id before getReportObject does. + $tmpGlobals["_REQUEST"]["log_conn_id"] = $logConnId; + CRM_Utils_GlobalStack::singleton()->push($tmpGlobals); + // Run the report. + $input = [ + 'filters' => ['log_conn_id' => $logConnId], + ]; + $obj = $this->getReportObject('CRM_Report_Form_Contact_LoggingDetail', $input); + } + +} -- 2.25.1