MDL-62564 privacy: Create request for deleted users when setting enabled
authorMihail Geshoski <mihail@moodle.com>
Tue, 6 Nov 2018 05:11:34 +0000 (13:11 +0800)
committerMihail Geshoski <mihail@moodle.com>
Thu, 8 Nov 2018 01:29:38 +0000 (09:29 +0800)
admin/tool/dataprivacy/classes/task/delete_existing_deleted_users.php
admin/tool/dataprivacy/tests/task_test.php

index a078747..83827ef 100644 (file)
@@ -57,8 +57,10 @@ class delete_existing_deleted_users extends scheduled_task {
     public function execute() {
         global $DB;
 
-        // Select all deleted users that do not have any delete data requests created for them.
-        $sql = "SELECT DISTINCT(u.id)
+        // Automatic creation of deletion requests must be enabled.
+        if (get_config('tool_dataprivacy', 'automaticdeletionrequests')) {
+            // Select all deleted users that do not have any delete data requests created for them.
+            $sql = "SELECT DISTINCT(u.id)
                   FROM {user} u
              LEFT JOIN {tool_dataprivacy_request} r
                        ON u.id = r.userid
@@ -66,23 +68,24 @@ class delete_existing_deleted_users extends scheduled_task {
                        AND (r.id IS NULL
                            OR r.type != ?)";
 
-        $params = [
-            1,
-            api::DATAREQUEST_TYPE_DELETE
-        ];
+            $params = [
+                1,
+                api::DATAREQUEST_TYPE_DELETE
+            ];
 
-        $deletedusers = $DB->get_records_sql($sql, $params);
-        $createdrequests = 0;
+            $deletedusers = $DB->get_records_sql($sql, $params);
+            $createdrequests = 0;
 
-        foreach ($deletedusers as $user) {
-            api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE,
+            foreach ($deletedusers as $user) {
+                api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE,
                     get_string('datarequestcreatedfromscheduledtask', 'tool_dataprivacy'),
                     data_request::DATAREQUEST_CREATION_AUTO);
-            $createdrequests++;
-        }
+                $createdrequests++;
+            }
 
-        if ($createdrequests > 0) {
-            mtrace($createdrequests . ' delete data request(s) created for existing deleted users');
+            if ($createdrequests > 0) {
+                mtrace($createdrequests . ' delete data request(s) created for existing deleted users');
+            }
         }
     }
 }
index e6be1b8..1006364 100644 (file)
@@ -53,6 +53,10 @@ class tool_dataprivacy_task_testcase extends data_privacy_testcase {
 
         $this->resetAfterTest();
         $this->setAdminUser();
+
+        // Enable automatic creation of delete data requests.
+        set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
+
         // Create a user.
         $user = $this->getDataGenerator()->create_user();
         // Mark the user as deleted.
@@ -69,6 +73,35 @@ class tool_dataprivacy_task_testcase extends data_privacy_testcase {
                 [api::DATAREQUEST_TYPE_DELETE]));
     }
 
+    /**
+     * Ensure that a delete data request for pre-existing deleted users
+     * is not being created when automatic creation of delete data requests is disabled.
+     */
+    public function test_delete_existing_deleted_users_task_automatic_creation_disabled() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Disable automatic creation of delete data requests.
+        set_config('automaticdeletionrequests', 0, 'tool_dataprivacy');
+
+        // Create a user.
+        $user = $this->getDataGenerator()->create_user();
+        // Mark the user as deleted.
+        $user->deleted = 1;
+        $DB->update_record('user', $user);
+
+        // The user should not have a delete data request.
+        $this->assertCount(0, api::get_data_requests($user->id, [],
+            [api::DATAREQUEST_TYPE_DELETE]));
+
+        $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users');
+        // After running the scheduled task, the deleted user should still not have a delete data request.
+        $this->assertCount(0, api::get_data_requests($user->id, [],
+            [api::DATAREQUEST_TYPE_DELETE]));
+    }
+
     /**
      * Ensure that a delete data request for pre-existing deleted users
      * is created when there are existing non-delete data requests
@@ -79,6 +112,10 @@ class tool_dataprivacy_task_testcase extends data_privacy_testcase {
 
         $this->resetAfterTest();
         $this->setAdminUser();
+
+        // Enable automatic creation of delete data requests.
+        set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
+
         // Create a user.
         $user = $this->getDataGenerator()->create_user();
         // Create export data request for the user.
@@ -106,8 +143,14 @@ class tool_dataprivacy_task_testcase extends data_privacy_testcase {
      * for that particular user.
      */
     public function test_delete_existing_deleted_users_task_existing_ongoing_delete_data_requests() {
+        global $DB;
+
         $this->resetAfterTest();
         $this->setAdminUser();
+
+        // Enable automatic creation of delete data requests.
+        set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
+
         // Create a user.
         $user = $this->getDataGenerator()->create_user();
         $this->setUser($user);
@@ -120,9 +163,10 @@ class tool_dataprivacy_task_testcase extends data_privacy_testcase {
         $this->assertCount(1, api::get_data_requests($user->id,
                 [api::DATAREQUEST_STATUS_AWAITING_APPROVAL], [api::DATAREQUEST_TYPE_DELETE]));
 
-        $this->setAdminUser();
-        // Delete the user.
-        delete_user($user);
+        // Mark the user as deleted.
+        $user->deleted = 1;
+        $DB->update_record('user', $user);
+
         // The user should still have the existing ongoing delete data request.
         $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id,
                 [api::DATAREQUEST_STATUS_AWAITING_APPROVAL], [api::DATAREQUEST_TYPE_DELETE]));
@@ -142,8 +186,14 @@ class tool_dataprivacy_task_testcase extends data_privacy_testcase {
      * for that particular user.
      */
     public function test_delete_existing_deleted_users_task_existing_finished_delete_data_requests() {
+        global $DB;
+
         $this->resetAfterTest();
         $this->setAdminUser();
+
+        // Enable automatic creation of delete data requests.
+        set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
+
         // Create a user.
         $user = $this->getDataGenerator()->create_user();
         $this->setUser($user);
@@ -158,9 +208,10 @@ class tool_dataprivacy_task_testcase extends data_privacy_testcase {
         // The user should not have an ongoing data requests.
         $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
 
-        $this->setAdminUser();
-        // Delete the user.
-        delete_user($user);
+        // Mark the user as deleted.
+        $user->deleted = 1;
+        $DB->update_record('user', $user);
+
         // The user should still have the existing finished delete data request.
         $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id,
                 [api::DATAREQUEST_STATUS_CANCELLED], [api::DATAREQUEST_TYPE_DELETE]));