Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310]

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