Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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_multichoice
  21   * @copyright  2021 The Open university
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace qtype_multichoice\privacy;
  25  
  26  use core_privacy\local\metadata\collection;
  27  use core_privacy\local\request\user_preference_provider;
  28  use qtype_multichoice\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/multichoice/classes/privacy/provider.php');
  36  
  37  /**
  38   * Privacy provider tests class.
  39   *
  40   * @package    qtype_multichoice
  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_multichoice');
  49          $actual = \qtype_multichoice\privacy\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_multichoice_$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_multichoice');
  78          foreach ($preferences as $key => $pref) {
  79              $preference = get_user_preferences("qtype_multichoice_{$key}", null, $user->id);
  80              if ($preference === null) {
  81                  continue;
  82              }
  83              $desc = get_string("privacy:preference:{$key}", 'qtype_multichoice');
  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 2' => ['defaultmark', 2, 2],
  97                  'penalty 33.33333%' => ['penalty', 0.3333333, '33.33333%'],
  98                  'single/multiple radio buttons' => ['single', 1, 'One answer only'],
  99                  'single/multiple checkboxes' => ['single', 0, 'Multiple answers allowed'],
 100                  'shuffle yes' => ['shuffleanswers', 1, 'Yes'],
 101                  'shuffle no' => ['shuffleanswers', 0, 'No'],
 102                  'answernumbering abc' => ['answernumbering', 'abc', 'a., b., c., ...'],
 103                  'answernumbering ABC' => ['answernumbering', 'ABCD', 'A., B., C., ...'],
 104                  'answernumbering 123' => ['answernumbering', '123', '1., 2., 3., ...'],
 105                  'answernumbering iii' => ['answernumbering', 'iii', 'i., ii., iii., ...'],
 106                  'answernumbering III' => ['answernumbering', 'IIII', 'I., II., III., ...'],
 107                  'show standard instruction yes' => ['showstandardinstruction', 1, 'Yes'],
 108                  'show standard instruction no' => ['showstandardinstruction', 0, 'No']
 109          ];
 110      }
 111  }