Fix case detail report breaking when sorted by case type.
authoreileen <emcnaughton@wikimedia.org>
Fri, 22 Feb 2019 04:22:51 +0000 (17:22 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 22 Feb 2019 04:27:41 +0000 (17:27 +1300)
I removed the hacks to cope with poor metadata declaration & fixed the declaration - this should stop
breaking now. Test added

CRM/Report/Form/Case/Detail.php
tests/phpunit/api/v3/ReportTemplateTest.php

index 2493d21b1d31815c499ba6aade82e93a0c2f7c5c..0a38995fcba8cc2066eb3cb071baf00e34e45870 100644 (file)
@@ -142,11 +142,16 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form {
           'status_id' => [
             'title' => ts('Status'),
           ],
-          'case_type_name' => [
-            'title' => 'Case Type',
-          ],
         ],
       ],
+      'civicrm_case_type' => [
+        'dao' => 'CRM_Case_DAO_Case',
+        'order_bys' => [
+          'case_type_title' => [
+            'title' => 'Case Type',
+            'name' => 'title',
+        ]]
+      ],
       'civicrm_contact' => [
         'dao' => 'CRM_Contact_DAO_Contact',
         'fields' => [
@@ -339,6 +344,8 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form {
   }
 
   public function select() {
+    // @todo - get rid of this function & use parent. Use selectWhere to setthe clause for the
+    // few fields that need custom handling.
     $select = [];
     $this->_columnHeaders = [];
     foreach ($this->_columns as $tableName => $table) {
@@ -375,15 +382,6 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form {
       }
     }
 
-    if ($orderBys = $this->_params['order_bys']) {
-      foreach ($orderBys as $orderBy) {
-        if ($orderBy['column'] == 'case_type_name') {
-          $select[] = "civireport_case_types.title as case_type_name";
-          $this->_caseTypeNameOrderBy = TRUE;
-        }
-      }
-    }
-
     $this->_selectClauses = $select;
 
     $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
@@ -427,11 +425,10 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form {
       $this->_from .= " LEFT JOIN civicrm_activity {$this->_aliases['civicrm_activity_last_completed']} ON ( {$this->_aliases['civicrm_activity_last_completed']}.id = ( SELECT max(activity_id) FROM civicrm_case_activity cca, civicrm_activity ca WHERE ca.id = cca.activity_id AND cca.case_id = {$case}.id AND ca.status_id = 2 ) )";
     }
 
-    //include case type name
-    if ($this->_caseTypeNameOrderBy) {
+    if ($this->isTableSelected('civicrm_case_type')) {
       $this->_from .= "
-        LEFT JOIN civicrm_case_type civireport_case_types
-          ON {$case}.case_type_id = civireport_case_types.id
+        LEFT JOIN civicrm_case_type {$this->_aliases['civicrm_case_type']}
+          ON {$this->_aliases['civicrm_case']}.case_type_id = {$this->_aliases['civicrm_case_type']}.id
       ";
     }
   }
@@ -521,17 +518,6 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form {
     return $statistics;
   }
 
-  public function orderBy() {
-    parent::orderBy();
-
-    if ($this->_caseTypeNameOrderBy) {
-      $this->_orderBy = str_replace('case_civireport.case_type_name', 'civireport_case_types.title', $this->_orderBy);
-      if (isset($this->_sections['civicrm_case_case_type_name'])) {
-        $this->_sections['civicrm_case_case_type_name']['dbAlias'] = 'civireport_case_types.title';
-      }
-    }
-  }
-
   public function caseDetailSpecialColumnProcess() {
     if (!$this->_includeCaseDetailExtra) {
       return;
index 00b257d998c4ee3bd639817efcb099e58dabfa67..2f02b1409a04966fa4c69c31619e5b5f8d1e65d6 100644 (file)
@@ -552,6 +552,23 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase {
     $this->assertEquals(2, $rows['values'][0]['civicrm_contribution_total_amount_count']);
   }
 
+  /**
+   * Test no fatal on order by per https://lab.civicrm.org/dev/core/issues/739
+   */
+  public function testCaseDetailsCaseTypeHeader() {
+    $this->callAPISuccess('report_template', 'getrows', [
+      'report_id' => 'case/detail',
+      'fields' => ['subject' => 1, 'client_sort_name' => 1],
+       'order_bys' => [
+         1 => [
+          'column' => 'case_type_title',
+          'order' => 'ASC',
+          'section' => '1',
+        ],
+      ],
+    ]);
+  }
+
   /**
    * Test the group filter works on the contribution summary.
    */