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   * PHPUnit data generator tests.
  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  /**
  29   * PHPUnit data generator test case.
  30   *
  31   * @since      Moodle 2.8
  32   * @package    tool_monitor
  33   * @category   test
  34   * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class tool_monitor_generator_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Set up method.
  41       */
  42      public function setUp(): void {
  43          // Enable monitor.
  44          set_config('enablemonitor', 1, 'tool_monitor');
  45      }
  46  
  47      /**
  48       * Test create_rule data generator.
  49       */
  50      public function test_create_rule() {
  51          $this->setAdminUser();
  52          $this->resetAfterTest(true);
  53          $course = $this->getDataGenerator()->create_course();
  54          $user = $this->getDataGenerator()->create_user();
  55  
  56          $rulegenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  57  
  58          $record = new stdClass();
  59          $record->courseid = $course->id;
  60          $record->userid = $user->id;
  61  
  62          $rule = $rulegenerator->create_rule($record);
  63          $this->assertInstanceOf('tool_monitor\rule', $rule);
  64          $this->assertEquals($rule->userid, $record->userid);
  65          $this->assertEquals($rule->courseid, $record->courseid);
  66      }
  67  
  68      /**
  69       * Test create_subscription data generator.
  70       */
  71      public function test_create_subscription() {
  72          $this->setAdminUser();
  73          $this->resetAfterTest(true);
  74  
  75          $user = $this->getDataGenerator()->create_user();
  76          $course = $this->getDataGenerator()->create_course();
  77          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  78          $rule = $monitorgenerator->create_rule();
  79  
  80          $record = new stdClass();
  81          $record->courseid = $course->id;
  82          $record->userid = $user->id;
  83          $record->ruleid = $rule->id;
  84  
  85          $subscription = $monitorgenerator->create_subscription($record);
  86          $this->assertEquals($record->courseid, $subscription->courseid);
  87          $this->assertEquals($record->ruleid, $subscription->ruleid);
  88          $this->assertEquals($record->userid, $subscription->userid);
  89          $this->assertEquals(0, $subscription->cmid);
  90  
  91          // Make sure rule id is always required.
  92          $this->expectException('coding_exception');
  93          unset($record->ruleid);
  94          $monitorgenerator->create_subscription($record);
  95      }
  96  
  97      /**
  98       * Test create_event data generator.
  99       */
 100      public function test_create_event_entries() {
 101          $this->setAdminUser();
 102          $this->resetAfterTest(true);
 103          $context = \context_system::instance();
 104  
 105          // Default data generator values.
 106          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 107  
 108          // First create and assertdata using default values.
 109          $eventdata = $monitorgenerator->create_event_entries();
 110          $this->assertEquals('\core\event\user_loggedin', $eventdata->eventname);
 111          $this->assertEquals($context->id, $eventdata->contextid);
 112          $this->assertEquals($context->contextlevel, $eventdata->contextlevel);
 113      }
 114  
 115      /**
 116       * Test create_history data generator.
 117       */
 118      public function test_create_history() {
 119          $this->setAdminUser();
 120          $this->resetAfterTest(true);
 121          $user = $this->getDataGenerator()->create_user();
 122          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 123          $rule = $monitorgenerator->create_rule();
 124  
 125          $record = new \stdClass();
 126          $record->userid = $user->id;
 127          $record->ruleid = $rule->id;
 128          $sid = $monitorgenerator->create_subscription($record)->id;
 129          $record->sid = $sid;
 130          $historydata = $monitorgenerator->create_history($record);
 131          $this->assertEquals($record->userid, $historydata->userid);
 132          $this->assertEquals($record->sid, $historydata->sid);
 133  
 134          // Test using default values.
 135          $record->userid = 1;
 136          $record->sid = 1;
 137          $historydata = $monitorgenerator->create_history($record);
 138          $this->assertEquals(1, $historydata->userid);
 139          $this->assertEquals(1, $historydata->sid);
 140      }
 141  }