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 * 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 27 use block_online_users\fetcher; 28 29 defined('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 */ 40 class 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(): void { 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 152 /** 153 * Check user visibility setting for course group members. 154 */ 155 public function test_user_visibility_course1_group1_members() { 156 global $CFG; 157 158 // Enable users to set their visibility to others in the online users block. 159 $CFG->block_online_users_onlinestatushiding = true; 160 $groupid = $this->data['group1']->id; 161 $now = time(); 162 $timetoshowusers = $CFG->block_online_users_timetosee * 60; 163 $context = context_course::instance($this->data['course1']->id); 164 $courseid = $this->data['course1']->id; 165 $user1 = $this->data['user1']; 166 $user2 = $this->data['user2']; 167 // Set user2 as logged user. 168 $this->setUser($user2); 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 user1 as logged user. 176 $this->setUser($user1); 177 // Set visibility to 'hide' for user1. 178 set_user_preference('block_online_users_uservisibility', 0); 179 // Test if the fetcher gets all the users including user1. 180 $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid); 181 $users = $onlineusers->get_users(); 182 $usercount = $onlineusers->count_users(); 183 // User1 should be displayed in the online users block. 184 $this->assertEquals(3, $usercount); 185 $this->assertTrue(array_key_exists($user1->id, $users)); 186 // Set user2 as logged user. 187 $this->setUser($user2); 188 // Test if the fetcher gets all the users excluding user1. 189 $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid); 190 $users = $onlineusers->get_users(); 191 $usercount = $onlineusers->count_users(); 192 // User1 should not be displayed in the online users block. 193 $this->assertEquals(2, $usercount); 194 $this->assertFalse(array_key_exists($user1->id, $users)); 195 196 // Disable users to set their visibility to others in the online users block. 197 // All users should be displayed now and the visibility status of a users should be ignored, 198 // as the capability of setting the visibility to other user has been disabled. 199 $CFG->block_online_users_onlinestatushiding = false; 200 // Test if the fetcher gets all the users including user1. 201 $onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid); 202 $users = $onlineusers->get_users(); 203 $usercount = $onlineusers->count_users(); 204 // User1 should be displayed in the online users block. 205 $this->assertEquals(3, $usercount); 206 $this->assertTrue(array_key_exists($user1->id, $users)); 207 } 208 209 /** 210 * Check user visibility setting at course level. 211 */ 212 public function test_user_visibility_courses() { 213 global $CFG; 214 215 $currentgroup = null; 216 $now = time(); 217 $timetoshowusers = $CFG->block_online_users_timetosee * 60; 218 $context = context_course::instance($this->data['course1']->id); 219 $courseid = $this->data['course1']->id; 220 $user1 = $this->data['user1']; 221 $user2 = $this->data['user2']; 222 // Set user2 as logged user. 223 $this->setUser($user2); 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 user1 as logged user. 232 $this->setUser($user1); 233 // Set visibility to 'hide' for user1. 234 set_user_preference('block_online_users_uservisibility', 0); 235 // Test if the fetcher gets all the users including user1. 236 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid); 237 $users = $onlineusers->get_users(); 238 $usercount = $onlineusers->count_users(); 239 // User1 should be displayed in the online users block. 240 $this->assertEquals(9, $usercount); 241 $this->assertTrue(array_key_exists($user1->id, $users)); 242 // Set user2 as logged user. 243 $this->setUser($user2); 244 // Test if the fetcher gets all the users excluding user1. 245 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid); 246 $users = $onlineusers->get_users(); 247 $usercount = $onlineusers->count_users(); 248 // User1 should not be displayed in the online users block. 249 $this->assertEquals(8, $usercount); 250 $this->assertFalse(array_key_exists($user1->id, $users)); 251 252 // Disable users to set their visibility to others in the online users block. 253 // All users should be displayed now and the visibility status of a users should be ignored, 254 // as the capability of setting the visibility to other user has been disabled. 255 $CFG->block_online_users_onlinestatushiding = false; 256 // Test if the fetcher gets all the users including user1. 257 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid); 258 $users = $onlineusers->get_users(); 259 $usercount = $onlineusers->count_users(); 260 // User1 should be displayed in the online users block. 261 $this->assertEquals(9, $usercount); 262 $this->assertTrue(array_key_exists($user1->id, $users)); 263 } 264 265 /** 266 * Check user visibility setting at site level. 267 */ 268 public function test_user_visibility_sitelevel() { 269 global $CFG; 270 271 $currentgroup = null; 272 $now = time(); 273 $timetoshowusers = $CFG->block_online_users_timetosee * 60; 274 $context = context_system::instance(); 275 $user1 = $this->data['user1']; 276 $user2 = $this->data['user2']; 277 // Set user2 as logged user. 278 $this->setUser($user2); 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 user1 as logged user. 287 $this->setUser($user1); 288 // Set visibility to 'hide' for user1. 289 set_user_preference('block_online_users_uservisibility', 0); 290 // Test if the fetcher gets all the users including user1. 291 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true); 292 $users = $onlineusers->get_users(); 293 $usercount = $onlineusers->count_users(); 294 // User1 should be displayed in the online users block. 295 $this->assertEquals(12, $usercount); 296 $this->assertTrue(array_key_exists($user1->id, $users)); 297 // Set user2 as logged user. 298 $this->setUser($user2); 299 // Test if the fetcher gets all the users excluding user1. 300 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true); 301 $users = $onlineusers->get_users(); 302 $usercount = $onlineusers->count_users(); 303 // User1 should not be displayed in the online users block. 304 $this->assertEquals(11, $usercount); 305 $this->assertFalse(array_key_exists($user1->id, $users)); 306 307 // Disable users to set their visibility to others in the online users block. 308 // All users should be displayed now and the visibility status of a users should be ignored, 309 // as the capability of setting the visibility to other user has been disabled. 310 $CFG->block_online_users_onlinestatushiding = false; 311 // Test if the fetcher gets all the users including user1. 312 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true); 313 $users = $onlineusers->get_users(); 314 $usercount = $onlineusers->count_users(); 315 // User1 should be displayed in the online users block. 316 $this->assertEquals(12, $usercount); 317 $this->assertTrue(array_key_exists($user1->id, $users)); 318 } 319 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body