From 7feac02414335e1033266f7de1c4d51ffda13b31 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 10 May 2018 11:47:59 +0800 Subject: [PATCH] MDL-61951 core_grading: Several fixes to the privacy implementation - Fixed legacy polyfill method definitions - Added unit test for legacy polyfill - Fixed delete_gradingform_for_userid in gradingform provider --- .../privacy/gradingform_legacy_polyfill.php | 19 +- .../classes/privacy/gradingform_provider.php | 7 +- .../tests/privacy_legacy_polyfill_test.php | 162 ++++++++++++++++++ 3 files changed, 176 insertions(+), 12 deletions(-) create mode 100644 grade/grading/tests/privacy_legacy_polyfill_test.php diff --git a/grade/grading/classes/privacy/gradingform_legacy_polyfill.php b/grade/grading/classes/privacy/gradingform_legacy_polyfill.php index 1fee16cd3e7..86ef9d10536 100644 --- a/grade/grading/classes/privacy/gradingform_legacy_polyfill.php +++ b/grade/grading/classes/privacy/gradingform_legacy_polyfill.php @@ -39,20 +39,20 @@ trait gradingform_legacy_polyfill { /** * This method is used to export any user data this sub-plugin has using the object to get the context and userid. * - * @param \context $context Context owner of the data. - * @param \stdClass $definition Grading definition entry to export. - * @param int $userid The user whose information is to be exported. + * @param context $context Context owner of the data. + * @param stdClass $definition Grading definition entry to export. + * @param int $userid The user whose information is to be exported. * - * @return \stdClass The data to export. + * @return stdClass The data to export. */ - public static function get_gradingform_export_data(\context $context, $definition, $userid) { + public static function get_gradingform_export_data(\context $context, $definition, int $userid) { return static::_get_gradingform_export_data($context, $definition, $userid); } /** * Any call to this method should delete all user data for the context defined. * - * @param \context $context Context owner of the data. + * @param context $context Context owner of the data. */ public static function delete_gradingform_for_context(\context $context) { static::_delete_gradingform_for_context($context); @@ -61,9 +61,10 @@ trait gradingform_legacy_polyfill { /** * A call to this method should delete user data (where practicle) from the userid and context. * - * @param approved_contextlist $contextlist The approved contexts and user information to delete information for. + * @param int $userid The user whose information is to be deleted. + * @param context $context Context owner of the data. */ - public static function delete_gradingform_for_userid(approved_contextlist $contextlist) { - static::delete_gradingform_for_userid($contextlist); + public static function delete_gradingform_for_userid(int $userid, \context $context) { + static::_delete_gradingform_for_userid($userid, $context); } } diff --git a/grade/grading/classes/privacy/gradingform_provider.php b/grade/grading/classes/privacy/gradingform_provider.php index f73059481db..b8239f13841 100644 --- a/grade/grading/classes/privacy/gradingform_provider.php +++ b/grade/grading/classes/privacy/gradingform_provider.php @@ -31,7 +31,7 @@ defined('MOODLE_INTERNAL') || die(); use core_privacy\local\request\approved_contextlist; interface gradingform_provider extends - \core_privacy\local\request\plugin\subplugin_provider { + \core_privacy\local\request\plugin\subsystem_provider { /** * This method is used to export any user data this sub-plugin has using the object to get the context and userid. @@ -54,7 +54,8 @@ interface gradingform_provider extends /** * A call to this method should delete user data (where practicle) from the userid and context. * - * @param approved_contextlist $contextlist The approved contexts and user information to delete information for. + * @param int $userid The user to delete. + * @param \context $context the context to refine the deletion. */ - public static function delete_gradingform_for_userid(approved_contextlist $contextlist); + public static function delete_gradingform_for_userid(int $userid, \context $context); } diff --git a/grade/grading/tests/privacy_legacy_polyfill_test.php b/grade/grading/tests/privacy_legacy_polyfill_test.php new file mode 100644 index 00000000000..c0c2e3b8707 --- /dev/null +++ b/grade/grading/tests/privacy_legacy_polyfill_test.php @@ -0,0 +1,162 @@ +. + +/** + * Unit tests for the privacy legacy polyfill for gradingform. + * + * @package core_grading + * @category test + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Unit tests for the Grading API's privacy legacy_polyfill. + * + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class gradeform_privacy_legacy_polyfill_test extends advanced_testcase { + /** + * Test that the core_grading\privacy\legacy_polyfill works and that the static _get_grading_export_data can be called. + */ + public function test_get_gradingform_export_data() { + $userid = 476; + $context = context_system::instance(); + + $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class); + $mock->expects($this->once()) + ->method('get_return_value') + ->with('_get_gradingform_export_data', [$context, (object)[], $userid]); + + test_legacy_polyfill_gradingform_provider::$mock = $mock; + test_legacy_polyfill_gradingform_provider::get_gradingform_export_data($context, (object)[], $userid); + } + + /** + * Test for _get_metadata shim. + */ + public function test_get_metadata() { + $collection = new \core_privacy\local\metadata\collection('core_gradingform'); + $this->assertSame($collection, test_legacy_polyfill_gradingform_provider::get_metadata($collection)); + } + + /** + * Test the _delete_gradingform_for_context shim. + */ + public function test_delete_gradingform_for_context() { + $context = context_system::instance(); + + $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class); + $mock->expects($this->once()) + ->method('get_return_value') + ->with('_delete_gradingform_for_context', [$context]); + + test_legacy_polyfill_gradingform_provider::$mock = $mock; + test_legacy_polyfill_gradingform_provider::delete_gradingform_for_context($context); + } + + /** + * Test the _delete_gradingform_for_context shim. + */ + public function test_delete_gradingform_for_user() { + $userid = 696; + $context = \context_system::instance(); + + $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class); + $mock->expects($this->once()) + ->method('get_return_value') + ->with('_delete_gradingform_for_userid', [$userid, $context]); + + test_legacy_polyfill_gradingform_provider::$mock = $mock; + test_legacy_polyfill_gradingform_provider::delete_gradingform_for_userid($userid, $context); + } +} + +/** + * Legacy polyfill test class for the gradingform_provider. + * + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class test_legacy_polyfill_gradingform_provider implements + \core_privacy\local\metadata\provider, + \core_grading\privacy\gradingform_provider { + + use \core_grading\privacy\gradingform_legacy_polyfill; + use \core_privacy\local\legacy_polyfill; + + /** + * @var test_legacy_polyfill_gradingform_provider $mock. + */ + public static $mock = null; + + /** + * Export all user data for the gradingform plugin. + * + * @param context $context + * @param stdClass $definition + * @param int $userid + */ + protected static function _get_gradingform_export_data(\context $context, $definition, $userid) { + static::$mock->get_return_value(__FUNCTION__, func_get_args()); + } + + /** + * Deletes all user data for the given context. + * + * @param context $context + */ + protected static function _delete_gradingform_for_context(\context $context) { + static::$mock->get_return_value(__FUNCTION__, func_get_args()); + } + + /** + * Delete personal data for the given user and context. + * + * @param int $userid + * @param context $context + */ + protected static function _delete_gradingform_for_userid($userid, \context $context) { + static::$mock->get_return_value(__FUNCTION__, func_get_args()); + } + + /** + * Returns metadata about this plugin. + * + * @param \core_privacy\local\metadata\collection $collection The initialised collection to add items to. + * @return \core_privacy\local\metadata\collection A listing of user data stored through this system. + */ + protected static function _get_metadata(\core_privacy\local\metadata\collection $collection) { + return $collection; + } +} + +/** + * Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params. + * + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class test_gradingform_legacy_polyfill_mock_wrapper { + /** + * Get the return value for the specified item. + */ + public function get_return_value() { + } +} -- 2.43.0