| 1 |
efrain |
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 core\hub;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Class containing unit tests for the site registration class.
|
|
|
21 |
*
|
| 1441 |
ariadna |
22 |
* @package core
|
| 1 |
efrain |
23 |
* @copyright 2023 Matt Porritt <matt.porritt@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 1441 |
ariadna |
25 |
* @covers \core\hub\registration
|
| 1 |
efrain |
26 |
*/
|
| 1441 |
ariadna |
27 |
final class registration_test extends \advanced_testcase {
|
| 1 |
efrain |
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Test getting site registration information.
|
|
|
31 |
*/
|
|
|
32 |
public function test_get_site_info(): void {
|
|
|
33 |
global $CFG;
|
|
|
34 |
$this->resetAfterTest();
|
|
|
35 |
|
|
|
36 |
// Create some courses with end dates.
|
|
|
37 |
$generator = $this->getDataGenerator();
|
|
|
38 |
$generator->create_course(['enddate' => time() + 1000]);
|
|
|
39 |
$generator->create_course(['enddate' => time() + 1000]);
|
|
|
40 |
|
|
|
41 |
$generator->create_course(); // Course with no end date.
|
|
|
42 |
|
|
|
43 |
$siteinfo = registration::get_site_info();
|
|
|
44 |
|
|
|
45 |
$this->assertNull($siteinfo['policyagreed']);
|
|
|
46 |
$this->assertEquals($CFG->dbtype, $siteinfo['dbtype']);
|
|
|
47 |
$this->assertEquals('manual', $siteinfo['primaryauthtype']);
|
|
|
48 |
$this->assertEquals(1, $siteinfo['coursesnodates']);
|
|
|
49 |
}
|
| 1441 |
ariadna |
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Test getting the plugin usage data.
|
|
|
53 |
*/
|
|
|
54 |
public function test_get_plugin_usage(): void {
|
|
|
55 |
global $DB;
|
|
|
56 |
$this->resetAfterTest();
|
|
|
57 |
|
|
|
58 |
// Create some courses with end dates.
|
|
|
59 |
$generator = $this->getDataGenerator();
|
|
|
60 |
$course = $generator->create_course();
|
|
|
61 |
|
|
|
62 |
// Create some assignments.
|
|
|
63 |
$generator->create_module('assign', ['course' => $course->id]);
|
|
|
64 |
$generator->create_module('assign', ['course' => $course->id]);
|
|
|
65 |
$generator->create_module('assign', ['course' => $course->id]);
|
|
|
66 |
|
|
|
67 |
// Create some quizzes.
|
|
|
68 |
$generator->create_module('quiz', ['course' => $course->id]);
|
|
|
69 |
$generator->create_module('quiz', ['course' => $course->id]);
|
|
|
70 |
|
|
|
71 |
// Add some blocks.
|
|
|
72 |
$generator->create_block('online_users');
|
|
|
73 |
$generator->create_block('online_users');
|
|
|
74 |
$generator->create_block('online_users');
|
|
|
75 |
$generator->create_block('online_users');
|
|
|
76 |
|
|
|
77 |
// Disabled a plugin.
|
|
|
78 |
$DB->set_field('modules', 'visible', 0, ['name' => 'feedback']);
|
|
|
79 |
|
|
|
80 |
// Check our plugin usage counts and enabled states are correct.
|
|
|
81 |
$pluginusage = registration::get_plugin_usage_data();
|
|
|
82 |
$this->assertEquals(3, $pluginusage['mod']['assign']['count']);
|
|
|
83 |
$this->assertEquals(2, $pluginusage['mod']['quiz']['count']);
|
|
|
84 |
$this->assertEquals(4, $pluginusage['block']['online_users']['count']);
|
|
|
85 |
$this->assertEquals(0, $pluginusage['mod']['feedback']['enabled']);
|
|
|
86 |
$this->assertEquals(1, $pluginusage['mod']['assign']['enabled']);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Test the AI usage data is calculated correctly.
|
|
|
91 |
*/
|
|
|
92 |
public function test_get_ai_usage(): void {
|
|
|
93 |
$this->resetAfterTest();
|
|
|
94 |
|
|
|
95 |
$clock = $this->mock_clock_with_frozen(1700000000);
|
|
|
96 |
$this->generate_ai_usage_data();
|
|
|
97 |
|
|
|
98 |
// Get our site info and check the expected calculations are correct.
|
|
|
99 |
$siteinfo = registration::get_site_info();
|
|
|
100 |
$aisuage = json_decode($siteinfo['aiusage']);
|
|
|
101 |
// Check generated text.
|
|
|
102 |
$this->assertEquals(1, $aisuage->aiprovider_openai->generate_text->success_count);
|
|
|
103 |
$this->assertEquals(0, $aisuage->aiprovider_openai->generate_text->fail_count);
|
|
|
104 |
// Check generated images.
|
|
|
105 |
$this->assertEquals(2, $aisuage->aiprovider_openai->generate_image->success_count);
|
|
|
106 |
$this->assertEquals(3, $aisuage->aiprovider_openai->generate_image->fail_count);
|
|
|
107 |
$this->assertEquals(15, $aisuage->aiprovider_openai->generate_image->average_time);
|
|
|
108 |
$this->assertEquals(403, $aisuage->aiprovider_openai->generate_image->predominant_error);
|
|
|
109 |
// Check time range is set correctly.
|
|
|
110 |
$this->assertEquals($clock->time() - WEEKSECS, $aisuage->time_range->timefrom);
|
|
|
111 |
$this->assertEquals($clock->time(), $aisuage->time_range->timeto);
|
|
|
112 |
// Check model counts.
|
|
|
113 |
$gpt4omodel = 'gpt-4o';
|
|
|
114 |
$dalle3model = 'dall-e-3';
|
|
|
115 |
$this->assertEquals(1, $aisuage->aiprovider_openai->generate_text->models->{$gpt4omodel}->count);
|
|
|
116 |
$this->assertEquals(2, $aisuage->aiprovider_openai->generate_image->models->{$dalle3model}->count);
|
|
|
117 |
$this->assertEquals(3, $aisuage->aiprovider_openai->generate_image->models->unknown->count);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Create some dummy AI usage data.
|
|
|
122 |
*/
|
|
|
123 |
private function generate_ai_usage_data(): void {
|
|
|
124 |
global $DB;
|
|
|
125 |
|
|
|
126 |
$clock = $this->mock_clock_with_frozen(1700000000);
|
|
|
127 |
|
|
|
128 |
// Record some generated text.
|
|
|
129 |
$record = new \stdClass();
|
|
|
130 |
$record->provider = 'aiprovider_openai';
|
|
|
131 |
$record->actionname = 'generate_text';
|
|
|
132 |
$record->actionid = 1;
|
|
|
133 |
$record->userid = 1;
|
|
|
134 |
$record->contextid = 1;
|
|
|
135 |
$record->success = true;
|
|
|
136 |
$record->timecreated = $clock->time() - 5;
|
|
|
137 |
$record->timecompleted = $clock->time();
|
|
|
138 |
$record->model = 'gpt-4o';
|
|
|
139 |
$DB->insert_record('ai_action_register', $record);
|
|
|
140 |
|
|
|
141 |
// Record a generated image.
|
|
|
142 |
$record->actionname = 'generate_image';
|
|
|
143 |
$record->actionid = 111;
|
|
|
144 |
$record->timecreated = $clock->time() - 20;
|
|
|
145 |
$record->model = 'dall-e-3';
|
|
|
146 |
$DB->insert_record('ai_action_register', $record);
|
|
|
147 |
// Record another image.
|
|
|
148 |
$record->actionid = 222;
|
|
|
149 |
$record->timecreated = $clock->time() - 10;
|
|
|
150 |
$DB->insert_record('ai_action_register', $record);
|
|
|
151 |
|
|
|
152 |
// Record some errors.
|
|
|
153 |
$record->actionname = 'generate_image';
|
|
|
154 |
$record->actionid = 4;
|
|
|
155 |
$record->success = false;
|
|
|
156 |
$record->errorcode = 403;
|
|
|
157 |
$record->model = null;
|
|
|
158 |
$DB->insert_record('ai_action_register', $record);
|
|
|
159 |
$record->actionid = 5;
|
|
|
160 |
$record->errorcode = 403;
|
|
|
161 |
$DB->insert_record('ai_action_register', $record);
|
|
|
162 |
$record->actionid = 6;
|
|
|
163 |
$record->errorcode = 404;
|
|
|
164 |
$DB->insert_record('ai_action_register', $record);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Test the show AI usage data.
|
|
|
169 |
*/
|
|
|
170 |
public function test_show_ai_usage(): void {
|
|
|
171 |
$this->resetAfterTest();
|
|
|
172 |
|
|
|
173 |
// Init the registration class.
|
|
|
174 |
$registration = new registration();
|
|
|
175 |
|
|
|
176 |
// There should be no data to show yet.
|
|
|
177 |
$aisuagedata = $registration->show_ai_usage();
|
|
|
178 |
$this->assertTrue(empty($aisuagedata));
|
|
|
179 |
|
|
|
180 |
// After generating some data, there should now be some data to show.
|
|
|
181 |
$this->generate_ai_usage_data();
|
|
|
182 |
$aisuagedata = $registration->show_ai_usage();
|
|
|
183 |
$this->assertTrue(!empty($aisuagedata));
|
|
|
184 |
|
|
|
185 |
foreach ($aisuagedata['providers'] as $provider) {
|
|
|
186 |
$this->assertEquals('OpenAI API provider', $provider['providername']);
|
|
|
187 |
$this->assertTrue(!empty($provider['aiactions']));
|
|
|
188 |
|
|
|
189 |
foreach ($provider['aiactions'] as $action) {
|
|
|
190 |
$actionname = $action['actionname'];
|
|
|
191 |
$this->assertTrue(!empty($actionname));
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
$timerange = $aisuagedata['timerange'];
|
|
|
196 |
$this->assertEquals(get_string('time_range', 'hub'), $timerange['label']);
|
|
|
197 |
$this->assertTrue(!empty($timerange['values']));
|
|
|
198 |
}
|
| 1 |
efrain |
199 |
}
|