Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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   * Privacy provider tests.
  19   *
  20   * @package    mod_choice
  21   * @copyright  2018 Jun Pataleta
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace mod_choice\privacy;
  25  
  26  use core_privacy\local\metadata\collection;
  27  use core_privacy\local\request\deletion_criteria;
  28  use mod_choice\privacy\provider;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Privacy provider tests class.
  34   *
  35   * @package    mod_choice
  36   * @copyright  2018 Jun Pataleta
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class provider_test extends \core_privacy\tests\provider_testcase {
  40      /** @var stdClass The student object. */
  41      protected $student;
  42  
  43      /** @var stdClass The choice object. */
  44      protected $choice;
  45  
  46      /** @var stdClass The course object. */
  47      protected $course;
  48  
  49      /**
  50       * {@inheritdoc}
  51       */
  52      protected function setUp(): void {
  53          $this->resetAfterTest();
  54  
  55          global $DB;
  56          $generator = $this->getDataGenerator();
  57          $course = $generator->create_course();
  58          $options = ['fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'];
  59          $params = [
  60              'course' => $course->id,
  61              'option' => $options,
  62              'name' => 'First Choice Activity',
  63              'showpreview' => 0
  64          ];
  65  
  66          $plugingenerator = $generator->get_plugin_generator('mod_choice');
  67          // The choice activity the user will answer.
  68          $choice = $plugingenerator->create_instance($params);
  69          // Create another choice activity.
  70          $plugingenerator->create_instance($params);
  71          $cm = get_coursemodule_from_instance('choice', $choice->id);
  72  
  73          // Create a student which will make a choice.
  74          $student = $generator->create_user();
  75          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  76          $generator->enrol_user($student->id,  $course->id, $studentrole->id);
  77  
  78          $choicewithoptions = choice_get_choice($choice->id);
  79          $optionids = array_keys($choicewithoptions->option);
  80  
  81          choice_user_submit_response($optionids[2], $choice, $student->id, $course, $cm);
  82          $this->student = $student;
  83          $this->choice = $choice;
  84          $this->course = $course;
  85      }
  86  
  87      /**
  88       * Test for provider::get_metadata().
  89       */
  90      public function test_get_metadata() {
  91          $collection = new collection('mod_choice');
  92          $newcollection = provider::get_metadata($collection);
  93          $itemcollection = $newcollection->get_collection();
  94          $this->assertCount(1, $itemcollection);
  95  
  96          $table = reset($itemcollection);
  97          $this->assertEquals('choice_answers', $table->get_name());
  98  
  99          $privacyfields = $table->get_privacy_fields();
 100          $this->assertArrayHasKey('choiceid', $privacyfields);
 101          $this->assertArrayHasKey('optionid', $privacyfields);
 102          $this->assertArrayHasKey('userid', $privacyfields);
 103          $this->assertArrayHasKey('timemodified', $privacyfields);
 104  
 105          $this->assertEquals('privacy:metadata:choice_answers', $table->get_summary());
 106      }
 107  
 108      /**
 109       * Test for provider::get_contexts_for_userid().
 110       */
 111      public function test_get_contexts_for_userid() {
 112          $cm = get_coursemodule_from_instance('choice', $this->choice->id);
 113  
 114          $contextlist = provider::get_contexts_for_userid($this->student->id);
 115          $this->assertCount(1, $contextlist);
 116          $contextforuser = $contextlist->current();
 117          $cmcontext = \context_module::instance($cm->id);
 118          $this->assertEquals($cmcontext->id, $contextforuser->id);
 119      }
 120  
 121      /**
 122       * Test for provider::export_user_data().
 123       */
 124      public function test_export_for_context() {
 125          $cm = get_coursemodule_from_instance('choice', $this->choice->id);
 126          $cmcontext = \context_module::instance($cm->id);
 127  
 128          // Export all of the data for the context.
 129          $this->export_context_data_for_user($this->student->id, $cmcontext, 'mod_choice');
 130          $writer = \core_privacy\local\request\writer::with_context($cmcontext);
 131          $this->assertTrue($writer->has_any_data());
 132      }
 133  
 134      /**
 135       * Test for provider::delete_data_for_all_users_in_context().
 136       */
 137      public function test_delete_data_for_all_users_in_context() {
 138          global $DB;
 139  
 140          $choice = $this->choice;
 141          $generator = $this->getDataGenerator();
 142          $cm = get_coursemodule_from_instance('choice', $this->choice->id);
 143  
 144          // Create another student who will answer the choice activity.
 145          $student = $generator->create_user();
 146          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 147          $generator->enrol_user($student->id, $this->course->id, $studentrole->id);
 148  
 149          $choicewithoptions = choice_get_choice($choice->id);
 150          $optionids = array_keys($choicewithoptions->option);
 151  
 152          choice_user_submit_response($optionids[1], $choice, $student->id, $this->course, $cm);
 153  
 154          // Before deletion, we should have 2 responses.
 155          $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
 156          $this->assertEquals(2, $count);
 157  
 158          // Delete data based on context.
 159          $cmcontext = \context_module::instance($cm->id);
 160          provider::delete_data_for_all_users_in_context($cmcontext);
 161  
 162          // After deletion, the choice answers for that choice activity should have been deleted.
 163          $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
 164          $this->assertEquals(0, $count);
 165      }
 166  
 167      /**
 168       * Test for provider::delete_data_for_user().
 169       */
 170      public function test_delete_data_for_user_() {
 171          global $DB;
 172  
 173          $choice = $this->choice;
 174          $generator = $this->getDataGenerator();
 175          $cm1 = get_coursemodule_from_instance('choice', $this->choice->id);
 176  
 177          // Create a second choice activity.
 178          $options = ['Boracay', 'Camiguin', 'Bohol', 'Cebu', 'Coron'];
 179          $params = [
 180              'course' => $this->course->id,
 181              'option' => $options,
 182              'name' => 'Which do you think is the best island in the Philippines?',
 183              'showpreview' => 0
 184          ];
 185          $plugingenerator = $generator->get_plugin_generator('mod_choice');
 186          $choice2 = $plugingenerator->create_instance($params);
 187          $plugingenerator->create_instance($params);
 188          $cm2 = get_coursemodule_from_instance('choice', $choice2->id);
 189  
 190          // Make a selection for the first student for the 2nd choice activity.
 191          $choicewithoptions = choice_get_choice($choice2->id);
 192          $optionids = array_keys($choicewithoptions->option);
 193          choice_user_submit_response($optionids[2], $choice2, $this->student->id, $this->course, $cm2);
 194  
 195          // Create another student who will answer the first choice activity.
 196          $otherstudent = $generator->create_user();
 197          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 198          $generator->enrol_user($otherstudent->id, $this->course->id, $studentrole->id);
 199  
 200          $choicewithoptions = choice_get_choice($choice->id);
 201          $optionids = array_keys($choicewithoptions->option);
 202  
 203          choice_user_submit_response($optionids[1], $choice, $otherstudent->id, $this->course, $cm1);
 204  
 205          // Before deletion, we should have 2 responses.
 206          $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
 207          $this->assertEquals(2, $count);
 208  
 209          $context1 = \context_module::instance($cm1->id);
 210          $context2 = \context_module::instance($cm2->id);
 211          $contextlist = new \core_privacy\local\request\approved_contextlist($this->student, 'choice',
 212              [\context_system::instance()->id, $context1->id, $context2->id]);
 213          provider::delete_data_for_user($contextlist);
 214  
 215          // After deletion, the choice answers for the first student should have been deleted.
 216          $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id, 'userid' => $this->student->id]);
 217          $this->assertEquals(0, $count);
 218  
 219          // Confirm that we only have one choice answer available.
 220          $choiceanswers = $DB->get_records('choice_answers');
 221          $this->assertCount(1, $choiceanswers);
 222          $lastresponse = reset($choiceanswers);
 223          // And that it's the other student's response.
 224          $this->assertEquals($otherstudent->id, $lastresponse->userid);
 225      }
 226  
 227      /**
 228       * Test for provider::get_users_in_context().
 229       */
 230      public function test_get_users_in_context() {
 231          $cm = get_coursemodule_from_instance('choice', $this->choice->id);
 232          $cmcontext = \context_module::instance($cm->id);
 233  
 234          $userlist = new \core_privacy\local\request\userlist($cmcontext, 'mod_choice');
 235          \mod_choice\privacy\provider::get_users_in_context($userlist);
 236  
 237          $this->assertEquals(
 238                  [$this->student->id],
 239                  $userlist->get_userids()
 240          );
 241      }
 242  
 243      /**
 244       * Test for provider::get_users_in_context() with invalid context type.
 245       */
 246      public function test_get_users_in_context_invalid_context_type() {
 247          $systemcontext = \context_system::instance();
 248  
 249          $userlist = new \core_privacy\local\request\userlist($systemcontext, 'mod_choice');
 250          \mod_choice\privacy\provider::get_users_in_context($userlist);
 251  
 252          $this->assertCount(0, $userlist->get_userids());
 253      }
 254  
 255      /**
 256       * Test for provider::delete_data_for_users().
 257       */
 258      public function test_delete_data_for_users() {
 259          global $DB;
 260  
 261          $choice = $this->choice;
 262          $generator = $this->getDataGenerator();
 263          $cm1 = get_coursemodule_from_instance('choice', $this->choice->id);
 264  
 265          // Create a second choice activity.
 266          $options = ['Boracay', 'Camiguin', 'Bohol', 'Cebu', 'Coron'];
 267          $params = [
 268              'course' => $this->course->id,
 269              'option' => $options,
 270              'name' => 'Which do you think is the best island in the Philippines?',
 271              'showpreview' => 0
 272          ];
 273          $plugingenerator = $generator->get_plugin_generator('mod_choice');
 274          $choice2 = $plugingenerator->create_instance($params);
 275          $plugingenerator->create_instance($params);
 276          $cm2 = get_coursemodule_from_instance('choice', $choice2->id);
 277  
 278          // Make a selection for the first student for the 2nd choice activity.
 279          $choicewithoptions = choice_get_choice($choice2->id);
 280          $optionids = array_keys($choicewithoptions->option);
 281          choice_user_submit_response($optionids[2], $choice2, $this->student->id, $this->course, $cm2);
 282  
 283          // Create 2 other students who will answer the first choice activity.
 284          $otherstudent = $generator->create_and_enrol($this->course, 'student');
 285          $anotherstudent = $generator->create_and_enrol($this->course, 'student');
 286  
 287          $choicewithoptions = choice_get_choice($choice->id);
 288          $optionids = array_keys($choicewithoptions->option);
 289  
 290          choice_user_submit_response($optionids[1], $choice, $otherstudent->id, $this->course, $cm1);
 291          choice_user_submit_response($optionids[1], $choice, $anotherstudent->id, $this->course, $cm1);
 292  
 293          // Before deletion, we should have 3 responses in the first choice activity.
 294          $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
 295          $this->assertEquals(3, $count);
 296  
 297          $context1 = \context_module::instance($cm1->id);
 298          $approveduserlist = new \core_privacy\local\request\approved_userlist($context1, 'choice',
 299                  [$this->student->id, $otherstudent->id]);
 300          provider::delete_data_for_users($approveduserlist);
 301  
 302          // After deletion, the choice answers of the 2 students provided above should have been deleted
 303          // from the first choice activity. So there should only remain 1 answer which is for $anotherstudent.
 304          $choiceanswers = $DB->get_records('choice_answers', ['choiceid' => $choice->id]);
 305          $this->assertCount(1, $choiceanswers);
 306          $lastresponse = reset($choiceanswers);
 307          $this->assertEquals($anotherstudent->id, $lastresponse->userid);
 308  
 309          // Confirm that the answer that was submitted in the other choice activity is intact.
 310          $choiceanswers = $DB->get_records_select('choice_answers', 'choiceid <> ?', [$choice->id]);
 311          $this->assertCount(1, $choiceanswers);
 312          $lastresponse = reset($choiceanswers);
 313          // And that it's for the choice2 activity.
 314          $this->assertEquals($choice2->id, $lastresponse->choiceid);
 315      }
 316  }