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    quiz_overview
  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_overview\privacy;
  25  
  26  use core_privacy\local\metadata\collection;
  27  use quiz_overview\privacy\provider;
  28  use core_privacy\local\request\writer;
  29  use core_privacy\local\request\transform;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Privacy provider tests class.
  35   *
  36   * @package    quiz_overview
  37   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class provider_test extends \core_privacy\tests\provider_testcase {
  41      /**
  42       * When no preference exists, there should be no export.
  43       */
  44      public function test_preference_unset() {
  45          global $USER;
  46  
  47          $this->resetAfterTest();
  48          $this->setAdminUser();
  49  
  50          provider::export_user_preferences($USER->id);
  51  
  52          $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data());
  53      }
  54  
  55      /**
  56       * Preference does exist.
  57       */
  58      public function test_preference_yes() {
  59          $this->resetAfterTest();
  60  
  61          // Create test user, add some preferences.
  62          $user = $this->getDataGenerator()->create_user();
  63          $this->setUser($user);
  64  
  65          set_user_preference('quiz_overview_slotmarks', 1, $user);
  66  
  67          // Switch to admin user (so we can validate preferences of the correct user are being exported).
  68          $this->setAdminUser();
  69  
  70          // Export test users preferences.
  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_overview');
  77          $this->assertNotEmpty($preferences->slotmarks);
  78          $this->assertEquals(transform::yesno(1), $preferences->slotmarks->value);
  79          $description = get_string('privacy:preference:slotmarks:yes', 'quiz_overview');
  80          $this->assertEquals($description, $preferences->slotmarks->description);
  81      }
  82  
  83      /**
  84       * Preference does exist and is no.
  85       */
  86      public function test_preference_no() {
  87          $this->resetAfterTest();
  88  
  89          // Create test user, add some preferences.
  90          $user = $this->getDataGenerator()->create_user();
  91          $this->setUser($user);
  92  
  93          set_user_preference('quiz_overview_slotmarks', 0);
  94  
  95          // Switch to admin user (so we can validate preferences of the correct user are being exported).
  96          $this->setAdminUser();
  97  
  98          // Export test users preferences.
  99          provider::export_user_preferences($user->id);
 100  
 101          $writer = writer::with_context(\context_system::instance());
 102          $this->assertTrue($writer->has_any_data());
 103  
 104          $preferences = $writer->get_user_preferences('quiz_overview');
 105          $this->assertNotEmpty($preferences->slotmarks);
 106          $this->assertEquals(transform::yesno(0), $preferences->slotmarks->value);
 107          $description = get_string('privacy:preference:slotmarks:no', 'quiz_overview');
 108          $this->assertEquals($description, $preferences->slotmarks->description);
 109      }
 110  }