Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

Differences Between: [Versions 39 and 310]

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