Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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_data
  21   * @copyright  2018 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace mod_data\privacy;
  25  
  26  use core_privacy\local\metadata\collection;
  27  use core_privacy\local\request\approved_userlist;
  28  use mod_data\privacy\provider;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Privacy provider tests class.
  34   *
  35   * @package    mod_data
  36   * @copyright  2018 Marina Glancy
  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      /** @var stdClass The student object. */
  43      protected $student2;
  44      /** @var stdClass The student object. */
  45      protected $student3;
  46  
  47      /** @var stdClass The data object. */
  48      protected $datamodule;
  49  
  50      /** @var stdClass The course object. */
  51      protected $course;
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      protected function setUp(): void {
  57          $this->resetAfterTest();
  58  
  59          global $DB;
  60          $generator = $this->getDataGenerator();
  61          $course = $generator->create_course();
  62          $params = [
  63              'course' => $course->id,
  64              'name' => 'Database module',
  65              'comments' => 1,
  66              'assessed' => 1,
  67          ];
  68  
  69          // The database activity.
  70          $datamodule = $this->get_generator()->create_instance($params);
  71  
  72          $fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url',
  73              'latlong', 'file', 'picture');
  74          // Creating test Fields with default parameter values.
  75          foreach ($fieldtypes as $count => $fieldtype) {
  76              // Creating variables dynamically.
  77              $fieldname = 'field' . $count;
  78              $record = new \stdClass();
  79              $record->name = $fieldname;
  80              $record->description = $fieldname . ' descr';
  81              $record->type = $fieldtype;
  82  
  83              ${$fieldname} = $this->get_generator()->create_field($record, $datamodule);
  84          }
  85  
  86          $cm = get_coursemodule_from_instance('data', $datamodule->id);
  87  
  88          // Create a student.
  89          $student1 = $generator->create_user();
  90          $student2 = $generator->create_user();
  91          $student3 = $generator->create_user();
  92          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  93          $generator->enrol_user($student1->id,  $course->id, $studentrole->id);
  94          $generator->enrol_user($student2->id,  $course->id, $studentrole->id);
  95          $generator->enrol_user($student3->id,  $course->id, $studentrole->id);
  96  
  97          // Add records.
  98          $this->setUser($student1);
  99          $record1id = $this->generate_data_record($datamodule);
 100          $this->generate_data_record($datamodule);
 101  
 102          $this->setUser($student2);
 103          $this->generate_data_record($datamodule);
 104          $this->generate_data_record($datamodule);
 105          $this->generate_data_record($datamodule);
 106  
 107          $this->setUser($student3);
 108          $this->generate_data_record($datamodule);
 109  
 110          $this->student = $student1;
 111          $this->student2 = $student2;
 112          $this->student3 = $student3;
 113          $this->datamodule = $datamodule;
 114          $this->course = $course;
 115      }
 116  
 117      /**
 118       * Get mod_data generator
 119       *
 120       * @return mod_data_generator
 121       */
 122      protected function get_generator() {
 123          return $this->getDataGenerator()->get_plugin_generator('mod_data');
 124      }
 125  
 126      /**
 127       * Generates one record in the database module as the current student
 128       *
 129       * @param stdClass $datamodule
 130       * @return mixed
 131       */
 132      protected function generate_data_record($datamodule) {
 133          global $DB;
 134  
 135          static $counter = 0;
 136          $counter++;
 137  
 138          $contents = array();
 139          $contents[] = array('opt1', 'opt2', 'opt3', 'opt4');
 140          $contents[] = sprintf("%02f", $counter) . '-01-2000';
 141          $contents[] = 'menu1';
 142          $contents[] = array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4');
 143          $contents[] = 5 * $counter;
 144          $contents[] = 'radioopt1';
 145          $contents[] = 'text for testing' . $counter;
 146          $contents[] = "<p>text area testing $counter<br /></p>";
 147          $contents[] = array('example.url', 'sampleurl' . $counter);
 148          $contents[] = [-31.9489873, 115.8382036]; // Latlong.
 149          $contents[] = "Filename{$counter}.pdf"; // File - filename.
 150          $contents[] = array("Cat{$counter}.jpg", 'Cat' . $counter); // Picture - filename with alt text.
 151          $count = 0;
 152          $fieldcontents = array();
 153          $fields = $DB->get_records('data_fields', array('dataid' => $datamodule->id), 'id');
 154          foreach ($fields as $fieldrecord) {
 155              $fieldcontents[$fieldrecord->id] = $contents[$count++];
 156          }
 157          $tags = ['Cats', 'mice' . $counter];
 158          return $this->get_generator()->create_entry($datamodule, $fieldcontents, 0, $tags);
 159      }
 160  
 161      /**
 162       * Test for provider::get_metadata().
 163       */
 164      public function test_get_metadata() {
 165          $collection = new collection('mod_data');
 166          $newcollection = provider::get_metadata($collection);
 167          $itemcollection = $newcollection->get_collection();
 168          $this->assertCount(7, $itemcollection);
 169  
 170          $table = reset($itemcollection);
 171          $this->assertEquals('data_records', $table->get_name());
 172  
 173          $table = next($itemcollection);
 174          $this->assertEquals('data_content', $table->get_name());
 175      }
 176  
 177      /**
 178       * Test for provider::get_contexts_for_userid().
 179       */
 180      public function test_get_contexts_for_userid() {
 181          $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
 182  
 183          $contextlist = provider::get_contexts_for_userid($this->student->id);
 184          $this->assertCount(1, $contextlist);
 185          $contextforuser = $contextlist->current();
 186          $cmcontext = \context_module::instance($cm->id);
 187          $this->assertEquals($cmcontext->id, $contextforuser->id);
 188      }
 189  
 190      /**
 191       * Test for provider::get_users_in_context().
 192       */
 193      public function test_get_users_in_context() {
 194          $component = 'mod_data';
 195          $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
 196          $cmcontext = \context_module::instance($cm->id);
 197  
 198          $userlist = new \core_privacy\local\request\userlist($cmcontext, $component);
 199          provider::get_users_in_context($userlist);
 200  
 201          $this->assertCount(3, $userlist);
 202  
 203          $expected = [$this->student->id, $this->student2->id, $this->student3->id];
 204          $actual = $userlist->get_userids();
 205          sort($expected);
 206          sort($actual);
 207  
 208          $this->assertEquals($expected, $actual);
 209      }
 210  
 211      /**
 212       * Get test privacy writer
 213       *
 214       * @param context $context
 215       * @return \core_privacy\tests\request\content_writer
 216       */
 217      protected function get_writer($context) {
 218          return \core_privacy\local\request\writer::with_context($context);
 219      }
 220  
 221      /**
 222       * Test for provider::export_user_data().
 223       */
 224      public function test_export_for_context() {
 225          global $DB;
 226          $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
 227          $cmcontext = \context_module::instance($cm->id);
 228          $records = $DB->get_records_select('data_records', 'userid = :userid ORDER BY id', ['userid' => $this->student->id]);
 229          $record = reset($records);
 230          $contents = $DB->get_records('data_content', ['recordid' => $record->id]);
 231  
 232          // Export all of the data for the context.
 233          $this->export_context_data_for_user($this->student->id, $cmcontext, 'mod_data');
 234          $writer = $this->get_writer($cmcontext);
 235          $data = $writer->get_data([$record->id]);
 236          $this->assertNotEmpty($data);
 237          foreach ($contents as $content) {
 238              $data = $writer->get_data([$record->id, $content->id]);
 239              $this->assertNotEmpty($data);
 240              $hasfile = in_array($data->field['type'], ['file', 'picture']);
 241              $this->assertEquals($hasfile, !empty($writer->get_files([$record->id, $content->id])));
 242          }
 243          $tags = $writer->get_related_data([$record->id], 'tags');
 244          $this->assertNotEmpty($tags);
 245      }
 246  
 247      /**
 248       * Test for provider::delete_data_for_all_users_in_context().
 249       */
 250      public function test_delete_data_for_all_users_in_context() {
 251          $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
 252          $cmcontext = \context_module::instance($cm->id);
 253  
 254          provider::delete_data_for_all_users_in_context($cmcontext);
 255  
 256          $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
 257          provider::export_user_data($appctxt);
 258          $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
 259      }
 260  
 261      /**
 262       * Test for provider::delete_data_for_user().
 263       */
 264      public function test_delete_data_for_user() {
 265          $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
 266          $cmcontext = \context_module::instance($cm->id);
 267  
 268          $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
 269          provider::delete_data_for_user($appctxt);
 270  
 271          provider::export_user_data($appctxt);
 272          $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
 273      }
 274  
 275      /**
 276       * Test for provider::delete_data_for_users().
 277       */
 278      public function test_delete_data_for_users() {
 279          $cm = get_coursemodule_from_instance('data', $this->datamodule->id);
 280          $cmcontext = \context_module::instance($cm->id);
 281          $userstodelete = [$this->student->id, $this->student2->id];
 282  
 283          // Ensure student, student 2 and student 3 have data before being deleted.
 284          $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
 285          provider::export_user_data($appctxt);
 286          $this->assertTrue($this->get_writer($cmcontext)->has_any_data());
 287  
 288          $appctxt = new \core_privacy\local\request\approved_contextlist($this->student2, 'mod_data', [$cmcontext->id]);
 289          provider::export_user_data($appctxt);
 290          $this->assertTrue($this->get_writer($cmcontext)->has_any_data());
 291  
 292          // Delete data for student 1 and 2.
 293          $approvedlist = new approved_userlist($cmcontext, 'mod_data', $userstodelete);
 294          provider::delete_data_for_users($approvedlist);
 295  
 296          // Reset the writer so it doesn't contain the data from before deletion.
 297          \core_privacy\local\request\writer::reset();
 298  
 299          // Ensure data is now deleted for student and student 2.
 300          $appctxt = new \core_privacy\local\request\approved_contextlist($this->student, 'mod_data', [$cmcontext->id]);
 301          provider::export_user_data($appctxt);
 302  
 303          $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
 304  
 305          $appctxt = new \core_privacy\local\request\approved_contextlist($this->student2, 'mod_data', [$cmcontext->id]);
 306          provider::export_user_data($appctxt);
 307  
 308          $this->assertFalse($this->get_writer($cmcontext)->has_any_data());
 309  
 310          // Ensure data still intact for student 3.
 311          $appctxt = new \core_privacy\local\request\approved_contextlist($this->student3, 'mod_data', [$cmcontext->id]);
 312          provider::export_user_data($appctxt);
 313  
 314          $this->assertTrue($this->get_writer($cmcontext)->has_any_data());
 315      }
 316  }