Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy apiquery call from isContactInGroup #24062

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions CRM/Contact/BAO/GroupContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Contact;
use Civi\Api4\SubscriptionHistory;
use Civi\Core\Event\PostEvent;
use Civi\Core\HookInterface;
Expand Down Expand Up @@ -502,28 +503,19 @@ public static function create(array $params) {
}

/**
* 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
->addWhere('id', '=', $contactID)
->addWhere('id', '=', $contactID)
->selectRowCount()

Copy link
Member

@colemanw colemanw Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton this PR looks good overall, but if all we're doing is counting then this results in more efficient SQL.

->addWhere('groups', 'IN', [$groupID])
->selectRowCount()->execute()->count();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Contact/BAO/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,12 @@ public function testDeleteContact() {
/**
* 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'];
}

Expand Down