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    qtype_numerical
  21   * @copyright  2021 The Open university
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace qtype_numerical\privacy;
  25  
  26  use core_privacy\local\metadata\collection;
  27  use core_privacy\local\request\user_preference_provider;
  28  use qtype_numerical\privacy\provider;
  29  use core_privacy\local\request\writer;
  30  use core_privacy\local\request\transform;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  global $CFG;
  35  require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
  36  
  37  /**
  38   * Privacy provider tests class.
  39   *
  40   * @package    qtype_numerical
  41   * @copyright  2021 The Open university
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class provider_test extends \core_privacy\tests\provider_testcase {
  45      // Include the privacy helper which has assertions on it.
  46  
  47      public function test_get_metadata() {
  48          $collection = new \core_privacy\local\metadata\collection('qtype_numerical');
  49          $actual = provider::get_metadata($collection);
  50          $this->assertEquals($collection, $actual);
  51      }
  52  
  53      public function test_export_user_preferences_no_pref() {
  54          $this->resetAfterTest();
  55  
  56          $user = $this->getDataGenerator()->create_user();
  57          provider::export_user_preferences($user->id);
  58          $writer = writer::with_context(\context_system::instance());
  59          $this->assertFalse($writer->has_any_data());
  60      }
  61  
  62      /**
  63       * Test the export_user_preferences given different inputs
  64       * @dataProvider user_preference_provider
  65  
  66       * @param string $name The name of the user preference to get/set
  67       * @param string $value The value stored in the database
  68       * @param string $expected The expected transformed value
  69       */
  70      public function test_export_user_preferences($name, $value, $expected) {
  71          $this->resetAfterTest();
  72          $user = $this->getDataGenerator()->create_user();
  73          set_user_preference("qtype_numerical_$name", $value, $user);
  74          provider::export_user_preferences($user->id);
  75          $writer = writer::with_context(\context_system::instance());
  76          $this->assertTrue($writer->has_any_data());
  77          $preferences = $writer->get_user_preferences('qtype_numerical');
  78          foreach ($preferences as $key => $pref) {
  79              $preference = get_user_preferences("qtype_numerical_{$key}", null, $user->id);
  80              if ($preference === null) {
  81                  continue;
  82              }
  83              $desc = get_string("privacy:preference:{$key}", 'qtype_numerical');
  84              $this->assertEquals($expected, $pref->value);
  85              $this->assertEquals($desc, $pref->description);
  86          }
  87      }
  88  
  89      /**
  90       * Create an array of valid user preferences for the multiple choice question type.
  91       *
  92       * @return array Array of valid user preferences.
  93       */
  94      public function user_preference_provider() {
  95          return [
  96                  'default mark 1.5' => ['defaultmark', 1.5, 1.5],
  97                  'penalty 20%' => ['penalty', 0.2000000, '20%'],
  98                  'unitrole only numerical' => ['unitrole', \qtype_numerical::UNITNONE,
  99                          get_string('onlynumerical', 'qtype_numerical')],
 100                  'unitrole many numerical' => ['unitrole', \qtype_numerical::UNITOPTIONAL,
 101                          get_string('manynumerical', 'qtype_numerical')],
 102                  'unitrole unit graded' => ['unitrole', \qtype_numerical::UNITGRADED,
 103                          get_string('unitgraded', 'qtype_numerical')],
 104                  'unit penalty 0' => ['unitpenalty', 0.01, 0.01],
 105                  'unit grading types response grade' => ['unitgradingtypes', \qtype_numerical::UNITGRADEDOUTOFMARK,
 106                          get_string('decfractionofresponsegrade', 'qtype_numerical')],
 107                  'unit grading types question grade' => ['unitgradingtypes', \qtype_numerical::UNITGRADEDOUTOFMAX,
 108                          get_string('decfractionofquestiongrade', 'qtype_numerical')],
 109                  'multichoice display editable unit text' => ['multichoicedisplay', \qtype_numerical::UNITINPUT,
 110                          get_string('editableunittext', 'qtype_numerical')],
 111                  'multichoice display radio buttons' => ['multichoicedisplay', \qtype_numerical::UNITRADIO,
 112                          get_string('unitchoice', 'qtype_numerical')],
 113                  'multichoice display select menu' => ['multichoicedisplay', \qtype_numerical::UNITSELECT,
 114                          get_string('unitselect', 'qtype_numerical')],
 115                  'unitsleft left example' => ['unitsleft', '1', get_string('leftexample', 'qtype_numerical')],
 116                  'unitsleft left example' => ['unitsleft', '0', get_string('rightexample', 'qtype_numerical')]
 117          ];
 118      }
 119  }