Skip to content

Commit

Permalink
MDL-63535 core_rss: Add support for removal of context users
Browse files Browse the repository at this point in the history
This issue is part of the MDL-62560 Epic.
  • Loading branch information
Mihail Geshoski committed Oct 23, 2018
1 parent cc486e6 commit cee33e0
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
32 changes: 32 additions & 0 deletions rss/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use \core_privacy\local\request\approved_contextlist;
use \core_privacy\local\request\transform;
use \core_privacy\local\request\writer;
use core_privacy\local\request\userlist;
use \core_privacy\local\request\approved_userlist;

/**
* Privacy class for requesting user data.
Expand All @@ -40,6 +42,7 @@
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\subsystem\provider {

/**
Expand Down Expand Up @@ -74,6 +77,21 @@ public static function get_contexts_for_userid(int $userid) : contextlist {
return $contextlist;
}

/**
* Get the list of users within a specific context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();

if (!$context instanceof \context_user) {
return;
}

\core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'rss');
}

/**
* Export all user data for the specified user, in the specified contexts.
*
Expand Down Expand Up @@ -113,6 +131,20 @@ public static function delete_data_for_all_users_in_context(\context $context) {

}

/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {

$context = $userlist->get_context();

if ($context instanceof \context_user) {
\core_userkey\privacy\provider::delete_userkeys('rss', $context->instanceid);
}
}

/**
* Delete all user data for the specified user, in the specified contexts.
*
Expand Down
87 changes: 87 additions & 0 deletions rss/tests/privacy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use \core_rss\privacy\provider;
use \core_privacy\local\request\writer;
use \core_privacy\local\request\approved_contextlist;
use \core_privacy\local\request\approved_userlist;

/**
* Unit tests for rss\classes\privacy\provider.php
Expand Down Expand Up @@ -127,4 +128,90 @@ public function test_delete_data_for_user() {
$count = $DB->count_records('user_private_key', ['script' => 'rss']);
$this->assertEquals(0, $count);
}

/**
* Test that only users with a user context are fetched.
*/
public function test_get_users_in_context() {
$component = 'core_rss';
// Create a user.
$user = $this->getDataGenerator()->create_user();

$usercontext = \context_user::instance($user->id);
$userlist = new \core_privacy\local\request\userlist($usercontext, $component);
// The list of users should not return anything yet (related data still haven't been created).
provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist);
// Create private access key for user.
get_user_key('rss', $user->id);

// The list of users for user context should return the user.
provider::get_users_in_context($userlist);
$this->assertCount(1, $userlist);
$expected = [$user->id];
$actual = $userlist->get_userids();
$this->assertEquals($expected, $actual);

// The list of users for system context should not return any users.
$systemcontext = context_system::instance();
$userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist);
}

/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users() {
$component = 'core_rss';
// Create a user1.
$user1 = $this->getDataGenerator()->create_user();
$usercontext1 = \context_user::instance($user1->id);
// Create list of users with a related user data in usercontext1.
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);

// Create a user1.
$user2 = $this->getDataGenerator()->create_user();
$usercontext2 = \context_user::instance($user2->id);
// Create list of users with a related user data in usercontext2.
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);

// Create private access key for user1.
get_user_key('rss', $user1->id);
// Create private access key for user2.
get_user_key('rss', $user2->id);

// Ensure the user list for usercontext1 contains user1.
provider::get_users_in_context($userlist1);
$this->assertCount(1, $userlist1);
// Ensure the user list for usercontext2 contains user2.
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);

// Convert $userlist1 into an approved_contextlist.
$approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());

// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist);

// Re-fetch users in usercontext1 - The user list should now be empty.
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
// Re-fetch users in usercontext2 - The user list should not be empty (user2).
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);

// User data should be only removed in the user context.
$systemcontext = context_system::instance();
// Add userlist2 to the approved user list in the system context.
$approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
// Delete user1 data using delete_data_for_user.
provider::delete_data_for_users($approvedlist);
// Re-fetch users in usercontext2 - The user list should not be empty (user2).
$userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(1, $userlist1);
}
}

0 comments on commit cee33e0

Please sign in to comment.