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