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,26 @@
@report @report_log
Feature: In a course administration page, navigate through report page, test for report log page
In order to navigate through report page
As an admin
Go to course administration -> reports
Background:
Given the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| admin | C1 | editingteacher |
| student1 | C1 | student |
@javascript
Scenario: Report selector should be available in the report log page
Given I log in as "admin"
And I am on "Course 1" course homepage
When I navigate to "Reports" in current page administration
And I click on "Logs" "link"
Then "Report" "field" should exist in the "tertiary-navigation" "region"
And I should see "Logs" in the "tertiary-navigation" "region"
+36
View File
@@ -0,0 +1,36 @@
@report @report_log
Feature: In a report, admin can filter log data
In order to filter log data
As an admin
I need to log in with different user and go to log and apply filter
Background:
Given the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email | idnumber | middlename | alternatename | firstnamephonetic | lastnamephonetic |
| teacher1 | Teacher | One | teacher1@example.com | t1 | | fred | | |
| student1 | Grainne | Beauchamp | student1@example.com | s1 | Ann | Jill | Gronya | Beecham |
And the following "course enrolments" exist:
| user | course | role |
| admin | C1 | editingteacher |
| student1 | C1 | student |
And the following config values are set as admin:
| fullnamedisplay | firstname |
| alternativefullnameformat | middlename, alternatename, firstname, lastname |
And I log in as "admin"
Scenario: Filter log report for standard log reader
Given I am on "Course 1" course homepage
And I navigate to course participants
And I follow "Ann, Jill, Grainne, Beauchamp"
And I click on "Log in as" "link"
And I press "Continue"
And I log out
And I log in as "admin"
When I navigate to "Reports > Logs" in site administration
And I set the field "id" to "Acceptance test site (Site)"
And I set the field "user" to "All participants"
And I press "Get these logs"
Then I should see "User logged in as another user"
@@ -0,0 +1,85 @@
@report @report_log
Feature: In a report, admin can filter log data by action
In order to filter log data by action
As an admin
I need to view the logs and apply a filter
Background:
Given the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| idnumber | 0001 |
| name | Test assignment 1 |
| intro | Offline text |
| assignsubmission_file_enabled | 0 |
| section | 1 |
And I log in as "admin"
And I am on "Course 1" course homepage with editing mode on
# View Action.
And I follow "Test assignment 1"
# Update Action.
And I navigate to "Settings" in current page administration
And I press "Save and return to course"
# Delete Action.
And I delete "Test assignment 1" activity
And I log out
Scenario: View only create actions.
Given I log in as "admin"
When I navigate to "Reports > Logs" in site administration
And I set the field "menumodaction" to "Create"
And I press "Get these logs"
And I should not see "Course module updated"
And I should not see "The status of the submission has been viewed."
And I should not see "Course module deleted"
Scenario: View only update actions.
Given I log in as "admin"
When I navigate to "Reports > Logs" in site administration
And I set the field "menumodaction" to "Update"
And I press "Get these logs"
Then I should see "Course module updated"
And I should not see "Course module created"
And I should not see "The status of the submission has been viewed."
And I should not see "Course module deleted"
Scenario: View only view actions.
Given I log in as "admin"
When I navigate to "Reports > Logs" in site administration
And I set the field "menumodaction" to "View"
And I press "Get these logs"
Then I should see "The status of the submission has been viewed."
And I should not see "Course module created"
And I should not see "Course module updated"
And I should not see "Course module deleted"
Scenario: View only delete actions.
Given I log in as "admin"
When I navigate to "Reports > Logs" in site administration
And I set the field "menumodaction" to "Delete"
And I press "Get these logs"
Then I should see "Course module deleted"
And I should not see "Course module created"
And I should not see "Course module updated"
And I should not see "The status of the submission has been viewed."
Scenario: View only changes.
Given I log in as "admin"
When I navigate to "Reports > Logs" in site administration
And I set the field "menumodaction" to "All changes"
And I press "Get these logs"
Then I should see "Course module deleted"
And I should see "Course module updated"
And I should not see "The status of the submission has been viewed."
Scenario: View all actions.
Given I log in as "admin"
When I navigate to "Reports > Logs" in site administration
And I set the field "menumodaction" to "All actions"
And I press "Get these logs"
Then I should see "Course module deleted"
And I should see "Course module updated"
And I should see "The status of the submission has been viewed."
+75
View File
@@ -0,0 +1,75 @@
@report @report_log
Feature: User can view activity log.
In order to view user log
As an teacher
I need to view user today's and all report
Background:
Given the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email | idnumber | middlename | alternatename | firstnamephonetic | lastnamephonetic |
| teacher1 | Teacher | One | teacher1@example.com | t1 | | fred | | |
| student1 | Grainne | Beauchamp | student1@example.com | s1 | Ann | Jill | Gronya | Beecham |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activity" exists:
| activity | assign |
| course | C1 |
| idnumber | 0001 |
| name | Test assignment name |
| intro | Submit your online text |
| section | 1 |
| assignsubmission_onlinetext_enabled | 1 |
| assignsubmission_file_enabled | 0 |
And the following config values are set as admin:
| fullnamedisplay | firstname |
| alternativefullnameformat | middlename, alternatename, firstname, lastname |
And I log in as "student1"
And I am on "Course 1" course homepage
And I follow "Test assignment name"
When I press "Add submission"
And I set the following fields to these values:
| Online text | I'm the student first submission |
And I press "Save changes"
And I log out
Scenario: View Todays' and all log report for user
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to course participants
And I follow "Ann, Jill, Grainne, Beauchamp"
When I follow "Today's logs"
And I should see "Assignment: Test assignment name"
And I follow "Ann, Jill, Grainne, Beauchamp"
And I follow "All logs"
Then I should see "Assignment: Test assignment name"
Scenario: No log reader enabled should be visible when no log store enabled.
Given I log in as "admin"
And I navigate to "Plugins > Logging > Manage log stores" in site administration
And I click on "Disable" "link" in the "Standard log" "table_row"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to course participants
And I follow "Ann, Jill, Grainne, Beauchamp"
When I follow "Today's logs"
And I should see "No log reader enabled"
And I am on "Course 1" course homepage
And I navigate to course participants
And I follow "Ann, Jill, Grainne, Beauchamp"
And I follow "All logs"
Then I should see "No log reader enabled"
Scenario: View Todays' log report for user through Course log report
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Reports" in current page administration
And I click on "Logs" "link"
And I set the field with xpath "//select[@name='user']" to "Ann, Jill, Grainne, Beauchamp"
When I click on "Get these logs" "button"
Then I should see "Ann, Jill, Grainne, Beauchamp"
+101
View File
@@ -0,0 +1,101 @@
<?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/>.
/**
* Tests for report log events.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace report_log\event;
/**
* Class report_log_events_testcase
*
* Class for tests related to log events.
*
* @package report_log
* @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class events_test extends \advanced_testcase {
/**
* Setup testcase.
*/
public function setUp(): void {
$this->setAdminUser();
$this->resetAfterTest();
}
/**
* Test the report viewed event.
*
* It's not possible to use the moodle API to simulate the viewing of log report, so here we
* simply create the event and trigger it.
*/
public function test_report_viewed(): void {
$course = $this->getDataGenerator()->create_course();
$context = \context_course::instance($course->id);
// Trigger event for log report viewed.
$event = \report_log\event\report_viewed::create(array('context' => $context,
'relateduserid' => 0, 'other' => array('groupid' => 0, 'date' => 0, 'modid' => 0, 'modaction' => '',
'logformat' => 'showashtml')));
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\report_log\event\report_viewed', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$url = new \moodle_url('/report/log/index.php', array('id' => $event->courseid));
$this->assertEquals($url, $event->get_url());
}
/**
* Test the user report viewed event.
*
* It's not possible to use the moodle API to simulate the viewing of user log report, so here we
* simply create the event and trigger it.
*/
public function test_user_report_viewed(): void {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$context = \context_course::instance($course->id);
// Trigger event for user report viewed.
$event = \report_log\event\user_report_viewed::create(array('context' => $context,
'relateduserid' => $user->id, 'other' => array('mode' => 'today')));
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\report_log\event\user_report_viewed', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$url = new \moodle_url('/report/log/user.php', array('course' => $course->id, 'id' => $user->id, 'mode' => 'today'));
$this->assertEquals($url, $event->get_url());
}
}
+113
View File
@@ -0,0 +1,113 @@
<?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/>.
/**
* Tests for report library functions.
*
* @package report_log
* @copyright 2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace report_log;
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Class report_log_events_testcase.
*
* @package report_log
* @copyright 2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class lib_test extends \advanced_testcase {
/**
* @var stdClass The user.
*/
private $user;
/**
* @var stdClass The course.
*/
private $course;
/**
* @var \core_user\output\myprofile\tree The navigation tree.
*/
private $tree;
public function setUp(): void {
$this->user = $this->getDataGenerator()->create_user();
$this->course = $this->getDataGenerator()->create_course();
$this->tree = new \core_user\output\myprofile\tree();
$this->resetAfterTest();
}
/**
* Test report_log_supports_logstore.
*/
public function test_report_log_supports_logstore(): void {
$logmanager = get_log_manager();
$allstores = \core_component::get_plugin_list_with_class('logstore', 'log\store');
$supportedstores = array(
'logstore_database' => '\logstore_database\log\store',
'logstore_standard' => '\logstore_standard\log\store'
);
// Make sure all supported stores are installed.
$expectedstores = array_keys(array_intersect($allstores, $supportedstores));
$stores = $logmanager->get_supported_logstores('report_log');
$stores = array_keys($stores);
foreach ($expectedstores as $expectedstore) {
$this->assertContains($expectedstore, $stores);
}
}
/**
* Tests the report_log_myprofile_navigation() function as an admin viewing the logs for a user.
*/
public function test_report_log_myprofile_navigation(): void {
// Set as the admin.
$this->setAdminUser();
$iscurrentuser = false;
// Check the node tree is correct.
report_log_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayHasKey('alllogs', $nodes->getValue($this->tree));
$this->assertArrayHasKey('todayslogs', $nodes->getValue($this->tree));
}
/**
* Tests the report_log_myprofile_navigation() function as a user without permission.
*/
public function test_report_log_myprofile_navigation_without_permission(): void {
// Set to the other user.
$this->setUser($this->user);
$iscurrentuser = true;
// Check the node tree is correct.
report_log_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayNotHasKey('alllogs', $nodes->getValue($this->tree));
$this->assertArrayNotHasKey('todayslogs', $nodes->getValue($this->tree));
}
}
+439
View File
@@ -0,0 +1,439 @@
<?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 report_log;
use context_course;
use core_user;
/**
* Class report_log\renderable_test to cover functions in \report_log_renderable.
*
* @package report_log
* @copyright 2023 Stephan Robotta <stephan.robotta@bfh.ch>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class renderable_test extends \advanced_testcase {
/**
* @var int The course with separate groups.
*/
const COURSE_SEPARATE_GROUP = 0;
/**
* @var int The course with separate groups.
*/
const COURSE_VISIBLE_GROUP = 1;
/**
* @var int The course with separate groups.
*/
const COURSE_NO_GROUP = 2;
/**
* @var array The setup of users.
*/
const SETUP_USER_DEFS = [
// Make student2 also member of group1.
'student' => [
'student0' => ['group0'],
'student1' => ['group1'],
'student2' => ['group0', 'group1'],
'student3' => [],
],
// Make teacher2 also member of group1.
'teacher' => [
'teacher0' => ['group0'],
'teacher1' => ['group1'],
'teacher2' => ['group0', 'group1'],
'teacher3' => [],
],
// Make editingteacher also member of group1.
'editingteacher' => [
'editingteacher0' => ['group0'],
'editingteacher1' => ['group1'],
'editingteacher2' => ['group0', 'group1'],
],
];
/**
* @var array|\stdClass all users indexed by username.
*/
private $users = [];
/**
* @var array The groups by courses (array of array).
*/
private $groupsbycourse = [];
/**
* @var array The courses.
*/
private $courses;
/**
* Get the data provider for test_get_user_list().
*
* @return array
*/
public static function get_user_visibility_list_provider(): array {
return [
'separategroups: student 0' => [
self::COURSE_SEPARATE_GROUP,
'student0',
// All users in group 0.
[
'student0', 'student2',
'teacher0', 'teacher2',
'editingteacher0', 'editingteacher2',
],
],
'separategroups: student 1' => [
self::COURSE_SEPARATE_GROUP,
'student1',
// All users in group1.
[
'student1', 'student2',
'teacher1', 'teacher2',
'editingteacher1', 'editingteacher2',
],
],
'separategroups: student 2' => [
self::COURSE_SEPARATE_GROUP,
'student2',
// All users in group0 and group1.
[
'student0', 'student1', 'student2',
'teacher0', 'teacher1', 'teacher2',
'editingteacher0', 'editingteacher1', 'editingteacher2',
],
],
'separategroups: student3' => [
self::COURSE_SEPARATE_GROUP,
'student3',
// Student 3 is not in any group so should only see user without a group.
[
'student3',
'teacher3',
],
],
'separategroups: editing teacher 0' => [
self::COURSE_SEPARATE_GROUP,
'editingteacher0',
// All users including student 3 as we can see all users (event the one not in a group).
[
'student0', 'student1', 'student2', 'student3',
'teacher0', 'teacher1', 'teacher2', 'teacher3',
'editingteacher0', 'editingteacher1', 'editingteacher2',
],
],
'separategroups: teacher 0' => [
self::COURSE_SEPARATE_GROUP,
'teacher0',
// All users in group 0.
[
'student0', 'student2',
'teacher0', 'teacher2',
'editingteacher0', 'editingteacher2',
],
],
'separategroups: teacher 2' => [
self::COURSE_SEPARATE_GROUP,
'teacher2',
// All users in group0 and group1.
[
'student0', 'student1', 'student2',
'teacher0', 'teacher1', 'teacher2',
'editingteacher0', 'editingteacher1', 'editingteacher2',
],
],
'separategroups: teacher 3' => [
self::COURSE_SEPARATE_GROUP,
'teacher3',
// Teacher 3 is not in any group so should only see user without a group.
[
'student3',
'teacher3',
],
],
'separategroups: teacher 2 with group set to group 1' => [
self::COURSE_SEPARATE_GROUP,
'teacher2',
// All users in group1.
[
'student1', 'student2',
'teacher1', 'teacher2',
'editingteacher1', 'editingteacher2',
],
'group1',
],
'visiblegroup: editing teacher' => [
self::COURSE_VISIBLE_GROUP,
'editingteacher0',
// All users.
[
'student0', 'student1', 'student2', 'student3',
'teacher0', 'teacher1', 'teacher2', 'teacher3',
'editingteacher0', 'editingteacher1', 'editingteacher2',
],
],
'visiblegroup: student' => [
self::COURSE_VISIBLE_GROUP,
'student0',
// All users.
[
'student0', 'student1', 'student2', 'student3',
'teacher0', 'teacher1', 'teacher2', 'teacher3',
'editingteacher0', 'editingteacher1', 'editingteacher2',
],
],
'visiblegroup: teacher 0' => [
self::COURSE_VISIBLE_GROUP,
'teacher2',
// All users.
[
'student0', 'student1', 'student2', 'student3',
'teacher0', 'teacher1', 'teacher2', 'teacher3',
'editingteacher0', 'editingteacher1', 'editingteacher2',
],
],
'visiblegroup: editing teacher 0' => [
self::COURSE_VISIBLE_GROUP,
'editingteacher0',
// All users.
[
'student0', 'student1', 'student2', 'student3',
'teacher0', 'teacher1', 'teacher2', 'teacher3',
'editingteacher0', 'editingteacher1', 'editingteacher2',
],
],
'visiblegroup: teacher 2 with group set to group 1' => [
self::COURSE_VISIBLE_GROUP,
'teacher2',
// All users in group1.
[
'student1', 'student2',
'teacher1', 'teacher2',
'editingteacher1', 'editingteacher2',
],
'group1',
],
];
}
/**
* Data provider for test_get_group_list().
*
* @return array
*/
public static function get_group_list_provider(): array {
return [
// The student sees his own group only.
'separategroup: student in one group' => [self::COURSE_SEPARATE_GROUP, 'student0', 1],
'separategroup: student in two groups' => [self::COURSE_SEPARATE_GROUP, 'student2', 2],
// While the teacher is not allowed to see all groups.
'separategroup: teacher in one group' => [self::COURSE_SEPARATE_GROUP, 'teacher0', 1],
'separategroup: teacher in two groups' => [self::COURSE_SEPARATE_GROUP, 'teacher2', 2],
// But editing teacher should see all.
'separategroup: editingteacher' => [self::COURSE_SEPARATE_GROUP, 'editingteacher0', 2],
// The student sees all groups.
'visiblegroup: student in one group' => [self::COURSE_VISIBLE_GROUP, 'student0', 2],
// Same for teacher.
'visiblegroup: teacher in one group' => [self::COURSE_VISIBLE_GROUP, 'teacher0', 2],
// And editing teacher.
'visiblegroup: editingteacher' => [self::COURSE_VISIBLE_GROUP, 'editingteacher0', 2],
// No group.
'nogroups: student in one group' => [self::COURSE_NO_GROUP, 'student0', 0],
// Same for teacher.
'nogroups: teacher in one group' => [self::COURSE_NO_GROUP, 'teacher0', 0],
// And editing teacher.
'nogroups: editingteacher' => [self::COURSE_NO_GROUP, 'editingteacher0', 0],
];
}
/**
* Set up a course with two groups, three students being each in one of the groups,
* two teachers each in either group while the second teacher is also member of the other group.
*
* @return void
* @throws \coding_exception
*/
public function setUp(): void {
$this->resetAfterTest();
$this->courses[self::COURSE_SEPARATE_GROUP] = $this->getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS]);
$this->courses[self::COURSE_VISIBLE_GROUP] = $this->getDataGenerator()->create_course(['groupmode' => VISIBLEGROUPS]);
$this->courses[self::COURSE_NO_GROUP] = $this->getDataGenerator()->create_course();
foreach ($this->courses as $coursetype => $course) {
if ($coursetype == self::COURSE_NO_GROUP) {
continue;
}
$this->groupsbycourse[$coursetype] = [];
$this->groupsbycourse[$coursetype]['group0'] =
$this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'group0']);
$this->groupsbycourse[$coursetype]['group1'] =
$this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'group1']);
}
foreach (self::SETUP_USER_DEFS as $role => $userdefs) {
foreach ($userdefs as $username => $groups) {
$user = $this->getDataGenerator()->create_user(
[
'username' => $username,
'firstname' => "FN{$role}{$username}",
'lastname' => "LN{$role}{$username}",
]);
foreach ($this->courses as $coursetype => $course) {
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role);
foreach ($groups as $groupname) {
if ($coursetype == self::COURSE_NO_GROUP) {
continue;
}
$this->getDataGenerator()->create_group_member([
'groupid' => $this->groupsbycourse[$coursetype][$groupname]->id,
'userid' => $user->id,
]);
}
}
$this->users[$username] = $user;
}
}
}
/**
* Test report_log_renderable::get_user_list().
*
* @param int $courseindex
* @param string $username
* @param array $expectedusers
* @param string|null $groupname
* @covers \report_log_renderable::get_user_list
* @dataProvider get_user_visibility_list_provider
* @return void
*/
public function test_get_user_list(int $courseindex, string $username, array $expectedusers,
string $groupname = null): void {
global $PAGE, $CFG;
$currentcourse = $this->courses[$courseindex];
$PAGE->set_url('/report/log/index.php?id=' . $currentcourse->id);
// Fetch all users of group 1 and the guest user.
$currentuser = $this->users[$username];
$this->setUser($currentuser->id);
$groupid = 0;
if ($groupname) {
$groupid = $this->groupsbycourse[$courseindex][$groupname]->id;
}
$renderable = new \report_log_renderable(
"", (int) $currentcourse->id, $currentuser->id, 0, '', $groupid);
$userlist = $renderable->get_user_list();
unset($userlist[$CFG->siteguest]); // We ignore guest.
$usersid = array_keys($userlist);
$users = array_map(function($userid) {
return core_user::get_user($userid);
}, $usersid);
// Now check that the users are the expected ones.
asort($expectedusers);
$userlistbyname = array_column($users, 'username');
asort($userlistbyname);
$this->assertEquals(array_values($expectedusers), array_values($userlistbyname));
// Check that users are in order lastname > firstname > id.
$sortedusers = $users;
// Sort user by lastname > firstname > id.
usort($sortedusers, function($a, $b) {
if ($a->lastname != $b->lastname) {
return $a->lastname <=> $b->lastname;
}
if ($a->firstname != $b->firstname) {
return $a->firstname <=> $b->firstname;
}
return $a->id <=> $b->id;
});
$sortedusernames = array_column($sortedusers, 'username');
$userlistbyname = array_column($users, 'username');
$this->assertEquals($sortedusernames, $userlistbyname);
}
/**
* Test report_log_renderable::get_group_list().
*
* @covers \report_log_renderable::get_group_list
* @dataProvider get_group_list_provider
* @return void
*/
public function test_get_group_list($courseindex, $username, $expectedcount): void {
global $PAGE;
$PAGE->set_url('/report/log/index.php?id=' . $this->courses[$courseindex]->id);
$this->setUser($this->users[$username]->id);
$renderable = new \report_log_renderable("", (int) $this->courses[$courseindex]->id, $this->users[$username]->id);
$groups = $renderable->get_group_list();
$this->assertCount($expectedcount, $groups);
}
/**
* Test table_log
*
* @param int $courseindex
* @param string $username
* @param array $expectedusers
* @param string|null $groupname
* @covers \report_log_renderable::get_user_list
* @dataProvider get_user_visibility_list_provider
* @return void
*/
public function test_get_table_logs(int $courseindex, string $username, array $expectedusers, ?string $groupname = null): void {
global $DB, $PAGE;
$this->preventResetByRollback(); // This is to ensure that we can actually trigger event and record them in the log store.
// Configure log store.
set_config('enabled_stores', 'logstore_standard', 'tool_log');
$manager = get_log_manager(true);
$DB->delete_records('logstore_standard_log');
foreach ($this->courses as $course) {
foreach ($this->users as $user) {
$eventdata = [
'context' => context_course::instance($course->id),
'userid' => $user->id,
];
$event = \core\event\course_viewed::create($eventdata);
$event->trigger();
}
}
$stores = $manager->get_readers();
$store = $stores['logstore_standard'];
// Build the report.
$currentuser = $this->users[$username];
$this->setUser($currentuser->id);
$groupid = 0;
if ($groupname) {
$groupid = $this->groupsbycourse[$courseindex][$groupname]->id;
}
$PAGE->set_url('/report/log/index.php?id=' . $this->courses[$courseindex]->id);
$renderable = new \report_log_renderable("", (int) $this->courses[$courseindex]->id, 0, 0, '', $groupid);
$renderable->setup_table();
$table = $renderable->tablelog;
$store->flush();
$table->query_db(100);
$usernames = [];
foreach ($table->rawdata as $event) {
if (get_class($event) !== \core\event\course_viewed::class) {
continue;
}
$user = core_user::get_user($event->userid, '*', MUST_EXIST);
$usernames[] = $user->username;
}
sort($expectedusers);
sort($usernames);
$this->assertEquals($expectedusers, $usernames);
}
}