Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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 tests for gradingform_rubric
  19   *
  20   * @package    gradingform_rubric
  21   * @category   test
  22   * @copyright  2018 Adrian Greeve <adriangreeve.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace gradingform_rubric\privacy;
  26  
  27  use core_privacy\tests\provider_testcase;
  28  use core_privacy\local\request\writer;
  29  use gradingform_rubric\privacy\provider;
  30  use gradingform_rubric_controller;
  31  use context_module;
  32  
  33  /**
  34   * Privacy tests for gradingform_rubric
  35   *
  36   * @copyright  2018 Adrian Greeve <adriangreeve.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class provider_test extends provider_testcase {
  40  
  41      /**
  42       * Test the export of rubric data.
  43       */
  44      public function test_get_gradingform_export_data() {
  45          global $DB;
  46          $this->resetAfterTest();
  47          $course = $this->getDataGenerator()->create_course();
  48          $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
  49          $modulecontext = context_module::instance($module->cmid);
  50          $user = $this->getDataGenerator()->create_user();
  51          $this->setUser($user);
  52  
  53          // Generate a test rubric and get its controller.
  54          $controller = $this->get_test_rubric($modulecontext, 'assign', 'submissions');
  55  
  56          // In the situation of mod_assign this would be the id from assign_grades.
  57          $itemid = 1;
  58          $instance = $controller->create_instance($user->id, $itemid);
  59  
  60          $data = $this->get_test_form_data(
  61              $controller,
  62              $itemid,
  63              1, 'This user made several mistakes.',
  64              0, 'Please add more pictures.'
  65          );
  66  
  67          // Update this instance with data.
  68          $instance->update($data);
  69          $instanceid = $instance->get_data('id');
  70  
  71          // Let's try the method we are testing.
  72          provider::export_gradingform_instance_data($modulecontext, $instance->get_id(), ['Test']);
  73          $data = (array) writer::with_context($modulecontext)->get_data(['Test', 'Rubric', $instanceid]);
  74          $this->assertCount(2, $data);
  75          $this->assertEquals('Spelling is important', $data['Spelling is important']->description);
  76          $this->assertEquals('This user made several mistakes.', $data['Spelling is important']->remark);
  77          $this->assertEquals('Pictures', $data['Pictures']->description);
  78          $this->assertEquals('Please add more pictures.', $data['Pictures']->remark);
  79      }
  80  
  81      /**
  82       * Test the deletion of rubric user information via the instance ID.
  83       */
  84      public function test_delete_gradingform_for_instances() {
  85          global $DB;
  86          $this->resetAfterTest();
  87          $course = $this->getDataGenerator()->create_course();
  88          $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
  89          $modulecontext = context_module::instance($module->cmid);
  90          $user = $this->getDataGenerator()->create_user();
  91          $this->setUser($user);
  92  
  93          // Generate a test rubric and get its controller.
  94          $controller = $this->get_test_rubric($modulecontext, 'assign', 'submissions');
  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  
 100          $data = $this->get_test_form_data(
 101              $controller,
 102              $itemid,
 103              1, 'This user made several mistakes.',
 104              0, 'Please add more pictures.'
 105          );
 106  
 107          // Update this instance with data.
 108          $instance->update($data);
 109  
 110          // Second instance.
 111          $itemid = 2;
 112          $instance = $controller->create_instance($user->id, $itemid);
 113  
 114          $data = $this->get_test_form_data(
 115              $controller,
 116              $itemid,
 117              0, 'Too many mistakes. Please try again.',
 118              2, 'Great number of pictures. Well done.'
 119          );
 120  
 121          // Update this instance with data.
 122          $instance->update($data);
 123  
 124          // Check how many records we have in the fillings table.
 125          $records = $DB->get_records('gradingform_rubric_fillings');
 126          $this->assertCount(4, $records);
 127          // Let's delete one of the instances (the last one would be the easiest).
 128          provider::delete_gradingform_for_instances([$instance->get_id()]);
 129          $records = $DB->get_records('gradingform_rubric_fillings');
 130          $this->assertCount(2, $records);
 131          foreach ($records as $record) {
 132              $this->assertNotEquals($instance->get_id(), $record->instanceid);
 133          }
 134      }
 135  
 136      /**
 137       * Generate a rubric controller with sample data required for testing of this class.
 138       *
 139       * @param context_module $context
 140       * @param string $component
 141       * @param string $area
 142       * @return gradingform_rubric_controller
 143       */
 144      protected function get_test_rubric(context_module $context, string $component, string $area): gradingform_rubric_controller {
 145          $generator = \testing_util::get_data_generator();
 146          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 147  
 148          return $rubricgenerator->get_test_rubric($context, $component, $area);
 149      }
 150  
 151      /**
 152       * Fetch a set of sample data.
 153       *
 154       * @param gradingform_rubric_controller $controller
 155       * @param int $itemid
 156       * @param float $spellingscore
 157       * @param string $spellingremark
 158       * @param float $picturescore
 159       * @param string $pictureremark
 160       * @return array
 161       */
 162      protected function get_test_form_data(
 163          gradingform_rubric_controller $controller,
 164          int $itemid,
 165          float $spellingscore,
 166          string $spellingremark,
 167          float $picturescore,
 168          string $pictureremark
 169      ): array {
 170          $generator = \testing_util::get_data_generator();
 171          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 172  
 173          return $rubricgenerator->get_test_form_data(
 174              $controller,
 175              $itemid,
 176              $spellingscore,
 177              $spellingremark,
 178              $picturescore,
 179              $pictureremark
 180          );
 181      }
 182  }