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  namespace core_adminpresets\privacy;
  18  
  19  use context_system;
  20  use context_user;
  21  use core_privacy\local\metadata\collection;
  22  use core_privacy\tests\provider_testcase;
  23  
  24  /**
  25   * Tests for the privacy provider class.
  26   *
  27   * @package    core_adminpresets
  28   * @category   test
  29   * @copyright  2021 Sara Arjona (sara@moodle.com)
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   * @coversDefaultClass \core_adminpresets\privacy\provider
  32   */
  33  class privacy_provider_test extends provider_testcase {
  34  
  35      /**
  36       * Test for provider::get_metadata().
  37       * @covers ::get_metadata
  38       */
  39      public function test_get_metadata() {
  40          $collection = new collection('core_adminpresets');
  41          $newcollection = provider::get_metadata($collection);
  42          $itemcollection = $newcollection->get_collection();
  43          $this->assertCount(2, $itemcollection);
  44  
  45          // The expected metadata fields are covered by test_metadata_provider() in privacy/tests/provider_test.php.
  46      }
  47  
  48      /**
  49       * Test for provider::get_contexts_for_userid() doesn't return any context.
  50       * @covers ::get_contexts_for_userid
  51       */
  52      public function test_get_contexts_for_userid() {
  53          global $USER;
  54  
  55          $this->resetAfterTest();
  56          $this->setAdminUser();
  57  
  58          // Create a preset.
  59          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  60          $generator->create_preset();
  61  
  62          $contextlist = provider::get_contexts_for_userid($USER->id);
  63          $this->assertEmpty($contextlist);
  64      }
  65  
  66      /**
  67       * Test for provider::get_users_in_context() doesn't return any user.
  68       * @covers ::get_users_in_context
  69       */
  70      public function test_get_users_in_context() {
  71          global $USER;
  72  
  73          $this->resetAfterTest();
  74          $this->setAdminUser();
  75  
  76          // Create a preset.
  77          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  78          $generator->create_preset();
  79  
  80          $usercontext = context_user::instance($USER->id);
  81          $userlist = new \core_privacy\local\request\userlist($usercontext, 'core_adminpresets');
  82          \core_message\privacy\provider::get_users_in_context($userlist);
  83          $this->assertEmpty($userlist->get_userids());
  84      }
  85  
  86  
  87      /**
  88       * Test for provider::export_user_data().
  89       * @covers ::export_user_data
  90       */
  91      public function test_export_user_data() {
  92          global $USER;
  93  
  94          $this->resetAfterTest();
  95          $this->setAdminUser();
  96  
  97          // Create a preset.
  98          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  99          $generator->create_preset();
 100  
 101          // Check data is not exported in user context.
 102          $usercontext = context_user::instance($USER->id);
 103          $this->export_context_data_for_user($USER->id, $usercontext, 'core_adminpresets');
 104          $writer = \core_privacy\local\request\writer::with_context($usercontext);
 105  
 106          $this->assertEmpty($writer->get_data([get_string('siteadminpresetspluginname', 'core_adminpresets')]));
 107          $this->assertEmpty($writer->get_all_metadata([]));
 108          $this->assertEmpty($writer->get_files([]));
 109  
 110          // Check data is not exported in system context either.
 111          $systemcontext = context_system::instance();
 112          $this->export_context_data_for_user($USER->id, $systemcontext, 'core_adminpresets');
 113          $writer = \core_privacy\local\request\writer::with_context($systemcontext);
 114  
 115          $this->assertEmpty($writer->get_data([get_string('siteadminpresetspluginname', 'core_adminpresets')]));
 116          $this->assertEmpty($writer->get_all_metadata([]));
 117          $this->assertEmpty($writer->get_files([]));
 118      }
 119  
 120      /**
 121       * Test for provider::delete_data_for_all_users_in_context().
 122       * @covers ::delete_data_for_all_users_in_context
 123       */
 124      public function test_delete_data_for_all_users_in_context() {
 125          global $DB, $USER;
 126  
 127          $this->resetAfterTest();
 128          $this->setAdminUser();
 129  
 130          $currentpresets = $DB->count_records('adminpresets');
 131  
 132          // Create a preset.
 133          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
 134          $generator->create_preset();
 135          $this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
 136  
 137          $usercontext = context_user::instance($USER->id);
 138  
 139          provider::delete_data_for_all_users_in_context($usercontext);
 140  
 141          // Confirm the presets haven't been removed.
 142          $this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
 143      }
 144  
 145      /**
 146       * Test for provider::delete_data_for_user().
 147       * @covers ::delete_data_for_user
 148       */
 149      public function test_delete_data_for_user() {
 150          global $DB, $USER;
 151  
 152          $this->resetAfterTest();
 153          $this->setAdminUser();
 154  
 155          $currentpresets = $DB->count_records('adminpresets');
 156  
 157          // Create a preset.
 158          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
 159          $generator->create_preset();
 160          $this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
 161  
 162          $usercontext = context_user::instance($USER->id);
 163          $contextlist = new \core_privacy\local\request\approved_contextlist($USER, 'core_adminpresets', [$usercontext->id]);
 164          provider::delete_data_for_user($contextlist);
 165  
 166          // Confirm the presets haven't been removed.
 167          $this->assertEquals($currentpresets + 1, $DB->count_records('adminpresets'));
 168      }
 169  
 170  }