[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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_choice 21 * @copyright 2013 Adrian Greeve <adrian@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/mod/choice/lib.php'); 29 30 /** 31 * Events tests class. 32 * 33 * @package mod_choice 34 * @copyright 2013 Adrian Greeve <adrian@moodle.com> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class mod_choice_events_testcase extends advanced_testcase { 38 /** @var choice_object */ 39 protected $choice; 40 41 /** @var course_object */ 42 protected $course; 43 44 /** @var cm_object Course module object. */ 45 protected $cm; 46 47 /** @var context_object */ 48 protected $context; 49 50 /** 51 * Setup often used objects for the following tests. 52 */ 53 protected function setup() { 54 global $DB; 55 56 $this->resetAfterTest(); 57 58 $this->course = $this->getDataGenerator()->create_course(); 59 $this->choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id)); 60 $this->cm = $DB->get_record('course_modules', array('id' => $this->choice->cmid)); 61 $this->context = context_module::instance($this->choice->cmid); 62 } 63 64 /** 65 * Test to ensure that event data is being stored correctly. 66 */ 67 public function test_answer_submitted() { 68 global $DB; 69 // Generate user data. 70 $user = $this->getDataGenerator()->create_user(); 71 72 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id))); 73 // Redirect event. 74 $sink = $this->redirectEvents(); 75 choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm); 76 $events = $sink->get_events(); 77 78 // Data checking. 79 $this->assertCount(1, $events); 80 $this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]); 81 $this->assertEquals($user->id, $events[0]->userid); 82 $this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context()); 83 $this->assertEquals($this->choice->id, $events[0]->other['choiceid']); 84 $this->assertEquals(array($optionids[3]), $events[0]->other['optionid']); 85 $expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $this->cm->id, $this->choice->id, $this->cm->id); 86 $this->assertEventLegacyLogData($expected, $events[0]); 87 $this->assertEventContextNotUsed($events[0]); 88 $sink->close(); 89 } 90 91 /** 92 * Test to ensure that multiple choice data is being stored correctly. 93 */ 94 public function test_answer_submitted_multiple() { 95 global $DB; 96 97 // Generate user data. 98 $user = $this->getDataGenerator()->create_user(); 99 100 // Create multiple choice. 101 $choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id, 102 'allowmultiple' => 1)); 103 $cm = $DB->get_record('course_modules', array('id' => $choice->cmid)); 104 $context = context_module::instance($choice->cmid); 105 106 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $choice->id))); 107 $submittedoptionids = array($optionids[1], $optionids[3]); 108 109 // Redirect event. 110 $sink = $this->redirectEvents(); 111 choice_user_submit_response($submittedoptionids, $choice, $user->id, $this->course, $cm); 112 $events = $sink->get_events(); 113 114 // Data checking. 115 $this->assertCount(1, $events); 116 $this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]); 117 $this->assertEquals($user->id, $events[0]->userid); 118 $this->assertEquals(context_module::instance($choice->cmid), $events[0]->get_context()); 119 $this->assertEquals($choice->id, $events[0]->other['choiceid']); 120 $this->assertEquals($submittedoptionids, $events[0]->other['optionid']); 121 $expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $cm->id, $choice->id, $cm->id); 122 $this->assertEventLegacyLogData($expected, $events[0]); 123 $this->assertEventContextNotUsed($events[0]); 124 $sink->close(); 125 } 126 127 /** 128 * Test custom validations. 129 * 130 * @expectedException coding_exception 131 */ 132 public function test_answer_submitted_other_exception() { 133 // Generate user data. 134 $user = $this->getDataGenerator()->create_user(); 135 136 $eventdata = array(); 137 $eventdata['context'] = $this->context; 138 $eventdata['objectid'] = 2; 139 $eventdata['userid'] = $user->id; 140 $eventdata['courseid'] = $this->course->id; 141 $eventdata['other'] = array(); 142 143 // Make sure content identifier is always set. 144 $event = \mod_choice\event\answer_submitted::create($eventdata); 145 $event->trigger(); 146 $this->assertEventContextNotUsed($event); 147 } 148 149 /** 150 * Test to ensure that event data is being stored correctly. 151 */ 152 public function test_answer_updated() { 153 global $DB; 154 // Generate user data. 155 $user = $this->getDataGenerator()->create_user(); 156 157 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id))); 158 159 // Create the first answer. 160 choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm); 161 162 // Redirect event. 163 $sink = $this->redirectEvents(); 164 // Now choose a different answer. 165 choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm); 166 167 $events = $sink->get_events(); 168 169 // Data checking. 170 $this->assertCount(1, $events); 171 $this->assertInstanceOf('\mod_choice\event\answer_updated', $events[0]); 172 $this->assertEquals($user->id, $events[0]->userid); 173 $this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context()); 174 $this->assertEquals($this->choice->id, $events[0]->other['choiceid']); 175 $this->assertEquals($optionids[3], $events[0]->other['optionid']); 176 $expected = array($this->course->id, "choice", "choose again", 'view.php?id=' . $this->cm->id, 177 $this->choice->id, $this->cm->id); 178 $this->assertEventLegacyLogData($expected, $events[0]); 179 $this->assertEventContextNotUsed($events[0]); 180 $sink->close(); 181 } 182 183 /** 184 * Test custom validations for answer_updated event. 185 * 186 * @expectedException coding_exception 187 */ 188 public function test_answer_updated_other_exception() { 189 // Generate user data. 190 $user = $this->getDataGenerator()->create_user(); 191 192 $eventdata = array(); 193 $eventdata['context'] = $this->context; 194 $eventdata['objectid'] = 2; 195 $eventdata['userid'] = $user->id; 196 $eventdata['courseid'] = $this->course->id; 197 $eventdata['other'] = array(); 198 199 // Make sure content identifier is always set. 200 $event = \mod_choice\event\answer_updated::create($eventdata); 201 $event->trigger(); 202 $this->assertEventContextNotUsed($event); 203 } 204 205 /** 206 * Test to ensure that event data is being stored correctly. 207 */ 208 public function test_answer_deleted() { 209 global $DB, $USER; 210 // Generate user data. 211 $user = $this->getDataGenerator()->create_user(); 212 213 $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id))); 214 215 // Create the first answer. 216 choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm); 217 // Get the users response. 218 $answer = $DB->get_record('choice_answers', array('userid' => $user->id, 'choiceid' => $this->choice->id), 219 '*', $strictness = IGNORE_MULTIPLE); 220 221 // Redirect event. 222 $sink = $this->redirectEvents(); 223 // Now delete the answer. 224 choice_delete_responses(array($answer->id), $this->choice, $this->cm, $this->course); 225 226 // Get our event event. 227 $events = $sink->get_events(); 228 $event = reset($events); 229 230 // Data checking. 231 $this->assertInstanceOf('\mod_choice\event\answer_deleted', $event); 232 $this->assertEquals($USER->id, $event->userid); 233 $this->assertEquals($user->id, $event->relateduserid); 234 $this->assertEquals(context_module::instance($this->choice->cmid), $event->get_context()); 235 $this->assertEquals($this->choice->id, $event->other['choiceid']); 236 $this->assertEquals($answer->optionid, $event->other['optionid']); 237 $this->assertEventContextNotUsed($event); 238 $sink->close(); 239 } 240 241 /** 242 * Test to ensure that event data is being stored correctly. 243 */ 244 public function test_report_viewed() { 245 global $USER; 246 247 $this->resetAfterTest(); 248 249 // Generate user data. 250 $this->setAdminUser(); 251 252 $eventdata = array(); 253 $eventdata['objectid'] = $this->choice->id; 254 $eventdata['context'] = $this->context; 255 $eventdata['courseid'] = $this->course->id; 256 $eventdata['other']['content'] = 'choicereportcontentviewed'; 257 258 // This is fired in a page view so we can't run this through a function. 259 $event = \mod_choice\event\report_viewed::create($eventdata); 260 261 // Redirect event. 262 $sink = $this->redirectEvents(); 263 $event->trigger(); 264 $event = $sink->get_events(); 265 266 // Data checking. 267 $this->assertCount(1, $event); 268 $this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]); 269 $this->assertEquals($USER->id, $event[0]->userid); 270 $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context()); 271 $expected = array($this->course->id, "choice", "report", 'report.php?id=' . $this->context->instanceid, 272 $this->choice->id, $this->context->instanceid); 273 $this->assertEventLegacyLogData($expected, $event[0]); 274 $this->assertEventContextNotUsed($event[0]); 275 $sink->close(); 276 } 277 278 /** 279 * Test to ensure that event data is being stored correctly. 280 */ 281 public function test_report_downloaded() { 282 global $USER; 283 284 $this->resetAfterTest(); 285 286 // Generate user data. 287 $this->setAdminUser(); 288 289 $eventdata = array(); 290 $eventdata['context'] = $this->context; 291 $eventdata['courseid'] = $this->course->id; 292 $eventdata['other']['content'] = 'choicereportcontentviewed'; 293 $eventdata['other']['format'] = 'csv'; 294 $eventdata['other']['choiceid'] = $this->choice->id; 295 296 // This is fired in a page view so we can't run this through a function. 297 $event = \mod_choice\event\report_downloaded::create($eventdata); 298 299 // Redirect event. 300 $sink = $this->redirectEvents(); 301 $event->trigger(); 302 $event = $sink->get_events(); 303 304 // Data checking. 305 $this->assertCount(1, $event); 306 $this->assertInstanceOf('\mod_choice\event\report_downloaded', $event[0]); 307 $this->assertEquals($USER->id, $event[0]->userid); 308 $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context()); 309 $this->assertEquals('csv', $event[0]->other['format']); 310 $this->assertEquals($this->choice->id, $event[0]->other['choiceid']); 311 $this->assertEventContextNotUsed($event[0]); 312 $sink->close(); 313 } 314 315 /** 316 * Test to ensure that event data is being stored correctly. 317 */ 318 public function test_course_module_viewed() { 319 global $USER; 320 321 // Generate user data. 322 $this->setAdminUser(); 323 324 $eventdata = array(); 325 $eventdata['objectid'] = $this->choice->id; 326 $eventdata['context'] = $this->context; 327 $eventdata['courseid'] = $this->course->id; 328 $eventdata['other']['content'] = 'pageresourceview'; 329 330 // This is fired in a page view so we can't run this through a function. 331 $event = \mod_choice\event\course_module_viewed::create($eventdata); 332 333 // Redirect event. 334 $sink = $this->redirectEvents(); 335 $event->trigger(); 336 $event = $sink->get_events(); 337 338 // Data checking. 339 $this->assertCount(1, $event); 340 $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]); 341 $this->assertEquals($USER->id, $event[0]->userid); 342 $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context()); 343 $expected = array($this->course->id, "choice", "view", 'view.php?id=' . $this->context->instanceid, 344 $this->choice->id, $this->context->instanceid); 345 $this->assertEventLegacyLogData($expected, $event[0]); 346 $this->assertEventContextNotUsed($event[0]); 347 $sink->close(); 348 } 349 350 /** 351 * Test to ensure that event data is being stored correctly. 352 */ 353 public function test_course_module_instance_list_viewed_viewed() { 354 global $USER; 355 356 // Not much can be tested here as the event is only triggered on a page load, 357 // let's just check that the event contains the expected basic information. 358 $this->setAdminUser(); 359 360 $params = array('context' => context_course::instance($this->course->id)); 361 $event = \mod_choice\event\course_module_instance_list_viewed::create($params); 362 $sink = $this->redirectEvents(); 363 $event->trigger(); 364 $events = $sink->get_events(); 365 $event = reset($events); 366 $this->assertInstanceOf('\mod_choice\event\course_module_instance_list_viewed', $event); 367 $this->assertEquals($USER->id, $event->userid); 368 $this->assertEquals(context_course::instance($this->course->id), $event->get_context()); 369 $expected = array($this->course->id, 'choice', 'view all', 'index.php?id=' . $this->course->id, ''); 370 $this->assertEventLegacyLogData($expected, $event); 371 $this->assertEventContextNotUsed($event); 372 } 373 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |