diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php
index f24066c6fbb8..bda424a9eff9 100644
--- a/CRM/Case/BAO/Case.php
+++ b/CRM/Case/BAO/Case.php
@@ -594,7 +594,7 @@ public static function getCases($allCases = TRUE, $params = [], $context = 'dash
);
$phone = empty($case['phone']) ? '' : '
' . $case['phone'] . '';
- $casesList[$key]['contact_id'] = sprintf('%s%s
%s: %d',
+ $casesList[$key]['sort_name'] = sprintf('%s%s
%s: %d',
CRM_Utils_System::url('civicrm/contact/view', ['cid' => $case['contact_id']]),
$case['sort_name'],
$phone,
diff --git a/CRM/Utils/JSON.php b/CRM/Utils/JSON.php
index 144c66900220..5f3bcabd85a2 100644
--- a/CRM/Utils/JSON.php
+++ b/CRM/Utils/JSON.php
@@ -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();
diff --git a/templates/CRM/Case/Page/DashboardSelector.tpl b/templates/CRM/Case/Page/DashboardSelector.tpl
index 7b3bbe1d4309..c9d46d9b467b 100644
--- a/templates/CRM/Case/Page/DashboardSelector.tpl
+++ b/templates/CRM/Case/Page/DashboardSelector.tpl
@@ -13,7 +13,7 @@
|
- {ts}Contact{/ts} |
+ {ts}Contact{/ts} |
{ts}Subject{/ts} |
{ts}Status{/ts} |
{ts}Type{/ts} |
diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php
index f90a5da77d4a..f7786f9f5bd4 100644
--- a/tests/phpunit/CRM/Case/BAO/CaseTest.php
+++ b/tests/phpunit/CRM/Case/BAO/CaseTest.php
@@ -136,6 +136,115 @@ private function assertCasesOfUser($loggedInUser, $caseId, $caseCount) {
$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.
*