From 620e5285fec109618eddbaba4c126f4bdc8d51ee Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Thu, 27 Aug 2020 13:02:10 +0100 Subject: [PATCH] MDL-69586 tool_usertours: use specified user in privacy prefs export. --- .../usertours/classes/privacy/provider.php | 4 +- .../usertours/tests/privacy_provider_test.php | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/admin/tool/usertours/classes/privacy/provider.php b/admin/tool/usertours/classes/privacy/provider.php index 3713d071236..21b70deaf3b 100644 --- a/admin/tool/usertours/classes/privacy/provider.php +++ b/admin/tool/usertours/classes/privacy/provider.php @@ -47,7 +47,7 @@ class provider implements /** * Returns meta data about this system. * - * @param collection $itemcollection The initialised item collection to add items to. + * @param collection $items The initialised item collection to add items to. * @return collection A listing of user data stored through this system. */ public static function get_metadata(collection $items) : collection { @@ -64,7 +64,7 @@ class provider implements * @param int $userid The userid of the user whose data is to be exported. */ public static function export_user_preferences(int $userid) { - $preferences = get_user_preferences(); + $preferences = get_user_preferences(null, null, $userid); foreach ($preferences as $name => $value) { $descriptionidentifier = null; $tourid = null; diff --git a/admin/tool/usertours/tests/privacy_provider_test.php b/admin/tool/usertours/tests/privacy_provider_test.php index db0139d4b29..63f51a2451a 100644 --- a/admin/tool/usertours/tests/privacy_provider_test.php +++ b/admin/tool/usertours/tests/privacy_provider_test.php @@ -15,9 +15,9 @@ // along with Moodle. If not, see . /** - * Unit tests for the block_html implementation of the privacy API. + * Unit tests for the tool_usertours implementation of the privacy API. * - * @package block_html + * @package tool_usertours * @category test * @copyright 2018 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -27,18 +27,16 @@ defined('MOODLE_INTERNAL') || die(); use \core_privacy\local\metadata\collection; use \core_privacy\local\request\writer; -use \core_privacy\local\request\approved_contextlist; -use \core_privacy\local\request\deletion_criteria; use \tool_usertours\tour; use \tool_usertours\privacy\provider; /** - * Unit tests for the block_html implementation of the privacy API. + * Unit tests for the tool_usertours implementation of the privacy API. * * @copyright 2018 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class tool_usertours_privacy_testcase extends \core_privacy\tests\provider_testcase { +class tool_usertours_privacy_provider_testcase extends \core_privacy\tests\provider_testcase { /** * Ensure that get_metadata exports valid content. @@ -113,6 +111,40 @@ class tool_usertours_privacy_testcase extends \core_privacy\tests\provider_testc $this->assertCount(2, (array) $prefs); } + /** + * Make sure we are exporting preferences for the correct user + */ + public function test_export_user_preferences_correct_user(): void { + global $DB; + + $this->resetAfterTest(); + + $alltours = $DB->get_records('tool_usertours_tours'); + $tour = tour::instance(reset($alltours)->id); + + // Create test user, mark them as having completed the tour. + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + $tour->mark_user_completed(); + + // Switch to admin user, mark them as having reset the tour. + $this->setAdminUser(); + $tour->request_user_reset(); + + // Export test users preferences. + provider::export_user_preferences($user->id); + + $writer = writer::with_context(\context_system::instance()); + $this->assertTrue($writer->has_any_data()); + + $prefs = $writer->get_user_preferences('tool_usertours'); + $this->assertCount(1, (array) $prefs); + + // We should have received back the "completed tour" preference of the test user. + $this->assertStringStartsWith('You last marked the "' . $tour->get_name() . '" user tour as completed on', + reset($prefs)->description); + } + /** * Ensure that export_user_preferences excludes deleted tours. */