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 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   * Privacy provider tests.
  19   *
  20   * @package    enrol_lti
  21   * @copyright  2018 Mark Nelson <markn@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use enrol_lti\privacy\provider;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Privacy provider tests class.
  31   *
  32   * @package    enrol_lti
  33   * @copyright  2018 Mark Nelson <markn@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class enrol_lti_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
  37  
  38      /**
  39       * @var stdClass The user
  40       */
  41      protected $user = null;
  42  
  43      /**
  44       * @var stdClass Another user
  45       */
  46      protected $anotheruser = null;
  47  
  48      /**
  49       * @var stdClass The course
  50       */
  51      protected $course = null;
  52  
  53      /**
  54       * @var stdClass The activity
  55       */
  56      protected $activity = null;
  57  
  58      /**
  59       * Basic setup for these tests.
  60       */
  61      public function setUp(): void {
  62          $this->resetAfterTest();
  63  
  64          $this->course = $this->getDataGenerator()->create_course();
  65          $this->user = $this->getDataGenerator()->create_user();
  66          $this->activity = $this->getDataGenerator()->create_module('forum', ['course' => $this->course->id]);
  67  
  68          // Get the course and activity contexts.
  69          $coursecontext = \context_course::instance($this->course->id);
  70          $cmcontext = \context_module::instance($this->activity->cmid);
  71  
  72          // Create LTI tools in different contexts.
  73          $this->create_lti_users($coursecontext, $this->user->id);
  74          $this->create_lti_users($coursecontext, $this->user->id);
  75          $this->create_lti_users($cmcontext, $this->user->id);
  76  
  77          // Create another LTI user.
  78          $this->anotheruser = $this->getDataGenerator()->create_user();
  79          $this->create_lti_users($coursecontext, $this->anotheruser->id);
  80      }
  81  
  82      /**
  83       * Test getting the context for the user ID related to this plugin.
  84       */
  85      public function test_get_contexts_for_userid() {
  86          $contextlist = provider::get_contexts_for_userid($this->user->id);
  87  
  88          $this->assertCount(2, $contextlist);
  89  
  90          $coursectx = context_course::instance($this->course->id);
  91          $activityctx = context_module::instance($this->activity->cmid);
  92          $expectedids = [$coursectx->id, $activityctx->id];
  93  
  94          $actualids = $contextlist->get_contextids();
  95          $this->assertEqualsCanonicalizing($expectedids, $actualids);
  96      }
  97  
  98      /**
  99       * Test for provider::export_user_data().
 100       */
 101      public function test_export_for_context() {
 102          $coursecontext = context_course::instance($this->course->id);
 103          $cmcontext = \context_module::instance($this->activity->cmid);
 104  
 105          // Export all of the data for the course context.
 106          $this->export_context_data_for_user($this->user->id, $coursecontext, 'enrol_lti');
 107          $writer = \core_privacy\local\request\writer::with_context($coursecontext);
 108          $this->assertTrue($writer->has_any_data());
 109  
 110          $data = (array) $writer->get_data(['enrol_lti_users']);
 111          $this->assertCount(2, $data);
 112          foreach ($data as $ltiuser) {
 113              $this->assertArrayHasKey('lastgrade', $ltiuser);
 114              $this->assertArrayHasKey('timecreated', $ltiuser);
 115              $this->assertArrayHasKey('timemodified', $ltiuser);
 116          }
 117  
 118          // Export all of the data for the activity context.
 119          $this->export_context_data_for_user($this->user->id, $cmcontext, 'enrol_lti');
 120          $writer = \core_privacy\local\request\writer::with_context($cmcontext);
 121          $this->assertTrue($writer->has_any_data());
 122  
 123          $data = (array) $writer->get_data(['enrol_lti_users']);
 124          $this->assertCount(1, $data);
 125          foreach ($data as $ltiuser) {
 126              $this->assertArrayHasKey('lastgrade', $ltiuser);
 127              $this->assertArrayHasKey('timecreated', $ltiuser);
 128              $this->assertArrayHasKey('timemodified', $ltiuser);
 129          }
 130      }
 131  
 132      /**
 133       * Test for provider::delete_data_for_all_users_in_context().
 134       */
 135      public function test_delete_data_for_all_users_in_context() {
 136          global $DB;
 137  
 138          $count = $DB->count_records('enrol_lti_users');
 139          $this->assertEquals(4, $count);
 140  
 141          // Delete data based on context.
 142          $coursecontext = context_course::instance($this->course->id);
 143          provider::delete_data_for_all_users_in_context($coursecontext);
 144  
 145          $ltiusers = $DB->get_records('enrol_lti_users');
 146          $this->assertCount(1, $ltiusers);
 147  
 148          $ltiuser = reset($ltiusers);
 149          $this->assertEquals($ltiuser->userid, $this->user->id);
 150      }
 151  
 152      /**
 153       * Test for provider::delete_data_for_user().
 154       */
 155      public function test_delete_data_for_user() {
 156          global $DB;
 157  
 158          $cmcontext = context_module::instance($this->activity->cmid);
 159          $coursecontext = context_course::instance($this->course->id);
 160  
 161          $count = $DB->count_records('enrol_lti_users');
 162          $this->assertEquals(4, $count);
 163  
 164          $contextlist = new \core_privacy\local\request\approved_contextlist($this->user, 'enrol_lti',
 165              [context_system::instance()->id, $coursecontext->id, $cmcontext->id]);
 166          provider::delete_data_for_user($contextlist);
 167  
 168          $ltiusers = $DB->get_records('enrol_lti_users');
 169          $this->assertCount(1, $ltiusers);
 170  
 171          $ltiuser = reset($ltiusers);
 172          $this->assertNotEquals($ltiuser->userid, $this->user->id);
 173      }
 174  
 175      /**
 176       * Creates a LTI user given the provided context
 177       *
 178       * @param context $context
 179       * @param int $userid
 180       */
 181      private function create_lti_users(\context $context, int $userid) {
 182          global $DB;
 183  
 184          // Create a tool.
 185          $ltitool = (object) [
 186              'enrolid' => 5,
 187              'contextid' => $context->id,
 188              'roleinstructor' => 5,
 189              'rolelearner' => 5,
 190              'timecreated' => time(),
 191              'timemodified' => time() + DAYSECS
 192          ];
 193          $toolid = $DB->insert_record('enrol_lti_tools', $ltitool);
 194  
 195          // Create a user.
 196          $ltiuser = (object) [
 197              'userid' => $userid,
 198              'toolid' => $toolid,
 199              'lastgrade' => 50,
 200              'lastaccess' => time() + DAYSECS,
 201              'timecreated' => time()
 202          ];
 203          $DB->insert_record('enrol_lti_users', $ltiuser);
 204      }
 205  
 206      /**
 207       * Test for provider::get_users_in_context() when the context is a course.
 208       */
 209      public function test_get_users_in_context_course() {
 210          $coursecontext = context_course::instance($this->course->id);
 211          $userlist = new \core_privacy\local\request\userlist($coursecontext, 'enrol_paypal');
 212          provider::get_users_in_context($userlist);
 213  
 214          $this->assertEqualsCanonicalizing(
 215                  [$this->user->id, $this->anotheruser->id],
 216                  $userlist->get_userids());
 217      }
 218  
 219      /**
 220       * Test for provider::get_users_in_context() when the context is an activity.
 221       */
 222      public function test_get_users_in_context_activity() {
 223          $activityctx = context_module::instance($this->activity->cmid);
 224          $userlist = new \core_privacy\local\request\userlist($activityctx, 'enrol_paypal');
 225          provider::get_users_in_context($userlist);
 226  
 227          $this->assertEquals(
 228                  [$this->user->id],
 229                  $userlist->get_userids());
 230      }
 231  
 232      /**
 233       * Test for provider::delete_data_for_users() when the context is a course.
 234       */
 235      public function test_delete_data_for_users_course() {
 236          global $DB;
 237  
 238          $coursecontext = context_course::instance($this->course->id);
 239  
 240          $count = $DB->count_records('enrol_lti_users');
 241          $this->assertEquals(4, $count);
 242  
 243          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext, 'enrol_paypal',
 244                  [$this->user->id]);
 245          provider::delete_data_for_users($approveduserlist);
 246  
 247          $ltiusers = $DB->get_records('enrol_lti_users');
 248          $this->assertCount(2, $ltiusers);
 249  
 250          foreach ($ltiusers as $ltiuser) {
 251              $leftover = false;
 252              if ($ltiuser->userid == $this->user->id) {
 253                  $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]);
 254                  if ($contextid == $coursecontext->id) {
 255                      $leftover = true;
 256                  }
 257              }
 258          }
 259          $this->assertFalse($leftover);
 260      }
 261  
 262      /**
 263       * Test for provider::delete_data_for_users() when the context is an activity.
 264       */
 265      public function test_delete_data_for_users_activity() {
 266          global $DB;
 267  
 268          $cmcontext = context_module::instance($this->activity->cmid);
 269  
 270          $count = $DB->count_records('enrol_lti_users');
 271          $this->assertEquals(4, $count);
 272  
 273          $approveduserlist = new \core_privacy\local\request\approved_userlist($cmcontext, 'enrol_paypal',
 274                  [$this->user->id]);
 275          provider::delete_data_for_users($approveduserlist);
 276  
 277          $ltiusers = $DB->get_records('enrol_lti_users');
 278          $this->assertCount(3, $ltiusers);
 279  
 280          foreach ($ltiusers as $ltiuser) {
 281              $leftover = false;
 282              if ($ltiuser->userid == $this->user->id) {
 283                  $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]);
 284                  if ($contextid == $cmcontext->id) {
 285                      $leftover = true;
 286                  }
 287              }
 288          }
 289          $this->assertFalse($leftover);
 290      }
 291  }