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 data_privacy_testcase;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  require_once ('data_privacy_testcase.php');
  23  
  24  /**
  25   * Tests for the data_request persistent.
  26   *
  27   * @package    tool_dataprivacy
  28   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class data_request_test extends data_privacy_testcase {
  32  
  33      /**
  34       * Data provider for testing is_resettable, and is_active.
  35       *
  36       * @return  array
  37       */
  38      public function status_state_provider() : array {
  39          return [
  40              [
  41                  'state' => api::DATAREQUEST_STATUS_PENDING,
  42                  'resettable' => false,
  43                  'active' => false,
  44              ],
  45              [
  46                  'state' => api::DATAREQUEST_STATUS_AWAITING_APPROVAL,
  47                  'resettable' => false,
  48                  'active' => false,
  49              ],
  50              [
  51                  'state' => api::DATAREQUEST_STATUS_APPROVED,
  52                  'resettable' => true,
  53                  'active' => true,
  54              ],
  55              [
  56                  'state' => api::DATAREQUEST_STATUS_PROCESSING,
  57                  'resettable' => false,
  58                  'active' => false,
  59              ],
  60              [
  61                  'state' => api::DATAREQUEST_STATUS_COMPLETE,
  62                  'resettable' => false,
  63                  'active' => false,
  64              ],
  65              [
  66                  'state' => api::DATAREQUEST_STATUS_CANCELLED,
  67                  'resettable' => false,
  68                  'active' => false,
  69              ],
  70              [
  71                  'state' => api::DATAREQUEST_STATUS_REJECTED,
  72                  'resettable' => true,
  73                  'active' => false,
  74              ],
  75              [
  76                  'state' => api::DATAREQUEST_STATUS_DOWNLOAD_READY,
  77                  'resettable' => false,
  78                  'active' => false,
  79              ],
  80              [
  81                  'state' => api::DATAREQUEST_STATUS_EXPIRED,
  82                  'resettable' => false,
  83                  'active' => false,
  84              ],
  85          ];
  86      }
  87  
  88      /**
  89       * Test the pseudo states of a data request with an export request.
  90       *
  91       * @dataProvider        status_state_provider
  92       * @param       int     $status
  93       * @param       bool    $resettable
  94       * @param       bool    $active
  95       */
  96      public function test_pseudo_states_export(int $status, bool $resettable, bool $active) {
  97          $uut = new \tool_dataprivacy\data_request();
  98          $uut->set('status', $status);
  99          $uut->set('type', api::DATAREQUEST_TYPE_EXPORT);
 100  
 101          $this->assertEquals($resettable, $uut->is_resettable());
 102          $this->assertEquals($active, $uut->is_active());
 103      }
 104  
 105      /**
 106       * Test the pseudo states of a data request with a delete request.
 107       *
 108       * @dataProvider        status_state_provider
 109       * @param       int     $status
 110       * @param       bool    $resettable
 111       * @param       bool    $active
 112       */
 113      public function test_pseudo_states_delete(int $status, bool $resettable, bool $active) {
 114          $uut = new \tool_dataprivacy\data_request();
 115          $uut->set('status', $status);
 116          $uut->set('type', api::DATAREQUEST_TYPE_DELETE);
 117  
 118          $this->assertEquals($resettable, $uut->is_resettable());
 119          $this->assertEquals($active, $uut->is_active());
 120      }
 121  
 122      /**
 123       * Test the pseudo states of a data request.
 124       *
 125       * @dataProvider        status_state_provider
 126       * @param       int     $status
 127       */
 128      public function test_can_reset_others($status) {
 129          $uut = new \tool_dataprivacy\data_request();
 130          $uut->set('status', $status);
 131          $uut->set('type', api::DATAREQUEST_TYPE_OTHERS);
 132  
 133          $this->assertFalse($uut->is_resettable());
 134      }
 135  
 136      /**
 137       * Data provider for states which are not resettable.
 138       *
 139       * @return      array
 140       */
 141      public function non_resettable_provider() : array {
 142          $states = [];
 143          foreach ($this->status_state_provider() as $thisstatus) {
 144              if (!$thisstatus['resettable']) {
 145                  $states[] = $thisstatus;
 146              }
 147          }
 148  
 149          return $states;
 150      }
 151  
 152      /**
 153       * Ensure that requests which are not resettable cause an exception to be thrown.
 154       *
 155       * @dataProvider        non_resettable_provider
 156       * @param       int     $status
 157       */
 158      public function test_non_resubmit_request($status) {
 159          $uut = new \tool_dataprivacy\data_request();
 160          $uut->set('status', $status);
 161  
 162          $this->expectException(\moodle_exception::class);
 163          $this->expectExceptionMessage(get_string('cannotreset', 'tool_dataprivacy'));
 164  
 165          $uut->resubmit_request();
 166      }
 167  
 168      /**
 169       * Ensure that a rejected request can be reset.
 170       */
 171      public function test_resubmit_request() {
 172          $this->resetAfterTest();
 173  
 174          $uut = new \tool_dataprivacy\data_request();
 175          $uut->set('status', api::DATAREQUEST_STATUS_REJECTED);
 176          $uut->set('type', api::DATAREQUEST_TYPE_DELETE);
 177          $uut->set('comments', 'Foo');
 178          $uut->set('requestedby', 42);
 179          $uut->set('dpo', 98);
 180  
 181          $newrequest = $uut->resubmit_request();
 182  
 183          $this->assertEquals('Foo', $newrequest->get('comments'));
 184          $this->assertEquals(42, $newrequest->get('requestedby'));
 185          $this->assertEquals(98, $newrequest->get('dpo'));
 186          $this->assertEquals(api::DATAREQUEST_STATUS_AWAITING_APPROVAL, $newrequest->get('status'));
 187          $this->assertEquals(api::DATAREQUEST_TYPE_DELETE, $newrequest->get('type'));
 188  
 189          $this->assertEquals(api::DATAREQUEST_STATUS_REJECTED, $uut->get('status'));
 190      }
 191  
 192      /**
 193       * Ensure that an active request can be reset.
 194       */
 195      public function test_resubmit_active_request() {
 196          $this->resetAfterTest();
 197  
 198          $uut = new \tool_dataprivacy\data_request();
 199          $uut->set('status', api::DATAREQUEST_STATUS_APPROVED);
 200          $uut->set('type', api::DATAREQUEST_TYPE_DELETE);
 201          $uut->set('comments', 'Foo');
 202          $uut->set('requestedby', 42);
 203          $uut->set('dpo', 98);
 204  
 205          $newrequest = $uut->resubmit_request();
 206  
 207          $this->assertEquals('Foo', $newrequest->get('comments'));
 208          $this->assertEquals(42, $newrequest->get('requestedby'));
 209          $this->assertEquals(98, $newrequest->get('dpo'));
 210          $this->assertEquals(api::DATAREQUEST_STATUS_AWAITING_APPROVAL, $newrequest->get('status'));
 211          $this->assertEquals(api::DATAREQUEST_TYPE_DELETE, $newrequest->get('type'));
 212  
 213          $this->assertEquals(api::DATAREQUEST_STATUS_REJECTED, $uut->get('status'));
 214      }
 215  
 216      /**
 217       * Create a data request for the user.
 218       *
 219       * @param   int     $userid
 220       * @param   int     $type
 221       * @param   int     $status
 222       * @return  data_request
 223       */
 224      public function create_request_for_user_with_status(int $userid, int $type, int $status) : data_request {
 225          $request = new data_request(0, (object) [
 226                  'userid' => $userid,
 227                  'type' => $type,
 228                  'status' => $status,
 229              ]);
 230  
 231          $request->save();
 232  
 233          return $request;
 234      }
 235  }