MDL-69586 tool_usertours: use specified user in privacy prefs export.

This commit is contained in:
Paul Holden 2020-08-27 13:02:10 +01:00
parent 5486b031ee
commit 6a2720b0e6
2 changed files with 42 additions and 8 deletions

View File

@ -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;

View File

@ -15,9 +15,9 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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 <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@ -27,19 +27,22 @@ 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 <andrew@nicols.co.uk>
* @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 {
/**
* Helper method for creating a tour
*
* @return tour
*/
protected function create_test_tour(): tour {
return (new tour())
->set_name('test_tour')
@ -118,6 +121,37 @@ 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 {
$this->resetAfterTest();
$tour = $this->create_test_tour();
// 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.
*/