From 22d70ba394e3e049ad08975382e096eb7e434770 Mon Sep 17 00:00:00 2001 From: Tunbola Ogunwande Date: Tue, 24 Mar 2020 17:43:10 +0100 Subject: [PATCH] dev/core#1659: Add test to check that Case clients are not returned in duplicates. --- tests/phpunit/api/v3/CaseTest.php | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index 3e3e6fe379..4701172692 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -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. * -- 2.25.1