Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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   * PHPUnit tests for privacy provider.
  19   *
  20   * @package    quizaccess_seb
  21   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  22   * @copyright  2020 Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace quizaccess_seb\privacy;
  27  
  28  use core_privacy\local\request\approved_userlist;
  29  use core_privacy\local\request\userlist;
  30  use core_privacy\local\request\writer;
  31  use core_privacy\tests\request\approved_contextlist;
  32  use core_privacy\tests\provider_testcase;
  33  use quizaccess_seb\privacy\provider;
  34  use quizaccess_seb\seb_quiz_settings;
  35  
  36  defined('MOODLE_INTERNAL') || die();
  37  
  38  require_once (__DIR__ . '/../test_helper_trait.php');
  39  
  40  /**
  41   * PHPUnit tests for privacy provider.
  42   *
  43   * @copyright  2020 Catalyst IT
  44   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class provider_test extends provider_testcase {
  47      use \quizaccess_seb_test_helper_trait;
  48  
  49      /**
  50       * Setup the user, the quiz and ensure that the user is the last user to modify the SEB quiz settings.
  51       */
  52      public function setup_test_data() {
  53          $this->resetAfterTest();
  54  
  55          $this->setAdminUser();
  56  
  57          $this->course = $this->getDataGenerator()->create_course();
  58          $this->quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
  59  
  60          $this->user = $this->getDataGenerator()->create_user();
  61          $this->setUser($this->user);
  62  
  63          $template = $this->create_template();
  64  
  65          $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
  66  
  67          // Modify settings so usermodified is updated. This is the user data we are testing for.
  68          $quizsettings->set('requiresafeexambrowser', \quizaccess_seb\settings_provider::USE_SEB_TEMPLATE);
  69          $quizsettings->set('templateid', $template->get('id'));
  70          $quizsettings->save();
  71  
  72      }
  73  
  74      /**
  75       * Test that the module context for a user who last modified the module is retrieved.
  76       */
  77      public function test_get_contexts_for_userid() {
  78          $this->setup_test_data();
  79  
  80          $contexts = provider::get_contexts_for_userid($this->user->id);
  81          $contextids = $contexts->get_contextids();
  82          $this->assertEquals(\context_module::instance($this->quiz->cmid)->id, reset($contextids));
  83      }
  84  
  85      /**
  86       * That that no module context is found for a user who has not modified any quiz settings.
  87       */
  88      public function test_get_no_contexts_for_userid() {
  89          $this->resetAfterTest();
  90  
  91          $user = $this->getDataGenerator()->create_user();
  92          $contexts = provider::get_contexts_for_userid($user->id);
  93          $contextids = $contexts->get_contextids();
  94          $this->assertEmpty($contextids);
  95      }
  96  
  97      /**
  98       * Test that user data is exported in format expected.
  99       */
 100      public function test_export_user_data() {
 101          $this->setup_test_data();
 102  
 103          $context = \context_module::instance($this->quiz->cmid);
 104  
 105          // Add another course_module of a differenty type - doing this lets us
 106          // test that the data exporter is correctly limiting its selection to
 107          // the quiz and not anything with the same instance id.
 108          // (note this is only effective with databases not using fed (+1000) sequences
 109          // per table, like postgres and mysql do, rendering this useless. In any
 110          // case better to have the situation covered by some DBs,
 111          // like sqlsrv or oracle than by none).
 112          $this->getDataGenerator()->create_module('label', ['course' => $this->course->id]);
 113  
 114          $contextlist = provider::get_contexts_for_userid($this->user->id);
 115          $approvedcontextlist = new approved_contextlist(
 116              $this->user,
 117              'quizaccess_seb',
 118              $contextlist->get_contextids()
 119          );
 120  
 121          writer::reset();
 122          /** @var \core_privacy\tests\request\content_writer $writer */
 123          $writer = writer::with_context($context);
 124          $this->assertFalse($writer->has_any_data());
 125          provider::export_user_data($approvedcontextlist);
 126  
 127          $index = '1'; // Get first data returned from the quizsettings table metadata.
 128          $data = $writer->get_data([
 129              get_string('pluginname', 'quizaccess_seb'),
 130              seb_quiz_settings::TABLE,
 131              $index,
 132          ]);
 133          $this->assertNotEmpty($data);
 134  
 135          $index = '1'; // Get first data returned from the template table metadata.
 136          $data = $writer->get_data([
 137              get_string('pluginname', 'quizaccess_seb'),
 138              \quizaccess_seb\template::TABLE,
 139              $index,
 140          ]);
 141          $this->assertNotEmpty($data);
 142  
 143          $index = '2'; // There should not be more than one instance with data.
 144          $data = $writer->get_data([
 145              get_string('pluginname', 'quizaccess_seb'),
 146              seb_quiz_settings::TABLE,
 147              $index,
 148          ]);
 149          $this->assertEmpty($data);
 150  
 151          $index = '2'; // There should not be more than one instance with data.
 152          $data = $writer->get_data([
 153              get_string('pluginname', 'quizaccess_seb'),
 154              \quizaccess_seb\template::TABLE,
 155              $index,
 156          ]);
 157          $this->assertEmpty($data);
 158      }
 159  
 160      /**
 161       * Test that a userlist with module context is populated by usermodified user.
 162       */
 163      public function test_get_users_in_context() {
 164          $this->setup_test_data();
 165  
 166          // Create empty userlist with quiz module context.
 167          $userlist = new userlist(\context_module::instance($this->quiz->cmid), 'quizaccess_seb');
 168  
 169          // Test that the userlist is populated with expected user/s.
 170          provider::get_users_in_context($userlist);
 171          $this->assertEquals($this->user->id, $userlist->get_userids()[0]);
 172      }
 173  
 174      /**
 175       * Test that data is deleted for a list of users.
 176       */
 177      public function test_delete_data_for_users() {
 178          $this->setup_test_data();
 179  
 180          $approveduserlist = new approved_userlist(\context_module::instance($this->quiz->cmid),
 181                  'quizaccess_seb', [$this->user->id]);
 182  
 183          // Test data exists.
 184          $this->assertNotEmpty(seb_quiz_settings::get_record(['quizid' => $this->quiz->id]));
 185  
 186          // Test data is deleted.
 187          provider::delete_data_for_users($approveduserlist);
 188          $record = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
 189          $this->assertEmpty($record->get('usermodified'));
 190  
 191          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 192          $this->assertEmpty($template->get('usermodified'));
 193      }
 194  
 195      /**
 196       * Test that data is deleted for a list of contexts.
 197       */
 198      public function test_delete_data_for_user() {
 199          $this->setup_test_data();
 200  
 201          $context = \context_module::instance($this->quiz->cmid);
 202          $approvedcontextlist = new approved_contextlist($this->user,
 203                  'quizaccess_seb', [$context->id]);
 204  
 205          // Test data exists.
 206          $this->assertNotEmpty(seb_quiz_settings::get_record(['quizid' => $this->quiz->id]));
 207  
 208          // Test data is deleted.
 209          provider::delete_data_for_user($approvedcontextlist);
 210          $record = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
 211          $this->assertEmpty($record->get('usermodified'));
 212  
 213          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 214          $this->assertEmpty($template->get('usermodified'));
 215      }
 216  
 217      /**
 218       * Test that data is deleted for a single context.
 219       */
 220      public function test_delete_data_for_all_users_in_context() {
 221          $this->setup_test_data();
 222  
 223          $context = \context_module::instance($this->quiz->cmid);
 224  
 225          // Test data exists.
 226          $record = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
 227          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 228          $this->assertNotEmpty($record->get('usermodified'));
 229          $this->assertNotEmpty($template->get('usermodified'));
 230  
 231          // Test data is deleted.
 232          provider::delete_data_for_all_users_in_context($context);
 233  
 234          $record = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
 235          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 236          $this->assertEmpty($record->get('usermodified'));
 237          $this->assertEmpty($template->get('usermodified'));
 238      }
 239  }