From 605528e979a62e9814e3f3e9406389485b7cd597 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 26 Mar 2018 11:47:22 +0800 Subject: [PATCH 1/2] MDL-61751 core_plagiarism: add polyfill for plagiarism provider --- .../classes/privacy/legacy_polyfill.php | 68 ++++++++ .../tests/privacy_legacy_polyfill_test.php | 160 ++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 plagiarism/classes/privacy/legacy_polyfill.php create mode 100644 plagiarism/tests/privacy_legacy_polyfill_test.php diff --git a/plagiarism/classes/privacy/legacy_polyfill.php b/plagiarism/classes/privacy/legacy_polyfill.php new file mode 100644 index 00000000000..7d4fda774d8 --- /dev/null +++ b/plagiarism/classes/privacy/legacy_polyfill.php @@ -0,0 +1,68 @@ +. + +/** + * This file contains the polyfill to allow a plugin to operate with Moodle 3.3 up. + * + * @package core_plagiarism + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core_plagiarism\privacy; + +defined('MOODLE_INTERNAL') || die(); + +use \core_privacy\local\metadata\collection; + +/** + * The trait used to provide a backwards compatibility for third-party plugins. + * + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +trait legacy_polyfill { + + /** + * Export all plagiarism data from each plagiarism plugin for the specified userid and context. + * + * @param int $userid The user to export. + * @param \context $context The context to export. + * @param array $subcontext The subcontext within the context to export this information to. + * @param array $linkarray The weird and wonderful link array used to display information for a specific item + */ + public static function export_plagiarism_user_data(int $userid, \context $context, array $subcontext, array $linkarray) { + static::_export_plagiarism_user_data($userid, $context, $subcontext, $linkarray); + } + + /** + * Delete all user information for the provided context. + * + * @param \context $context The context to delete user data for. + */ + public static function delete_plagiarism_for_context(\context $context) { + static::_delete_plagiarism_for_context($context); + } + + /** + * Delete all user information for the provided user and context. + * + * @param int $userid The user to delete + * @param \context $context The context to refine the deletion. + */ + public static function delete_plagiarism_for_user(int $userid, \context $context) { + static::_delete_plagiarism_for_user($userid, $context); + } +} diff --git a/plagiarism/tests/privacy_legacy_polyfill_test.php b/plagiarism/tests/privacy_legacy_polyfill_test.php new file mode 100644 index 00000000000..c016f6d70ba --- /dev/null +++ b/plagiarism/tests/privacy_legacy_polyfill_test.php @@ -0,0 +1,160 @@ +. + +/** + * Unit tests for the privacy legacy polyfill for plagiarism. + * + * @package core_privacy + * @category test + * @copyright 2018 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Unit tests for the Plagiarism API's privacy legacy_polyfill. + * + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_plagiarism_privacy_legacy_polyfill_test extends advanced_testcase { + /** + * Test that the core_plagiarism\privacy\legacy_polyfill works and that the static _export_plagiarism_user_data can be called. + */ + public function test_export_plagiarism_user_data() { + $userid = 476; + $context = context_system::instance(); + + $mock = $this->createMock(test_plagiarism_legacy_polyfill_mock_wrapper::class); + $mock->expects($this->once()) + ->method('get_return_value') + ->with('_export_plagiarism_user_data', [$userid, $context, [], []]); + + test_legacy_polyfill_plagiarism_provider::$mock = $mock; + test_legacy_polyfill_plagiarism_provider::export_plagiarism_user_data($userid, $context, [], []); + } + + /** + * Test for _get_metadata shim. + */ + public function test_get_metadata() { + $collection = new \core_privacy\local\metadata\collection('core_plagiarism'); + $this->assertSame($collection, test_legacy_polyfill_plagiarism_provider::get_metadata($collection)); + } + + /** + * Test the _delete_plagiarism_for_context shim. + */ + public function test_delete_plagiarism_for_context() { + $context = context_system::instance(); + + $mock = $this->createMock(test_plagiarism_legacy_polyfill_mock_wrapper::class); + $mock->expects($this->once()) + ->method('get_return_value') + ->with('_delete_plagiarism_for_context', [$context]); + + test_legacy_polyfill_plagiarism_provider::$mock = $mock; + test_legacy_polyfill_plagiarism_provider::delete_plagiarism_for_context($context); + } + + /** + * Test the _delete_plagiarism_for_context shim. + */ + public function test_delete_plagiarism_for_user() { + $userid = 696; + $context = \context_system::instance(); + + $mock = $this->createMock(test_plagiarism_legacy_polyfill_mock_wrapper::class); + $mock->expects($this->once()) + ->method('get_return_value') + ->with('_delete_plagiarism_for_user', [$userid, $context]); + + test_legacy_polyfill_plagiarism_provider::$mock = $mock; + test_legacy_polyfill_plagiarism_provider::delete_plagiarism_for_user($userid, $context); + } +} + +/** + * Legacy polyfill test class for the plagiarism_provider. + * + * @copyright 2018 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class test_legacy_polyfill_plagiarism_provider implements \core_plagiarism\privacy\plagiarism_provider { + + use \core_plagiarism\privacy\legacy_polyfill; + + /** + * @var test_legacy_polyfill_plagiarism_provider $mock. + */ + public static $mock = null; + + /** + * Export all user data for the plagiarism plugin. + * + * @param int $userid + * @param context $context + * @param array $subcontext + * @param array $linkarray + */ + protected static function _export_plagiarism_user_data($userid, \context $context, array $subcontext, array $linkarray) { + static::$mock->get_return_value(__FUNCTION__, func_get_args()); + } + + /** + * Deletes all user data for the given context. + * + * @param context $context + */ + protected static function _delete_plagiarism_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_plagiarism_for_user($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_plagiarism_legacy_polyfill_mock_wrapper { + /** + * Get the return value for the specified item. + */ + public function get_return_value() { + } +} From 6fb1fd31837d6109e4b0b9417ea72254920f8d6b Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sun, 1 Apr 2018 13:34:39 +0800 Subject: [PATCH 2/2] MDL-61751 core_privacy: Correct test return --- privacy/tests/legacy_polyfill_test.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/privacy/tests/legacy_polyfill_test.php b/privacy/tests/legacy_polyfill_test.php index 1aeb89867e6..17d0f6feae0 100644 --- a/privacy/tests/legacy_polyfill_test.php +++ b/privacy/tests/legacy_polyfill_test.php @@ -66,8 +66,7 @@ class core_privacy_legacy_polyfill_test extends advanced_testcase { $mock = $this->createMock(test_legacy_polyfill_mock_wrapper::class); $mock->expects($this->once()) ->method('get_return_value') - ->with('_export_user_preferences', [$userid]) - ->willReturn(null); + ->with('_export_user_preferences', [$userid]); test_legacy_polyfill_user_preference_provider::$mock = $mock; test_legacy_polyfill_user_preference_provider::export_user_preferences($userid);