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