source: moodle/trunk/fuentes/blocks/online_users/tests/online_users_test.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 6.3 KB
Line 
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 * Online users tests
19 *
20 * @package    block_online_users
21 * @category   test
22 * @copyright  2015 University of Nottingham <www.nottingham.ac.uk>
23 * @author     Barry Oosthuizen <barry.oosthuizen@nottingham.ac.uk>
24 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27use block_online_users\fetcher;
28
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * Online users testcase
33 *
34 * @package    block_online_users
35 * @category   test
36 * @copyright  2015 University of Nottingham <www.nottingham.ac.uk>
37 * @author     Barry Oosthuizen <barry.oosthuizen@nottingham.ac.uk>
38 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class block_online_users_testcase extends advanced_testcase {
41
42    protected $data;
43
44    /**
45     * Tests initial setup.
46     *
47     * Prepare the site with some courses, groups, users and
48     * simulate various recent accesses.
49     */
50    protected function setUp() {
51
52        // Generate (simulated) recently logged-in users.
53        $generator = $this->getDataGenerator()->get_plugin_generator('block_online_users');
54        $this->data = $generator->create_logged_in_users();
55
56        // Confirm we have modified the site and requires reset.
57        $this->resetAfterTest(true);
58    }
59
60    /**
61     * Check logged in group 1, 2 & 3 members in course 1 (should be 3, 4 and 0).
62     *
63     * @param array $data Array of user, course and group objects
64     * @param int $now Current Unix timestamp
65     * @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
66     */
67    public function test_fetcher_course1_group_members() {
68        global $CFG;
69
70        $groupid = $this->data['group1']->id;
71        $now = time();
72        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
73        $context = context_course::instance($this->data['course1']->id);
74        $courseid = $this->data['course1']->id;
75        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
76
77        $usercount = $onlineusers->count_users();
78        $users = $onlineusers->get_users();
79        $this->assertEquals(3, $usercount, 'There was a problem counting the number of online users in group 1');
80        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 1');
81
82        $groupid = $this->data['group2']->id;
83        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
84
85        $usercount = $onlineusers->count_users();
86        $users = $onlineusers->get_users();
87        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 2');
88        $this->assertEquals(4, $usercount, 'There was a problem counting the number of online users in group 2');
89
90        $groupid = $this->data['group3']->id;
91        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
92
93        $usercount = $onlineusers->count_users();
94        $users = $onlineusers->get_users();
95        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 3');
96        $this->assertEquals(0, $usercount, 'There was a problem counting the number of online users in group 3');
97    }
98
99    /**
100     * Check logged in users in courses 1 & 2 (should be 9 and 0).
101     *
102     * @param array $data Array of user, course and group objects
103     * @param int $now Current Unix timestamp
104     * @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
105     */
106    public function test_fetcher_courses() {
107
108        global $CFG;
109
110        $currentgroup = null;
111        $now = time();
112        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
113        $context = context_course::instance($this->data['course1']->id);
114        $courseid = $this->data['course1']->id;
115        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
116
117        $usercount = $onlineusers->count_users();
118        $users = $onlineusers->get_users();
119        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in course 1');
120        $this->assertEquals(9, $usercount, 'There was a problem counting the number of online users in course 1');
121
122        $courseid = $this->data['course2']->id;
123        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
124
125        $usercount = $onlineusers->count_users();
126        $users = $onlineusers->get_users();
127        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in course 2');
128        $this->assertEquals(0, $usercount, 'There was a problem counting the number of online users in course 2');
129    }
130
131    /**
132     * Check logged in at the site level (should be 12).
133     *
134     * @param int $now Current Unix timestamp
135     * @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
136     */
137    public function test_fetcher_sitelevel() {
138        global $CFG;
139
140        $currentgroup = null;
141        $now = time();
142        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
143        $context = context_system::instance();
144        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
145
146        $usercount = $onlineusers->count_users();
147        $users = $onlineusers->get_users();
148        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users at site level');
149        $this->assertEquals(12, $usercount, 'There was a problem counting the number of online users at site level');
150    }
151}
Note: See TracBrowser for help on using the repository browser.