| 1 | 
           efrain | 
           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 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           global $CFG;
  | 
        
        
            | 
            | 
           22 | 
           require_once($CFG->dirroot . '/blog/locallib.php');
  | 
        
        
            | 
            | 
           23 | 
           require_once($CFG->dirroot . '/blog/lib.php');
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           /**
  | 
        
        
            | 
            | 
           26 | 
            * Unit tests for event observers.
  | 
        
        
            | 
            | 
           27 | 
            *
  | 
        
        
            | 
            | 
           28 | 
            * @package    tool_monitor
  | 
        
        
            | 
            | 
           29 | 
            * @category   test
  | 
        
        
            | 
            | 
           30 | 
            * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  | 
        
        
            | 
            | 
           31 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           32 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           33 | 
           final class eventobservers_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           34 | 
               /**
  | 
        
        
            | 
            | 
           35 | 
                * Set up method.
  | 
        
        
            | 
            | 
           36 | 
                */
  | 
        
        
            | 
            | 
           37 | 
               public function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           38 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           39 | 
                   // Enable monitor.
  | 
        
        
            | 
            | 
           40 | 
                   set_config('enablemonitor', 1, 'tool_monitor');
  | 
        
        
            | 
            | 
           41 | 
               }
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
               /**
  | 
        
        
            | 
            | 
           44 | 
                * Test observer for course delete event.
  | 
        
        
            | 
            | 
           45 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           46 | 
               public function test_course_deleted(): void {
  | 
        
        
           | 1 | 
           efrain | 
           47 | 
                   global $DB;
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           50 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           53 | 
                   $course1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           54 | 
                   $course2 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           55 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
                   $rule = new \stdClass();
  | 
        
        
            | 
            | 
           58 | 
                   $rule->userid = $user->id;
  | 
        
        
            | 
            | 
           59 | 
                   $rule->courseid = $course1->id;
  | 
        
        
            | 
            | 
           60 | 
                   $rule->plugin = 'test';
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
                   $sub = new \stdClass();
  | 
        
        
            | 
            | 
           63 | 
                   $sub->courseid = $course1->id;
  | 
        
        
            | 
            | 
           64 | 
                   $sub->userid = $user->id;
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                   // Add 10 rules for this course with subscriptions.
  | 
        
        
            | 
            | 
           67 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           68 | 
                       $createdrule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           69 | 
                       $sub->ruleid = $createdrule->id;
  | 
        
        
            | 
            | 
           70 | 
                       $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           71 | 
                   }
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
                   // Add 10 random rules for course 2.
  | 
        
        
            | 
            | 
           74 | 
                   $rule->courseid = $course2->id;
  | 
        
        
            | 
            | 
           75 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           76 | 
                       $createdrule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           77 | 
                       $sub->courseid = $rule->courseid;
  | 
        
        
            | 
            | 
           78 | 
                       $sub->ruleid = $createdrule->id;
  | 
        
        
            | 
            | 
           79 | 
                       $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           80 | 
                   }
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   // Add a site rule.
  | 
        
        
            | 
            | 
           83 | 
                   $rule = new \stdClass();
  | 
        
        
            | 
            | 
           84 | 
                   $rule->userid = $user->id;
  | 
        
        
            | 
            | 
           85 | 
                   $rule->courseid = 0;
  | 
        
        
            | 
            | 
           86 | 
                   $rule->plugin = 'core';
  | 
        
        
            | 
            | 
           87 | 
                   $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
                   // Verify that if we do not specify that we do not want the site rules, they are returned.
  | 
        
        
            | 
            | 
           90 | 
                   $courserules = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id);
  | 
        
        
            | 
            | 
           91 | 
                   $this->assertCount(11, $courserules);
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   // Verify data before course delete.
  | 
        
        
            | 
            | 
           94 | 
                   $totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
  | 
        
        
            | 
            | 
           95 | 
                   $this->assertCount(20, $totalrules);
  | 
        
        
            | 
            | 
           96 | 
                   $courserules = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id, 0, 0, false);
  | 
        
        
            | 
            | 
           97 | 
                   $this->assertCount(10, $courserules);
  | 
        
        
            | 
            | 
           98 | 
                   $this->assertEquals(20, $DB->count_records('tool_monitor_subscriptions'));
  | 
        
        
            | 
            | 
           99 | 
                   $coursesubs = \tool_monitor\subscription_manager::get_user_subscriptions_for_course($course1->id, 0, 0, $user->id);
  | 
        
        
            | 
            | 
           100 | 
                   $this->assertCount(10, $coursesubs);
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   // Let us delete the course now.
  | 
        
        
            | 
            | 
           103 | 
                   delete_course($course1->id, false);
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                   // Confirm the site rule still exists.
  | 
        
        
            | 
            | 
           106 | 
                   $this->assertEquals(1, $DB->count_records('tool_monitor_rules', array('courseid' => 0)));
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                   // Verify data after course delete.
  | 
        
        
            | 
            | 
           109 | 
                   $totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
  | 
        
        
            | 
            | 
           110 | 
                   $this->assertCount(10, $totalrules);
  | 
        
        
            | 
            | 
           111 | 
                   $courserules = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id, 0, 0, false);
  | 
        
        
            | 
            | 
           112 | 
                   $this->assertCount(0, $courserules); // Making sure all rules are deleted.
  | 
        
        
            | 
            | 
           113 | 
                   $this->assertEquals(10, $DB->count_records('tool_monitor_subscriptions'));
  | 
        
        
            | 
            | 
           114 | 
                   $coursesubs = \tool_monitor\subscription_manager::get_user_subscriptions_for_course($course1->id, 0, 0, $user->id);
  | 
        
        
            | 
            | 
           115 | 
                   $this->assertCount(0, $coursesubs); // Making sure all subscriptions are deleted.
  | 
        
        
            | 
            | 
           116 | 
               }
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
               /**
  | 
        
        
            | 
            | 
           119 | 
                * This tests if writing of the events to the table tool_monitor_events is working fine.
  | 
        
        
            | 
            | 
           120 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           121 | 
               public function test_flush(): void {
  | 
        
        
           | 1 | 
           efrain | 
           122 | 
                   global $DB;
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
                   // Create the necessary items for testing.
  | 
        
        
            | 
            | 
           127 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           128 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           129 | 
                   $course2 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           130 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
                   // Fire a bunch of events.
  | 
        
        
            | 
            | 
           133 | 
                   // Trigger a bunch of other events.
  | 
        
        
            | 
            | 
           134 | 
                   $eventparams = array(
  | 
        
        
            | 
            | 
           135 | 
                       'context' => \context_course::instance($course->id)
  | 
        
        
            | 
            | 
           136 | 
                   );
  | 
        
        
            | 
            | 
           137 | 
                   for ($i = 0; $i < 5; $i++) {
  | 
        
        
            | 
            | 
           138 | 
                       \core\event\course_viewed::create($eventparams)->trigger();
  | 
        
        
            | 
            | 
           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 | 
                   // Confirm that nothing was stored in the tool_monitor_events table
  | 
        
        
            | 
            | 
           144 | 
                   // as we do not have any subscriptions associated for the above events.
  | 
        
        
            | 
            | 
           145 | 
                   $this->assertEquals(0, $DB->count_records('tool_monitor_events'));
  | 
        
        
            | 
            | 
           146 | 
              | 
        
        
            | 
            | 
           147 | 
                   // Now, let's create a rule so an event can be stored.
  | 
        
        
            | 
            | 
           148 | 
                   $rule = new \stdClass();
  | 
        
        
            | 
            | 
           149 | 
                   $rule->courseid = $course->id;
  | 
        
        
            | 
            | 
           150 | 
                   $rule->plugin = 'mod_book';
  | 
        
        
            | 
            | 
           151 | 
                   $rule->eventname = '\mod_book\event\course_module_instance_list_viewed';
  | 
        
        
            | 
            | 
           152 | 
                   $rule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           153 | 
              | 
        
        
            | 
            | 
           154 | 
                   // Let's subscribe to this rule.
  | 
        
        
            | 
            | 
           155 | 
                   $sub = new \stdClass;
  | 
        
        
            | 
            | 
           156 | 
                   $sub->courseid = $course->id;
  | 
        
        
            | 
            | 
           157 | 
                   $sub->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           158 | 
                   $sub->userid = $user->id;
  | 
        
        
            | 
            | 
           159 | 
                   $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
                   // Again, let's just fire more events to make sure they still aren't stored.
  | 
        
        
            | 
            | 
           162 | 
                   for ($i = 0; $i < 5; $i++) {
  | 
        
        
            | 
            | 
           163 | 
                       \core\event\course_viewed::create($eventparams)->trigger();
  | 
        
        
            | 
            | 
           164 | 
                       \mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger();
  | 
        
        
            | 
            | 
           165 | 
                       \mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger();
  | 
        
        
            | 
            | 
           166 | 
                   }
  | 
        
        
            | 
            | 
           167 | 
              | 
        
        
            | 
            | 
           168 | 
                   // Fire the event we want to capture.
  | 
        
        
            | 
            | 
           169 | 
                   $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course);
  | 
        
        
            | 
            | 
           170 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
                   // Check that the event data is valid.
  | 
        
        
            | 
            | 
           173 | 
                   $events = $DB->get_records('tool_monitor_events');
  | 
        
        
            | 
            | 
           174 | 
                   $this->assertEquals(1, count($events));
  | 
        
        
            | 
            | 
           175 | 
                   $monitorevent = array_pop($events);
  | 
        
        
            | 
            | 
           176 | 
                   $this->assertEquals($event->eventname, $monitorevent->eventname);
  | 
        
        
            | 
            | 
           177 | 
                   $this->assertEquals($event->contextid, $monitorevent->contextid);
  | 
        
        
            | 
            | 
           178 | 
                   $this->assertEquals($event->contextlevel, $monitorevent->contextlevel);
  | 
        
        
            | 
            | 
           179 | 
                   $this->assertEquals($event->get_url()->out(), $monitorevent->link);
  | 
        
        
            | 
            | 
           180 | 
                   $this->assertEquals($event->courseid, $monitorevent->courseid);
  | 
        
        
            | 
            | 
           181 | 
                   $this->assertEquals($event->timecreated, $monitorevent->timecreated);
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
                   // Remove the stored events.
  | 
        
        
            | 
            | 
           184 | 
                   $DB->delete_records('tool_monitor_events');
  | 
        
        
            | 
            | 
           185 | 
              | 
        
        
            | 
            | 
           186 | 
                   // Now, let's create a site wide rule.
  | 
        
        
            | 
            | 
           187 | 
                   $rule = new \stdClass();
  | 
        
        
            | 
            | 
           188 | 
                   $rule->courseid = 0;
  | 
        
        
            | 
            | 
           189 | 
                   $rule->plugin = 'mod_book';
  | 
        
        
            | 
            | 
           190 | 
                   $rule->eventname = '\mod_book\event\course_module_instance_list_viewed';
  | 
        
        
            | 
            | 
           191 | 
                   $rule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           192 | 
              | 
        
        
            | 
            | 
           193 | 
                   // Let's subscribe to this rule.
  | 
        
        
            | 
            | 
           194 | 
                   $sub = new \stdClass;
  | 
        
        
            | 
            | 
           195 | 
                   $sub->courseid = 0;
  | 
        
        
            | 
            | 
           196 | 
                   $sub->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           197 | 
                   $sub->userid = $user->id;
  | 
        
        
            | 
            | 
           198 | 
                   $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           199 | 
              | 
        
        
            | 
            | 
           200 | 
                   // Fire the event we want to capture - but in a different course.
  | 
        
        
            | 
            | 
           201 | 
                   $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course2);
  | 
        
        
            | 
            | 
           202 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           203 | 
              | 
        
        
            | 
            | 
           204 | 
                   // Check that the event data is valid.
  | 
        
        
            | 
            | 
           205 | 
                   $events = $DB->get_records('tool_monitor_events');
  | 
        
        
            | 
            | 
           206 | 
                   $this->assertEquals(1, count($events));
  | 
        
        
            | 
            | 
           207 | 
                   $monitorevent = array_pop($events);
  | 
        
        
            | 
            | 
           208 | 
                   $this->assertEquals($event->eventname, $monitorevent->eventname);
  | 
        
        
            | 
            | 
           209 | 
                   $this->assertEquals($event->contextid, $monitorevent->contextid);
  | 
        
        
            | 
            | 
           210 | 
                   $this->assertEquals($event->contextlevel, $monitorevent->contextlevel);
  | 
        
        
            | 
            | 
           211 | 
                   $this->assertEquals($event->get_url()->out(), $monitorevent->link);
  | 
        
        
            | 
            | 
           212 | 
                   $this->assertEquals($event->courseid, $monitorevent->courseid);
  | 
        
        
            | 
            | 
           213 | 
                   $this->assertEquals($event->timecreated, $monitorevent->timecreated);
  | 
        
        
            | 
            | 
           214 | 
               }
  | 
        
        
            | 
            | 
           215 | 
              | 
        
        
            | 
            | 
           216 | 
               /**
  | 
        
        
            | 
            | 
           217 | 
                * Test the notification sending features.
  | 
        
        
            | 
            | 
           218 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           219 | 
               public function test_process_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           220 | 
              | 
        
        
            | 
            | 
           221 | 
                   global $DB, $USER;
  | 
        
        
            | 
            | 
           222 | 
              | 
        
        
            | 
            | 
           223 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           224 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           225 | 
                   $msgsink = $this->redirectMessages();
  | 
        
        
            | 
            | 
           226 | 
              | 
        
        
            | 
            | 
           227 | 
                   // Generate data.
  | 
        
        
            | 
            | 
           228 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           229 | 
                   $toolgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           230 | 
              | 
        
        
            | 
            | 
           231 | 
                   $rulerecord = new \stdClass();
  | 
        
        
            | 
            | 
           232 | 
                   $rulerecord->courseid = $course->id;
  | 
        
        
            | 
            | 
           233 | 
                   $rulerecord->eventname = '\mod_book\event\course_module_instance_list_viewed';
  | 
        
        
            | 
            | 
           234 | 
                   $rulerecord->frequency = 1;
  | 
        
        
            | 
            | 
           235 | 
              | 
        
        
            | 
            | 
           236 | 
                   $rule = $toolgenerator->create_rule($rulerecord);
  | 
        
        
            | 
            | 
           237 | 
              | 
        
        
            | 
            | 
           238 | 
                   $subrecord = new \stdClass();
  | 
        
        
            | 
            | 
           239 | 
                   $subrecord->courseid = $course->id;
  | 
        
        
            | 
            | 
           240 | 
                   $subrecord->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           241 | 
                   $subrecord->userid = $USER->id;
  | 
        
        
            | 
            | 
           242 | 
                   $toolgenerator->create_subscription($subrecord);
  | 
        
        
            | 
            | 
           243 | 
              | 
        
        
            | 
            | 
           244 | 
                   $recordexists = $DB->record_exists('task_adhoc', array('component' => 'tool_monitor'));
  | 
        
        
            | 
            | 
           245 | 
                   $this->assertFalse($recordexists);
  | 
        
        
            | 
            | 
           246 | 
              | 
        
        
            | 
            | 
           247 | 
                   // Now let us trigger the event.
  | 
        
        
            | 
            | 
           248 | 
                   $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course);
  | 
        
        
            | 
            | 
           249 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           250 | 
              | 
        
        
            | 
            | 
           251 | 
                   $this->verify_processed_data($msgsink);
  | 
        
        
            | 
            | 
           252 | 
              | 
        
        
            | 
            | 
           253 | 
                   // Clean up.
  | 
        
        
            | 
            | 
           254 | 
                   \tool_monitor\rule_manager::delete_rule($rule->id);
  | 
        
        
            | 
            | 
           255 | 
                   $DB->delete_records('tool_monitor_events');
  | 
        
        
            | 
            | 
           256 | 
              | 
        
        
            | 
            | 
           257 | 
                   // Let us create a rule with more than 1 frequency.
  | 
        
        
            | 
            | 
           258 | 
                   $rulerecord->frequency = 5;
  | 
        
        
            | 
            | 
           259 | 
                   $rule = $toolgenerator->create_rule($rulerecord);
  | 
        
        
            | 
            | 
           260 | 
                   $subrecord->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           261 | 
                   $toolgenerator->create_subscription($subrecord);
  | 
        
        
            | 
            | 
           262 | 
              | 
        
        
            | 
            | 
           263 | 
                   // Let us trigger events.
  | 
        
        
            | 
            | 
           264 | 
                   for ($i = 0; $i < 5; $i++) {
  | 
        
        
            | 
            | 
           265 | 
                       $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course);
  | 
        
        
            | 
            | 
           266 | 
                       $event->trigger();
  | 
        
        
            | 
            | 
           267 | 
                       if ($i != 4) {
  | 
        
        
            | 
            | 
           268 | 
                           $this->verify_message_not_sent_yet($msgsink);
  | 
        
        
            | 
            | 
           269 | 
                       }
  | 
        
        
            | 
            | 
           270 | 
                   }
  | 
        
        
            | 
            | 
           271 | 
              | 
        
        
            | 
            | 
           272 | 
                   $this->verify_processed_data($msgsink);
  | 
        
        
            | 
            | 
           273 | 
              | 
        
        
            | 
            | 
           274 | 
                   // Clean up.
  | 
        
        
            | 
            | 
           275 | 
                   \tool_monitor\rule_manager::delete_rule($rule->id);
  | 
        
        
            | 
            | 
           276 | 
                   $DB->delete_records('tool_monitor_events');
  | 
        
        
            | 
            | 
           277 | 
              | 
        
        
            | 
            | 
           278 | 
                   // Now let us create a rule specific to a module instance.
  | 
        
        
            | 
            | 
           279 | 
                   $cm = new \stdClass();
  | 
        
        
            | 
            | 
           280 | 
                   $cm->course = $course->id;
  | 
        
        
            | 
            | 
           281 | 
                   $book = $this->getDataGenerator()->create_module('book', $cm);
  | 
        
        
            | 
            | 
           282 | 
                   $rulerecord->eventname = '\mod_book\event\course_module_viewed';
  | 
        
        
            | 
            | 
           283 | 
                   $rulerecord->cmid = $book->cmid;
  | 
        
        
            | 
            | 
           284 | 
                   $rule = $toolgenerator->create_rule($rulerecord);
  | 
        
        
            | 
            | 
           285 | 
                   $subrecord->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           286 | 
                   $toolgenerator->create_subscription($subrecord);
  | 
        
        
            | 
            | 
           287 | 
              | 
        
        
            | 
            | 
           288 | 
                   // Let us trigger events.
  | 
        
        
            | 
            | 
           289 | 
                   $params = array(
  | 
        
        
            | 
            | 
           290 | 
                       'context' => \context_module::instance($book->cmid),
  | 
        
        
            | 
            | 
           291 | 
                       'objectid' => $book->id
  | 
        
        
            | 
            | 
           292 | 
                   );
  | 
        
        
            | 
            | 
           293 | 
                   for ($i = 0; $i < 5; $i++) {
  | 
        
        
            | 
            | 
           294 | 
                       $event = \mod_book\event\course_module_viewed::create($params);
  | 
        
        
            | 
            | 
           295 | 
                       $event->trigger();
  | 
        
        
            | 
            | 
           296 | 
                       if ($i != 4) {
  | 
        
        
            | 
            | 
           297 | 
                           $this->verify_message_not_sent_yet($msgsink);
  | 
        
        
            | 
            | 
           298 | 
                       }
  | 
        
        
            | 
            | 
           299 | 
                   }
  | 
        
        
            | 
            | 
           300 | 
              | 
        
        
            | 
            | 
           301 | 
                   $this->verify_processed_data($msgsink);
  | 
        
        
            | 
            | 
           302 | 
              | 
        
        
            | 
            | 
           303 | 
                   // Clean up.
  | 
        
        
            | 
            | 
           304 | 
                   \tool_monitor\rule_manager::delete_rule($rule->id);
  | 
        
        
            | 
            | 
           305 | 
                   $DB->delete_records('tool_monitor_events');
  | 
        
        
            | 
            | 
           306 | 
              | 
        
        
            | 
            | 
           307 | 
                   // Now let us create a rule for event that happens in category context events.
  | 
        
        
            | 
            | 
           308 | 
                   $rulerecord->eventname = '\core\event\course_category_created';
  | 
        
        
            | 
            | 
           309 | 
                   $rulerecord->courseid = 0;
  | 
        
        
            | 
            | 
           310 | 
                   $rule = $toolgenerator->create_rule($rulerecord);
  | 
        
        
            | 
            | 
           311 | 
                   $subrecord->courseid = 0;
  | 
        
        
            | 
            | 
           312 | 
                   $subrecord->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           313 | 
                   $toolgenerator->create_subscription($subrecord);
  | 
        
        
            | 
            | 
           314 | 
              | 
        
        
            | 
            | 
           315 | 
                   // Let us trigger events.
  | 
        
        
            | 
            | 
           316 | 
                   for ($i = 0; $i < 5; $i++) {
  | 
        
        
            | 
            | 
           317 | 
                       $this->getDataGenerator()->create_category();
  | 
        
        
            | 
            | 
           318 | 
                       if ($i != 4) {
  | 
        
        
            | 
            | 
           319 | 
                           $this->verify_message_not_sent_yet($msgsink);
  | 
        
        
            | 
            | 
           320 | 
                       }
  | 
        
        
            | 
            | 
           321 | 
                   }
  | 
        
        
            | 
            | 
           322 | 
                   $this->verify_processed_data($msgsink);
  | 
        
        
            | 
            | 
           323 | 
              | 
        
        
            | 
            | 
           324 | 
                   // Clean up.
  | 
        
        
            | 
            | 
           325 | 
                   \tool_monitor\rule_manager::delete_rule($rule->id);
  | 
        
        
            | 
            | 
           326 | 
                   $DB->delete_records('tool_monitor_events');
  | 
        
        
            | 
            | 
           327 | 
              | 
        
        
            | 
            | 
           328 | 
                   // Now let us create a rule at site level.
  | 
        
        
            | 
            | 
           329 | 
                   $rulerecord->eventname = '\core\event\blog_entry_created';
  | 
        
        
            | 
            | 
           330 | 
                   $rulerecord->courseid = 0;
  | 
        
        
            | 
            | 
           331 | 
                   $rule = $toolgenerator->create_rule($rulerecord);
  | 
        
        
            | 
            | 
           332 | 
                   $subrecord->courseid = 0;
  | 
        
        
            | 
            | 
           333 | 
                   $subrecord->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           334 | 
                   $toolgenerator->create_subscription($subrecord);
  | 
        
        
            | 
            | 
           335 | 
              | 
        
        
            | 
            | 
           336 | 
                   // Let us trigger events.
  | 
        
        
            | 
            | 
           337 | 
                   $blog = new \blog_entry();
  | 
        
        
            | 
            | 
           338 | 
                   $blog->subject = "Subject of blog";
  | 
        
        
            | 
            | 
           339 | 
                   $blog->userid = $USER->id;
  | 
        
        
            | 
            | 
           340 | 
                   $states = \blog_entry::get_applicable_publish_states();
  | 
        
        
            | 
            | 
           341 | 
                   $blog->publishstate = reset($states);
  | 
        
        
            | 
            | 
           342 | 
                   for ($i = 0; $i < 5; $i++) {
  | 
        
        
            | 
            | 
           343 | 
                       $newblog = fullclone($blog);
  | 
        
        
            | 
            | 
           344 | 
                       $newblog->add();
  | 
        
        
            | 
            | 
           345 | 
                       if ($i != 4) {
  | 
        
        
            | 
            | 
           346 | 
                           $this->verify_message_not_sent_yet($msgsink);
  | 
        
        
            | 
            | 
           347 | 
                       }
  | 
        
        
            | 
            | 
           348 | 
                   }
  | 
        
        
            | 
            | 
           349 | 
              | 
        
        
            | 
            | 
           350 | 
                   $this->verify_processed_data($msgsink);
  | 
        
        
            | 
            | 
           351 | 
               }
  | 
        
        
            | 
            | 
           352 | 
              | 
        
        
            | 
            | 
           353 | 
               /**
  | 
        
        
            | 
            | 
           354 | 
                * Test that same events are not used twice to calculate conditions for a single subscription.
  | 
        
        
            | 
            | 
           355 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           356 | 
               public function test_multiple_notification_not_sent(): void {
  | 
        
        
           | 1 | 
           efrain | 
           357 | 
                   global $USER;
  | 
        
        
            | 
            | 
           358 | 
              | 
        
        
            | 
            | 
           359 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           360 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           361 | 
                   $messagesink = $this->redirectMessages();
  | 
        
        
            | 
            | 
           362 | 
              | 
        
        
            | 
            | 
           363 | 
                   // Generate data.
  | 
        
        
            | 
            | 
           364 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           365 | 
                   $toolgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           366 | 
              | 
        
        
            | 
            | 
           367 | 
                   $rulerecord = new \stdClass();
  | 
        
        
            | 
            | 
           368 | 
                   $rulerecord->courseid = $course->id;
  | 
        
        
            | 
            | 
           369 | 
                   $rulerecord->eventname = '\mod_book\event\course_module_instance_list_viewed';
  | 
        
        
            | 
            | 
           370 | 
                   $rulerecord->frequency = 5;
  | 
        
        
            | 
            | 
           371 | 
              | 
        
        
            | 
            | 
           372 | 
                   $rule = $toolgenerator->create_rule($rulerecord);
  | 
        
        
            | 
            | 
           373 | 
              | 
        
        
            | 
            | 
           374 | 
                   $subrecord = new \stdClass();
  | 
        
        
            | 
            | 
           375 | 
                   $subrecord->courseid = $course->id;
  | 
        
        
            | 
            | 
           376 | 
                   $subrecord->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           377 | 
                   $subrecord->userid = $USER->id;
  | 
        
        
            | 
            | 
           378 | 
                   $toolgenerator->create_subscription($subrecord);
  | 
        
        
            | 
            | 
           379 | 
              | 
        
        
            | 
            | 
           380 | 
                   for ($i = 0; $i < 7; $i++) {
  | 
        
        
            | 
            | 
           381 | 
                       // Now let us trigger 7 instances of the event.
  | 
        
        
            | 
            | 
           382 | 
                       $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course);
  | 
        
        
            | 
            | 
           383 | 
                       $event->trigger();
  | 
        
        
            | 
            | 
           384 | 
                       $this->waitForSecond(); // Add a second delay, to prevent time collisions.
  | 
        
        
            | 
            | 
           385 | 
                   }
  | 
        
        
            | 
            | 
           386 | 
                   $this->run_adhock_tasks();
  | 
        
        
            | 
            | 
           387 | 
                   $messages = $messagesink->get_messages();
  | 
        
        
            | 
            | 
           388 | 
                   $this->assertCount(1, $messages); // There should be only one message not 3.
  | 
        
        
            | 
            | 
           389 | 
                   for ($i = 0; $i < 3; $i++) {
  | 
        
        
            | 
            | 
           390 | 
                       // Now let us trigger 5 more instances of the event.
  | 
        
        
            | 
            | 
           391 | 
                       $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course);
  | 
        
        
            | 
            | 
           392 | 
                       $event->trigger();
  | 
        
        
            | 
            | 
           393 | 
                   }
  | 
        
        
            | 
            | 
           394 | 
              | 
        
        
            | 
            | 
           395 | 
                   $this->run_adhock_tasks();
  | 
        
        
            | 
            | 
           396 | 
                   $messages = $messagesink->get_messages();
  | 
        
        
            | 
            | 
           397 | 
                   $this->assertCount(2, $messages); // There should be two messages now.
  | 
        
        
            | 
            | 
           398 | 
               }
  | 
        
        
            | 
            | 
           399 | 
              | 
        
        
            | 
            | 
           400 | 
               /**
  | 
        
        
            | 
            | 
           401 | 
                * Run adhoc tasks.
  | 
        
        
            | 
            | 
           402 | 
                */
  | 
        
        
            | 
            | 
           403 | 
               protected function run_adhock_tasks() {
  | 
        
        
            | 
            | 
           404 | 
                   while ($task = \core\task\manager::get_next_adhoc_task(time())) {
  | 
        
        
            | 
            | 
           405 | 
                       $task->execute();
  | 
        
        
            | 
            | 
           406 | 
                       \core\task\manager::adhoc_task_complete($task);
  | 
        
        
            | 
            | 
           407 | 
                   }
  | 
        
        
            | 
            | 
           408 | 
                   $this->expectOutputRegex("/^Sending message to the user with id \d+ for the subscription with id \d+\.\.\..Sent./ms");
  | 
        
        
            | 
            | 
           409 | 
               }
  | 
        
        
            | 
            | 
           410 | 
              | 
        
        
            | 
            | 
           411 | 
               /**
  | 
        
        
            | 
            | 
           412 | 
                * Verify that task was scheduled and a message was sent as expected.
  | 
        
        
            | 
            | 
           413 | 
                *
  | 
        
        
            | 
            | 
           414 | 
                * @param phpunit_message_sink $msgsink Message sink
  | 
        
        
            | 
            | 
           415 | 
                */
  | 
        
        
            | 
            | 
           416 | 
               protected function verify_processed_data(\phpunit_message_sink $msgsink) {
  | 
        
        
            | 
            | 
           417 | 
                   global $DB, $USER;
  | 
        
        
            | 
            | 
           418 | 
              | 
        
        
            | 
            | 
           419 | 
                   $recordexists = $DB->count_records('task_adhoc', array('component' => 'tool_monitor'));
  | 
        
        
            | 
            | 
           420 | 
                   $this->assertEquals(1, $recordexists); // We should have an adhock task now to send notifications.
  | 
        
        
            | 
            | 
           421 | 
                   $this->run_adhock_tasks();
  | 
        
        
            | 
            | 
           422 | 
                   $this->assertEquals(1, $msgsink->count());
  | 
        
        
            | 
            | 
           423 | 
                   $msgs = $msgsink->get_messages();
  | 
        
        
            | 
            | 
           424 | 
                   $msg = array_pop($msgs);
  | 
        
        
            | 
            | 
           425 | 
                   $this->assertEquals($USER->id, $msg->useridto);
  | 
        
        
            | 
            | 
           426 | 
                   $this->assertEquals(1, $msg->notification);
  | 
        
        
            | 
            | 
           427 | 
                   $msgsink->clear();
  | 
        
        
            | 
            | 
           428 | 
               }
  | 
        
        
            | 
            | 
           429 | 
              | 
        
        
            | 
            | 
           430 | 
               /**
  | 
        
        
            | 
            | 
           431 | 
                * Verify that a message was not sent.
  | 
        
        
            | 
            | 
           432 | 
                *
  | 
        
        
            | 
            | 
           433 | 
                * @param phpunit_message_sink $msgsink Message sink
  | 
        
        
            | 
            | 
           434 | 
                */
  | 
        
        
            | 
            | 
           435 | 
               protected function verify_message_not_sent_yet(\phpunit_message_sink $msgsink) {
  | 
        
        
            | 
            | 
           436 | 
                   $msgs = $msgsink->get_messages();
  | 
        
        
            | 
            | 
           437 | 
                   $this->assertCount(0, $msgs);
  | 
        
        
            | 
            | 
           438 | 
                   $msgsink->clear();
  | 
        
        
            | 
            | 
           439 | 
               }
  | 
        
        
            | 
            | 
           440 | 
              | 
        
        
            | 
            | 
           441 | 
               /**
  | 
        
        
            | 
            | 
           442 | 
                * Tests for replace_placeholders method.
  | 
        
        
            | 
            | 
           443 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           444 | 
               public function test_replace_placeholders(): void {
  | 
        
        
           | 1 | 
           efrain | 
           445 | 
                   global $USER;
  | 
        
        
            | 
            | 
           446 | 
              | 
        
        
            | 
            | 
           447 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           448 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           449 | 
                   $msgsink = $this->redirectMessages();
  | 
        
        
            | 
            | 
           450 | 
              | 
        
        
            | 
            | 
           451 | 
                   // Generate data.
  | 
        
        
            | 
            | 
           452 | 
                   $course = $this->getDataGenerator()->create_course(['fullname' => 'Observed course', 'shortname' => 'obscourse']);
  | 
        
        
            | 
            | 
           453 | 
                   $toolgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           454 | 
                   $context = \context_user::instance($USER->id, IGNORE_MISSING);
  | 
        
        
            | 
            | 
           455 | 
              | 
        
        
            | 
            | 
           456 | 
                   // Creating book.
  | 
        
        
            | 
            | 
           457 | 
                   $cm = new \stdClass();
  | 
        
        
            | 
            | 
           458 | 
                   $cm->course = $course->id;
  | 
        
        
            | 
            | 
           459 | 
                   $cm->name = 'Observed book';
  | 
        
        
            | 
            | 
           460 | 
                   $book = $this->getDataGenerator()->create_module('book', $cm);
  | 
        
        
            | 
            | 
           461 | 
              | 
        
        
            | 
            | 
           462 | 
                   // Creating rule.
  | 
        
        
            | 
            | 
           463 | 
                   $rulerecord = new \stdClass();
  | 
        
        
            | 
            | 
           464 | 
                   $rulerecord->courseid = $course->id;
  | 
        
        
            | 
            | 
           465 | 
                   $rulerecord->eventname = '\mod_book\event\course_module_viewed';
  | 
        
        
            | 
            | 
           466 | 
                   $rulerecord->cmid = $book->cmid;
  | 
        
        
            | 
            | 
           467 | 
                   $rulerecord->frequency = 1;
  | 
        
        
            | 
            | 
           468 | 
                   $rulerecord->template = '## {link} ##
  | 
        
        
            | 
            | 
           469 | 
              | 
        
        
            | 
            | 
           470 | 
           * {modulelink}
  | 
        
        
            | 
            | 
           471 | 
           * __{rulename}__
  | 
        
        
            | 
            | 
           472 | 
           * {description}
  | 
        
        
            | 
            | 
           473 | 
           * {eventname}
  | 
        
        
            | 
            | 
           474 | 
           * {modulename}
  | 
        
        
            | 
            | 
           475 | 
           * {coursefullname}
  | 
        
        
            | 
            | 
           476 | 
           * {courseshortname}';
  | 
        
        
            | 
            | 
           477 | 
                   $rulerecord->templateformat = FORMAT_MARKDOWN;
  | 
        
        
            | 
            | 
           478 | 
              | 
        
        
            | 
            | 
           479 | 
                   $rule = $toolgenerator->create_rule($rulerecord);
  | 
        
        
            | 
            | 
           480 | 
              | 
        
        
            | 
            | 
           481 | 
                   // Creating subscription.
  | 
        
        
            | 
            | 
           482 | 
                   $subrecord = new \stdClass();
  | 
        
        
            | 
            | 
           483 | 
                   $subrecord->courseid = $course->id;
  | 
        
        
            | 
            | 
           484 | 
                   $subrecord->ruleid = $rule->id;
  | 
        
        
            | 
            | 
           485 | 
                   $subrecord->userid = $USER->id;
  | 
        
        
            | 
            | 
           486 | 
                   $toolgenerator->create_subscription($subrecord);
  | 
        
        
            | 
            | 
           487 | 
              | 
        
        
            | 
            | 
           488 | 
                   // Now let us trigger the event.
  | 
        
        
            | 
            | 
           489 | 
                   $params = array(
  | 
        
        
            | 
            | 
           490 | 
                       'context' => \context_module::instance($book->cmid),
  | 
        
        
            | 
            | 
           491 | 
                       'objectid' => $book->id
  | 
        
        
            | 
            | 
           492 | 
                   );
  | 
        
        
            | 
            | 
           493 | 
              | 
        
        
            | 
            | 
           494 | 
                   $event = \mod_book\event\course_module_viewed::create($params);
  | 
        
        
            | 
            | 
           495 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           496 | 
                   $this->run_adhock_tasks();
  | 
        
        
            | 
            | 
           497 | 
                   $msgs = $msgsink->get_messages();
  | 
        
        
            | 
            | 
           498 | 
                   $msg = array_pop($msgs);
  | 
        
        
            | 
            | 
           499 | 
              | 
        
        
            | 
            | 
           500 | 
                   $modurl = new \moodle_url('/mod/book/view.php', array('id' => $book->cmid));
  | 
        
        
            | 
            | 
           501 | 
              | 
        
        
            | 
            | 
           502 | 
                   $this->assertMatchesRegularExpression('~<h2>.*' . preg_quote($event->get_url()->out(), '~') . '.*</h2>~',
  | 
        
        
            | 
            | 
           503 | 
                       $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           504 | 
                   $this->assertMatchesRegularExpression('~<li>.*' . preg_quote($modurl->out(), '~') . '.*</li>~', $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           505 | 
                   $this->assertStringContainsString('<li><strong>'.$rule->get_name($context).'</strong></li>', $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           506 | 
                   $this->assertStringContainsString('<li>'.$rule->get_description($context).'</li>', $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           507 | 
                   $this->assertStringContainsString('<li>'.$rule->get_event_name().'</li>', $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           508 | 
                   $this->assertStringContainsString('<li>' . $cm->name . '</li>', $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           509 | 
                   $this->assertStringContainsString('<li>' . $course->fullname . '</li>', $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           510 | 
                   $this->assertStringContainsString('<li>' . $course->shortname . '</li>', $msg->fullmessagehtml);
  | 
        
        
            | 
            | 
           511 | 
              | 
        
        
            | 
            | 
           512 | 
                   $this->assertEquals(FORMAT_PLAIN, $msg->fullmessageformat);
  | 
        
        
            | 
            | 
           513 | 
                   $this->assertStringNotContainsString('<h2>', $msg->fullmessage);
  | 
        
        
            | 
            | 
           514 | 
                   $this->assertStringNotContainsString('##', $msg->fullmessage);
  | 
        
        
            | 
            | 
           515 | 
                   $this->assertStringContainsString(strtoupper($event->get_url()->out()), $msg->fullmessage);
  | 
        
        
            | 
            | 
           516 | 
                   $this->assertStringContainsString('* '.$modurl->out(), $msg->fullmessage);
  | 
        
        
            | 
            | 
           517 | 
                   $this->assertStringContainsString('* '.strtoupper($rule->get_name($context)), $msg->fullmessage);
  | 
        
        
            | 
            | 
           518 | 
                   $this->assertStringContainsString('* '.$rule->get_description($context), $msg->fullmessage);
  | 
        
        
            | 
            | 
           519 | 
                   $this->assertStringContainsString('* '.$rule->get_event_name(), $msg->fullmessage);
  | 
        
        
            | 
            | 
           520 | 
                   $this->assertStringContainsString('* ' . $cm->name, $msg->fullmessage);
  | 
        
        
            | 
            | 
           521 | 
                   $this->assertStringContainsString('* ' . $course->fullname, $msg->fullmessage);
  | 
        
        
            | 
            | 
           522 | 
                   $this->assertStringContainsString('* ' . $course->shortname, $msg->fullmessage);
  | 
        
        
            | 
            | 
           523 | 
              | 
        
        
            | 
            | 
           524 | 
               }
  | 
        
        
            | 
            | 
           525 | 
              | 
        
        
            | 
            | 
           526 | 
               /**
  | 
        
        
            | 
            | 
           527 | 
                * Test observer for user delete event.
  | 
        
        
            | 
            | 
           528 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           529 | 
               public function test_user_deleted(): void {
  | 
        
        
           | 1 | 
           efrain | 
           530 | 
                   global $DB;
  | 
        
        
            | 
            | 
           531 | 
              | 
        
        
            | 
            | 
           532 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           533 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           534 | 
              | 
        
        
            | 
            | 
           535 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           536 | 
                   $course1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           537 | 
                   $course2 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           538 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           539 | 
              | 
        
        
            | 
            | 
           540 | 
                   $rule = new \stdClass();
  | 
        
        
            | 
            | 
           541 | 
                   $rule->userid = $user->id;
  | 
        
        
            | 
            | 
           542 | 
                   $rule->courseid = $course1->id;
  | 
        
        
            | 
            | 
           543 | 
                   $rule->plugin = 'test';
  | 
        
        
            | 
            | 
           544 | 
              | 
        
        
            | 
            | 
           545 | 
                   $sub = new \stdClass();
  | 
        
        
            | 
            | 
           546 | 
                   $sub->courseid = $course1->id;
  | 
        
        
            | 
            | 
           547 | 
                   $sub->userid = $user->id;
  | 
        
        
            | 
            | 
           548 | 
              | 
        
        
            | 
            | 
           549 | 
                   // Add 10 rules for this course with subscriptions.
  | 
        
        
            | 
            | 
           550 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           551 | 
                       $createdrule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           552 | 
                       $sub->ruleid = $createdrule->id;
  | 
        
        
            | 
            | 
           553 | 
                       $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           554 | 
                   }
  | 
        
        
            | 
            | 
           555 | 
              | 
        
        
            | 
            | 
           556 | 
                   // Add 10 random rules for course 2.
  | 
        
        
            | 
            | 
           557 | 
                   $rule->courseid = $course2->id;
  | 
        
        
            | 
            | 
           558 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           559 | 
                       $createdrule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           560 | 
                       $sub->courseid = $rule->courseid;
  | 
        
        
            | 
            | 
           561 | 
                       $sub->ruleid = $createdrule->id;
  | 
        
        
            | 
            | 
           562 | 
                       $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           563 | 
                   }
  | 
        
        
            | 
            | 
           564 | 
              | 
        
        
            | 
            | 
           565 | 
                   // Verify data before user delete.
  | 
        
        
            | 
            | 
           566 | 
                   $totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
  | 
        
        
            | 
            | 
           567 | 
                   $this->assertCount(20, $totalrules);
  | 
        
        
            | 
            | 
           568 | 
                   $totalsubs = $DB->get_records('tool_monitor_subscriptions');
  | 
        
        
            | 
            | 
           569 | 
                   $this->assertCount(20, $totalsubs);
  | 
        
        
            | 
            | 
           570 | 
              | 
        
        
            | 
            | 
           571 | 
                   // Let us delete the user now.
  | 
        
        
            | 
            | 
           572 | 
                   delete_user($user);
  | 
        
        
            | 
            | 
           573 | 
              | 
        
        
            | 
            | 
           574 | 
                   // Verify data after course delete.
  | 
        
        
            | 
            | 
           575 | 
                   $totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
  | 
        
        
            | 
            | 
           576 | 
                   $this->assertCount(20, $totalrules);
  | 
        
        
            | 
            | 
           577 | 
                   $totalsubs = $DB->get_records('tool_monitor_subscriptions');
  | 
        
        
            | 
            | 
           578 | 
                   $this->assertCount(0, $totalsubs); // Make sure all subscriptions are deleted.
  | 
        
        
            | 
            | 
           579 | 
               }
  | 
        
        
            | 
            | 
           580 | 
              | 
        
        
            | 
            | 
           581 | 
               /**
  | 
        
        
            | 
            | 
           582 | 
                * Test observer for course module delete event.
  | 
        
        
            | 
            | 
           583 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           584 | 
               public function test_course_module_deleted(): void {
  | 
        
        
           | 1 | 
           efrain | 
           585 | 
                   global $DB;
  | 
        
        
            | 
            | 
           586 | 
              | 
        
        
            | 
            | 
           587 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           588 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           589 | 
              | 
        
        
            | 
            | 
           590 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           591 | 
                   $course1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           592 | 
                   $course2 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           593 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           594 | 
              | 
        
        
            | 
            | 
           595 | 
                   // Now let us create a rule specific to a module instance.
  | 
        
        
            | 
            | 
           596 | 
                   $cm = new \stdClass();
  | 
        
        
            | 
            | 
           597 | 
                   $cm->course = $course1->id;
  | 
        
        
            | 
            | 
           598 | 
                   $book = $this->getDataGenerator()->create_module('book', $cm);
  | 
        
        
            | 
            | 
           599 | 
              | 
        
        
            | 
            | 
           600 | 
                   $rule = new \stdClass();
  | 
        
        
            | 
            | 
           601 | 
                   $rule->userid = $user->id;
  | 
        
        
            | 
            | 
           602 | 
                   $rule->courseid = $course1->id;
  | 
        
        
            | 
            | 
           603 | 
                   $rule->plugin = 'test';
  | 
        
        
            | 
            | 
           604 | 
              | 
        
        
            | 
            | 
           605 | 
                   $sub = new \stdClass();
  | 
        
        
            | 
            | 
           606 | 
                   $sub->courseid = $course1->id;
  | 
        
        
            | 
            | 
           607 | 
                   $sub->userid = $user->id;
  | 
        
        
            | 
            | 
           608 | 
                   $sub->cmid = $book->cmid;
  | 
        
        
            | 
            | 
           609 | 
              | 
        
        
            | 
            | 
           610 | 
                   // Add 10 rules for this course with subscriptions for this module.
  | 
        
        
            | 
            | 
           611 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           612 | 
                       $createdrule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           613 | 
                       $sub->ruleid = $createdrule->id;
  | 
        
        
            | 
            | 
           614 | 
                       $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           615 | 
                   }
  | 
        
        
            | 
            | 
           616 | 
              | 
        
        
            | 
            | 
           617 | 
                   // Add 10 random rules for course 2.
  | 
        
        
            | 
            | 
           618 | 
                   $rule->courseid = $course2->id;
  | 
        
        
            | 
            | 
           619 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           620 | 
                       $createdrule = $monitorgenerator->create_rule($rule);
  | 
        
        
            | 
            | 
           621 | 
                       $sub->courseid = $rule->courseid;
  | 
        
        
            | 
            | 
           622 | 
                       $sub->ruleid = $createdrule->id;
  | 
        
        
            | 
            | 
           623 | 
                       $sub->cmid = 0;
  | 
        
        
            | 
            | 
           624 | 
                       $monitorgenerator->create_subscription($sub);
  | 
        
        
            | 
            | 
           625 | 
                   }
  | 
        
        
            | 
            | 
           626 | 
              | 
        
        
            | 
            | 
           627 | 
                   // Verify data before module delete.
  | 
        
        
            | 
            | 
           628 | 
                   $totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
  | 
        
        
            | 
            | 
           629 | 
                   $this->assertCount(20, $totalrules);
  | 
        
        
            | 
            | 
           630 | 
                   $totalsubs = $DB->get_records('tool_monitor_subscriptions');
  | 
        
        
            | 
            | 
           631 | 
                   $this->assertCount(20, $totalsubs);
  | 
        
        
            | 
            | 
           632 | 
              | 
        
        
            | 
            | 
           633 | 
                   // Let us delete the user now.
  | 
        
        
            | 
            | 
           634 | 
                   course_delete_module($book->cmid);
  | 
        
        
            | 
            | 
           635 | 
              | 
        
        
            | 
            | 
           636 | 
                   // Verify data after course delete.
  | 
        
        
            | 
            | 
           637 | 
                   $totalrules = \tool_monitor\rule_manager::get_rules_by_plugin('test');
  | 
        
        
            | 
            | 
           638 | 
                   $this->assertCount(20, $totalrules);
  | 
        
        
            | 
            | 
           639 | 
                   $totalsubs = $DB->get_records('tool_monitor_subscriptions');
  | 
        
        
            | 
            | 
           640 | 
                   $this->assertCount(10, $totalsubs); // Make sure only relevant subscriptions are deleted.
  | 
        
        
            | 
            | 
           641 | 
               }
  | 
        
        
            | 
            | 
           642 | 
              | 
        
        
            | 
            | 
           643 | 
           }
  |