<?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 core\hub;
/**
* Class containing unit tests for the site registration class.
*
* @package core
* @copyright 2023 Matt Porritt <matt.porritt@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core\hub\registration
*/
final class registration_test extends \advanced_testcase {
/**
* Test getting site registration information.
*/
public function test_get_site_info(): void {
global $CFG;
$this->resetAfterTest();
// Create some courses with end dates.
$generator = $this->getDataGenerator();
$generator->create_course(['enddate' => time() + 1000]);
$generator->create_course(['enddate' => time() + 1000]);
$generator->create_course(); // Course with no end date.
$siteinfo = registration::get_site_info();
$this->assertNull($siteinfo['policyagreed']);
$this->assertEquals($CFG->dbtype, $siteinfo['dbtype']);
$this->assertEquals('manual', $siteinfo['primaryauthtype']);
$this->assertEquals(1, $siteinfo['coursesnodates']);
}
/**
* Test getting the plugin usage data.
*/
public function test_get_plugin_usage(): void {
global $DB;
$this->resetAfterTest();
// Create some courses with end dates.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
// Create some assignments.
$generator->create_module('assign', ['course' => $course->id]);
$generator->create_module('assign', ['course' => $course->id]);
$generator->create_module('assign', ['course' => $course->id]);
// Create some quizzes.
$generator->create_module('quiz', ['course' => $course->id]);
$generator->create_module('quiz', ['course' => $course->id]);
// Add some blocks.
$generator->create_block('online_users');
$generator->create_block('online_users');
$generator->create_block('online_users');
$generator->create_block('online_users');
// Disabled a plugin.
$DB->set_field('modules', 'visible', 0, ['name' => 'feedback']);
// Check our plugin usage counts and enabled states are correct.
$pluginusage = registration::get_plugin_usage_data();
$this->assertEquals(3, $pluginusage['mod']['assign']['count']);
$this->assertEquals(2, $pluginusage['mod']['quiz']['count']);
$this->assertEquals(4, $pluginusage['block']['online_users']['count']);
$this->assertEquals(0, $pluginusage['mod']['feedback']['enabled']);
$this->assertEquals(1, $pluginusage['mod']['assign']['enabled']);
}
/**
* Test the AI usage data is calculated correctly.
*/
public function test_get_ai_usage(): void {
global $CFG, $DB;
$this->resetAfterTest();
$clock = $this->mock_clock_with_frozen();
// Record some generated text.
$record = new \stdClass();
$record->provider = 'openai';
$record->actionname = 'generate_text';
$record->actionid = 1;
$record->userid = 1;
$record->contextid = 1;
$record->success = true;
$record->timecreated = $clock->time() - 5;
$record->timecompleted = $clock->time();
$DB->insert_record('ai_action_register', $record);
// Record a generated image.
$record->actionname = 'generate_image';
$record->actionid = 2;
$record->timecreated = $clock->time() - 20;
$DB->insert_record('ai_action_register', $record);
// Record another image.
$record->actionid = 3;
$record->timecreated = $clock->time() - 10;
$DB->insert_record('ai_action_register', $record);
// Record some errors.
$record->actionname = 'generate_image';
$record->actionid = 4;
$record->success = false;
$record->errorcode = 403;
$DB->insert_record('ai_action_register', $record);
$record->actionid = 5;
$record->errorcode = 403;
$DB->insert_record('ai_action_register', $record);
$record->actionid = 6;
$record->errorcode = 404;
$DB->insert_record('ai_action_register', $record);
// Get our site info and check the expected calculations are correct.
$siteinfo = registration::get_site_info();
$aisuage = json_decode($siteinfo['aiusage']);
// Check generated text.
$this->assertEquals(1, $aisuage->openai->generate_text->success_count);
$this->assertEquals(0, $aisuage->openai->generate_text->fail_count);
// Check generated images.
$this->assertEquals(2, $aisuage->openai->generate_image->success_count);
$this->assertEquals(3, $aisuage->openai->generate_image->fail_count);
$this->assertEquals(15, $aisuage->openai->generate_image->average_time);
$this->assertEquals(403, $aisuage->openai->generate_image->predominant_error);
// Check time range is set correctly.
$this->assertEquals($clock->time() - WEEKSECS, $aisuage->time_range->timefrom);
$this->assertEquals($clock->time(), $aisuage->time_range->timeto);
}
}