See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 * Unit tests for the tool_monitor clean events task. 19 * 20 * @package tool_monitor 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 /** 31 * Class used to test the tool_monitor clean events task. 32 */ 33 class tool_monitor_task_clean_events_testcase extends advanced_testcase { 34 35 /** 36 * Test set up. 37 */ 38 public function setUp() { 39 set_config('enablemonitor', 1, 'tool_monitor'); 40 $this->resetAfterTest(true); 41 } 42 43 /** 44 * Tests the cleaning up of events. 45 */ 46 public function test_clean_events() { 47 global $DB; 48 49 // Create the necessary items for testing. 50 $user = $this->getDataGenerator()->create_user(); 51 $course = $this->getDataGenerator()->create_course(); 52 $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); 53 $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id)); 54 $bookcontext = context_module::instance($book->cmid); 55 $bookchapter = $bookgenerator->create_chapter(array('bookid' => $book->id)); 56 $course2 = $this->getDataGenerator()->create_course(); 57 $book2 = $this->getDataGenerator()->create_module('book', array('course' => $course2->id)); 58 $book2context = context_module::instance($book2->cmid); 59 $book2chapter = $bookgenerator->create_chapter(array('bookid' => $book2->id)); 60 $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); 61 62 // Let's set some data for the rules we need before we can generate them. 63 $rule = new stdClass(); 64 $rule->userid = $user->id; 65 $rule->courseid = $course->id; 66 $rule->plugin = 'mod_book'; 67 $rule->eventname = '\mod_book\event\course_module_viewed'; 68 $rule->timewindow = 500; 69 70 // Let's add a few rules we want to monitor. 71 $rule1 = $monitorgenerator->create_rule($rule); 72 73 $rule->eventname = '\mod_book\event\course_module_instance_list_viewed'; 74 $rule2 = $monitorgenerator->create_rule($rule); 75 76 // Add the same rules for the same course, but this time with a lower timewindow (used to test that we do not 77 // remove an event for a course if there is still a rule where the maximum timewindow has not been reached). 78 $rule->eventname = '\mod_book\event\course_module_viewed'; 79 $rule->timewindow = 200; 80 $rule3 = $monitorgenerator->create_rule($rule); 81 82 $rule->eventname = '\mod_book\event\course_module_instance_list_viewed'; 83 $rule4 = $monitorgenerator->create_rule($rule); 84 85 // Add another rule in a different course. 86 $rule->courseid = $course2->id; 87 $rule->eventname = '\mod_book\event\chapter_viewed'; 88 $rule->timewindow = 200; 89 $rule5 = $monitorgenerator->create_rule($rule); 90 91 // Add a site wide rule. 92 $rule->courseid = 0; 93 $rule->eventname = '\mod_book\event\chapter_viewed'; 94 $rule->timewindow = 500; 95 $rule6 = $monitorgenerator->create_rule($rule); 96 97 98 // Let's subscribe to these rules. 99 $sub = new stdClass; 100 $sub->courseid = $course->id; 101 $sub->ruleid = $rule1->id; 102 $sub->userid = $user->id; 103 $monitorgenerator->create_subscription($sub); 104 105 $sub->ruleid = $rule2->id; 106 $monitorgenerator->create_subscription($sub); 107 108 $sub->ruleid = $rule3->id; 109 $monitorgenerator->create_subscription($sub); 110 111 $sub->ruleid = $rule4->id; 112 $monitorgenerator->create_subscription($sub); 113 114 $sub->ruleid = $rule5->id; 115 $sub->courseid = $course2->id; 116 $monitorgenerator->create_subscription($sub); 117 118 $sub->ruleid = $rule6->id; 119 $sub->courseid = 0; 120 $monitorgenerator->create_subscription($sub); 121 122 // Now let's populate the tool_monitor table with the events associated with those rules. 123 \mod_book\event\course_module_viewed::create_from_book($book, $bookcontext)->trigger(); 124 \mod_book\event\course_module_instance_list_viewed::create_from_course($course)->trigger(); 125 \mod_book\event\chapter_viewed::create_from_chapter($book, $bookcontext, $bookchapter)->trigger(); 126 127 // Let's trigger the viewed events again, but in another course. The rules created for these events are 128 // associated with another course, so these events should get deleted when we trigger the cleanup task. 129 \mod_book\event\course_module_viewed::create_from_book($book2, $book2context)->trigger(); 130 \mod_book\event\course_module_instance_list_viewed::create_from_course($course2)->trigger(); 131 // Trigger a chapter_viewed event in this course - this should not get deleted as the rule is site wide. 132 \mod_book\event\chapter_viewed::create_from_chapter($book2, $book2context, $book2chapter)->trigger(); 133 134 // Trigger a bunch of other events. 135 $eventparams = array( 136 'context' => context_course::instance($course->id) 137 ); 138 for ($i = 0; $i < 5; $i++) { 139 \mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger(); 140 \mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger(); 141 } 142 143 // We do not store events that have no subscriptions - so there will be only 4 events. 144 $this->assertEquals(4, $DB->count_records('tool_monitor_events')); 145 146 // Run the task and check that all the quiz, scorm and rule events are removed as well as the course_module_* 147 // viewed events in the second course. 148 $task = new \tool_monitor\task\clean_events(); 149 $task->execute(); 150 151 $events = $DB->get_records('tool_monitor_events', array(), 'id'); 152 $this->assertEquals(4, count($events)); 153 $event1 = array_shift($events); 154 $event2 = array_shift($events); 155 $event3 = array_shift($events); 156 $event4 = array_shift($events); 157 $this->assertEquals('\mod_book\event\course_module_viewed', $event1->eventname); 158 $this->assertEquals($course->id, $event1->courseid); 159 $this->assertEquals('\mod_book\event\course_module_instance_list_viewed', $event2->eventname); 160 $this->assertEquals($course->id, $event2->courseid); 161 $this->assertEquals('\mod_book\event\chapter_viewed', $event3->eventname); 162 $this->assertEquals($course->id, $event3->courseid); 163 $this->assertEquals('\mod_book\event\chapter_viewed', $event4->eventname); 164 $this->assertEquals($course2->id, $event4->courseid); 165 166 // Update the timewindow for two of the rules. 167 $updaterule = new stdClass(); 168 $updaterule->id = $rule1->id; 169 $updaterule->timewindow = 0; 170 \tool_monitor\rule_manager::update_rule($updaterule); 171 $updaterule->id = $rule2->id; 172 \tool_monitor\rule_manager::update_rule($updaterule); 173 174 // Run the task and check that the events remain as we still have not reached the maximum timewindow. 175 $task = new \tool_monitor\task\clean_events(); 176 $task->execute(); 177 178 $this->assertEquals(4, $DB->count_records('tool_monitor_events')); 179 180 // Now, remove the rules associated with course_module_* events so they get deleted. 181 \tool_monitor\rule_manager::delete_rule($rule1->id); 182 \tool_monitor\rule_manager::delete_rule($rule2->id); 183 \tool_monitor\rule_manager::delete_rule($rule3->id); 184 \tool_monitor\rule_manager::delete_rule($rule4->id); 185 186 // Run the task and check all the course_module_* events are gone. 187 $task = new \tool_monitor\task\clean_events(); 188 $task->execute(); 189 190 // We now should only have the chapter_viewed events. 191 $events = $DB->get_records('tool_monitor_events', array(), 'id'); 192 $this->assertEquals(2, count($events)); 193 $event1 = array_shift($events); 194 $event2 = array_shift($events); 195 $this->assertEquals('\mod_book\event\chapter_viewed', $event1->eventname); 196 $this->assertEquals($course->id, $event1->courseid); 197 $this->assertEquals('\mod_book\event\chapter_viewed', $event2->eventname); 198 $this->assertEquals($course2->id, $event2->courseid); 199 200 // Set the timewindow of the rule for the event chapter_viewed in the second course to 0. 201 $updaterule->id = $rule5->id; 202 \tool_monitor\rule_manager::update_rule($updaterule); 203 204 // Run the task. 205 $task = new \tool_monitor\task\clean_events(); 206 $task->execute(); 207 208 // Check that nothing was deleted as we still have a site wide rule for the chapter_viewed event. 209 $this->assertEquals(2, $DB->count_records('tool_monitor_events')); 210 211 // Set the timewindow back to 500. 212 $updaterule->id = $rule5->id; 213 $updaterule->timewindow = 500; 214 \tool_monitor\rule_manager::update_rule($updaterule); 215 216 // Set the site rule timewindow to 0. 217 $updaterule->id = $rule6->id; 218 $updaterule->timewindow = 0; 219 \tool_monitor\rule_manager::update_rule($updaterule); 220 221 // Run the task. 222 $task = new \tool_monitor\task\clean_events(); 223 $task->execute(); 224 225 // We now should only have one chapter_viewed event for the second course. 226 $events = $DB->get_records('tool_monitor_events'); 227 $this->assertEquals(1, count($events)); 228 $event1 = array_shift($events); 229 $this->assertEquals('\mod_book\event\chapter_viewed', $event1->eventname); 230 $this->assertEquals($course2->id, $event1->courseid); 231 232 // Remove the site rule. 233 \tool_monitor\rule_manager::delete_rule($rule6->id); 234 235 // Remove the last remaining rule. 236 \tool_monitor\rule_manager::delete_rule($rule5->id); 237 238 // Run the task. 239 $task = new \tool_monitor\task\clean_events(); 240 $task->execute(); 241 242 // There should be no events left. 243 $this->assertEquals(0, $DB->count_records('tool_monitor_events')); 244 } 245 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body