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 provider tests.
  19   *
  20   * @package    quiz_responses
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace quiz_responses\privacy;
  25  
  26  use core_privacy\local\metadata\collection;
  27  use quiz_responses\privacy\provider;
  28  use core_privacy\local\request\writer;
  29  use core_privacy\local\request\transform;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  global $CFG;
  34  require_once($CFG->dirroot . '/question/engine/questionattempt.php');
  35  
  36  /**
  37   * Privacy provider tests class.
  38   *
  39   * @package    quiz_responses
  40   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class provider_test extends \core_privacy\tests\provider_testcase {
  44      /**
  45       * When no preference exists, there should be no export.
  46       */
  47      public function test_preference_unset() {
  48          global $USER;
  49  
  50          $this->resetAfterTest();
  51          $this->setAdminUser();
  52  
  53          provider::export_user_preferences($USER->id);
  54  
  55          $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data());
  56      }
  57  
  58      /**
  59       * Preference does exist.
  60       */
  61      public function test_preference_bool_true() {
  62          global $USER;
  63  
  64          $this->resetAfterTest();
  65          $this->setAdminUser();
  66  
  67          set_user_preference('quiz_report_responses_qtext', true);
  68          set_user_preference('quiz_report_responses_resp', true);
  69          set_user_preference('quiz_report_responses_right', true);
  70  
  71          provider::export_user_preferences($USER->id);
  72  
  73          $writer = writer::with_context(\context_system::instance());
  74          $this->assertTrue($writer->has_any_data());
  75  
  76          $preferences = $writer->get_user_preferences('quiz_responses');
  77  
  78          $this->assertNotEmpty($preferences->qtext);
  79          $this->assertEquals(transform::yesno(1), $preferences->qtext->value);
  80  
  81          $this->assertNotEmpty($preferences->resp);
  82          $this->assertEquals(transform::yesno(1), $preferences->resp->value);
  83  
  84          $this->assertNotEmpty($preferences->right);
  85          $this->assertEquals(transform::yesno(1), $preferences->right->value);
  86      }
  87  
  88      /**
  89       * Preference does exist.
  90       */
  91      public function test_preference_bool_false() {
  92          global $USER;
  93  
  94          $this->resetAfterTest();
  95          $this->setAdminUser();
  96  
  97          set_user_preference('quiz_report_responses_qtext', false);
  98          set_user_preference('quiz_report_responses_resp', false);
  99          set_user_preference('quiz_report_responses_right', false);
 100  
 101          provider::export_user_preferences($USER->id);
 102  
 103          $writer = writer::with_context(\context_system::instance());
 104          $this->assertTrue($writer->has_any_data());
 105  
 106          $preferences = $writer->get_user_preferences('quiz_responses');
 107  
 108          $this->assertNotEmpty($preferences->qtext);
 109          $this->assertEquals(transform::yesno(0), $preferences->qtext->value);
 110  
 111          $this->assertNotEmpty($preferences->resp);
 112          $this->assertEquals(transform::yesno(0), $preferences->resp->value);
 113  
 114          $this->assertNotEmpty($preferences->right);
 115          $this->assertEquals(transform::yesno(0), $preferences->right->value);
 116      }
 117  
 118      /**
 119       * Preference does exist.
 120       */
 121      public function test_preference_bool_which_first() {
 122          global $USER;
 123  
 124          $this->resetAfterTest();
 125          $this->setAdminUser();
 126  
 127          set_user_preference('quiz_report_responses_which_tries', \question_attempt::FIRST_TRY);
 128  
 129          provider::export_user_preferences($USER->id);
 130  
 131          $writer = writer::with_context(\context_system::instance());
 132          $this->assertTrue($writer->has_any_data());
 133  
 134          $preferences = $writer->get_user_preferences('quiz_responses');
 135  
 136          $expected = get_string("privacy:preference:which_tries:first", 'quiz_responses');
 137          $this->assertNotEmpty($preferences->which_tries);
 138          $this->assertEquals($expected, $preferences->which_tries->value);
 139      }
 140  }