Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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 subscription 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 subscription_manager_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test count_rule_subscriptions method.
  31       */
  32      public function test_count_rule_subscriptions() {
  33  
  34          $this->setAdminUser();
  35          $this->resetAfterTest(true);
  36  
  37          // Create users.
  38          $user1 = $this->getDataGenerator()->create_user();
  39          $user2 = $this->getDataGenerator()->create_user();
  40  
  41          // Create few rules.
  42          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  43          $rule1 = $monitorgenerator->create_rule();
  44          $rule2 = $monitorgenerator->create_rule();
  45          $subs = \tool_monitor\subscription_manager::count_rule_subscriptions($rule1->id);
  46  
  47          // No subscriptions at this point.
  48          $this->assertEquals(0, $subs);
  49  
  50          // Subscribe user 1 to rule 1.
  51          $record = new \stdClass;
  52          $record->ruleid = $rule1->id;
  53          $record->userid = $user1->id;
  54          $monitorgenerator->create_subscription($record);
  55  
  56          // Subscribe user 2 to rule 1.
  57          $record->userid = $user2->id;
  58          $monitorgenerator->create_subscription($record);
  59  
  60          // Subscribe user 2 to rule 2.
  61          $record->ruleid = $rule2->id;
  62          $monitorgenerator->create_subscription($record);
  63  
  64          // Should have 2 subscriptions for rule 1 and 1 subscription for rule 2
  65          $subs1 = \tool_monitor\subscription_manager::count_rule_subscriptions($rule1->id);
  66          $subs2 = \tool_monitor\subscription_manager::count_rule_subscriptions($rule2->id);
  67          $this->assertEquals(2, $subs1);
  68          $this->assertEquals(1, $subs2);
  69      }
  70  }