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.
   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 tests for gradingform_guide.
  19   *
  20   * @package    gradingform_guide
  21   * @category   test
  22   * @copyright  2018 Sara Arjona <sara@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  use \core_privacy\tests\provider_testcase;
  31  use \core_privacy\local\request\writer;
  32  use \gradingform_guide\privacy\provider;
  33  
  34  /**
  35   * Privacy tests for gradingform_guide.
  36   *
  37   * @package    gradingform_guide
  38   * @copyright  2018 Sara Arjona <sara@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class gradingform_guide_privacy_testcase extends provider_testcase {
  42  
  43      /**
  44       * Ensure that export_user_preferences returns no data if the user has no data.
  45       */
  46      public function test_export_user_preferences_not_defined() {
  47          $user = \core_user::get_user_by_username('admin');
  48          provider::export_user_preferences($user->id);
  49  
  50          $writer = writer::with_context(\context_system::instance());
  51          $this->assertFalse($writer->has_any_data());
  52      }
  53  
  54      /**
  55       * Ensure that export_user_preferences returns single preferences.
  56       */
  57      public function test_export_user_preferences() {
  58          $this->resetAfterTest();
  59  
  60          // Define a user preference.
  61          $user = $this->getDataGenerator()->create_user();
  62          $this->setUser($user);
  63          set_user_preference('gradingform_guide-showmarkerdesc', 0, $user);
  64          set_user_preference('gradingform_guide-showstudentdesc', 1, $user);
  65  
  66          // Validate exported data.
  67          provider::export_user_preferences($user->id);
  68          $context = context_user::instance($user->id);
  69          $writer = writer::with_context($context);
  70          $this->assertTrue($writer->has_any_data());
  71          $prefs = $writer->get_user_preferences('gradingform_guide');
  72          $this->assertCount(2, (array) $prefs);
  73          $this->assertEquals(
  74              get_string('privacy:metadata:preference:showstudentdesc', 'gradingform_guide'),
  75              $prefs->{'gradingform_guide-showstudentdesc'}->description
  76          );
  77          $this->assertEquals(get_string('no'), $prefs->{'gradingform_guide-showmarkerdesc'}->value);
  78          $this->assertEquals(get_string('yes'), $prefs->{'gradingform_guide-showstudentdesc'}->value);
  79      }
  80  
  81      /**
  82       * Test the export of guide data.
  83       */
  84      public function test_get_gradingform_export_data() {
  85          global $DB;
  86          $this->resetAfterTest();
  87          $course = $this->getDataGenerator()->create_course();
  88          $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
  89          $user = $this->getDataGenerator()->create_user();
  90  
  91          $this->setUser($user);
  92  
  93          $modulecontext = context_module::instance($module->cmid);
  94          $controller = $this->get_test_guide($modulecontext);
  95  
  96          // In the situation of mod_assign this would be the id from assign_grades.
  97          $itemid = 1;
  98          $instance = $controller->create_instance($user->id, $itemid);
  99          $data = $this->get_test_form_data(
 100              $controller,
 101              $itemid,
 102              5, 'This user made several mistakes.',
 103              10, 'This user has two pictures.'
 104          );
 105  
 106          $instance->update($data);
 107          $instanceid = $instance->get_data('id');
 108  
 109          // Let's try the method we are testing.
 110          provider::export_gradingform_instance_data($modulecontext, $instance->get_id(), ['Test']);
 111          $data = (array) writer::with_context($modulecontext)->get_data(['Test', 'Marking guide', $instanceid]);
 112          $this->assertCount(2, $data);
 113          $this->assertEquals('Spelling mistakes', $data['Spelling mistakes']->shortname);
 114          $this->assertEquals('This user made several mistakes.', $data['Spelling mistakes']->remark);
 115          $this->assertEquals('Pictures', $data['Pictures']->shortname);
 116          $this->assertEquals('This user has two pictures.', $data['Pictures']->remark);
 117      }
 118  
 119      /**
 120       * Test the deletion of guide user information via the instance ID.
 121       */
 122      public function test_delete_gradingform_for_instances() {
 123          global $DB;
 124          $this->resetAfterTest();
 125          $course = $this->getDataGenerator()->create_course();
 126          $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
 127          $user = $this->getDataGenerator()->create_user();
 128  
 129          $this->setUser($user);
 130  
 131          $modulecontext = context_module::instance($module->cmid);
 132          $controller = $this->get_test_guide($modulecontext);
 133  
 134          // In the situation of mod_assign this would be the id from assign_grades.
 135          $itemid = 1;
 136          $instance = $controller->create_instance($user->id, $itemid);
 137          $data = $this->get_test_form_data(
 138              $controller,
 139              $itemid,
 140              5, 'This user made several mistakes.',
 141              10, 'This user has two pictures.'
 142          );
 143  
 144          $instance->update($data);
 145          $instanceid = $instance->get_data('id');
 146  
 147          $itemid = 2;
 148          $instance = $controller->create_instance($user->id, $itemid);
 149          $data = $this->get_test_form_data(
 150              $controller,
 151              $itemid,
 152              25, 'This user made no mistakes.',
 153              5, 'This user has one pictures.'
 154          );
 155  
 156          $instance->update($data);
 157          $instanceid = $instance->get_data('id');
 158  
 159          // Check how many records we have in the fillings table.
 160          $records = $DB->get_records('gradingform_guide_fillings');
 161          $this->assertCount(4, $records);
 162          // Let's delete one of the instances (the last one would be the easiest).
 163          provider::delete_gradingform_for_instances([$instance->get_id()]);
 164          $records = $DB->get_records('gradingform_guide_fillings');
 165          $this->assertCount(2, $records);
 166          foreach ($records as $record) {
 167              $this->assertNotEquals($instance->get_id(), $record->instanceid);
 168          }
 169      }
 170  
 171      /**
 172       * Generate a guide controller with sample data required for testing of this class.
 173       *
 174       * @param context_module $context
 175       * @return gradingform_guide_controller
 176       */
 177      protected function get_test_guide(context_module $context): gradingform_guide_controller {
 178          $generator = \testing_util::get_data_generator();
 179          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 180  
 181          return $guidegenerator->get_test_guide($context);
 182      }
 183  
 184      /**
 185       * Fetch a set of sample data.
 186       *
 187       * @param gradingform_guide_controller $controller
 188       * @param int $itemid
 189       * @param float $spellingscore
 190       * @param string $spellingremark
 191       * @param float $picturescore
 192       * @param string $pictureremark
 193       * @return array
 194       */
 195      protected function get_test_form_data(
 196          gradingform_guide_controller $controller,
 197          int $itemid,
 198          float $spellingscore,
 199          string $spellingremark,
 200          float $picturescore,
 201          string $pictureremark
 202      ): array {
 203          $generator = \testing_util::get_data_generator();
 204          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 205  
 206          return $guidegenerator->get_test_form_data(
 207              $controller,
 208              $itemid,
 209              $spellingscore,
 210              $spellingremark,
 211              $picturescore,
 212              $pictureremark
 213          );
 214      }
 215  }