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