| 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 | 
           /**
  | 
        
        
            | 
            | 
           20 | 
            * Unit tests for rule manager api.
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * @package    tool_monitor
  | 
        
        
            | 
            | 
           23 | 
            * @category   test
  | 
        
        
            | 
            | 
           24 | 
            * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
  | 
        
        
            | 
            | 
           25 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           26 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           27 | 
           final class rule_manager_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
               /**
  | 
        
        
            | 
            | 
           30 | 
                * Set up method.
  | 
        
        
            | 
            | 
           31 | 
                */
  | 
        
        
            | 
            | 
           32 | 
               public function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           33 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           34 | 
                   // Enable monitor.
  | 
        
        
            | 
            | 
           35 | 
                   set_config('enablemonitor', 1, 'tool_monitor');
  | 
        
        
            | 
            | 
           36 | 
               }
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
               /**
  | 
        
        
            | 
            | 
           39 | 
                * Test add_rule method.
  | 
        
        
            | 
            | 
           40 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           41 | 
               public function test_add_rule(): void {
  | 
        
        
           | 1 | 
           efrain | 
           42 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           43 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           46 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           47 | 
                   $now = time();
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
                   $rule = new \stdClass();
  | 
        
        
            | 
            | 
           50 | 
                   $rule->userid = $user->id;
  | 
        
        
            | 
            | 
           51 | 
                   $rule->courseid = $course->id;
  | 
        
        
            | 
            | 
           52 | 
                   $rule->name = 'test rule 1';
  | 
        
        
            | 
            | 
           53 | 
                   $rule->plugin = 'core';
  | 
        
        
            | 
            | 
           54 | 
                   $rule->eventname = '\core\event\course_updated';
  | 
        
        
            | 
            | 
           55 | 
                   $rule->description = 'test description 1';
  | 
        
        
            | 
            | 
           56 | 
                   $rule->descriptionformat = FORMAT_HTML;
  | 
        
        
            | 
            | 
           57 | 
                   $rule->frequency = 15;
  | 
        
        
            | 
            | 
           58 | 
                   $rule->template = 'test template message';
  | 
        
        
            | 
            | 
           59 | 
                   $rule->templateformat = FORMAT_HTML;
  | 
        
        
            | 
            | 
           60 | 
                   $rule->timewindow = 300;
  | 
        
        
            | 
            | 
           61 | 
                   $rule->timecreated = $now;
  | 
        
        
            | 
            | 
           62 | 
                   $rule->timemodified = $now;
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   $ruledata = \tool_monitor\rule_manager::add_rule($rule);
  | 
        
        
            | 
            | 
           65 | 
                   foreach ($rule as $prop => $value) {
  | 
        
        
            | 
            | 
           66 | 
                       $this->assertEquals($ruledata->$prop, $value);
  | 
        
        
            | 
            | 
           67 | 
                   }
  | 
        
        
            | 
            | 
           68 | 
               }
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
               /**
  | 
        
        
            | 
            | 
           71 | 
                * Test get_rule method.
  | 
        
        
            | 
            | 
           72 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           73 | 
               public function test_get_rule(): void {
  | 
        
        
           | 1 | 
           efrain | 
           74 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           75 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           78 | 
                   $rule = $monitorgenerator->create_rule();
  | 
        
        
            | 
            | 
           79 | 
                   $rules1 = \tool_monitor\rule_manager::get_rule($rule->id);
  | 
        
        
            | 
            | 
           80 | 
                   $this->assertInstanceOf('tool_monitor\rule', $rules1);
  | 
        
        
            | 
            | 
           81 | 
                   $this->assertEquals($rules1, $rule);
  | 
        
        
            | 
            | 
           82 | 
               }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
               /**
  | 
        
        
            | 
            | 
           85 | 
                * Test update_rule method.
  | 
        
        
            | 
            | 
           86 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           87 | 
               public function test_update_rule(): void {
  | 
        
        
           | 1 | 
           efrain | 
           88 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           89 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           92 | 
                   $rule = $monitorgenerator->create_rule();
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   $ruledata = new \stdClass;
  | 
        
        
            | 
            | 
           95 | 
                   $ruledata->id = $rule->id;
  | 
        
        
            | 
            | 
           96 | 
                   $ruledata->frequency = 25;
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
                   \tool_monitor\rule_manager::update_rule($ruledata);
  | 
        
        
            | 
            | 
           99 | 
                   $this->assertEquals(25, $ruledata->frequency);
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
               }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
               /**
  | 
        
        
            | 
            | 
           104 | 
                * Test get_rules_by_courseid method.
  | 
        
        
            | 
            | 
           105 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           106 | 
               public function test_get_rules_by_courseid(): void {
  | 
        
        
           | 1 | 
           efrain | 
           107 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           108 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                   $course1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           113 | 
                   $course2 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
                   $record = new \stdClass();
  | 
        
        
            | 
            | 
           116 | 
                   $record->courseid = $course1->id;
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
                   $record2 = new \stdClass();
  | 
        
        
            | 
            | 
           119 | 
                   $record2->courseid = $course2->id;
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                   $ruleids = array();
  | 
        
        
            | 
            | 
           122 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           123 | 
                       $rule = $monitorgenerator->create_rule($record);
  | 
        
        
            | 
            | 
           124 | 
                       $ruleids[] = $rule->id;
  | 
        
        
            | 
            | 
           125 | 
                       $rule = $monitorgenerator->create_rule(); // Create some site level rules.
  | 
        
        
            | 
            | 
           126 | 
                       $ruleids[] = $rule->id;
  | 
        
        
            | 
            | 
           127 | 
                       $rule = $monitorgenerator->create_rule($record2); // Create rules in a different course.
  | 
        
        
            | 
            | 
           128 | 
                   }
  | 
        
        
            | 
            | 
           129 | 
                   $ruledata = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id);
  | 
        
        
            | 
            | 
           130 | 
                   $this->assertEmpty(array_merge(array_diff(array_keys($ruledata), $ruleids), array_diff($ruleids, array_keys($ruledata))));
  | 
        
        
            | 
            | 
           131 | 
                   $this->assertCount(20, $ruledata);
  | 
        
        
            | 
            | 
           132 | 
               }
  | 
        
        
            | 
            | 
           133 | 
              | 
        
        
            | 
            | 
           134 | 
               /**
  | 
        
        
            | 
            | 
           135 | 
                * Test get_rules_by_plugin method.
  | 
        
        
            | 
            | 
           136 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           137 | 
               public function test_get_rules_by_plugin(): void {
  | 
        
        
           | 1 | 
           efrain | 
           138 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           139 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
                   $record = new \stdClass();
  | 
        
        
            | 
            | 
           144 | 
                   $record->plugin = 'core';
  | 
        
        
            | 
            | 
           145 | 
              | 
        
        
            | 
            | 
           146 | 
                   $record2 = new \stdClass();
  | 
        
        
            | 
            | 
           147 | 
                   $record2->plugin = 'mod_assign';
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
                   $ruleids = array();
  | 
        
        
            | 
            | 
           150 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           151 | 
                       $rule = $monitorgenerator->create_rule($record);
  | 
        
        
            | 
            | 
           152 | 
                       $ruleids[] = $rule->id;
  | 
        
        
            | 
            | 
           153 | 
                       $rule = $monitorgenerator->create_rule($record2); // Create rules in a different plugin.
  | 
        
        
            | 
            | 
           154 | 
                   }
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
                   $ruledata = \tool_monitor\rule_manager::get_rules_by_plugin('core');
  | 
        
        
            | 
            | 
           157 | 
                   $this->assertEmpty(array_merge(array_diff(array_keys($ruledata), $ruleids), array_diff($ruleids, array_keys($ruledata))));
  | 
        
        
            | 
            | 
           158 | 
                   $this->assertCount(10, $ruledata);
  | 
        
        
            | 
            | 
           159 | 
               }
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
               /**
  | 
        
        
            | 
            | 
           162 | 
                * Test get_rules_by_event method.
  | 
        
        
            | 
            | 
           163 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           164 | 
               public function test_get_rules_by_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           165 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           166 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           167 | 
              | 
        
        
            | 
            | 
           168 | 
                   $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  | 
        
        
            | 
            | 
           169 | 
                   $rule = $monitorgenerator->create_rule();
  | 
        
        
            | 
            | 
           170 | 
              | 
        
        
            | 
            | 
           171 | 
                   $record = new \stdClass();
  | 
        
        
            | 
            | 
           172 | 
                   $record->eventname = '\core\event\calendar_event_created';
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                   $record2 = new \stdClass();
  | 
        
        
            | 
            | 
           175 | 
                   $record2->eventname = '\core\event\calendar_event_updated';
  | 
        
        
            | 
            | 
           176 | 
              | 
        
        
            | 
            | 
           177 | 
                   $ruleids = array();
  | 
        
        
            | 
            | 
           178 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           179 | 
                       $rule = $monitorgenerator->create_rule($record);
  | 
        
        
            | 
            | 
           180 | 
                       $ruleids[] = $rule->id;
  | 
        
        
            | 
            | 
           181 | 
                       $rule = $monitorgenerator->create_rule($record2); // Create rules in a different plugin.
  | 
        
        
            | 
            | 
           182 | 
                   }
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
                   $ruledata = \tool_monitor\rule_manager::get_rules_by_event('\core\event\calendar_event_created');
  | 
        
        
            | 
            | 
           185 | 
                   $this->assertEmpty(array_diff(array_keys($ruledata), $ruleids));
  | 
        
        
            | 
            | 
           186 | 
                   $this->assertCount(10, $ruledata);
  | 
        
        
            | 
            | 
           187 | 
               }
  | 
        
        
            | 
            | 
           188 | 
           }
  |