Search moodle.org's
Developer Documentation

See Release Notes

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

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace tool_monitor;
  18  
  19  /**
  20   * PHPUnit data generator test case.
  21   *
  22   * @since      Moodle 2.8
  23   * @package    tool_monitor
  24   * @category   test
  25   * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class generator_test extends \advanced_testcase {
  29  
  30      /**
  31       * Set up method.
  32       */
  33      public function setUp(): void {
  34          // Enable monitor.
  35          set_config('enablemonitor', 1, 'tool_monitor');
  36      }
  37  
  38      /**
  39       * Test create_rule data generator.
  40       */
  41      public function test_create_rule() {
  42          $this->setAdminUser();
  43          $this->resetAfterTest(true);
  44          $course = $this->getDataGenerator()->create_course();
  45          $user = $this->getDataGenerator()->create_user();
  46  
  47          $rulegenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  48  
  49          $record = new \stdClass();
  50          $record->courseid = $course->id;
  51          $record->userid = $user->id;
  52  
  53          $rule = $rulegenerator->create_rule($record);
  54          $this->assertInstanceOf('tool_monitor\rule', $rule);
  55          $this->assertEquals($rule->userid, $record->userid);
  56          $this->assertEquals($rule->courseid, $record->courseid);
  57      }
  58  
  59      /**
  60       * Test create_subscription data generator.
  61       */
  62      public function test_create_subscription() {
  63          $this->setAdminUser();
  64          $this->resetAfterTest(true);
  65  
  66          $user = $this->getDataGenerator()->create_user();
  67          $course = $this->getDataGenerator()->create_course();
  68          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  69          $rule = $monitorgenerator->create_rule();
  70  
  71          $record = new \stdClass();
  72          $record->courseid = $course->id;
  73          $record->userid = $user->id;
  74          $record->ruleid = $rule->id;
  75  
  76          $subscription = $monitorgenerator->create_subscription($record);
  77          $this->assertEquals($record->courseid, $subscription->courseid);
  78          $this->assertEquals($record->ruleid, $subscription->ruleid);
  79          $this->assertEquals($record->userid, $subscription->userid);
  80          $this->assertEquals(0, $subscription->cmid);
  81  
  82          // Make sure rule id is always required.
  83          $this->expectException('coding_exception');
  84          unset($record->ruleid);
  85          $monitorgenerator->create_subscription($record);
  86      }
  87  
  88      /**
  89       * Test create_event data generator.
  90       */
  91      public function test_create_event_entries() {
  92          $this->setAdminUser();
  93          $this->resetAfterTest(true);
  94          $context = \context_system::instance();
  95  
  96          // Default data generator values.
  97          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  98  
  99          // First create and assertdata using default values.
 100          $eventdata = $monitorgenerator->create_event_entries();
 101          $this->assertEquals('\core\event\user_loggedin', $eventdata->eventname);
 102          $this->assertEquals($context->id, $eventdata->contextid);
 103          $this->assertEquals($context->contextlevel, $eventdata->contextlevel);
 104      }
 105  
 106      /**
 107       * Test create_history data generator.
 108       */
 109      public function test_create_history() {
 110          $this->setAdminUser();
 111          $this->resetAfterTest(true);
 112          $user = $this->getDataGenerator()->create_user();
 113          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 114          $rule = $monitorgenerator->create_rule();
 115  
 116          $record = new \stdClass();
 117          $record->userid = $user->id;
 118          $record->ruleid = $rule->id;
 119          $sid = $monitorgenerator->create_subscription($record)->id;
 120          $record->sid = $sid;
 121          $historydata = $monitorgenerator->create_history($record);
 122          $this->assertEquals($record->userid, $historydata->userid);
 123          $this->assertEquals($record->sid, $historydata->sid);
 124  
 125          // Test using default values.
 126          $record->userid = 1;
 127          $record->sid = 1;
 128          $historydata = $monitorgenerator->create_history($record);
 129          $this->assertEquals(1, $historydata->userid);
 130          $this->assertEquals(1, $historydata->sid);
 131      }
 132  }