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
+96
View File
@@ -0,0 +1,96 @@
<?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 stats report events.
*
* @package report_stats
* @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace report_stats\event;
/**
* Class report_stats_events_testcase
*
* Class for tests related to stats report events.
*
* @package report_stats
* @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 stats report viewed event.
*
* It's not possible to use the moodle API to simulate the viewing of stats report, so here we
* simply create the event and trigger it.
*/
public function test_report_viewed(): void {
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$context = \context_course::instance($course->id);
// Trigger event for stats report viewed.
$event = \report_stats\event\report_viewed::create(array('context' => $context, 'relateduserid' => $user->id,
'other' => array('report' => 0, 'time' => 0, 'mode' => 1)));
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\report_stats\event\report_viewed', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
}
/**
* Test the user stats report viewed event.
*
* It's not possible to use the moodle API to simulate the viewing of user stats 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 stats report viewed.
$event = \report_stats\event\user_report_viewed::create(array('context' => $context, 'relateduserid' => $user->id));
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\report_stats\event\user_report_viewed', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
}
}
+127
View File
@@ -0,0 +1,127 @@
<?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_stats
* @copyright 2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace report_stats;
defined('MOODLE_INTERNAL') || die();
/**
* Class report_stats_lib_testcase
*
* @package report_stats
* @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_participation_supports_logstore(): void {
$logmanager = get_log_manager();
$allstores = \core_component::get_plugin_list_with_class('logstore', 'log\store');
$supportedstores = array(
'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_stats');
$stores = array_keys($stores);
foreach ($expectedstores as $expectedstore) {
$this->assertContains($expectedstore, $stores);
}
}
/**
* Tests the report_stats_myprofile_navigation() function.
*/
public function test_report_stats_myprofile_navigation(): void {
$this->setAdminUser();
$iscurrentuser = false;
// Enable stats.
set_config('enablestats', true);
report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayHasKey('stats', $nodes->getValue($this->tree));
}
/**
* Tests the report_stats_myprofile_navigation() function when stats are disabled.
*/
public function test_report_stats_myprofile_navigation_stats_disabled(): void {
$this->setAdminUser();
$iscurrentuser = false;
// Disable stats.
set_config('enablestats', false);
report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayNotHasKey('stats', $nodes->getValue($this->tree));
}
/**
* Tests the report_stats_myprofile_navigation() function without permission.
*/
public function test_report_stats_myprofile_navigation_without_permission(): void {
// Try to see as a user without permission.
$this->setUser($this->user);
$iscurrentuser = true;
// Enable stats.
set_config('enablestats', true);
report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
$reflector = new \ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$this->assertArrayNotHasKey('stats', $nodes->getValue($this->tree));
}
}
@@ -0,0 +1,320 @@
<?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 privacy functions.
*
* @package report_stats
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace report_stats\privacy;
use report_stats\privacy\provider;
use core_privacy\local\request\approved_userlist;
use core_privacy\tests\provider_testcase;
/**
* Privacy provider test for report_stats.
*
* @package report_stats
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class provider_test extends provider_testcase {
/**
* Convenience function to create stats.
*
* @param int $courseid Course ID for this record.
* @param int $userid User ID for this record.
* @param string $table Stat table to insert into.
*/
protected function create_stats($courseid, $userid, $table) {
global $DB;
$data = (object) [
'courseid' => $courseid,
'userid' => $userid,
'roleid' => 0,
'timeend' => time(),
'statsreads' => rand(1, 50),
'statswrites' => rand(1, 50),
'stattype' => 'activity'
];
$DB->insert_record($table, $data);
}
/**
* Get all of the contexts related to a user and stat tables.
*/
public function test_get_contexts_for_userid(): void {
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();
$context1 = \context_course::instance($course1->id);
$context2 = \context_course::instance($course2->id);
$context3 = \context_course::instance($course3->id);
$this->create_stats($course1->id, $user1->id, 'stats_user_daily');
$this->create_stats($course2->id, $user1->id, 'stats_user_monthly');
$this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
$contextlist = provider::get_contexts_for_userid($user1->id);
$this->assertCount(2, $contextlist->get_contextids());
foreach ($contextlist->get_contexts() as $context) {
$this->assertEquals(CONTEXT_COURSE, $context->contextlevel);
$this->assertNotEquals($context3, $context);
}
$contextlist = provider::get_contexts_for_userid($user2->id);
$this->assertCount(1, $contextlist->get_contextids());
$this->assertEquals($context1, $contextlist->current());
}
/**
* Test that stat data is exported as required.
*/
public function test_export_user_data(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$context1 = \context_course::instance($course1->id);
$context2 = \context_course::instance($course2->id);
$this->create_stats($course1->id, $user->id, 'stats_user_daily');
$this->create_stats($course1->id, $user->id, 'stats_user_daily');
$this->create_stats($course2->id, $user->id, 'stats_user_weekly');
$this->create_stats($course2->id, $user->id, 'stats_user_monthly');
$this->create_stats($course1->id, $user->id, 'stats_user_monthly');
$approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'report_stats', [$context1->id, $context2->id]);
provider::export_user_data($approvedlist);
$writer = \core_privacy\local\request\writer::with_context($context1);
$dailystats = (array) $writer->get_data([get_string('privacy:dailypath', 'report_stats')]);
$this->assertCount(2, $dailystats);
$monthlystats = (array) $writer->get_data([get_string('privacy:monthlypath', 'report_stats')]);
$this->assertCount(1, $monthlystats);
$data = array_shift($monthlystats);
$this->assertEquals($course1->fullname, $data['course']);
$writer = \core_privacy\local\request\writer::with_context($context2);
$monthlystats = (array) $writer->get_data([get_string('privacy:monthlypath', 'report_stats')]);
$this->assertCount(1, $monthlystats);
$data = array_shift($monthlystats);
$this->assertEquals($course2->fullname, $data['course']);
$weeklystats = (array) $writer->get_data([get_string('privacy:weeklypath', 'report_stats')]);
$this->assertCount(1, $weeklystats);
$data = array_shift($weeklystats);
$this->assertEquals($course2->fullname, $data['course']);
}
/**
* Test that stat data is deleted for a whole context.
*/
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$context1 = \context_course::instance($course1->id);
$context2 = \context_course::instance($course2->id);
$this->create_stats($course1->id, $user1->id, 'stats_user_daily');
$this->create_stats($course1->id, $user1->id, 'stats_user_daily');
$this->create_stats($course1->id, $user1->id, 'stats_user_monthly');
$this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
$this->create_stats($course2->id, $user2->id, 'stats_user_daily');
$this->create_stats($course2->id, $user2->id, 'stats_user_weekly');
$this->create_stats($course2->id, $user2->id, 'stats_user_monthly');
$dailyrecords = $DB->get_records('stats_user_daily');
$this->assertCount(3, $dailyrecords);
$weeklyrecords = $DB->get_records('stats_user_weekly');
$this->assertCount(2, $weeklyrecords);
$monthlyrecords = $DB->get_records('stats_user_monthly');
$this->assertCount(2, $monthlyrecords);
// Delete all user data for course 1.
provider::delete_data_for_all_users_in_context($context1);
$dailyrecords = $DB->get_records('stats_user_daily');
$this->assertCount(1, $dailyrecords);
$weeklyrecords = $DB->get_records('stats_user_weekly');
$this->assertCount(1, $weeklyrecords);
$monthlyrecords = $DB->get_records('stats_user_monthly');
$this->assertCount(1, $monthlyrecords);
}
/**
* Test that stats are deleted for one user.
*/
public function test_delete_data_for_user(): void {
global $DB;
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$context1 = \context_course::instance($course1->id);
$context2 = \context_course::instance($course2->id);
$this->create_stats($course1->id, $user1->id, 'stats_user_daily');
$this->create_stats($course1->id, $user1->id, 'stats_user_daily');
$this->create_stats($course1->id, $user1->id, 'stats_user_monthly');
$this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
$this->create_stats($course2->id, $user2->id, 'stats_user_daily');
$this->create_stats($course2->id, $user2->id, 'stats_user_weekly');
$this->create_stats($course2->id, $user2->id, 'stats_user_monthly');
$dailyrecords = $DB->get_records('stats_user_daily');
$this->assertCount(3, $dailyrecords);
$weeklyrecords = $DB->get_records('stats_user_weekly');
$this->assertCount(2, $weeklyrecords);
$monthlyrecords = $DB->get_records('stats_user_monthly');
$this->assertCount(2, $monthlyrecords);
// Delete all user data for course 1.
$approvedlist = new \core_privacy\local\request\approved_contextlist($user1, 'report_stats', [$context1->id]);
provider::delete_data_for_user($approvedlist);
$dailyrecords = $DB->get_records('stats_user_daily');
$this->assertCount(1, $dailyrecords);
$weeklyrecords = $DB->get_records('stats_user_weekly');
$this->assertCount(2, $weeklyrecords);
$monthlyrecords = $DB->get_records('stats_user_monthly');
$this->assertCount(1, $monthlyrecords);
}
/**
* Test that only users within a course context are fetched.
*/
public function test_get_users_in_context(): void {
$this->resetAfterTest();
$component = 'report_stats';
// Create user1.
$user1 = $this->getDataGenerator()->create_user();
// Create user2.
$user2 = $this->getDataGenerator()->create_user();
// Create course1.
$course1 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
// Create course2.
$course2 = $this->getDataGenerator()->create_course();
$coursecontext2 = \context_course::instance($course2->id);
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(0, $userlist2);
$this->create_stats($course1->id, $user1->id, 'stats_user_daily');
$this->create_stats($course2->id, $user1->id, 'stats_user_monthly');
$this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
// The list of users within the course context should contain users.
provider::get_users_in_context($userlist1);
$this->assertCount(2, $userlist1);
$this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
$this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
$this->assertTrue(in_array($user1->id, $userlist2->get_userids()));
// The list of users within other contexts than course should be empty.
$systemcontext = \context_system::instance();
$userlist3 = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist3);
$this->assertCount(0, $userlist3);
}
/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users(): void {
$this->resetAfterTest();
$component = 'report_stats';
// Create user1.
$user1 = $this->getDataGenerator()->create_user();
// Create user2.
$user2 = $this->getDataGenerator()->create_user();
// Create user3.
$user3 = $this->getDataGenerator()->create_user();
// Create course1.
$course1 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
// Create course2.
$course2 = $this->getDataGenerator()->create_course();
$coursecontext2 = \context_course::instance($course2->id);
$this->create_stats($course1->id, $user1->id, 'stats_user_daily');
$this->create_stats($course2->id, $user1->id, 'stats_user_monthly');
$this->create_stats($course1->id, $user2->id, 'stats_user_weekly');
$this->create_stats($course1->id, $user3->id, 'stats_user_weekly');
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(3, $userlist1);
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
// Convert $userlist1 into an approved_contextlist.
$approvedlist1 = new approved_userlist($coursecontext1, $component, [$user1->id, $user2->id]);
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist1);
// Re-fetch users in coursecontext1.
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
provider::get_users_in_context($userlist1);
// The approved user data in coursecontext1 should be deleted.
// The user list should still return user3.
$this->assertCount(1, $userlist1);
$this->assertTrue(in_array($user3->id, $userlist1->get_userids()));
// Re-fetch users in coursecontext2.
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
// The user data in coursecontext2 should be still present.
$this->assertCount(1, $userlist2);
// Convert $userlist2 into an approved_contextlist in the system context.
$systemcontext = \context_system::instance();
$approvedlist2 = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist2);
// Re-fetch users in coursecontext2.
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
// The user data in systemcontext should not be deleted.
$this->assertCount(1, $userlist2);
}
}