Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace tool_dataprivacy;
  18  
  19  use tool_dataprivacy\event\user_deleted_observer;
  20  
  21  /**
  22   * Event observer test.
  23   *
  24   * @package    tool_dataprivacy
  25   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class user_deleted_observer_test extends \advanced_testcase {
  29  
  30      /**
  31       * Ensure that a delete data request is created upon user deletion.
  32       */
  33      public function test_create_delete_data_request() {
  34          $this->resetAfterTest();
  35  
  36          // Enable automatic creation of delete data requests.
  37          set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
  38  
  39          // Create another user who is not a DPO.
  40          $user = $this->getDataGenerator()->create_user();
  41  
  42          $event = $this->trigger_delete_user_event($user);
  43  
  44          user_deleted_observer::create_delete_data_request($event);
  45          // Validate that delete data request has been created.
  46          $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
  47      }
  48  
  49      /**
  50       * Ensure that a delete data request is not created upon user deletion if automatic creation of
  51       * delete data requests is disabled.
  52       */
  53      public function test_create_delete_data_request_automatic_creation_disabled() {
  54          $this->resetAfterTest();
  55  
  56          // Disable automatic creation of delete data requests.
  57          set_config('automaticdeletionrequests', 0, 'tool_dataprivacy');
  58  
  59          // Create another user who is not a DPO.
  60          $user = $this->getDataGenerator()->create_user();
  61  
  62          $event = $this->trigger_delete_user_event($user);
  63  
  64          user_deleted_observer::create_delete_data_request($event);
  65          // Validate that delete data request has been created.
  66          $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
  67      }
  68  
  69      /**
  70       * Ensure that a delete data request is being created upon user deletion
  71       * if an ongoing export data request (or any other except delete data request) for that user already exists.
  72       */
  73      public function test_create_delete_data_request_export_data_request_preexists() {
  74          $this->resetAfterTest();
  75          $this->setAdminUser();
  76  
  77          // Enable automatic creation of delete data requests.
  78          set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
  79  
  80          // Create another user who is not a DPO.
  81          $user = $this->getDataGenerator()->create_user();
  82          // Create a delete data request for $user.
  83          api::create_data_request($user->id, api::DATAREQUEST_TYPE_EXPORT);
  84          // Validate that delete data request has been created.
  85          $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_EXPORT));
  86          $this->assertEquals(0, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE]));
  87  
  88          $event = $this->trigger_delete_user_event($user);
  89  
  90          user_deleted_observer::create_delete_data_request($event);
  91          // Validate that delete data request has been created.
  92          $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE]));
  93      }
  94  
  95      /**
  96       * Ensure that a delete data request is not being created upon user deletion
  97       * if an ongoing delete data request for that user already exists.
  98       */
  99      public function test_create_delete_data_request_ongoing_delete_data_request_preexists() {
 100          $this->resetAfterTest();
 101          $this->setAdminUser();
 102  
 103          // Enable automatic creation of delete data requests.
 104          set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
 105  
 106          // Create another user who is not a DPO.
 107          $user = $this->getDataGenerator()->create_user();
 108          // Create a delete data request for $user.
 109          api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE);
 110          // Validate that delete data request has been created.
 111          $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
 112  
 113          $event = $this->trigger_delete_user_event($user);
 114  
 115          user_deleted_observer::create_delete_data_request($event);
 116          // Validate that additional delete data request has not been created.
 117          $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE]));
 118      }
 119  
 120      /**
 121       * Ensure that a delete data request is being created upon user deletion
 122       * if a finished delete data request (excluding complete) for that user already exists.
 123       */
 124      public function test_create_delete_data_request_canceled_delete_data_request_preexists() {
 125          $this->resetAfterTest();
 126          $this->setAdminUser();
 127  
 128          // Enable automatic creation of delete data requests.
 129          set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
 130  
 131          // Create another user who is not a DPO.
 132          $user = $this->getDataGenerator()->create_user();
 133          // Create a delete data request for $user.
 134          $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE);
 135          $requestid = $datarequest->get('id');
 136          api::update_request_status($requestid, api::DATAREQUEST_STATUS_CANCELLED);
 137  
 138          // Validate that delete data request has been created and the status has been updated to 'Canceled'.
 139          $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE]));
 140          $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
 141  
 142          $event = $this->trigger_delete_user_event($user);
 143  
 144          user_deleted_observer::create_delete_data_request($event);
 145          // Validate that additional delete data request has been created.
 146          $this->assertEquals(2, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE]));
 147          $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
 148      }
 149  
 150      /**
 151       * Ensure that a delete data request is being created upon user deletion
 152       * if a completed delete data request for that user already exists.
 153       */
 154      public function test_create_delete_data_request_completed_delete_data_request_preexists() {
 155          $this->resetAfterTest();
 156          $this->setAdminUser();
 157  
 158          // Enable automatic creation of delete data requests.
 159          set_config('automaticdeletionrequests', 1, 'tool_dataprivacy');
 160  
 161          // Create another user who is not a DPO.
 162          $user = $this->getDataGenerator()->create_user();
 163          // Create a delete data request for $user.
 164          $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE);
 165          $requestid = $datarequest->get('id');
 166          api::update_request_status($requestid, api::DATAREQUEST_STATUS_COMPLETE);
 167  
 168          // Validate that delete data request has been created and the status has been updated to 'Completed'.
 169          $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE]));
 170          $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
 171  
 172          $event = $this->trigger_delete_user_event($user);
 173  
 174          user_deleted_observer::create_delete_data_request($event);
 175          // Validate that additional delete data request has not been created.
 176          $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE]));
 177          $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE));
 178      }
 179  
 180      /**
 181       * Helper to trigger and capture the delete user event.
 182       *
 183       * @param object $user The user object.
 184       * @return \core\event\user_deleted $event The returned event.
 185       */
 186      private function trigger_delete_user_event($user) {
 187  
 188          $sink = $this->redirectEvents();
 189          delete_user($user);
 190          $events = $sink->get_events();
 191          $sink->close();
 192          $event = reset($events);
 193          // Validate event data.
 194          $this->assertInstanceOf('\core\event\user_deleted', $event);
 195  
 196          return $event;
 197      }
 198  }