Merge pull request #17612 from eileenmcnaughton/format
[civicrm-core.git] / tests / phpunit / CRM / Case / BAO / CaseTest.php
index f90a5da77d4a75039e3e77fd3a16cd4e4647533e..9ad9bffe201d3d8aa5bea7f71102f46c67784499 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.
    *
@@ -247,10 +356,49 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
     $this->assertEquals(1, $cases['rows']['Housing Support']['Ongoing']['count']);
   }
 
-  /* FIXME: requires activities
-   * function testGetRelatedCases() {
-   * }
+  /**
+   * Test that getRelatedCases() returns the other case when you create a
+   * Link Cases activity on one of the cases.
    */
+  public function testGetRelatedCases() {
+    $loggedInUser = $this->createLoggedInUser();
+    // create some cases
+    $client_id_1 = $this->individualCreate([], 0);
+    $caseObj_1 = $this->createCase($client_id_1, $loggedInUser);
+    $case_id_1 = $caseObj_1->id;
+    $client_id_2 = $this->individualCreate([], 1);
+    $caseObj_2 = $this->createCase($client_id_2, $loggedInUser);
+    $case_id_2 = $caseObj_2->id;
+
+    // Create link case activity. We could go thru the whole form processes
+    // but we really just want to test the BAO function so just need the
+    // activity to exist.
+    $result = $this->callAPISuccess('activity', 'create', [
+      'activity_type_id' => 'Link Cases',
+      'subject' => 'Test Link Cases',
+      'status_id' => 'Completed',
+      'source_contact_id' => $loggedInUser,
+      'target_contact_id' => $client_id_1,
+      'case_id' => $case_id_1,
+    ]);
+
+    // Put it in the format needed for endPostProcess
+    $activity = new StdClass();
+    $activity->id = $result['id'];
+    $params = [
+      'link_to_case_id' => $case_id_2,
+    ];
+    CRM_Case_Form_Activity_LinkCases::endPostProcess(NULL, $params, $activity);
+
+    // Get related cases for case 1
+    $cases = CRM_Case_BAO_Case::getRelatedCases($case_id_1);
+    // It should have case 2
+    $this->assertEquals($case_id_2, $cases[$case_id_2]['case_id']);
+
+    // Ditto but reverse the cases
+    $cases = CRM_Case_BAO_Case::getRelatedCases($case_id_2);
+    $this->assertEquals($case_id_1, $cases[$case_id_1]['case_id']);
+  }
 
   /**
    * Test various things after a case is closed.