fixes reporting#85 - Don't crash Contact Logging Detail report when viewing a contrib...
authorJon Goldberg <jon@megaphonetech.com>
Mon, 13 Dec 2021 19:14:55 +0000 (14:14 -0500)
committerJon Goldberg <jon@megaphonetech.com>
Mon, 28 Feb 2022 20:22:06 +0000 (15:22 -0500)
CRM/Logging/ReportDetail.php
tests/phpunit/CRM/Report/Form/Contact/LoggingDetailTest.php [new file with mode: 0644]

index 36404cf1d5450671ad898a8d09ed979b026ba2d0..e026f3d4adb03b611967cafd4626487983ff1a47 100644 (file)
@@ -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 (file)
index 0000000..0f812be
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *  Test Activity report outcome
+ *
+ * @package CiviCRM
+ */
+class CRM_Report_Form_Contact_LoggingDetailTest extends CiviReportTestCase {
+  protected $_tablesToTruncate = [
+    'civicrm_contact',
+    'log_civicrm_contact',
+  ];
+
+  public function setUp(): void {
+    parent::setUp();
+    $this->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);
+  }
+
+}