Skip to content

Commit

Permalink
core#1623: My Case dashlet doesn't sort by name but contact_id instead
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb authored and magnolia61 committed Mar 3, 2020
1 parent d49eb93 commit 334729a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public static function getCases($allCases = TRUE, $params = [], $context = 'dash
);

$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,
Expand Down
3 changes: 3 additions & 0 deletions CRM/Utils/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Case/Page/DashboardSelector.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
109 changes: 109 additions & 0 deletions tests/phpunit/CRM/Case/BAO/CaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 334729a

Please sign in to comment.