From 92b99d977aa995a9ffe14d363fbd835575ef06c1 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 27 Jul 2022 15:45:46 +0100 Subject: [PATCH] Remove legacy apiquery call from 'isContactInGroup' --- CRM/Contact/BAO/GroupContact.php | 26 +++++++------------ tests/phpunit/CRM/Contact/BAO/ContactTest.php | 4 +-- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/CRM/Contact/BAO/GroupContact.php b/CRM/Contact/BAO/GroupContact.php index ddae644d62..8580a787d3 100644 --- a/CRM/Contact/BAO/GroupContact.php +++ b/CRM/Contact/BAO/GroupContact.php @@ -9,6 +9,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\Contact; use Civi\Api4\SubscriptionHistory; use Civi\Core\Event\PostEvent; use Civi\Core\HookInterface; @@ -502,28 +503,19 @@ SELECT * } /** + * Function that doesn't do much. + * * @param int $contactID * @param int $groupID * + * @deprecated * @return bool */ - public static function isContactInGroup($contactID, $groupID) { - if (!CRM_Utils_Rule::positiveInteger($contactID) || - !CRM_Utils_Rule::positiveInteger($groupID) - ) { - return FALSE; - } - - $params = [ - ['group', 'IN', [$groupID], 0, 0], - ['contact_id', '=', $contactID, 0, 0], - ]; - [$contacts] = CRM_Contact_BAO_Query::apiQuery($params, ['contact_id']); - - if (!empty($contacts)) { - return TRUE; - } - return FALSE; + public static function isContactInGroup(int $contactID, int $groupID) { + return (bool) Contact::get(FALSE) + ->addWhere('id', '=', $contactID) + ->addWhere('groups', 'IN', [$groupID]) + ->selectRowCount()->execute()->count(); } /** diff --git a/tests/phpunit/CRM/Contact/BAO/ContactTest.php b/tests/phpunit/CRM/Contact/BAO/ContactTest.php index 021eb09094..a5125814e9 100644 --- a/tests/phpunit/CRM/Contact/BAO/ContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/ContactTest.php @@ -648,12 +648,12 @@ class CRM_Contact_BAO_ContactTest extends CiviUnitTestCase { /** * Test case for createProfileContact. */ - public function testCreateProfileContact() { + public function testCreateProfileContact(): void { //Create 3 groups. foreach (['group1', 'group2', 'group3'] as $key => $title) { $this->groups["id{$key}"] = $this->callAPISuccess('Group', 'create', [ 'title' => $title, - 'visibility' => "Public Pages", + 'visibility' => 'Public Pages', ])['id']; } -- 2.25.1