From 9cf08be44e11507addabc090359d8f988cb1e352 Mon Sep 17 00:00:00 2001 From: Tunbola Ogunwande Date: Wed, 18 Mar 2020 16:16:37 +0100 Subject: [PATCH 1/3] dev/core#1659: Set the param type for client Id. --- CRM/Case/BAO/Case.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 0d12748dfe33..3f0783aee480 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -1176,9 +1176,11 @@ public static function getRelatedContacts($caseID, $includeDetails = TRUE) { AND cr.is_active AND cc.id NOT IN (%2) HERESQL; + + $clientIdType = !empty($caseInfo['client_id']) ? 'CommaSeparatedIntegers' : 'String'; $params = [ 1 => [$caseID, 'Integer'], - 2 => [implode(',', $caseInfo['client_id']), 'String'], + 2 => [implode(',', $caseInfo['client_id']), $clientIdType], ]; $dao = CRM_Core_DAO::executeQuery($query, $params); From d32a853d13d5d90b7b79650225ce720b0d4c3e8a Mon Sep 17 00:00:00 2001 From: Tunbola Ogunwande Date: Tue, 24 Mar 2020 17:41:58 +0100 Subject: [PATCH 2/3] dev/core#1659: Implement logic case client condition SQL condition. --- CRM/Case/BAO/Case.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 3f0783aee480..3a81395d2256 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -1141,6 +1141,7 @@ public static function getRelatedContacts($caseID, $includeDetails = TRUE) { } $values = []; + $caseClientCondition = !empty($caseInfo['client_id']) ? "AND cc.id NOT IN (%2)" : ''; $query = << [$caseID, 'Integer'], - 2 => [implode(',', $caseInfo['client_id']), $clientIdType], ]; + + if ($caseClientCondition) { + $params[2] = [implode(',', $caseInfo['client_id']), 'CommaSeparatedIntegers']; + } $dao = CRM_Core_DAO::executeQuery($query, $params); while ($dao->fetch()) { From 22d70ba394e3e049ad08975382e096eb7e434770 Mon Sep 17 00:00:00 2001 From: Tunbola Ogunwande Date: Tue, 24 Mar 2020 17:43:10 +0100 Subject: [PATCH 3/3] 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 3e3e6fe379e9..470117269217 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -839,6 +839,53 @@ public function testCaseGetOrderByClient() { $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. *