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.
   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   * mod_h5pactivity privacy tests
  19   *
  20   * @package    mod_h5pactivity
  21   * @category   test
  22   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_h5pactivity\privacy;
  27  
  28  use \core_privacy\local\request\approved_contextlist;
  29  use \core_privacy\local\request\approved_userlist;
  30  use \core_privacy\local\request\writer;
  31  use \core_privacy\tests\provider_testcase;
  32  
  33  /**
  34   * Privacy tests class for mod_h5pactivity.
  35   *
  36   * @package    mod_h5pactivity
  37   * @category   test
  38   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class mod_h5pactivity_privacy_testcase extends provider_testcase {
  42  
  43      /** @var stdClass User without any attempt. */
  44      protected $student0;
  45  
  46      /** @var stdClass User with some attempt. */
  47      protected $student1;
  48  
  49      /** @var stdClass User with some attempt. */
  50      protected $student2;
  51  
  52      /** @var context context_module of the H5P activity. */
  53      protected $context;
  54  
  55      /**
  56       * Test getting the context for the user ID related to this plugin.
  57       */
  58      public function test_get_contexts_for_userid() {
  59          $this->resetAfterTest(true);
  60          $this->setAdminUser();
  61          $this->h5pactivity_setup_test_scenario_data();
  62  
  63          // The student0 hasn't any attempt.
  64          $contextlist = provider::get_contexts_for_userid($this->student0->id);
  65          $this->assertCount(0, (array) $contextlist->get_contextids());
  66  
  67          // The student1 has data in the mod_h5pactivity context.
  68          $contextlist = provider::get_contexts_for_userid($this->student1->id);
  69          $this->assertCount(1, (array) $contextlist->get_contextids());
  70          $this->assertContains($this->context->id, $contextlist->get_contextids());
  71      }
  72  
  73      /**
  74       * Test getting the user IDs for the context related to this plugin.
  75       */
  76      public function test_get_users_in_context() {
  77          $this->resetAfterTest(true);
  78          $this->setAdminUser();
  79          $this->h5pactivity_setup_test_scenario_data();
  80          $component = 'mod_h5pactivity';
  81  
  82          $userlist = new \core_privacy\local\request\userlist($this->context, $component);
  83          provider::get_users_in_context($userlist);
  84  
  85          // Students 1 and 2 have attempts in the H5P context, student 0 does not.
  86          $this->assertCount(2, $userlist);
  87  
  88          $expected = [$this->student1->id, $this->student2->id];
  89          $actual = $userlist->get_userids();
  90          sort($expected);
  91          sort($actual);
  92          $this->assertEquals($expected, $actual);
  93      }
  94  
  95      /**
  96       * Test that data is exported correctly for this plugin.
  97       */
  98      public function test_export_user_data() {
  99          $this->resetAfterTest(true);
 100          $this->setAdminUser();
 101          $this->h5pactivity_setup_test_scenario_data();
 102          $component = 'mod_h5pactivity';
 103  
 104          // Validate exported data for student0 (without any attempt).
 105          $this->setUser($this->student0);
 106          $writer = writer::with_context($this->context);
 107  
 108          $this->export_context_data_for_user($this->student0->id, $this->context, $component);
 109          $subcontextattempt1 = [
 110              get_string('myattempts', 'mod_h5pactivity'),
 111              get_string('attempt', 'mod_h5pactivity'). " 1"
 112          ];
 113          $data = $writer->get_data($subcontextattempt1);
 114          $this->assertEmpty($data);
 115  
 116          // Validate exported data for student1.
 117          writer::reset();
 118          $this->setUser($this->student1);
 119          $writer = writer::with_context($this->context);
 120          $this->assertFalse($writer->has_any_data());
 121          $this->export_context_data_for_user($this->student1->id, $this->context, $component);
 122  
 123          $data = $writer->get_data([]);
 124          $this->assertEquals('H5P 1', $data->name);
 125  
 126          $data = $writer->get_data($subcontextattempt1);
 127          $this->assertCount(1, (array) $data);
 128          $this->assertCount(3, (array) reset($data));
 129          $subcontextattempt2 = [
 130              get_string('myattempts', 'mod_h5pactivity'),
 131              get_string('attempt', 'mod_h5pactivity'). " 2"
 132          ];
 133          $data = $writer->get_data($subcontextattempt2);
 134          $this->assertCount(3, (array) reset($data));
 135          // The student1 has only 1 tracked attempts.
 136          $subcontextattempt3 = [
 137              get_string('myattempts', 'mod_h5pactivity'),
 138              get_string('attempt', 'mod_h5pactivity'). " 3"
 139          ];
 140          $data = $writer->get_data($subcontextattempt3);
 141          $this->assertEmpty($data);
 142      }
 143  
 144      /**
 145       * Test for provider::delete_data_for_all_users_in_context().
 146       */
 147      public function test_delete_data_for_all_users_in_context() {
 148          global $DB;
 149  
 150          $this->resetAfterTest(true);
 151          $this->setAdminUser();
 152          $this->h5pactivity_setup_test_scenario_data();
 153  
 154          // Before deletion, we should have 4 entries in the attempts table.
 155          $count = $DB->count_records('h5pactivity_attempts');
 156          $this->assertEquals(4, $count);
 157          // Before deletion, we should have 12 entries in the results table.
 158          $count = $DB->count_records('h5pactivity_attempts_results');
 159          $this->assertEquals(12, $count);
 160  
 161          // Delete data based on the context.
 162          provider::delete_data_for_all_users_in_context($this->context);
 163  
 164          // After deletion, the attempts entries should have been deleted.
 165          $count = $DB->count_records('h5pactivity_attempts');
 166          $this->assertEquals(0, $count);
 167          // After deletion, the results entries should have been deleted.
 168          $count = $DB->count_records('h5pactivity_attempts_results');
 169          $this->assertEquals(0, $count);
 170      }
 171  
 172      /**
 173       * Test for provider::delete_data_for_user().
 174       */
 175      public function test_delete_data_for_user() {
 176          global $DB;
 177  
 178          $this->resetAfterTest(true);
 179          $this->setAdminUser();
 180          $this->h5pactivity_setup_test_scenario_data();
 181  
 182          $params = ['userid' => $this->student1->id];
 183  
 184          // Before deletion, we should have 4 entries in the attempts table.
 185          $count = $DB->count_records('h5pactivity_attempts');
 186          $this->assertEquals(4, $count);
 187          // Before deletion, we should have 12 entries in the results table.
 188          $count = $DB->count_records('h5pactivity_attempts_results');
 189          $this->assertEquals(12, $count);
 190  
 191          // Save student1 attempts ids.
 192          $attemptsids = $DB->get_records_menu('h5pactivity_attempts', $params, '', 'attempt, id');
 193          list($resultselect, $attemptids) = $DB->get_in_or_equal($attemptsids);
 194          $resultselect = 'id ' . $resultselect;
 195  
 196          $approvedcontextlist = new approved_contextlist($this->student1, 'h5pactivity', [$this->context->id]);
 197          provider::delete_data_for_user($approvedcontextlist);
 198  
 199          // After deletion, the h5pactivity_attempts entries for the first student should have been deleted.
 200          $count = $DB->count_records('h5pactivity_attempts', $params);
 201          $this->assertEquals(0, $count);
 202  
 203          $count = $DB->count_records('h5pactivity_attempts');
 204          $this->assertEquals(2, $count);
 205          // After deletion, the results entries for the first student should have been deleted.
 206          $count = $DB->count_records_select('h5pactivity_attempts_results', $resultselect, $attemptids);
 207          $this->assertEquals(0, $count);
 208          $count = $DB->count_records('h5pactivity_attempts_results');
 209          $this->assertEquals(6, $count);
 210  
 211          // Confirm that the h5pactivity hasn't been removed.
 212          $h5pactivitycount = $DB->get_records('h5pactivity');
 213          $this->assertCount(1, (array) $h5pactivitycount);
 214  
 215          // Delete track for student0 (nothing has to be removed).
 216          $approvedcontextlist = new approved_contextlist($this->student0, 'h5pactivity', [$this->context->id]);
 217          provider::delete_data_for_user($approvedcontextlist);
 218  
 219          $count = $DB->count_records('h5pactivity_attempts');
 220          $this->assertEquals(2, $count);
 221          $count = $DB->count_records('h5pactivity_attempts_results');
 222          $this->assertEquals(6, $count);
 223      }
 224  
 225      /**
 226       * Test for provider::delete_data_for_users().
 227       */
 228      public function test_delete_data_for_users() {
 229          global $DB;
 230          $component = 'mod_h5pactivity';
 231  
 232          $this->resetAfterTest(true);
 233          $this->setAdminUser();
 234          // In this scenario we need a 3rd user to test batch deletion.
 235          // Create student2 with 2 attempts.
 236          $this->h5pactivity_setup_test_scenario_data(true);
 237  
 238          // Before deletion, we should have 6 entries in the attempts table.
 239          $count = $DB->count_records('h5pactivity_attempts');
 240          $this->assertEquals(6, $count);
 241          // Before deletion, we should have 18 entries in the results table.
 242          $count = $DB->count_records('h5pactivity_attempts_results');
 243          $this->assertEquals(18, $count);
 244  
 245          // Save student1 and student2 attempts ids.
 246          $params1 = ['userid' => $this->student1->id];
 247          $attempts1ids = $DB->get_records_menu('h5pactivity_attempts', $params1, '', 'attempt, id');
 248          $params2 = ['userid' => $this->student2->id];
 249          $attempts2ids = $DB->get_records_menu('h5pactivity_attempts', $params2, '', 'attempt, id');
 250          list($resultselect, $attemptids) = $DB->get_in_or_equal(array_merge($attempts1ids, $attempts2ids));
 251          $resultselect = 'id ' . $resultselect;
 252  
 253          // Delete student 1 ans 2 data, retain student 3 data.
 254          $approveduserids = [$this->student1->id, $this->student2->id];
 255          $approvedlist = new approved_userlist($this->context, $component, $approveduserids);
 256          provider::delete_data_for_users($approvedlist);
 257  
 258          // After deletion, the h5pactivity_attempts entries for student1 and student2 should have been deleted.
 259          $count = $DB->count_records('h5pactivity_attempts', $params1);
 260          $this->assertEquals(0, $count);
 261          $count = $DB->count_records('h5pactivity_attempts', $params2);
 262          $this->assertEquals(0, $count);
 263  
 264          $count = $DB->count_records('h5pactivity_attempts');
 265          $this->assertEquals(2, $count);
 266          // After deletion, the results entries for the first and second student should have been deleted.
 267          $count = $DB->count_records_select('h5pactivity_attempts_results', $resultselect, $attemptids);
 268          $this->assertEquals(0, $count);
 269          $count = $DB->count_records('h5pactivity_attempts_results');
 270          $this->assertEquals(6, $count);
 271  
 272          // Confirm that the h5pactivity hasn't been removed.
 273          $h5pactivitycount = $DB->get_records('h5pactivity');
 274          $this->assertCount(1, (array) $h5pactivitycount);
 275  
 276          // Delete results track for student0 (nothing has to be removed).
 277          $approveduserids = [$this->student0->id];
 278          $approvedlist = new approved_userlist($this->context, $component, $approveduserids);
 279          provider::delete_data_for_users($approvedlist);
 280  
 281          $count = $DB->count_records('h5pactivity_attempts');
 282          $this->assertEquals(2, $count);
 283          $count = $DB->count_records('h5pactivity_attempts_results');
 284          $this->assertEquals(6, $count);
 285      }
 286  
 287      /**
 288       * Helper function to setup 3 users and 2 H5P attempts for student1 and student2.
 289       * $this->student0 is always created without any attempt.
 290       *
 291       * @param bool $extrauser generate a 3rd user (default false).
 292       */
 293      protected function h5pactivity_setup_test_scenario_data(bool $extrauser = false): void {
 294          global $DB;
 295  
 296          $generator = $this->getDataGenerator();
 297  
 298          $course = $this->getDataGenerator()->create_course();
 299          $params = ['course' => $course];
 300          $activity = $this->getDataGenerator()->create_module('h5pactivity', $params);
 301          $cm = get_coursemodule_from_id('h5pactivity', $activity->cmid, 0, false, MUST_EXIST);
 302          $this->context = \context_module::instance($activity->cmid);
 303  
 304          // Users enrolments.
 305          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 306  
 307          $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
 308  
 309          // Create student0 withot any attempt.
 310          $this->student0 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 311  
 312          // Create student1 with 2 attempts.
 313          $this->student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 314          $params = ['cmid' => $cm->id, 'userid' => $this->student1->id];
 315          $generator->create_content($activity, $params);
 316          $generator->create_content($activity, $params);
 317  
 318          // Create student2 with 2 attempts.
 319          $this->student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 320          $params = ['cmid' => $cm->id, 'userid' => $this->student2->id];
 321          $generator->create_content($activity, $params);
 322          $generator->create_content($activity, $params);
 323  
 324          if ($extrauser) {
 325              $this->student3 = $this->getDataGenerator()->create_and_enrol($course, 'student');
 326              $params = ['cmid' => $cm->id, 'userid' => $this->student3->id];
 327              $generator->create_content($activity, $params);
 328              $generator->create_content($activity, $params);
 329          }
 330      }
 331  }