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.

Differences Between: [Versions 311 and 402] [Versions 311 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\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 = 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', array('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          $writer = writer::with_context($context);
 123          $this->assertFalse($writer->has_any_data());
 124          provider::export_user_data($approvedcontextlist);
 125  
 126          $index = '1'; // Get first data returned from the quizsettings table metadata.
 127          $data = $writer->get_data([
 128              get_string('pluginname', 'quizaccess_seb'),
 129              quiz_settings::TABLE,
 130              $index,
 131          ]);
 132          $this->assertNotEmpty($data);
 133  
 134          $index = '1'; // Get first data returned from the template table metadata.
 135          $data = $writer->get_data([
 136              get_string('pluginname', 'quizaccess_seb'),
 137              \quizaccess_seb\template::TABLE,
 138              $index,
 139          ]);
 140          $this->assertNotEmpty($data);
 141  
 142          $index = '2'; // There should not be more than one instance with data.
 143          $data = $writer->get_data([
 144              get_string('pluginname', 'quizaccess_seb'),
 145              quiz_settings::TABLE,
 146              $index,
 147          ]);
 148          $this->assertEmpty($data);
 149  
 150          $index = '2'; // There should not be more than one instance with data.
 151          $data = $writer->get_data([
 152              get_string('pluginname', 'quizaccess_seb'),
 153              \quizaccess_seb\template::TABLE,
 154              $index,
 155          ]);
 156          $this->assertEmpty($data);
 157      }
 158  
 159      /**
 160       * Test that a userlist with module context is populated by usermodified user.
 161       */
 162      public function test_get_users_in_context() {
 163          $this->setup_test_data();
 164  
 165          // Create empty userlist with quiz module context.
 166          $userlist = new userlist(\context_module::instance($this->quiz->cmid), 'quizaccess_seb');
 167  
 168          // Test that the userlist is populated with expected user/s.
 169          provider::get_users_in_context($userlist);
 170          $this->assertEquals($this->user->id, $userlist->get_userids()[0]);
 171      }
 172  
 173      /**
 174       * Test that data is deleted for a list of users.
 175       */
 176      public function test_delete_data_for_users() {
 177          $this->setup_test_data();
 178  
 179          $approveduserlist = new approved_userlist(\context_module::instance($this->quiz->cmid),
 180                  'quizaccess_seb', [$this->user->id]);
 181  
 182          // Test data exists.
 183          $this->assertNotEmpty(quiz_settings::get_record(['quizid' => $this->quiz->id]));
 184  
 185          // Test data is deleted.
 186          provider::delete_data_for_users($approveduserlist);
 187          $record = quiz_settings::get_record(['quizid' => $this->quiz->id]);
 188          $this->assertEmpty($record->get('usermodified'));
 189  
 190          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 191          $this->assertEmpty($template->get('usermodified'));
 192      }
 193  
 194      /**
 195       * Test that data is deleted for a list of contexts.
 196       */
 197      public function test_delete_data_for_user() {
 198          $this->setup_test_data();
 199  
 200          $context = \context_module::instance($this->quiz->cmid);
 201          $approvedcontextlist = new approved_contextlist($this->user,
 202                  'quizaccess_seb', [$context->id]);
 203  
 204          // Test data exists.
 205          $this->assertNotEmpty(quiz_settings::get_record(['quizid' => $this->quiz->id]));
 206  
 207          // Test data is deleted.
 208          provider::delete_data_for_user($approvedcontextlist);
 209          $record = quiz_settings::get_record(['quizid' => $this->quiz->id]);
 210          $this->assertEmpty($record->get('usermodified'));
 211  
 212          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 213          $this->assertEmpty($template->get('usermodified'));
 214      }
 215  
 216      /**
 217       * Test that data is deleted for a single context.
 218       */
 219      public function test_delete_data_for_all_users_in_context() {
 220          $this->setup_test_data();
 221  
 222          $context = \context_module::instance($this->quiz->cmid);
 223  
 224          // Test data exists.
 225          $record = quiz_settings::get_record(['quizid' => $this->quiz->id]);
 226          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 227          $this->assertNotEmpty($record->get('usermodified'));
 228          $this->assertNotEmpty($template->get('usermodified'));
 229  
 230          // Test data is deleted.
 231          provider::delete_data_for_all_users_in_context($context);
 232  
 233          $record = quiz_settings::get_record(['quizid' => $this->quiz->id]);
 234          $template = \quizaccess_seb\template::get_record(['id' => $record->get('templateid')]);
 235          $this->assertEmpty($record->get('usermodified'));
 236          $this->assertEmpty($template->get('usermodified'));
 237      }
 238  }