Skip to content

Commit

Permalink
MDL-62318 core_group: Updating cache when deleting group memberships
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed May 9, 2018
1 parent 55c1865 commit 5c87353
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions group/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public static function delete_groups_for_all_users(\context $context, string $co
}

$DB->delete_records_select('groups_members', $select, $params);

// Purge the group and grouping cache for users.
\cache_helper::purge_by_definition('core', 'user_group_groupings');
}

/**
Expand Down Expand Up @@ -182,6 +185,9 @@ public static function delete_groups_for_user(approved_contextlist $contextlist,
}

$DB->delete_records_select('groups_members', $select, $params);

// Invalidate the group and grouping cache for the user.
\cache_helper::invalidate_by_definition('core', 'user_group_groupings', array(), array($userid));
}

/**
Expand Down
59 changes: 59 additions & 0 deletions group/tests/privacy_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,37 @@ public function test_delete_groups_for_all_users_for_component() {
);
}

/**
* Test for provider::delete_groups_for_all_users() to check deleting from cache.
*/
public function test_delete_groups_for_all_users_deletes_cache() {
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();

$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();

$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);

$this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id));
$this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2->id));
$this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1->id));

$this->assertEquals([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user1->id), '', 0.0, 10, true);
$this->assertEquals([[$group1->id]], groups_get_user_groups($course->id, $user2->id));

$coursecontext = context_course::instance($course->id);
provider::delete_groups_for_all_users($coursecontext, '');

$this->assertEquals([[]], groups_get_user_groups($course->id, $user1->id));
$this->assertEquals([[]], groups_get_user_groups($course->id, $user2->id));
}

/**
* Test for provider::delete_groups_for_user() to delete manual group memberships.
*/
Expand Down Expand Up @@ -477,6 +508,34 @@ public function test_delete_groups_for_user_for_component() {
);
}

/**
* Test for provider::delete_groups_for_user() to check deleting from cache.
*/
public function test_delete_groups_for_user_deletes_cache() {
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();

$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));

$user = $this->getDataGenerator()->create_user();

$this->getDataGenerator()->enrol_user($user->id, $course->id);

$this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group1->id));
$this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group2->id));

$this->assertEquals([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user->id), '', 0.0, 10, true);

$this->setUser($user);
$coursecontext = context_course::instance($course->id);
$approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user, 'core_group', [$coursecontext->id]);
provider::delete_groups_for_user($approvedcontextlist, '');

$this->assertEquals([[]], groups_get_user_groups($course->id, $user->id));
}

/**
* Test for provider::get_contexts_for_userid().
*/
Expand Down

0 comments on commit 5c87353

Please sign in to comment.