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 * Privacy tests for gradingform_guide. 19 * 20 * @package gradingform_guide 21 * @category test 22 * @copyright 2018 Sara Arjona <sara@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 namespace gradingform_guide\privacy; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 use core_privacy\tests\provider_testcase; 32 use core_privacy\local\request\writer; 33 use gradingform_guide\privacy\provider; 34 35 /** 36 * Privacy tests for gradingform_guide. 37 * 38 * @package gradingform_guide 39 * @copyright 2018 Sara Arjona <sara@moodle.com> 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class provider_test extends provider_testcase { 43 44 /** 45 * Ensure that export_user_preferences returns no data if the user has no data. 46 */ 47 public function test_export_user_preferences_not_defined() { 48 $user = \core_user::get_user_by_username('admin'); 49 provider::export_user_preferences($user->id); 50 51 $writer = writer::with_context(\context_system::instance()); 52 $this->assertFalse($writer->has_any_data()); 53 } 54 55 /** 56 * Ensure that export_user_preferences returns single preferences. 57 */ 58 public function test_export_user_preferences() { 59 $this->resetAfterTest(); 60 61 // Define a user preference. 62 $user = $this->getDataGenerator()->create_user(); 63 $this->setUser($user); 64 set_user_preference('gradingform_guide-showmarkerdesc', 0, $user); 65 set_user_preference('gradingform_guide-showstudentdesc', 1, $user); 66 67 // Validate exported data. 68 provider::export_user_preferences($user->id); 69 $context = \context_user::instance($user->id); 70 $writer = writer::with_context($context); 71 $this->assertTrue($writer->has_any_data()); 72 $prefs = $writer->get_user_preferences('gradingform_guide'); 73 $this->assertCount(2, (array) $prefs); 74 $this->assertEquals( 75 get_string('privacy:metadata:preference:showstudentdesc', 'gradingform_guide'), 76 $prefs->{'gradingform_guide-showstudentdesc'}->description 77 ); 78 $this->assertEquals(get_string('no'), $prefs->{'gradingform_guide-showmarkerdesc'}->value); 79 $this->assertEquals(get_string('yes'), $prefs->{'gradingform_guide-showstudentdesc'}->value); 80 } 81 82 /** 83 * Test the export of guide data. 84 */ 85 public function test_get_gradingform_export_data() { 86 global $DB; 87 $this->resetAfterTest(); 88 $course = $this->getDataGenerator()->create_course(); 89 $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]); 90 $user = $this->getDataGenerator()->create_user(); 91 92 $this->setUser($user); 93 94 $modulecontext = \context_module::instance($module->cmid); 95 $controller = $this->get_test_guide($modulecontext); 96 97 // In the situation of mod_assign this would be the id from assign_grades. 98 $itemid = 1; 99 $instance = $controller->create_instance($user->id, $itemid); 100 $data = $this->get_test_form_data( 101 $controller, 102 $itemid, 103 5, 'This user made several mistakes.', 104 10, 'This user has two pictures.' 105 ); 106 107 $instance->update($data); 108 $instanceid = $instance->get_data('id'); 109 110 // Let's try the method we are testing. 111 provider::export_gradingform_instance_data($modulecontext, $instance->get_id(), ['Test']); 112 $data = (array) writer::with_context($modulecontext)->get_data(['Test', 'Marking guide', $instanceid]); 113 $this->assertCount(2, $data); 114 $this->assertEquals('Spelling mistakes', $data['Spelling mistakes']->shortname); 115 $this->assertEquals('This user made several mistakes.', $data['Spelling mistakes']->remark); 116 $this->assertEquals('Pictures', $data['Pictures']->shortname); 117 $this->assertEquals('This user has two pictures.', $data['Pictures']->remark); 118 } 119 120 /** 121 * Test the deletion of guide user information via the instance ID. 122 */ 123 public function test_delete_gradingform_for_instances() { 124 global $DB; 125 $this->resetAfterTest(); 126 $course = $this->getDataGenerator()->create_course(); 127 $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]); 128 $user = $this->getDataGenerator()->create_user(); 129 130 $this->setUser($user); 131 132 $modulecontext = \context_module::instance($module->cmid); 133 $controller = $this->get_test_guide($modulecontext); 134 135 // In the situation of mod_assign this would be the id from assign_grades. 136 $itemid = 1; 137 $instance = $controller->create_instance($user->id, $itemid); 138 $data = $this->get_test_form_data( 139 $controller, 140 $itemid, 141 5, 'This user made several mistakes.', 142 10, 'This user has two pictures.' 143 ); 144 145 $instance->update($data); 146 $instanceid = $instance->get_data('id'); 147 148 $itemid = 2; 149 $instance = $controller->create_instance($user->id, $itemid); 150 $data = $this->get_test_form_data( 151 $controller, 152 $itemid, 153 25, 'This user made no mistakes.', 154 5, 'This user has one pictures.' 155 ); 156 157 $instance->update($data); 158 $instanceid = $instance->get_data('id'); 159 160 // Check how many records we have in the fillings table. 161 $records = $DB->get_records('gradingform_guide_fillings'); 162 $this->assertCount(4, $records); 163 // Let's delete one of the instances (the last one would be the easiest). 164 provider::delete_gradingform_for_instances([$instance->get_id()]); 165 $records = $DB->get_records('gradingform_guide_fillings'); 166 $this->assertCount(2, $records); 167 foreach ($records as $record) { 168 $this->assertNotEquals($instance->get_id(), $record->instanceid); 169 } 170 } 171 172 /** 173 * Generate a guide controller with sample data required for testing of this class. 174 * 175 * @param context_module $context 176 * @return gradingform_guide_controller 177 */ 178 protected function get_test_guide(\context_module $context): \gradingform_guide_controller { 179 $generator = \testing_util::get_data_generator(); 180 $guidegenerator = $generator->get_plugin_generator('gradingform_guide'); 181 182 return $guidegenerator->get_test_guide($context); 183 } 184 185 /** 186 * Fetch a set of sample data. 187 * 188 * @param gradingform_guide_controller $controller 189 * @param int $itemid 190 * @param float $spellingscore 191 * @param string $spellingremark 192 * @param float $picturescore 193 * @param string $pictureremark 194 * @return array 195 */ 196 protected function get_test_form_data( 197 \gradingform_guide_controller $controller, 198 int $itemid, 199 float $spellingscore, 200 string $spellingremark, 201 float $picturescore, 202 string $pictureremark 203 ): array { 204 $generator = \testing_util::get_data_generator(); 205 $guidegenerator = $generator->get_plugin_generator('gradingform_guide'); 206 207 return $guidegenerator->get_test_form_data( 208 $controller, 209 $itemid, 210 $spellingscore, 211 $spellingremark, 212 $picturescore, 213 $pictureremark 214 ); 215 } 216 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body