core#1623: My Case dashlet doesn't sort by name but contact_id instead
authorMonish Deb <deb.monish@gmail.com>
Fri, 28 Feb 2020 09:17:29 +0000 (14:47 +0530)
committerMonish Deb <deb.monish@gmail.com>
Mon, 2 Mar 2020 04:58:52 +0000 (10:28 +0530)
CRM/Case/BAO/Case.php
CRM/Utils/JSON.php
templates/CRM/Case/Page/DashboardSelector.tpl
tests/phpunit/CRM/Case/BAO/CaseTest.php

index f24066c6fbb8cf7efd4ddf775ad44037c35c192f..bda424a9eff909f6ac02524f7445be2e5ec851be 100644 (file)
@@ -594,7 +594,7 @@ HERESQL;
       );
 
       $phone = empty($case['phone']) ? '' : '<br /><span class="description">' . $case['phone'] . '</span>';
-      $casesList[$key]['contact_id'] = sprintf('<a href="%s">%s</a>%s<br /><span class="description">%s: %d</span>',
+      $casesList[$key]['sort_name'] = sprintf('<a href="%s">%s</a>%s<br /><span class="description">%s: %d</span>',
         CRM_Utils_System::url('civicrm/contact/view', ['cid' => $case['contact_id']]),
         $case['sort_name'],
         $phone,
index 144c66900220bf3183ac3a7da64bc8c95da98cd3..5f3bcabd85a2e3c275ca917e77d5f413a67d3d73 100644 (file)
@@ -25,6 +25,9 @@ class CRM_Utils_JSON {
    * @param mixed $input
    */
   public static function output($input) {
+    if (CIVICRM_UF === 'UnitTests') {
+      throw new CRM_Core_Exception_PrematureExitException('civiExit called', $input);
+    }
     CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
     echo json_encode($input);
     CRM_Utils_System::civiExit();
index 7b3bbe1d4309815e4d5d9e8fb1a9db68a14fae22..c9d46d9b467b276cdf356cb067d3e71500cb9b15 100644 (file)
@@ -13,7 +13,7 @@
 <thead>
   <tr>
     <th data-data="activity_list" data-orderable="false" class="crm-case-activity_list"></th>
-    <th data-data="contact_id" class="crm-case-contact">{ts}Contact{/ts}</th>
+    <th data-data="sort_name" class="crm-case-contact">{ts}Contact{/ts}</th>
     <th data-data="subject" cell-class="crmf-subject crm-editable" class="crm-case-subject">{ts}Subject{/ts}</th>
     <th data-data="case_status" class="crm-case-status">{ts}Status{/ts}</th>
     <th data-data="case_type" class="crm-case-type">{ts}Type{/ts}</th>
index f90a5da77d4a75039e3e77fd3a16cd4e4647533e..f7786f9f5bd4c0486e051c1db81314234320fc37 100644 (file)
@@ -136,6 +136,115 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
     $this->assertEquals($caseCount, count($caseRoles), 'Total case roles for logged in users must be ' . $caseCount);
   }
 
+  /**
+   * core/issue-1623: My Case dashlet doesn't sort by name but contact_id instead
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testSortByCaseContact() {
+    // delete any cases if present
+    $this->callAPISuccess('Case', 'get', ['api.Case.delete' => ['id' => '$value.id']]);
+
+    // create three contacts with different name, later used in respective cases
+    $contacts = [
+      $this->individualCreate(['first_name' => 'Antonia', 'last_name' => 'D`souza']),
+      $this->individualCreate(['first_name' => 'Darric', 'last_name' => 'Roy']),
+      $this->individualCreate(['first_name' => 'Adam', 'last_name' => 'Pitt']),
+    ];
+    $loggedInUser = $this->createLoggedInUser();
+    $relationshipType = $this->relationshipTypeCreate([
+      'contact_type_b' => 'Individual',
+    ]);
+
+    // create cases for each contact
+    $cases = [];
+    foreach ($contacts as $contactID) {
+      $cases[] = $caseID = $this->createCase($contactID)->id;
+      $this->callAPISuccess('Relationship', 'create', [
+        'contact_id_a'         => $contactID,
+        'contact_id_b'         => $loggedInUser,
+        'relationship_type_id' => $relationshipType,
+        'case_id'              => $caseID,
+        'is_active'            => TRUE,
+      ]);
+    }
+
+    // USECASE A: fetch all cases using the AJAX fn without any sorting criteria, and match the result
+    global $_GET;
+    $_GET = [
+      'start' => 0,
+      'length' => 10,
+      'type' => 'any',
+      'all' => 1,
+      'is_unittest' => 1,
+    ];
+
+    $cases = [];
+    try {
+      CRM_Case_Page_AJAX::getCases();
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $cases = $e->errorData['data'];
+    }
+
+    // list of expected sorted names in order the respective cases were created
+    $unsortedExpectedContactNames = [
+      'D`souza, Antonia',
+      'Roy, Darric',
+      'Pitt, Adam',
+    ];
+    $unsortedActualContactNames = CRM_Utils_Array::collect('sort_name', $cases);
+    foreach ($unsortedExpectedContactNames as $key => $name) {
+      $this->assertContains($name, $unsortedActualContactNames[$key]);
+    }
+
+    // USECASE B: fetch all cases using the AJAX fn based any 'Contact' sorting criteria, and match the result against expected sequence of names
+    $_GET = [
+      'start' => 0,
+      'length' => 10,
+      'type' => 'any',
+      'all' => 1,
+      'is_unittest' => 1,
+      'columns' => [
+        1 => [
+          'data' => 'sort_name',
+          'name' => NULL,
+          'searchable' => TRUE,
+          'orderable' => TRUE,
+          'search' => [
+            'value' => NULL,
+            'regex' => FALSE,
+          ],
+        ],
+      ],
+      'order' => [
+        [
+          'column' => 1,
+          'dir' => 'asc',
+        ],
+      ],
+    ];
+
+    $cases = [];
+    try {
+      CRM_Case_Page_AJAX::getCases();
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $cases = $e->errorData['data'];
+    }
+
+    // list of expected sorted names in ASC order
+    $sortedExpectedContactNames = [
+      'D`souza, Antonia',
+      'Pitt, Adam',
+      'Roy, Darric',
+    ];
+    $sortedActualContactNames = CRM_Utils_Array::collect('sort_name', $cases);
+    foreach ($sortedExpectedContactNames as $key => $name) {
+      $this->assertContains($name, $sortedActualContactNames[$key]);
+    }
+  }
+
   /**
    * Test that Case count is exactly one for logged in user for user's active role.
    *