Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for rule manager api.
  19   *
  20   * @package    tool_monitor
  21   * @category   test
  22   * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  /**
  31   * Tests for rule manager.
  32   *
  33   * Class tool_monitor_rule_manager_testcase
  34   */
  35  class tool_monitor_rule_manager_testcase extends advanced_testcase {
  36  
  37      /**
  38       * Set up method.
  39       */
  40      public function setUp(): void {
  41          // Enable monitor.
  42          set_config('enablemonitor', 1, 'tool_monitor');
  43      }
  44  
  45      /**
  46       * Test add_rule method.
  47       */
  48      public function test_add_rule() {
  49          $this->setAdminUser();
  50          $this->resetAfterTest(true);
  51  
  52          $user = $this->getDataGenerator()->create_user();
  53          $course = $this->getDataGenerator()->create_course();
  54          $now = time();
  55  
  56          $rule = new stdClass();
  57          $rule->userid = $user->id;
  58          $rule->courseid = $course->id;
  59          $rule->name = 'test rule 1';
  60          $rule->plugin = 'core';
  61          $rule->eventname = '\core\event\course_updated';
  62          $rule->description = 'test description 1';
  63          $rule->descriptionformat = FORMAT_HTML;
  64          $rule->frequency = 15;
  65          $rule->template = 'test template message';
  66          $rule->templateformat = FORMAT_HTML;
  67          $rule->timewindow = 300;
  68          $rule->timecreated = $now;
  69          $rule->timemodified = $now;
  70  
  71          $ruledata = \tool_monitor\rule_manager::add_rule($rule);
  72          foreach ($rule as $prop => $value) {
  73              $this->assertEquals($ruledata->$prop, $value);
  74          }
  75      }
  76  
  77      /**
  78       * Test get_rule method.
  79       */
  80      public function test_get_rule() {
  81          $this->setAdminUser();
  82          $this->resetAfterTest(true);
  83  
  84          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  85          $rule = $monitorgenerator->create_rule();
  86          $rules1 = \tool_monitor\rule_manager::get_rule($rule->id);
  87          $this->assertInstanceOf('tool_monitor\rule', $rules1);
  88          $this->assertEquals($rules1, $rule);
  89      }
  90  
  91      /**
  92       * Test update_rule method.
  93       */
  94      public function test_update_rule() {
  95          $this->setAdminUser();
  96          $this->resetAfterTest(true);
  97  
  98          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  99          $rule = $monitorgenerator->create_rule();
 100  
 101          $ruledata = new stdClass;
 102          $ruledata->id = $rule->id;
 103          $ruledata->frequency = 25;
 104  
 105          \tool_monitor\rule_manager::update_rule($ruledata);
 106          $this->assertEquals(25, $ruledata->frequency);
 107  
 108      }
 109  
 110      /**
 111       * Test get_rules_by_courseid method.
 112       */
 113      public function test_get_rules_by_courseid() {
 114          $this->setAdminUser();
 115          $this->resetAfterTest(true);
 116  
 117          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 118  
 119          $course1 = $this->getDataGenerator()->create_course();
 120          $course2 = $this->getDataGenerator()->create_course();
 121  
 122          $record = new stdClass();
 123          $record->courseid = $course1->id;
 124  
 125          $record2 = new stdClass();
 126          $record2->courseid = $course2->id;
 127  
 128          $ruleids = array();
 129          for ($i = 0; $i < 10; $i++) {
 130              $rule = $monitorgenerator->create_rule($record);
 131              $ruleids[] = $rule->id;
 132              $rule = $monitorgenerator->create_rule(); // Create some site level rules.
 133              $ruleids[] = $rule->id;
 134              $rule = $monitorgenerator->create_rule($record2); // Create rules in a different course.
 135          }
 136          $ruledata = \tool_monitor\rule_manager::get_rules_by_courseid($course1->id);
 137          $this->assertEmpty(array_merge(array_diff(array_keys($ruledata), $ruleids), array_diff($ruleids, array_keys($ruledata))));
 138          $this->assertCount(20, $ruledata);
 139      }
 140  
 141      /**
 142       * Test get_rules_by_plugin method.
 143       */
 144      public function test_get_rules_by_plugin() {
 145          $this->setAdminUser();
 146          $this->resetAfterTest(true);
 147  
 148          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 149  
 150          $record = new stdClass();
 151          $record->plugin = 'core';
 152  
 153          $record2 = new stdClass();
 154          $record2->plugin = 'mod_assign';
 155  
 156          $ruleids = array();
 157          for ($i = 0; $i < 10; $i++) {
 158              $rule = $monitorgenerator->create_rule($record);
 159              $ruleids[] = $rule->id;
 160              $rule = $monitorgenerator->create_rule($record2); // Create rules in a different plugin.
 161          }
 162  
 163          $ruledata = \tool_monitor\rule_manager::get_rules_by_plugin('core');
 164          $this->assertEmpty(array_merge(array_diff(array_keys($ruledata), $ruleids), array_diff($ruleids, array_keys($ruledata))));
 165          $this->assertCount(10, $ruledata);
 166      }
 167  
 168      /**
 169       * Test get_rules_by_event method.
 170       */
 171      public function test_get_rules_by_event() {
 172          $this->setAdminUser();
 173          $this->resetAfterTest(true);
 174  
 175          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 176          $rule = $monitorgenerator->create_rule();
 177  
 178          $record = new stdClass();
 179          $record->eventname = '\core\event\calendar_event_created';
 180  
 181          $record2 = new stdClass();
 182          $record2->eventname = '\core\event\calendar_event_updated';
 183  
 184          $ruleids = array();
 185          for ($i = 0; $i < 10; $i++) {
 186              $rule = $monitorgenerator->create_rule($record);
 187              $ruleids[] = $rule->id;
 188              $rule = $monitorgenerator->create_rule($record2); // Create rules in a different plugin.
 189          }
 190  
 191          $ruledata = \tool_monitor\rule_manager::get_rules_by_event('\core\event\calendar_event_created');
 192          $this->assertEmpty(array_diff(array_keys($ruledata), $ruleids));
 193          $this->assertCount(10, $ruledata);
 194      }
 195  }