Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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