Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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