MDL-62601 core_privacy: Ensure providers can handle deleted users
authorAndrew Nicols <andrew@nicols.co.uk>
Thu, 25 Oct 2018 03:04:08 +0000 (11:04 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 31 Oct 2018 05:45:00 +0000 (13:45 +0800)
privacy/tests/provider_test.php

index 0ca9768..f59b042 100644 (file)
@@ -178,6 +178,29 @@ class provider_testcase extends advanced_testcase {
         $this->assertTrue($manager->component_is_compliant($component));
     }
 
+    /**
+     * Ensure that providers do not throw an error when processing a deleted user.
+     *
+     * @dataProvider    is_user_data_provider
+     * @param   string  $component
+     */
+    public function test_component_understands_deleted_users($component) {
+        $this->resetAfterTest();
+
+        // Create a user.
+        $user = $this->getDataGenerator()->create_user();
+
+        // Delete the user and their context.
+        delete_user($user);
+        $usercontext = \context_user::instance($user->id);
+        $usercontext->delete();
+
+        $contextlist = manager::component_class_callback($component, \core_privacy\local\request\core_user_data_provider::class,
+                'get_contexts_for_userid', [$user->id]);
+
+        $this->assertInstanceOf(\core_privacy\local\request\contextlist::class, $contextlist);
+    }
+
     /**
      * Data provider for the metadata\provider tests.
      *
@@ -192,6 +215,20 @@ class provider_testcase extends advanced_testcase {
         });
     }
 
+    /**
+     * List of providers which implement the core_user_data_provider.
+     *
+     * @return array
+     */
+    public function is_user_data_provider() {
+        return array_filter($this->get_component_list(), function($component) {
+                return static::component_implements(
+                    $component['classname'],
+                    \core_privacy\local\request\core_user_data_provider::class
+                );
+        });
+    }
+
     /**
      * Checks whether the component's provider class implements the specified interface, either directly or as a grandchild.
      *