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

Fix problem with group.update api #26260

Merged
merged 2 commits into from
May 29, 2023
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
19 changes: 14 additions & 5 deletions CRM/Contact/BAO/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ public function addSelectWhereClause() {
* The new group BAO (if created)
*/
public static function create(&$params) {

// only check if we need to remove parents if a params was provided
$parentsParamProvided = array_key_exists('parents', $params);

$params += [
'group_type' => NULL,
'parents' => NULL,
Expand Down Expand Up @@ -382,8 +386,12 @@ public static function create(&$params) {
$params['name'] = CRM_Utils_String::titleToVar($params['title']);
}

if (!CRM_Utils_System::isNull($params['parents'])) {
if ($parentsParamProvided) {
$params['parents'] = CRM_Utils_Array::convertCheckboxFormatToArray((array) $params['parents']);
Copy link
Member

Choose a reason for hiding this comment

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

Refactoring note: not a blocker for this PR, but this function really shouldn't be called from the BAO as it's only needed on the quickform.

// failsafe: forbid adding itself as parent
if (!empty($params['id']) && ($key = array_search($params['id'], $params['parents'])) !== FALSE) {
unset($params['parents'][$key]);
}
}

// convert params if array type
Expand Down Expand Up @@ -448,7 +456,7 @@ public static function create(&$params) {
}
Copy link
Member

Choose a reason for hiding this comment

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

This block (see above) will actually force in a parents param, so we either need to alter $parentsParamProvided here or else modify the check below for refreshing the cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, any preference ? we could also go back to a safer approach where the cache is updated no matter what.


// first deal with removed parents
if (array_key_exists('parents', $params) && !empty($currentParentGroupIDs)) {
if ($parentsParamProvided && !empty($currentParentGroupIDs)) {
foreach ($currentParentGroupIDs as $parentGroupId) {
// no more parents or not in the new list, let's remove
if (empty($params['parents']) || !in_array($parentGroupId, $params['parents'])) {
Expand All @@ -466,9 +474,10 @@ public static function create(&$params) {
}
}

// this is always required, since we don't know when a
// parent group is removed
CRM_Contact_BAO_GroupNestingCache::update();
// refresh cache if parents param was provided
if ($parentsParamProvided || !empty($params['parents'])) {
CRM_Contact_BAO_GroupNestingCache::update();
}

// update group contact cache for all parent groups
$parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id);
Expand Down
55 changes: 55 additions & 0 deletions tests/phpunit/api/v4/Entity/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,59 @@ public function testGetParents() {
$this->assertEquals([$parent1['title'], $parent2['title']], $get['parents:label']);
}

public function testAddRemoveParents() {
$group1 = Group::create(FALSE)
->addValue('title', uniqid())
->execute()->single();
$parent1 = Group::create(FALSE)
->addValue('title', uniqid())
->execute()->single();
$parent2 = Group::create(FALSE)
->addValue('title', uniqid())
->execute()->single();

// ensure self is not added as parent
Group::update(FALSE)
->addValue('parents', [$group1['id']])
->addWhere('id', '=', $group1['id'])
->execute();
$get = Group::get(FALSE)
->addWhere('id', '=', $group1['id'])
->addSelect('parents')
->execute()->single();
$this->assertEmpty($get['parents']);

Group::update(FALSE)
->addValue('parents', [$parent1['id'], $parent2['id'], $group1['id']])
->addWhere('id', '=', $group1['id'])
->execute();
$get = Group::get(FALSE)
->addWhere('id', '=', $group1['id'])
->addSelect('parents')
->execute()->single();
$this->assertEquals([$parent1['id'], $parent2['id']], $get['parents']);

// ensure adding something else doesn't impact parents
Group::update(FALSE)
->addValue('title', uniqid())
->addWhere('id', '=', $group1['id'])
->execute();
$get = Group::get(FALSE)
->addWhere('id', '=', $group1['id'])
->addSelect('parents')
->execute()->single();
$this->assertEquals([$parent1['id'], $parent2['id']], $get['parents']);

// ensure removing parent is working
Group::update(FALSE)
->addValue('parents', [$parent2['id']])
->addWhere('id', '=', $group1['id'])
->execute();
$get = Group::get(FALSE)
->addWhere('id', '=', $group1['id'])
->addSelect('parents')
->execute()->single();
$this->assertEquals([$parent2['id']], $get['parents']);
}

}