dev/core#1659: Add test to check that Case clients are not returned in duplicates.
authorTunbola Ogunwande <tunbolawande@yahoo.com>
Tue, 24 Mar 2020 16:43:10 +0000 (17:43 +0100)
committerTunbola Ogunwande <tunbolawande@yahoo.com>
Tue, 24 Mar 2020 16:43:10 +0000 (17:43 +0100)
tests/phpunit/api/v3/CaseTest.php

index 3e3e6fe379e908e9ba4aa098c87f80550f5cd775..470117269217050024ce755ef6cc3f6ca652be1a 100644 (file)
@@ -839,6 +839,53 @@ class api_v3_CaseTest extends CiviCaseTestCase {
     $this->assertCount(3, $result['values']);
   }
 
+  /**
+   * Test Case.Get does not return case clients as part of related contacts.
+   *
+   * For multi-client cases, case clients should not be returned in duplicates for contacts.
+   */
+  public function testCaseGetDoesNotReturnClientsAsPartOfRelatedContacts() {
+    $contact1 = $this->individualCreate(['first_name' => 'Aa', 'last_name' => 'Zz']);
+    $contact2 = $this->individualCreate(['first_name' => 'Bb', 'last_name' => 'Zz']);
+    $relContact = $this->individualCreate(['first_name' => 'Rel', 'last_name' => 'Contact']);
+
+    $case = $this->callAPISuccess('Case', 'create', [
+      'contact_id' => [$contact1, $contact2],
+      'subject' => "Test case 1",
+      'case_type_id' => $this->caseTypeId,
+    ]);
+
+    $relType = $this->relationshipTypeCreate(['name_a_b' => 'Test AB', 'name_b_a' => 'Test BA', 'contact_type_b' => 'Individual']);
+    $relContact = $this->individualCreate(['first_name' => 'First', 'last_name' => 'Last']);
+    $_REQUEST = [
+      'rel_type' => "{$relType}_b_a",
+      'rel_contact' => $relContact,
+      'case_id' => $case['id'],
+      'is_unit_test' => TRUE,
+    ];
+    CRM_Contact_Page_AJAX::relationship();
+
+    $result = $this->callAPISuccess('Case', 'get', [
+      'id' => $case['id'],
+      'sequential' => 1,
+      'return' => ['id', 'contacts'],
+    ]);
+
+    $caseContacts = $result['values'][0]['contacts'];
+    $contactIds = array_column($caseContacts, 'contact_id');
+    // We basically need to ensure that the case clients are not returned more than once.
+    // i.e there should be no duplicates for case clients.
+    $caseContactInstances = (array_count_values($contactIds));
+    $this->assertEquals(1, $caseContactInstances[$contact1]);
+    $this->assertEquals(1, $caseContactInstances[$contact2]);
+
+    // Verify that the case clients are not part of related contacts.
+    $relatedContacts = CRM_Case_BAO_Case::getRelatedContacts($case['id']);
+    $relatedContacts = array_column($relatedContacts, 'contact_id');
+    $this->assertNotContains($contact1, $relatedContacts);
+    $this->assertNotContains($contact2, $relatedContacts);
+  }
+
   /**
    * Test the ability to add a timeline to an existing case.
    *