Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 block_online_users;
18
 
19
/**
20
 * Online users testcase
21
 *
22
 * @package    block_online_users
23
 * @category   test
24
 * @copyright  2015 University of Nottingham <www.nottingham.ac.uk>
25
 * @author     Barry Oosthuizen <barry.oosthuizen@nottingham.ac.uk>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
1441 ariadna 28
final class online_users_test extends \advanced_testcase {
1 efrain 29
 
30
    protected $data;
31
 
32
    /**
33
     * Tests initial setup.
34
     *
35
     * Prepare the site with some courses, groups, users and
36
     * simulate various recent accesses.
37
     */
38
    protected function setUp(): void {
1441 ariadna 39
        parent::setUp();
1 efrain 40
 
41
        // Generate (simulated) recently logged-in users.
42
        $generator = $this->getDataGenerator()->get_plugin_generator('block_online_users');
43
        $this->data = $generator->create_logged_in_users();
44
 
45
        // Confirm we have modified the site and requires reset.
46
        $this->resetAfterTest(true);
47
    }
48
 
49
    /**
50
     * Check logged in group 1, 2 & 3 members in course 1 (should be 3, 4 and 0).
51
     *
52
     * @param array $data Array of user, course and group objects
53
     * @param int $now Current Unix timestamp
54
     * @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
55
     */
11 efrain 56
    public function test_fetcher_course1_group_members(): void {
1 efrain 57
        global $CFG;
58
 
59
        $groupid = $this->data['group1']->id;
60
        $now = time();
61
        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
62
        $context = \context_course::instance($this->data['course1']->id);
63
        $courseid = $this->data['course1']->id;
64
        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
65
 
66
        $usercount = $onlineusers->count_users();
67
        $users = $onlineusers->get_users();
68
        $this->assertEquals(3, $usercount, 'There was a problem counting the number of online users in group 1');
69
        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 1');
70
 
71
        $groupid = $this->data['group2']->id;
72
        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
73
 
74
        $usercount = $onlineusers->count_users();
75
        $users = $onlineusers->get_users();
76
        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 2');
77
        $this->assertEquals(4, $usercount, 'There was a problem counting the number of online users in group 2');
78
 
79
        $groupid = $this->data['group3']->id;
80
        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
81
 
82
        $usercount = $onlineusers->count_users();
83
        $users = $onlineusers->get_users();
84
        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 3');
85
        $this->assertEquals(0, $usercount, 'There was a problem counting the number of online users in group 3');
86
    }
87
 
88
    /**
89
     * Check logged in users in courses 1 & 2 (should be 9 and 0).
90
     *
91
     * @param array $data Array of user, course and group objects
92
     * @param int $now Current Unix timestamp
93
     * @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
94
     */
11 efrain 95
    public function test_fetcher_courses(): void {
1 efrain 96
 
97
        global $CFG;
98
 
99
        $currentgroup = null;
100
        $now = time();
101
        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
102
        $context = \context_course::instance($this->data['course1']->id);
103
        $courseid = $this->data['course1']->id;
104
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
105
 
106
        $usercount = $onlineusers->count_users();
107
        $users = $onlineusers->get_users();
108
        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in course 1');
109
        $this->assertEquals(9, $usercount, 'There was a problem counting the number of online users in course 1');
110
 
111
        $courseid = $this->data['course2']->id;
112
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
113
 
114
        $usercount = $onlineusers->count_users();
115
        $users = $onlineusers->get_users();
116
        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in course 2');
117
        $this->assertEquals(0, $usercount, 'There was a problem counting the number of online users in course 2');
118
    }
119
 
120
    /**
121
     * Check logged in at the site level (should be 12).
122
     *
123
     * @param int $now Current Unix timestamp
124
     * @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
125
     */
11 efrain 126
    public function test_fetcher_sitelevel(): void {
1 efrain 127
        global $CFG;
128
 
129
        $currentgroup = null;
130
        $now = time();
131
        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
132
        $context = \context_system::instance();
133
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
134
 
135
        $usercount = $onlineusers->count_users();
136
        $users = $onlineusers->get_users();
137
        $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users at site level');
138
        $this->assertEquals(12, $usercount, 'There was a problem counting the number of online users at site level');
139
    }
140
 
141
    /**
142
     * Check user visibility setting for course group members.
143
     */
11 efrain 144
    public function test_user_visibility_course1_group1_members(): void {
1 efrain 145
        global $CFG;
146
 
147
        // Enable users to set their visibility to others in the online users block.
148
        $CFG->block_online_users_onlinestatushiding = true;
149
        $groupid = $this->data['group1']->id;
150
        $now = time();
151
        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
152
        $context = \context_course::instance($this->data['course1']->id);
153
        $courseid = $this->data['course1']->id;
154
        $user1 = $this->data['user1'];
155
        $user2 = $this->data['user2'];
156
        // Set user2 as logged user.
157
        $this->setUser($user2);
158
        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
159
        $users = $onlineusers->get_users();
160
        $usercount = $onlineusers->count_users();
161
        // User1 should be displayed in the online users block.
162
        $this->assertEquals(3, $usercount);
163
        $this->assertTrue(array_key_exists($user1->id, $users));
164
        // Set user1 as logged user.
165
        $this->setUser($user1);
166
        // Set visibility to 'hide' for user1.
167
        set_user_preference('block_online_users_uservisibility', 0);
168
        // Test if the fetcher gets all the users including user1.
169
        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
170
        $users = $onlineusers->get_users();
171
        $usercount = $onlineusers->count_users();
172
        // User1 should be displayed in the online users block.
173
        $this->assertEquals(3, $usercount);
174
        $this->assertTrue(array_key_exists($user1->id, $users));
175
        // Set user2 as logged user.
176
        $this->setUser($user2);
177
        // Test if the fetcher gets all the users excluding user1.
178
        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
179
        $users = $onlineusers->get_users();
180
        $usercount = $onlineusers->count_users();
181
        // User1 should not be displayed in the online users block.
182
        $this->assertEquals(2, $usercount);
183
        $this->assertFalse(array_key_exists($user1->id, $users));
184
 
185
        // Disable users to set their visibility to others in the online users block.
186
        // All users should be displayed now and the visibility status of a users should be ignored,
187
        // as the capability of setting the visibility to other user has been disabled.
188
        $CFG->block_online_users_onlinestatushiding = false;
189
        // Test if the fetcher gets all the users including user1.
190
        $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
191
        $users = $onlineusers->get_users();
192
        $usercount = $onlineusers->count_users();
193
        // User1 should be displayed in the online users block.
194
        $this->assertEquals(3, $usercount);
195
        $this->assertTrue(array_key_exists($user1->id, $users));
196
    }
197
 
198
    /**
199
     * Check user visibility setting at course level.
200
     */
11 efrain 201
    public function test_user_visibility_courses(): void {
1 efrain 202
        global $CFG;
203
 
204
        $currentgroup = null;
205
        $now = time();
206
        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
207
        $context = \context_course::instance($this->data['course1']->id);
208
        $courseid = $this->data['course1']->id;
209
        $user1 = $this->data['user1'];
210
        $user2 = $this->data['user2'];
211
        // Set user2 as logged user.
212
        $this->setUser($user2);
213
        // Test if the fetcher gets all the users including user1.
214
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
215
        $users = $onlineusers->get_users();
216
        $usercount = $onlineusers->count_users();
217
        // User1 should be displayed in the online users block.
218
        $this->assertEquals(9, $usercount);
219
        $this->assertTrue(array_key_exists($user1->id, $users));
220
        // Set user1 as logged user.
221
        $this->setUser($user1);
222
        // Set visibility to 'hide' for user1.
223
        set_user_preference('block_online_users_uservisibility', 0);
224
        // Test if the fetcher gets all the users including user1.
225
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
226
        $users = $onlineusers->get_users();
227
        $usercount = $onlineusers->count_users();
228
        // User1 should be displayed in the online users block.
229
        $this->assertEquals(9, $usercount);
230
        $this->assertTrue(array_key_exists($user1->id, $users));
231
        // Set user2 as logged user.
232
        $this->setUser($user2);
233
        // Test if the fetcher gets all the users excluding user1.
234
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
235
        $users = $onlineusers->get_users();
236
        $usercount = $onlineusers->count_users();
237
        // User1 should not be displayed in the online users block.
238
        $this->assertEquals(8, $usercount);
239
        $this->assertFalse(array_key_exists($user1->id, $users));
240
 
241
        // Disable users to set their visibility to others in the online users block.
242
        // All users should be displayed now and the visibility status of a users should be ignored,
243
        // as the capability of setting the visibility to other user has been disabled.
244
        $CFG->block_online_users_onlinestatushiding = false;
245
        // Test if the fetcher gets all the users including user1.
246
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
247
        $users = $onlineusers->get_users();
248
        $usercount = $onlineusers->count_users();
249
        // User1 should be displayed in the online users block.
250
        $this->assertEquals(9, $usercount);
251
        $this->assertTrue(array_key_exists($user1->id, $users));
252
    }
253
 
254
    /**
255
     * Check user visibility setting at site level.
256
     */
11 efrain 257
    public function test_user_visibility_sitelevel(): void {
1 efrain 258
        global $CFG;
259
 
260
        $currentgroup = null;
261
        $now = time();
262
        $timetoshowusers = $CFG->block_online_users_timetosee * 60;
263
        $context = \context_system::instance();
264
        $user1 = $this->data['user1'];
265
        $user2 = $this->data['user2'];
266
        // Set user2 as logged user.
267
        $this->setUser($user2);
268
        // Test if the fetcher gets all the users including user1.
269
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
270
        $users = $onlineusers->get_users();
271
        $usercount = $onlineusers->count_users();
272
        // User1 should be displayed in the online users block.
273
        $this->assertEquals(12, $usercount);
274
        $this->assertTrue(array_key_exists($user1->id, $users));
275
        // Set user1 as logged user.
276
        $this->setUser($user1);
277
        // Set visibility to 'hide' for user1.
278
        set_user_preference('block_online_users_uservisibility', 0);
279
        // Test if the fetcher gets all the users including user1.
280
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
281
        $users = $onlineusers->get_users();
282
        $usercount = $onlineusers->count_users();
283
        // User1 should be displayed in the online users block.
284
        $this->assertEquals(12, $usercount);
285
        $this->assertTrue(array_key_exists($user1->id, $users));
286
        // Set user2 as logged user.
287
        $this->setUser($user2);
288
        // Test if the fetcher gets all the users excluding user1.
289
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
290
        $users = $onlineusers->get_users();
291
        $usercount = $onlineusers->count_users();
292
        // User1 should not be displayed in the online users block.
293
        $this->assertEquals(11, $usercount);
294
        $this->assertFalse(array_key_exists($user1->id, $users));
295
 
296
        // Disable users to set their visibility to others in the online users block.
297
        // All users should be displayed now and the visibility status of a users should be ignored,
298
        // as the capability of setting the visibility to other user has been disabled.
299
        $CFG->block_online_users_onlinestatushiding = false;
300
        // Test if the fetcher gets all the users including user1.
301
        $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
302
        $users = $onlineusers->get_users();
303
        $usercount = $onlineusers->count_users();
304
        // User1 should be displayed in the online users block.
305
        $this->assertEquals(12, $usercount);
306
        $this->assertTrue(array_key_exists($user1->id, $users));
307
    }
308
}