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 * Events tests. 19 * 20 * @package mod_data 21 * @category test 22 * @copyright 2014 Mark Nelson <markn@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 30 class mod_data_events_testcase extends advanced_testcase { 31 32 /** 33 * Test set up. 34 * 35 * This is executed before running any test in this file. 36 */ 37 public function setUp(): void { 38 $this->resetAfterTest(); 39 } 40 41 /** 42 * Test the field created event. 43 */ 44 public function test_field_created() { 45 $this->setAdminUser(); 46 47 // Create a course we are going to add a data module to. 48 $course = $this->getDataGenerator()->create_course(); 49 50 // The generator used to create a data module. 51 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 52 53 // Create a data module. 54 $data = $generator->create_instance(array('course' => $course->id)); 55 56 // Now we want to create a field. 57 $field = data_get_field_new('text', $data); 58 $fielddata = new stdClass(); 59 $fielddata->name = 'Test'; 60 $fielddata->description = 'Test description'; 61 $field->define_field($fielddata); 62 63 // Trigger and capture the event for creating a field. 64 $sink = $this->redirectEvents(); 65 $field->insert_field(); 66 $events = $sink->get_events(); 67 $event = reset($events); 68 69 // Check that the event data is valid. 70 $this->assertInstanceOf('\mod_data\event\field_created', $event); 71 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 72 $expected = array($course->id, 'data', 'fields add', 'field.php?d=' . $data->id . '&mode=display&fid=' . 73 $field->field->id, $field->field->id, $data->cmid); 74 $this->assertEventLegacyLogData($expected, $event); 75 $this->assertEventContextNotUsed($event); 76 $url = new moodle_url('/mod/data/field.php', array('d' => $data->id)); 77 $this->assertEquals($url, $event->get_url()); 78 } 79 80 /** 81 * Test the field updated event. 82 */ 83 public function test_field_updated() { 84 $this->setAdminUser(); 85 86 // Create a course we are going to add a data module to. 87 $course = $this->getDataGenerator()->create_course(); 88 89 // The generator used to create a data module. 90 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 91 92 // Create a data module. 93 $data = $generator->create_instance(array('course' => $course->id)); 94 95 // Now we want to create a field. 96 $field = data_get_field_new('text', $data); 97 $fielddata = new stdClass(); 98 $fielddata->name = 'Test'; 99 $fielddata->description = 'Test description'; 100 $field->define_field($fielddata); 101 $field->insert_field(); 102 103 // Trigger and capture the event for updating the field. 104 $sink = $this->redirectEvents(); 105 $field->update_field(); 106 $events = $sink->get_events(); 107 $event = reset($events); 108 109 // Check that the event data is valid. 110 $this->assertInstanceOf('\mod_data\event\field_updated', $event); 111 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 112 $expected = array($course->id, 'data', 'fields update', 'field.php?d=' . $data->id . '&mode=display&fid=' . 113 $field->field->id, $field->field->id, $data->cmid); 114 $this->assertEventLegacyLogData($expected, $event); 115 $this->assertEventContextNotUsed($event); 116 $url = new moodle_url('/mod/data/field.php', array('d' => $data->id)); 117 $this->assertEquals($url, $event->get_url()); 118 } 119 120 /** 121 * Test the field deleted event. 122 */ 123 public function test_field_deleted() { 124 $this->setAdminUser(); 125 126 // Create a course we are going to add a data module to. 127 $course = $this->getDataGenerator()->create_course(); 128 129 // The generator used to create a data module. 130 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 131 132 // Create a data module. 133 $data = $generator->create_instance(array('course' => $course->id)); 134 135 // Now we want to create a field. 136 $field = data_get_field_new('text', $data); 137 $fielddata = new stdClass(); 138 $fielddata->name = 'Test'; 139 $fielddata->description = 'Test description'; 140 $field->define_field($fielddata); 141 $field->insert_field(); 142 143 // Trigger and capture the event for deleting the field. 144 $sink = $this->redirectEvents(); 145 $field->delete_field(); 146 $events = $sink->get_events(); 147 $event = reset($events); 148 149 // Check that the event data is valid. 150 $this->assertInstanceOf('\mod_data\event\field_deleted', $event); 151 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 152 $expected = array($course->id, 'data', 'fields delete', 'field.php?d=' . $data->id, $field->field->name, $data->cmid); 153 $this->assertEventLegacyLogData($expected, $event); 154 $this->assertEventContextNotUsed($event); 155 $url = new moodle_url('/mod/data/field.php', array('d' => $data->id)); 156 $this->assertEquals($url, $event->get_url()); 157 } 158 159 /** 160 * Test the record created event. 161 */ 162 public function test_record_created() { 163 // Create a course we are going to add a data module to. 164 $course = $this->getDataGenerator()->create_course(); 165 166 // The generator used to create a data module. 167 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 168 169 // Create a data module. 170 $data = $generator->create_instance(array('course' => $course->id)); 171 172 // Trigger and capture the event for creating the record. 173 $sink = $this->redirectEvents(); 174 $recordid = data_add_record($data); 175 $events = $sink->get_events(); 176 $event = reset($events); 177 178 // Check that the event data is valid. 179 $this->assertInstanceOf('\mod_data\event\record_created', $event); 180 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 181 $expected = array($course->id, 'data', 'add', 'view.php?d=' . $data->id . '&rid=' . $recordid, 182 $data->id, $data->cmid); 183 $this->assertEventLegacyLogData($expected, $event); 184 $this->assertEventContextNotUsed($event); 185 $url = new moodle_url('/mod/data/view.php', array('d' => $data->id, 'rid' => $recordid)); 186 $this->assertEquals($url, $event->get_url()); 187 } 188 189 /** 190 * Test the record updated event. 191 * 192 * There is no external API for updating a record, so the unit test will simply create 193 * and trigger the event and ensure the legacy log data is returned as expected. 194 */ 195 public function test_record_updated() { 196 // Create a course we are going to add a data module to. 197 $course = $this->getDataGenerator()->create_course(); 198 199 // The generator used to create a data module. 200 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 201 202 // Create a data module. 203 $data = $generator->create_instance(array('course' => $course->id)); 204 205 // Trigger an event for updating this record. 206 $event = \mod_data\event\record_updated::create(array( 207 'objectid' => 1, 208 'context' => context_module::instance($data->cmid), 209 'courseid' => $course->id, 210 'other' => array( 211 'dataid' => $data->id 212 ) 213 )); 214 215 // Trigger and capture the event for updating the data record. 216 $sink = $this->redirectEvents(); 217 $event->trigger(); 218 $events = $sink->get_events(); 219 $event = reset($events); 220 221 // Check that the event data is valid. 222 $this->assertInstanceOf('\mod_data\event\record_updated', $event); 223 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 224 $expected = array($course->id, 'data', 'update', 'view.php?d=' . $data->id . '&rid=1', $data->id, $data->cmid); 225 $this->assertEventLegacyLogData($expected, $event); 226 $this->assertEventContextNotUsed($event); 227 $url = new moodle_url('/mod/data/view.php', array('d' => $data->id, 'rid' => $event->objectid)); 228 $this->assertEquals($url, $event->get_url()); 229 } 230 231 /** 232 * Test the record deleted event. 233 */ 234 public function test_record_deleted() { 235 global $DB; 236 237 // Create a course we are going to add a data module to. 238 $course = $this->getDataGenerator()->create_course(); 239 240 // The generator used to create a data module. 241 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 242 243 // Create a data module. 244 $data = $generator->create_instance(array('course' => $course->id)); 245 246 // Now we want to create a field. 247 $field = data_get_field_new('text', $data); 248 $fielddata = new stdClass(); 249 $fielddata->name = 'Test'; 250 $fielddata->description = 'Test description'; 251 $field->define_field($fielddata); 252 $field->insert_field(); 253 254 // Create data record. 255 $datarecords = new stdClass(); 256 $datarecords->userid = '2'; 257 $datarecords->dataid = $data->id; 258 $datarecords->id = $DB->insert_record('data_records', $datarecords); 259 260 // Create data content. 261 $datacontent = new stdClass(); 262 $datacontent->fieldid = $field->field->id; 263 $datacontent->recordid = $datarecords->id; 264 $datacontent->id = $DB->insert_record('data_content', $datacontent); 265 266 // Trigger and capture the event for deleting the data record. 267 $sink = $this->redirectEvents(); 268 data_delete_record($datarecords->id, $data, $course->id, $data->cmid); 269 $events = $sink->get_events(); 270 $event = reset($events); 271 272 // Check that the event data is valid. 273 $this->assertInstanceOf('\mod_data\event\record_deleted', $event); 274 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 275 $expected = array($course->id, 'data', 'record delete', 'view.php?id=' . $data->cmid, $data->id, $data->cmid); 276 $this->assertEventLegacyLogData($expected, $event); 277 $this->assertEventContextNotUsed($event); 278 $url = new moodle_url('/mod/data/view.php', array('d' => $data->id)); 279 $this->assertEquals($url, $event->get_url()); 280 } 281 282 /** 283 * Test the template viewed event. 284 * 285 * There is no external API for viewing templates, so the unit test will simply create 286 * and trigger the event and ensure the legacy log data is returned as expected. 287 */ 288 public function test_template_viewed() { 289 // Create a course we are going to add a data module to. 290 $course = $this->getDataGenerator()->create_course(); 291 292 // The generator used to create a data module. 293 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 294 295 // Create a data module. 296 $data = $generator->create_instance(array('course' => $course->id)); 297 298 // Trigger an event for updating this record. 299 $event = \mod_data\event\template_viewed::create(array( 300 'context' => context_module::instance($data->cmid), 301 'courseid' => $course->id, 302 'other' => array( 303 'dataid' => $data->id 304 ) 305 )); 306 307 // Trigger and capture the event for updating the data record. 308 $sink = $this->redirectEvents(); 309 $event->trigger(); 310 $events = $sink->get_events(); 311 $event = reset($events); 312 313 // Check that the event data is valid. 314 $this->assertInstanceOf('\mod_data\event\template_viewed', $event); 315 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 316 $expected = array($course->id, 'data', 'templates view', 'templates.php?id=' . $data->cmid . '&d=' . 317 $data->id, $data->id, $data->cmid); 318 $this->assertEventLegacyLogData($expected, $event); 319 $this->assertEventContextNotUsed($event); 320 $url = new moodle_url('/mod/data/templates.php', array('d' => $data->id)); 321 $this->assertEquals($url, $event->get_url()); 322 } 323 324 /** 325 * Test the template updated event. 326 * 327 * There is no external API for updating a template, so the unit test will simply create 328 * and trigger the event and ensure the legacy log data is returned as expected. 329 */ 330 public function test_template_updated() { 331 // Create a course we are going to add a data module to. 332 $course = $this->getDataGenerator()->create_course(); 333 334 // The generator used to create a data module. 335 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 336 337 // Create a data module. 338 $data = $generator->create_instance(array('course' => $course->id)); 339 340 // Trigger an event for updating this record. 341 $event = \mod_data\event\template_updated::create(array( 342 'context' => context_module::instance($data->cmid), 343 'courseid' => $course->id, 344 'other' => array( 345 'dataid' => $data->id, 346 ) 347 )); 348 349 // Trigger and capture the event for updating the data record. 350 $sink = $this->redirectEvents(); 351 $event->trigger(); 352 $events = $sink->get_events(); 353 $event = reset($events); 354 355 // Check that the event data is valid. 356 $this->assertInstanceOf('\mod_data\event\template_updated', $event); 357 $this->assertEquals(context_module::instance($data->cmid), $event->get_context()); 358 $expected = array($course->id, 'data', 'templates saved', 'templates.php?id=' . $data->cmid . '&d=' . 359 $data->id, $data->id, $data->cmid); 360 $this->assertEventLegacyLogData($expected, $event); 361 $this->assertEventContextNotUsed($event); 362 $url = new moodle_url('/mod/data/templates.php', array('d' => $data->id)); 363 $this->assertEquals($url, $event->get_url()); 364 } 365 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body