Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 plugin privacy provider
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2020 Paul Holden <paulh@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_dataprivacy\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_privacy\local\request\userlist;
  29  use core_privacy\local\request\writer;
  30  use core_privacy\tests\provider_testcase;
  31  use tool_dataprivacy\api;
  32  use tool_dataprivacy\local\helper;
  33  use tool_dataprivacy\privacy\provider;
  34  
  35  /**
  36   * Privacy provider tests
  37   *
  38   * @package    tool_dataprivacy
  39   * @copyright  2020 Paul Holden <paulh@moodle.com>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class provider_test extends provider_testcase {
  43  
  44      /**
  45       * Test provider get_contexts_for_userid method
  46       *
  47       * @return void
  48       */
  49      public function test_get_contexts_for_userid() {
  50          $this->resetAfterTest();
  51  
  52          $user = $this->getDataGenerator()->create_user();
  53          $context = \context_user::instance($user->id);
  54  
  55          // Returned context list should contain a single item.
  56          $contextlist = $this->get_contexts_for_userid($user->id, 'tool_dataprivacy');
  57          $this->assertCount(1, $contextlist);
  58  
  59          // We should have the user context of our test user.
  60          $this->assertSame($context, $contextlist->current());
  61      }
  62  
  63      /**
  64       * Test provider get_users_in_context method
  65       *
  66       * @return void
  67       */
  68      public function test_get_users_in_context() {
  69          $this->resetAfterTest();
  70  
  71          $user = $this->getDataGenerator()->create_user();
  72          $context = \context_user::instance($user->id);
  73  
  74          $userlist = new userlist($context, 'tool_dataprivacy');
  75          provider::get_users_in_context($userlist);
  76  
  77          $this->assertEquals([$user->id], $userlist->get_userids());
  78      }
  79  
  80      /**
  81       * Test provider get_users_in_context method for a non-user context
  82       *
  83       * @return void
  84       */
  85      public function test_get_users_in_context_non_user_context() {
  86          $context = \context_system::instance();
  87  
  88          $userlist = new userlist($context, 'tool_dataprivacy');
  89          provider::get_users_in_context($userlist);
  90  
  91          $this->assertEmpty($userlist);
  92      }
  93  
  94      /**
  95       * Test provider export_user_data method
  96       *
  97       * @return void
  98       */
  99      public function test_export_user_data() {
 100          $this->resetAfterTest();
 101  
 102          $user = $this->getDataGenerator()->create_user();
 103          $context = \context_user::instance($user->id);
 104  
 105          $this->setUser($user);
 106  
 107          // Create an export request, approve it.
 108          $requestexport = api::create_data_request($user->id, api::DATAREQUEST_TYPE_EXPORT,
 109              'Please export my stuff');
 110          api::update_request_status($requestexport->get('id'), api::DATAREQUEST_STATUS_APPROVED);
 111  
 112          // Create a deletion request, reject it.
 113          $requestdelete = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE);
 114          api::update_request_status($requestdelete->get('id'), api::DATAREQUEST_STATUS_REJECTED, 0, 'Nope');
 115  
 116          $this->export_context_data_for_user($user->id, $context, 'tool_dataprivacy');
 117  
 118          /** @var \core_privacy\tests\request\content_writer $writer */
 119          $writer = writer::with_context($context);
 120          $this->assertTrue($writer->has_any_data());
 121  
 122          /** @var stdClass[] $data */
 123          $data = (array) $writer->get_data([
 124              get_string('privacyandpolicies', 'admin'),
 125              get_string('datarequests', 'tool_dataprivacy'),
 126          ]);
 127  
 128          $this->assertCount(2, $data);
 129  
 130          $strs = get_strings(['requesttypeexportshort', 'requesttypedeleteshort',
 131              'statusapproved', 'statusrejected', 'creationmanual'], 'tool_dataprivacy');
 132  
 133          // First item is the approved export request.
 134          $this->assertEquals($strs->requesttypeexportshort, $data[0]->type);
 135          $this->assertEquals($strs->statusapproved, $data[0]->status);
 136          $this->assertEquals($strs->creationmanual, $data[0]->creationmethod);
 137          $this->assertEquals($requestexport->get('comments'), $data[0]->comments);
 138          $this->assertEmpty($data[0]->dpocomment);
 139          $this->assertNotEmpty($data[0]->timecreated);
 140  
 141          // Next is the rejected deletion request.
 142          $this->assertEquals($strs->requesttypedeleteshort, $data[1]->type);
 143          $this->assertEquals($strs->statusrejected, $data[1]->status);
 144          $this->assertEquals($strs->creationmanual, $data[1]->creationmethod);
 145          $this->assertEmpty($data[1]->comments);
 146          $this->assertStringContainsString('Nope', $data[1]->dpocomment);
 147          $this->assertNotEmpty($data[1]->timecreated);
 148      }
 149  
 150      /**
 151       * Test class export_user_preferences method
 152       *
 153       * @return void
 154       */
 155      public function test_export_user_preferences() {
 156          $this->resetAfterTest();
 157  
 158          $user = $this->getDataGenerator()->create_user();
 159          $this->setUser($user);
 160  
 161          // Set filters preference.
 162          $filters = [
 163              helper::FILTER_TYPE . ':' . api::DATAREQUEST_TYPE_EXPORT,
 164              helper::FILTER_STATUS . ':' . api::DATAREQUEST_STATUS_PENDING,
 165          ];
 166          set_user_preference(helper::PREF_REQUEST_FILTERS, json_encode($filters), $user);
 167  
 168          // Set paging preference.
 169          set_user_preference(helper::PREF_REQUEST_PERPAGE, 6, $user);
 170  
 171          provider::export_user_preferences($user->id);
 172  
 173          /** @var \core_privacy\tests\request\content_writer $writer */
 174          $writer = writer::with_context(\context_system::instance());
 175          $this->assertTrue($writer->has_any_data());
 176  
 177          /** @var stdClass[] $preferences */
 178          $preferences = (array) $writer->get_user_preferences('tool_dataprivacy');
 179          $this->assertCount(2, $preferences);
 180  
 181          $this->assertEquals((object) [
 182              'value' => '1:1, 2:0',
 183              'description' => 'Type: Export, Status: Pending',
 184          ], $preferences[helper::PREF_REQUEST_FILTERS]);
 185  
 186          $this->assertEquals(6, $preferences[helper::PREF_REQUEST_PERPAGE]->value);
 187      }
 188  }