2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for subscription manager api.
20 * @package tool_monitor
22 * @copyright 2014 onwards Simey Lameze <simey@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
31 * Tests for subscription manager.
33 * Class tool_monitor_subscription_manager_testcase.
35 class tool_monitor_subscription_manager_testcase extends advanced_testcase {
38 * Test count_rule_subscriptions method.
40 public function test_count_rule_subscriptions() {
42 $this->setAdminUser();
43 $this->resetAfterTest(true);
46 $user1 = $this->getDataGenerator()->create_user();
47 $user2 = $this->getDataGenerator()->create_user();
50 $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
51 $rule1 = $monitorgenerator->create_rule();
52 $rule2 = $monitorgenerator->create_rule();
53 $subs = \tool_monitor\subscription_manager::count_rule_subscriptions($rule1->id);
55 // No subscriptions at this point.
56 $this->assertEquals(0, $subs);
58 // Subscribe user 1 to rule 1.
59 $record = new stdClass;
60 $record->ruleid = $rule1->id;
61 $record->userid = $user1->id;
62 $monitorgenerator->create_subscription($record);
64 // Subscribe user 2 to rule 1.
65 $record->userid = $user2->id;
66 $monitorgenerator->create_subscription($record);
68 // Subscribe user 2 to rule 2.
69 $record->ruleid = $rule2->id;
70 $monitorgenerator->create_subscription($record);
72 // Should have 2 subscriptions for rule 1 and 1 subscription for rule 2
73 $subs1 = \tool_monitor\subscription_manager::count_rule_subscriptions($rule1->id);
74 $subs2 = \tool_monitor\subscription_manager::count_rule_subscriptions($rule2->id);
75 $this->assertEquals(2, $subs1);
76 $this->assertEquals(1, $subs2);