first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,118 @@
@block @block_online_users
Feature: The online users block allow you to see who is currently online
In order to enable the online users block on an course page
As a teacher
I can add the online users block to a course page
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
Scenario: Add the online users on course page and see myself
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
When I add the "Online users" block
Then I should see "Teacher 1" in the "Online users" "block"
And I should see "1 online user" in the "Online users" "block"
Scenario: Add the online users on course page and see other logged in users
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I add the "Online users" block
And I log out
And I log in as "student2"
And I log out
When I log in as "student1"
And I am on "Course 1" course homepage
Then I should see "Teacher 1" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
And I should not see "Student 2" in the "Online users" "block"
And I should see "2 online users" in the "Online users" "block"
@javascript
Scenario: Hide/show user's online status from/to other users in the online users block on course page
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I add the "Online users" block
And I log out
When I log in as "student1"
And I am on "Course 1" course homepage
Then "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
When I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
Then "Show" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
When I log in as "teacher1"
And I am on "Course 1" course homepage
Then I should see "1 online user" in the "Online users" "block"
And I should see "Teacher 1" in the "Online users" "block"
And I should not see "Student 1" in the "Online users" "block"
And I log out
When I log in as "student1"
And I am on "Course 1" course homepage
Then "Show" "icon" should exist in the "#change-user-visibility" "css_element"
When I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
Then "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
When I log in as "teacher1"
And I am on "Course 1" course homepage
Then I should see "2 online users" in the "Online users" "block"
And I should see "Teacher 1" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
@javascript
Scenario: Hide/show icon is not visible in the online users block on course page when the setting is disabled
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I add the "Online users" block
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And "Hide" "icon" should exist in the ".block.block_online_users" "css_element"
And I log out
And the following config values are set as admin:
| block_online_users_onlinestatushiding | 0 |
When I log in as "student1"
And I am on "Course 1" course homepage
Then I should see "Student 1" in the "Online users" "block"
And "Hide" "icon" should not exist in the ".block.block_online_users" "css_element"
@javascript
Scenario: User is displayed in the online users block on course page when visibility setting is disabled,
ignoring the previously set visibility state
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I add the "Online users" block
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
And I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
And "Show" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
And the following config values are set as admin:
| block_online_users_onlinestatushiding | 0 |
And I log in as "teacher1"
When I am on "Course 1" course homepage
Then I should see "2 online users" in the "Online users" "block"
And I should see "Teacher 1" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
@@ -0,0 +1,109 @@
@block @block_online_users
Feature: The online users block allow you to see who is currently online on dashboard
There should be some commonality for the users to show up
In order to use the online users block on the dashboard
As a user
I can view the online users block on my dashboard
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| online_users | System | 1 | my-index | side-post |
Scenario: View the online users block on the dashboard and see myself
Given I log in as "teacher1"
Then I should see "Teacher 1" in the "Online users" "block"
And I should see "1 online user" in the "Online users" "block"
Scenario: View the online users block on the dashboard and see other logged in users
Given I log in as "student2"
And I log out
And I log in as "student1"
And I log out
When I log in as "teacher1"
Then I should see "Teacher 1" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should see "3 online users" in the "Online users" "block"
@javascript
Scenario: Hide/show user's online status from/to other users in the online users block on dashboard
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "student1"
And I should see "1 online user" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
And "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
When I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
Then "Show" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
When I log in as "student2"
Then I should see "1 online user" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should not see "Student 1" in the "Online users" "block"
And I log out
When I log in as "student1"
Then "Show" "icon" should exist in the "#change-user-visibility" "css_element"
When I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
Then "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
When I log in as "student2"
Then I should see "2 online users" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
@javascript
Scenario: Hide/show icon is not visible in the online users block when the setting is disabled
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "student1"
And I should see "1 online user" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
And "Hide" "icon" should exist in the ".block.block_online_users" "css_element"
And I log out
And the following config values are set as admin:
| block_online_users_onlinestatushiding | 0 |
When I log in as "student1"
Then I should see "1 online user" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
And "Hide" "icon" should not exist in the ".block.block_online_users" "css_element"
@javascript
Scenario: User is displayed in the online users block when visibility setting is disabled,
ignoring the previously set visibility state
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "student1"
And I should see "1 online user" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
And "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
And I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
And "Show" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
And I log in as "student2"
And I should see "1 online user" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should not see "Student 1" in the "Online users" "block"
And I log out
And the following config values are set as admin:
| block_online_users_onlinestatushiding | 0 |
When I log in as "student2"
Then I should see "2 online users" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
@@ -0,0 +1,154 @@
@block @block_online_users
Feature: The online users block allow you to see who is currently online on frontpage
There should be some commonality for the users to show up
In order to enable the online users block on the frontpage
As an admin
I can add the online users block to the frontpage
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| student2 | C1 | student |
Scenario: View the online users block on the front page and see myself
Given I log in as "admin"
And I am on site homepage
And I turn editing mode on
When I add the "Online users" block
Then I should see "Admin User" in the "Online users" "block"
And I should see "1 online user" in the "Online users" "block"
Scenario: View the online users block on the front page as a logged in user
Given I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I add the "Online users" block
And I log out
And I log in as "student2"
And I log out
When I log in as "student1"
And I am on site homepage
Then I should not see "Admin User" in the "Online users" "block"
And I should see "Other users (1)" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should see "3 online users" in the "Online users" "block"
Scenario: View the online users block on the front page as a guest
Given I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I add the "Online users" block
And I log out
And I log in as "student2"
And I log out
And I log in as "student1"
And I log out
When I log in as "guest"
And I am on site homepage
Then I should not see "Admin User" in the "Online users" "block"
And I should not see "Student 1" in the "Online users" "block"
And I should not see "Student 2" in the "Online users" "block"
And I should see "3 online users" in the "Online users" "block"
@javascript
Scenario: Hide/show user's online status from/to other users in the online users block on front page
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I add the "Online users" block
And I log out
When I log in as "student1"
And I am on site homepage
Then "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
When I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
Then "Show" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
When I log in as "student2"
And I am on site homepage
Then I should see "2 online user" in the "Online users" "block"
And I should not see "Admin" in the "Online users" "block"
And I should see "Other users (1)" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should not see "Student 1" in the "Online users" "block"
And I log out
When I log in as "student1"
And I am on site homepage
Then "Show" "icon" should exist in the "#change-user-visibility" "css_element"
When I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
Then "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
When I log in as "student2"
And I am on site homepage
Then I should see "3 online users" in the "Online users" "block"
And I should not see "Admin" in the "Online users" "block"
And I should see "Other users (1)" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
@javascript
Scenario: Hide/show icon is not visible in the online users block on front page when the setting is disabled
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I add the "Online users" block
And I log out
And I log in as "student1"
And I am on site homepage
And "Hide" "icon" should exist in the ".block.block_online_users" "css_element"
And I log out
And the following config values are set as admin:
| block_online_users_onlinestatushiding | 0 |
When I log in as "student1"
And I am on site homepage
Then I should see "Student 1" in the "Online users" "block"
And "Hide" "icon" should not exist in the ".block.block_online_users" "css_element"
@javascript
Scenario: User is displayed in the online users block on front page when visibility setting is disabled,
ignoring the previously set visibility state
Given the following config values are set as admin:
| block_online_users_onlinestatushiding | 1 |
And I log in as "admin"
And I am on site homepage
And I turn editing mode on
And I add the "Online users" block
And I log out
And I log in as "student1"
And I am on site homepage
And "Hide" "icon" should exist in the "#change-user-visibility" "css_element"
And I click on "#change-user-visibility" "css_element"
And I wait "1" seconds
And "Show" "icon" should exist in the "#change-user-visibility" "css_element"
And I log out
And I log in as "student2"
And I am on site homepage
And I should see "2 online user" in the "Online users" "block"
And I should not see "Admin" in the "Online users" "block"
And I should see "Other users (1)" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should not see "Student 1" in the "Online users" "block"
And I log out
And the following config values are set as admin:
| block_online_users_onlinestatushiding | 0 |
And I log in as "student2"
When I am on site homepage
Then I should see "3 online users" in the "Online users" "block"
And I should not see "Admin" in the "Online users" "block"
And I should see "Other users (1)" in the "Online users" "block"
And I should see "Student 2" in the "Online users" "block"
And I should see "Student 1" in the "Online users" "block"
@@ -0,0 +1,90 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* block_online_users data generator
*
* @package block_online_users
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Online users block data generator class
*
* @package block_online_users
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_online_users_generator extends testing_block_generator {
/**
* Create (simulated) logged in users and add some of them to groups in a course
*/
public function create_logged_in_users() {
global $DB;
$generator = advanced_testcase::getDataGenerator();
$data = array();
// Create 2 courses.
$course1 = $generator->create_course();
$data['course1'] = $course1;
$course2 = $generator->create_course();
$data['course2'] = $course2;
// Create 9 (simulated) logged in users enroled into $course1.
for ($i = 1; $i <= 9; $i++) {
$user = $generator->create_user();
$DB->set_field('user', 'lastaccess', time(), array('id' => $user->id));
$generator->enrol_user($user->id, $course1->id);
$DB->insert_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course1->id, 'timeaccess' => time()));
$data['user' . $i] = $user;
}
// Create 3 (simulated) logged in users who are not enroled into $course1.
for ($i = 10; $i <= 12; $i++) {
$user = $generator->create_user();
$DB->set_field('user', 'lastaccess', time(), array('id' => $user->id));
$data['user' . $i] = $user;
}
// Create 3 groups in course 1.
$group1 = $generator->create_group(array('courseid' => $course1->id));
$data['group1'] = $group1;
$group2 = $generator->create_group(array('courseid' => $course1->id));
$data['group2'] = $group2;
$group3 = $generator->create_group(array('courseid' => $course1->id));
$data['group3'] = $group3;
// Add 3 users to course group 1.
$generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user1']->id));
$generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user2']->id));
$generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user3']->id));
// Add 4 users to course group 2.
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user3']->id));
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user4']->id));
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user5']->id));
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user6']->id));
return $data; // Return the user, course and group objects.
}
}
@@ -0,0 +1,47 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace block_online_users;
/**
* PHPUnit data generator testcase
*
* @package block_online_users
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_test extends \advanced_testcase {
public function test_generator(): void {
global $DB;
$this->resetAfterTest(true);
$beforeblocks = $DB->count_records('block_instances');
$beforecontexts = $DB->count_records('context');
/** @var \block_online_users_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('block_online_users');
$this->assertInstanceOf('block_online_users_generator', $generator);
$this->assertEquals('online_users', $generator->get_blockname());
$generator->create_instance();
$generator->create_instance();
$bi = $generator->create_instance();
$this->assertEquals($beforeblocks+3, $DB->count_records('block_instances'));
}
}
@@ -0,0 +1,307 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace block_online_users;
/**
* Online users testcase
*
* @package block_online_users
* @category test
* @copyright 2015 University of Nottingham <www.nottingham.ac.uk>
* @author Barry Oosthuizen <barry.oosthuizen@nottingham.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class online_users_test extends \advanced_testcase {
protected $data;
/**
* Tests initial setup.
*
* Prepare the site with some courses, groups, users and
* simulate various recent accesses.
*/
protected function setUp(): void {
// Generate (simulated) recently logged-in users.
$generator = $this->getDataGenerator()->get_plugin_generator('block_online_users');
$this->data = $generator->create_logged_in_users();
// Confirm we have modified the site and requires reset.
$this->resetAfterTest(true);
}
/**
* Check logged in group 1, 2 & 3 members in course 1 (should be 3, 4 and 0).
*
* @param array $data Array of user, course and group objects
* @param int $now Current Unix timestamp
* @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
*/
public function test_fetcher_course1_group_members(): void {
global $CFG;
$groupid = $this->data['group1']->id;
$now = time();
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
$context = \context_course::instance($this->data['course1']->id);
$courseid = $this->data['course1']->id;
$onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
$usercount = $onlineusers->count_users();
$users = $onlineusers->get_users();
$this->assertEquals(3, $usercount, 'There was a problem counting the number of online users in group 1');
$this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 1');
$groupid = $this->data['group2']->id;
$onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
$usercount = $onlineusers->count_users();
$users = $onlineusers->get_users();
$this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 2');
$this->assertEquals(4, $usercount, 'There was a problem counting the number of online users in group 2');
$groupid = $this->data['group3']->id;
$onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
$usercount = $onlineusers->count_users();
$users = $onlineusers->get_users();
$this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in group 3');
$this->assertEquals(0, $usercount, 'There was a problem counting the number of online users in group 3');
}
/**
* Check logged in users in courses 1 & 2 (should be 9 and 0).
*
* @param array $data Array of user, course and group objects
* @param int $now Current Unix timestamp
* @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
*/
public function test_fetcher_courses(): void {
global $CFG;
$currentgroup = null;
$now = time();
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
$context = \context_course::instance($this->data['course1']->id);
$courseid = $this->data['course1']->id;
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
$usercount = $onlineusers->count_users();
$users = $onlineusers->get_users();
$this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in course 1');
$this->assertEquals(9, $usercount, 'There was a problem counting the number of online users in course 1');
$courseid = $this->data['course2']->id;
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
$usercount = $onlineusers->count_users();
$users = $onlineusers->get_users();
$this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users in course 2');
$this->assertEquals(0, $usercount, 'There was a problem counting the number of online users in course 2');
}
/**
* Check logged in at the site level (should be 12).
*
* @param int $now Current Unix timestamp
* @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
*/
public function test_fetcher_sitelevel(): void {
global $CFG;
$currentgroup = null;
$now = time();
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
$context = \context_system::instance();
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
$usercount = $onlineusers->count_users();
$users = $onlineusers->get_users();
$this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users at site level');
$this->assertEquals(12, $usercount, 'There was a problem counting the number of online users at site level');
}
/**
* Check user visibility setting for course group members.
*/
public function test_user_visibility_course1_group1_members(): void {
global $CFG;
// Enable users to set their visibility to others in the online users block.
$CFG->block_online_users_onlinestatushiding = true;
$groupid = $this->data['group1']->id;
$now = time();
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
$context = \context_course::instance($this->data['course1']->id);
$courseid = $this->data['course1']->id;
$user1 = $this->data['user1'];
$user2 = $this->data['user2'];
// Set user2 as logged user.
$this->setUser($user2);
$onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(3, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
// Set user1 as logged user.
$this->setUser($user1);
// Set visibility to 'hide' for user1.
set_user_preference('block_online_users_uservisibility', 0);
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(3, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
// Set user2 as logged user.
$this->setUser($user2);
// Test if the fetcher gets all the users excluding user1.
$onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should not be displayed in the online users block.
$this->assertEquals(2, $usercount);
$this->assertFalse(array_key_exists($user1->id, $users));
// Disable users to set their visibility to others in the online users block.
// All users should be displayed now and the visibility status of a users should be ignored,
// as the capability of setting the visibility to other user has been disabled.
$CFG->block_online_users_onlinestatushiding = false;
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($groupid, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(3, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
}
/**
* Check user visibility setting at course level.
*/
public function test_user_visibility_courses(): void {
global $CFG;
$currentgroup = null;
$now = time();
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
$context = \context_course::instance($this->data['course1']->id);
$courseid = $this->data['course1']->id;
$user1 = $this->data['user1'];
$user2 = $this->data['user2'];
// Set user2 as logged user.
$this->setUser($user2);
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(9, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
// Set user1 as logged user.
$this->setUser($user1);
// Set visibility to 'hide' for user1.
set_user_preference('block_online_users_uservisibility', 0);
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(9, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
// Set user2 as logged user.
$this->setUser($user2);
// Test if the fetcher gets all the users excluding user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should not be displayed in the online users block.
$this->assertEquals(8, $usercount);
$this->assertFalse(array_key_exists($user1->id, $users));
// Disable users to set their visibility to others in the online users block.
// All users should be displayed now and the visibility status of a users should be ignored,
// as the capability of setting the visibility to other user has been disabled.
$CFG->block_online_users_onlinestatushiding = false;
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, false, $courseid);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(9, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
}
/**
* Check user visibility setting at site level.
*/
public function test_user_visibility_sitelevel(): void {
global $CFG;
$currentgroup = null;
$now = time();
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
$context = \context_system::instance();
$user1 = $this->data['user1'];
$user2 = $this->data['user2'];
// Set user2 as logged user.
$this->setUser($user2);
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(12, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
// Set user1 as logged user.
$this->setUser($user1);
// Set visibility to 'hide' for user1.
set_user_preference('block_online_users_uservisibility', 0);
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(12, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
// Set user2 as logged user.
$this->setUser($user2);
// Test if the fetcher gets all the users excluding user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should not be displayed in the online users block.
$this->assertEquals(11, $usercount);
$this->assertFalse(array_key_exists($user1->id, $users));
// Disable users to set their visibility to others in the online users block.
// All users should be displayed now and the visibility status of a users should be ignored,
// as the capability of setting the visibility to other user has been disabled.
$CFG->block_online_users_onlinestatushiding = false;
// Test if the fetcher gets all the users including user1.
$onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
$users = $onlineusers->get_users();
$usercount = $onlineusers->count_users();
// User1 should be displayed in the online users block.
$this->assertEquals(12, $usercount);
$this->assertTrue(array_key_exists($user1->id, $users));
}
}