diff --git a/privacy/classes/manager.php b/privacy/classes/manager.php index a41f4cb5faf..fac8c85abca 100644 --- a/privacy/classes/manager.php +++ b/privacy/classes/manager.php @@ -26,7 +26,6 @@ use core_privacy\local\metadata\collection; use core_privacy\local\metadata\null_provider; use core_privacy\local\request\context_aware_provider; use core_privacy\local\request\contextlist_collection; -use core_privacy\local\request\core_data_provider; use core_privacy\local\request\core_user_data_provider; use core_privacy\local\request\data_provider; use core_privacy\local\request\user_preference_provider; @@ -110,6 +109,21 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class manager { + + /** + * @var manager_observer Observer. + */ + protected $observer; + + /** + * Set the failure handler. + * + * @param manager_observer $observer + */ + public function set_observer(manager_observer $observer) { + $this->observer = $observer; + } + /** * Checks whether the given component is compliant with the core_privacy API. * To be considered compliant, a component must declare whether (and where) it stores personal data. @@ -152,7 +166,8 @@ class manager { */ public function get_null_provider_reason($component) { if ($this->component_implements($component, null_provider::class)) { - return static::component_class_callback($component, null_provider::class, 'get_reason', []); + $reason = $this->handled_component_class_callback($component, null_provider::class, 'get_reason', []); + return empty($reason) ? 'privacy:reason' : $reason; } else { throw new \coding_exception('Call to undefined method', 'Please only call this method on a null provider.'); } @@ -184,7 +199,7 @@ class manager { // Get the metadata, and put into an assoc array indexed by component name. $metadata = []; foreach ($this->get_component_list() as $component) { - $componentmetadata = static::component_class_callback($component, metadata_provider::class, + $componentmetadata = $this->handled_component_class_callback($component, metadata_provider::class, 'get_metadata', [new collection($component)]); if ($componentmetadata !== null) { $metadata[$component] = $componentmetadata; @@ -196,6 +211,7 @@ class manager { /** * Gets a collection of resultset objects for all components. * + * * @param int $userid the id of the user we're fetching contexts for. * @return contextlist_collection the collection of contextlist items for the respective components. */ @@ -217,7 +233,7 @@ class manager { $a->progress++; $a->datetime = userdate(time()); $progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2); - $contextlist = static::component_class_callback($component, core_user_data_provider::class, + $contextlist = $this->handled_component_class_callback($component, core_user_data_provider::class, 'get_contexts_for_userid', [$userid]); if ($contextlist === null) { $contextlist = new local\request\contextlist(); @@ -278,7 +294,7 @@ class manager { if (count($approvedcontextlist)) { // This plugin has data it knows about. It is responsible for storing basic data about anything it is // told to export. - static::component_class_callback($component, core_user_data_provider::class, + $this->handled_component_class_callback($component, core_user_data_provider::class, 'export_user_data', [$approvedcontextlist]); } } else if (!$this->component_implements($component, context_aware_provider::class)) { @@ -300,12 +316,12 @@ class manager { $a->datetime = userdate(time()); $progress->output(get_string('trace:processingcomponent', 'core_privacy', $a), 2); // Core user preference providers. - static::component_class_callback($component, user_preference_provider::class, + $this->handled_component_class_callback($component, user_preference_provider::class, 'export_user_preferences', [$contextlistcollection->get_userid()]); // Contextual information providers. Give each component a chance to include context information based on the // existence of a child context in the contextlist_collection. - static::component_class_callback($component, context_aware_provider::class, + $this->handled_component_class_callback($component, context_aware_provider::class, 'export_context_data', [$contextlistcollection]); } $progress->output(get_string('trace:done', 'core_privacy'), 1); @@ -354,7 +370,7 @@ class manager { if (count($approvedcontextlist)) { // The component knows about data that it has. // Have it delete its own data. - static::component_class_callback($approvedcontextlist->get_component(), core_user_data_provider::class, + $this->handled_component_class_callback($approvedcontextlist->get_component(), core_user_data_provider::class, 'delete_data_for_user', [$approvedcontextlist]); } @@ -389,7 +405,7 @@ class manager { // If this component knows about specific data that it owns, // have it delete all of that user data for the context. - static::component_class_callback($component, core_user_data_provider::class, + $this->handled_component_class_callback($component, core_user_data_provider::class, 'delete_data_for_all_users_in_context', [$context]); // Delete any shared user data it doesn't know about. @@ -496,4 +512,45 @@ class manager { return new \text_progress_trace(); } + + /** + * Call the named method with the specified params on the supplied component if it implements the relevant interface + * on its provider. + * + * @param string $component The component to call + * @param string $interface The interface to implement + * @param string $methodname The method to call + * @param array $params The params to call + * @return mixed + */ + protected function handled_component_class_callback($component, $interface, $methodname, array $params) { + try { + return static::component_class_callback($component, $interface, $methodname, $params); + } catch (\Exception $e) { + debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace()); + $this->component_class_callback_failed($e, $component, $interface, $methodname, $params); + + return null; + } catch (\Error $e) { + debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace()); + $this->component_class_callback_failed($e, $component, $interface, $methodname, $params); + + return null; + } + } + + /** + * Notifies the observer of any failure. + * + * @param \Throwable $e + * @param string $component + * @param string $interface + * @param string $methodname + * @param array $params + */ + protected function component_class_callback_failed($e, $component, $interface, $methodname, array $params) { + if ($this->observer) { + call_user_func_array([$this->observer, 'handle_component_failure'], func_get_args()); + } + } } diff --git a/privacy/classes/manager_observer.php b/privacy/classes/manager_observer.php new file mode 100644 index 00000000000..b1d78477612 --- /dev/null +++ b/privacy/classes/manager_observer.php @@ -0,0 +1,47 @@ +. + +/** + * This file contains the interface required to observe failures in the manager. + * + * @package core_privacy + * @copyright 2018 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core_privacy; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The interface for a Manager observer. + * + * @package core_privacy + * @copyright 2018 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +interface manager_observer { + + /** + * Handle failure of a component. + * + * @param \Throwable $e + * @param string $component + * @param string $interface + * @param string $methodname + * @param array $params + */ + public function handle_component_failure($e, $component, $interface, $methodname, array $params); +} diff --git a/privacy/tests/fixtures/provider_a.php b/privacy/tests/fixtures/provider_a.php new file mode 100644 index 00000000000..56c9b42b8d7 --- /dev/null +++ b/privacy/tests/fixtures/provider_a.php @@ -0,0 +1,91 @@ +. + +/** + * Test provider which works. + * + * @package core_privacy + * @copyright 2018 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_component_a\privacy; + +defined('MOODLE_INTERNAL') || die(); + +use core_privacy\local\metadata\collection; +use core_privacy\local\request\contextlist; +use core_privacy\local\request\approved_contextlist; + +/** + * Mock core_user_data_provider for unit tests. + * + * @package core_privacy + * @copyright 2018 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class provider implements + \core_privacy\local\metadata\provider, + \core_privacy\local\request\plugin\provider { + + /** + * Get metadata. + * + * @param collection $collection The initialised collection to add items to. + * @return collection A listing of user data stored through this system. + */ + public static function get_metadata(collection $collection) { + $collection->add_subsystem_link('core_files'); + return $collection; + } + + /** + * Get the list of contexts that contain user information for the specified user. + * + * @param int $userid The user to search. + * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin. + */ + public static function get_contexts_for_userid($userid) { + $c = new \core_privacy\local\request\contextlist(); + $c->add_system_context(); + + return $c; + } + + /** + * Export all user data for the specified user, in the specified contexts. + * + * @param approved_contextlist $contextlist The approved contexts to export information for. + */ + public static function export_user_data(approved_contextlist $contextlist) { + } + + /** + * Delete all data for all users in the specified context. + * + * @param context $context The specific context to delete data for. + */ + public static function delete_data_for_all_users_in_context(\context $context) { + } + + /** + * Delete all user data for the specified user, in the specified contexts. + * + * @param approved_contextlist $contextlist The approved contexts and user information to delete information for. + */ + public static function delete_data_for_user(approved_contextlist $contextlist) { + } +} diff --git a/privacy/tests/fixtures/provider_throwing_exception.php b/privacy/tests/fixtures/provider_throwing_exception.php new file mode 100644 index 00000000000..7fd1d3ee1e1 --- /dev/null +++ b/privacy/tests/fixtures/provider_throwing_exception.php @@ -0,0 +1,88 @@ +. + +/** + * Test provider which has issues. + * + * @package core_privacy + * @copyright 2018 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_component_broken\privacy; + +use core_privacy\local\metadata\collection; +use core_privacy\local\request\contextlist; +use core_privacy\local\request\approved_contextlist; + +/** + * Mock core_user_data_provider for unit tests. + * + * @package core_privacy + * @copyright 2018 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class provider implements + \core_privacy\local\metadata\provider, + \core_privacy\local\request\plugin\provider { + + /** + * Get metadata. + * + * @param collection $collection The initialised collection to add items to. + * @return collection A listing of user data stored through this system. + */ + public static function get_metadata(collection $collection) { + throw new \coding_exception(__FUNCTION__); + } + + /** + * Get the list of contexts that contain user information for the specified user. + * + * @param int $userid The user to search. + * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin. + */ + public static function get_contexts_for_userid($userid) { + throw new \coding_exception(__FUNCTION__); + } + + /** + * Export all user data for the specified user, in the specified contexts. + * + * @param approved_contextlist $contextlist The approved contexts to export information for. + */ + public static function export_user_data(approved_contextlist $contextlist) { + throw new \coding_exception(__FUNCTION__); + } + + /** + * Delete all data for all users in the specified context. + * + * @param context $context The specific context to delete data for. + */ + public static function delete_data_for_all_users_in_context(\context $context) { + throw new \coding_exception(__FUNCTION__); + } + + /** + * Delete all user data for the specified user, in the specified contexts. + * + * @param approved_contextlist $contextlist The approved contexts and user information to delete information for. + */ + public static function delete_data_for_user(approved_contextlist $contextlist) { + throw new \coding_exception(__FUNCTION__); + } +} diff --git a/privacy/tests/manager_test.php b/privacy/tests/manager_test.php index b16e1c7aa9c..3f1f9acd55b 100644 --- a/privacy/tests/manager_test.php +++ b/privacy/tests/manager_test.php @@ -28,8 +28,11 @@ require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_null_provider.php'); require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_provider.php'); require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_plugin_subplugin_provider.php'); require_once($CFG->dirroot . '/privacy/tests/fixtures/mock_mod_with_user_data_provider.php'); +require_once($CFG->dirroot . '/privacy/tests/fixtures/provider_a.php'); +require_once($CFG->dirroot . '/privacy/tests/fixtures/provider_throwing_exception.php'); use \core_privacy\local\request\writer; +use \core_privacy\local\request\approved_contextlist; /** * Privacy manager unit tests. @@ -175,7 +178,7 @@ class privacy_manager_testcase extends advanced_testcase { // Create an approved contextlist. $approvedcontextlistcollection = new \core_privacy\local\request\contextlist_collection(10); foreach ($contextlistcollection->get_contextlists() as $contextlist) { - $approvedcontextlist = new \core_privacy\local\request\approved_contextlist(new stdClass(), $contextlist->get_component(), + $approvedcontextlist = new approved_contextlist(new stdClass(), $contextlist->get_component(), $contextlist->get_contextids()); $approvedcontextlistcollection->add_contextlist($approvedcontextlist); } @@ -210,7 +213,7 @@ class privacy_manager_testcase extends advanced_testcase { // Create an approved contextlist. $approvedcontextlistcollection = new \core_privacy\local\request\contextlist_collection($user->id); foreach ($contextlistcollection->get_contextlists() as $contextlist) { - $approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user, $contextlist->get_component(), + $approvedcontextlist = new approved_contextlist($user, $contextlist->get_component(), $contextlist->get_contextids()); $approvedcontextlistcollection->add_contextlist($approvedcontextlist); } @@ -300,4 +303,164 @@ class privacy_manager_testcase extends advanced_testcase { ], ]; } + + /** + * Test that get_contexts_for_userid() with a failing item. + */ + public function test_get_contexts_for_userid_with_failing() { + // Get a mock manager, in which the core components list is mocked to include all mock plugins. + // testcomponent is a core provider, testcomponent2 isa null provider, testcomponent3 is subplugin provider (non core). + $mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']); + + $observer = $this->getMockBuilder(\core_privacy\manager_observer::class) + ->setMethods(['handle_component_failure']) + ->getMock(); + $mockman->set_observer($observer); + + $observer->expects($this->once()) + ->method('handle_component_failure') + ->with( + $this->isInstanceOf(\coding_exception::class), + $this->identicalTo('mod_component_broken'), + $this->identicalTo(\core_privacy\local\request\core_user_data_provider::class), + $this->identicalTo('get_contexts_for_userid'), + $this->anything() + ); + + // Get the contextlist_collection. + $contextlistcollection = $mockman->get_contexts_for_userid(10); + $this->assertDebuggingCalled(); + $this->assertInstanceOf(\core_privacy\local\request\contextlist_collection::class, $contextlistcollection); + $this->assertCount(1, $contextlistcollection); + + // The component which completed shoudl have returned a contextlist. + $this->assertInstanceOf(\core_privacy\local\request\contextlist::class, + $contextlistcollection->get_contextlist_for_component('mod_component_a')); + $this->assertEmpty($contextlistcollection->get_contextlist_for_component('mod_component_broken')); + } + + /** + * Test that export_user_data() with a failing item. + */ + public function test_export_user_data_with_failing() { + $user = \core_user::get_user_by_username('admin'); + $mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']); + $context = \context_system::instance(); + $contextid = $context->id; + + $observer = $this->getMockBuilder(\core_privacy\manager_observer::class) + ->setMethods(['handle_component_failure']) + ->getMock(); + $mockman->set_observer($observer); + + $observer->expects($this->once()) + ->method('handle_component_failure') + ->with( + $this->isInstanceOf(\coding_exception::class), + $this->identicalTo('mod_component_broken'), + $this->identicalTo(\core_privacy\local\request\core_user_data_provider::class), + $this->identicalTo('export_user_data'), + $this->anything() + ); + + $collection = new \core_privacy\local\request\contextlist_collection(10); + $collection->add_contextlist(new approved_contextlist($user, 'mod_component_broken', [$contextid])); + $collection->add_contextlist(new approved_contextlist($user, 'mod_component_a', [$contextid])); + + // Get the contextlist_collection. + $mockman->export_user_data($collection); + $this->assertDebuggingCalled(); + } + + /** + * Test that delete_data_for_user() with a failing item. + */ + public function test_delete_data_for_user_with_failing() { + $user = \core_user::get_user_by_username('admin'); + $mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']); + $context = \context_system::instance(); + $contextid = $context->id; + + $observer = $this->getMockBuilder(\core_privacy\manager_observer::class) + ->setMethods(['handle_component_failure']) + ->getMock(); + $mockman->set_observer($observer); + + $observer->expects($this->once()) + ->method('handle_component_failure') + ->with( + $this->isInstanceOf(\coding_exception::class), + $this->identicalTo('mod_component_broken'), + $this->identicalTo(\core_privacy\local\request\core_user_data_provider::class), + $this->identicalTo('delete_data_for_user'), + $this->anything() + ); + + $collection = new \core_privacy\local\request\contextlist_collection(10); + $collection->add_contextlist(new approved_contextlist($user, 'mod_component_broken', [$contextid])); + $collection->add_contextlist(new approved_contextlist($user, 'mod_component_a', [$contextid])); + + // Get the contextlist_collection. + $mockman->delete_data_for_user($collection); + $this->assertDebuggingCalled(); + } + + /** + * Test that delete_data_for_all_users_in_context() with a failing item. + */ + public function test_delete_data_for_all_users_in_context_with_failing() { + $user = \core_user::get_user_by_username('admin'); + $mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']); + $context = \context_system::instance(); + + $observer = $this->getMockBuilder(\core_privacy\manager_observer::class) + ->setMethods(['handle_component_failure']) + ->getMock(); + $mockman->set_observer($observer); + + $observer->expects($this->once()) + ->method('handle_component_failure') + ->with( + $this->isInstanceOf(\coding_exception::class), + $this->identicalTo('mod_component_broken'), + $this->identicalTo(\core_privacy\local\request\core_user_data_provider::class), + $this->identicalTo('delete_data_for_all_users_in_context'), + $this->anything() + ); + + // Get the contextlist_collection. + $mockman->delete_data_for_all_users_in_context($context); + $this->assertDebuggingCalled(); + } + + /** + * Test that get_metadata_for_components() with a failing item. + */ + public function test_get_metadata_for_components_with_failing() { + $user = \core_user::get_user_by_username('admin'); + $mockman = $this->get_mock_manager_with_core_components(['mod_component_broken', 'mod_component_a']); + $context = \context_system::instance(); + + $observer = $this->getMockBuilder(\core_privacy\manager_observer::class) + ->setMethods(['handle_component_failure']) + ->getMock(); + $mockman->set_observer($observer); + + $observer->expects($this->once()) + ->method('handle_component_failure') + ->with( + $this->isInstanceOf(\coding_exception::class), + $this->identicalTo('mod_component_broken'), + $this->identicalTo(\core_privacy\local\metadata\provider::class), + $this->identicalTo('get_metadata'), + $this->anything() + ); + + // Get the contextlist_collection. + $metadata = $mockman->get_metadata_for_components(); + $this->assertDebuggingCalled(); + + $this->assertInternalType('array', $metadata); + $this->assertCount(1, $metadata); + } } diff --git a/version.php b/version.php index 1debb61f298..48426fa27c1 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2017051505.13; // 20170515 = branching date YYYYMMDD - do not modify! +$version = 2017051505.14; // 20170515 = branching date YYYYMMDD - do not modify! // RR = release increments - 00 in DEV branches. // .XX = incremental changes.