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   * 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() {
  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->assertEquals($expectedids, $actualids, '', 0.0, 10, true);
  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->assertEquals(
 215                  [$this->user->id, $this->anotheruser->id],
 216                  $userlist->get_userids(),
 217                  '', 0.0, 10, true);
 218      }
 219  
 220      /**
 221       * Test for provider::get_users_in_context() when the context is an activity.
 222       */
 223      public function test_get_users_in_context_activity() {
 224          $activityctx = context_module::instance($this->activity->cmid);
 225          $userlist = new \core_privacy\local\request\userlist($activityctx, 'enrol_paypal');
 226          provider::get_users_in_context($userlist);
 227  
 228          $this->assertEquals(
 229                  [$this->user->id],
 230                  $userlist->get_userids());
 231      }
 232  
 233      /**
 234       * Test for provider::delete_data_for_users() when the context is a course.
 235       */
 236      public function test_delete_data_for_users_course() {
 237          global $DB;
 238  
 239          $coursecontext = context_course::instance($this->course->id);
 240  
 241          $count = $DB->count_records('enrol_lti_users');
 242          $this->assertEquals(4, $count);
 243  
 244          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext, 'enrol_paypal',
 245                  [$this->user->id]);
 246          provider::delete_data_for_users($approveduserlist);
 247  
 248          $ltiusers = $DB->get_records('enrol_lti_users');
 249          $this->assertCount(2, $ltiusers);
 250  
 251          foreach ($ltiusers as $ltiuser) {
 252              $leftover = false;
 253              if ($ltiuser->userid == $this->user->id) {
 254                  $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]);
 255                  if ($contextid == $coursecontext->id) {
 256                      $leftover = true;
 257                  }
 258              }
 259          }
 260          $this->assertFalse($leftover);
 261      }
 262  
 263      /**
 264       * Test for provider::delete_data_for_users() when the context is an activity.
 265       */
 266      public function test_delete_data_for_users_activity() {
 267          global $DB;
 268  
 269          $cmcontext = context_module::instance($this->activity->cmid);
 270  
 271          $count = $DB->count_records('enrol_lti_users');
 272          $this->assertEquals(4, $count);
 273  
 274          $approveduserlist = new \core_privacy\local\request\approved_userlist($cmcontext, 'enrol_paypal',
 275                  [$this->user->id]);
 276          provider::delete_data_for_users($approveduserlist);
 277  
 278          $ltiusers = $DB->get_records('enrol_lti_users');
 279          $this->assertCount(3, $ltiusers);
 280  
 281          foreach ($ltiusers as $ltiuser) {
 282              $leftover = false;
 283              if ($ltiuser->userid == $this->user->id) {
 284                  $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]);
 285                  if ($contextid == $cmcontext->id) {
 286                      $leftover = true;
 287                  }
 288              }
 289          }
 290          $this->assertFalse($leftover);
 291      }
 292  }