Search moodle.org's
Developer Documentation

See Release Notes

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