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