| Línea 17... |
Línea 17... |
| 17 |
namespace core\hub;
|
17 |
namespace core\hub;
|
| Línea 18... |
Línea 18... |
| 18 |
|
18 |
|
| 19 |
/**
|
19 |
/**
|
| 20 |
* Class containing unit tests for the site registration class.
|
20 |
* Class containing unit tests for the site registration class.
|
| 21 |
*
|
21 |
*
|
| 22 |
* @package core
|
22 |
* @package core
|
| 23 |
* @copyright 2023 Matt Porritt <matt.porritt@moodle.com>
|
23 |
* @copyright 2023 Matt Porritt <matt.porritt@moodle.com>
|
| 24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 25 |
*
|
25 |
* @covers \core\hub\registration
|
| 26 |
*/
|
26 |
*/
|
| Línea 27... |
Línea 27... |
| 27 |
class registration_test extends \advanced_testcase {
|
27 |
final class registration_test extends \advanced_testcase {
|
| 28 |
|
28 |
|
| 29 |
/**
|
- |
|
| 30 |
* Test getting site registration information.
|
- |
|
| 31 |
*
|
- |
|
| 32 |
* return void
|
29 |
/**
|
| 33 |
* @covers \core\hub\registration
|
30 |
* Test getting site registration information.
|
| 34 |
*/
|
31 |
*/
|
| 35 |
public function test_get_site_info(): void {
|
32 |
public function test_get_site_info(): void {
|
| Línea 48... |
Línea 45... |
| 48 |
$this->assertNull($siteinfo['policyagreed']);
|
45 |
$this->assertNull($siteinfo['policyagreed']);
|
| 49 |
$this->assertEquals($CFG->dbtype, $siteinfo['dbtype']);
|
46 |
$this->assertEquals($CFG->dbtype, $siteinfo['dbtype']);
|
| 50 |
$this->assertEquals('manual', $siteinfo['primaryauthtype']);
|
47 |
$this->assertEquals('manual', $siteinfo['primaryauthtype']);
|
| 51 |
$this->assertEquals(1, $siteinfo['coursesnodates']);
|
48 |
$this->assertEquals(1, $siteinfo['coursesnodates']);
|
| 52 |
}
|
49 |
}
|
| - |
|
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 |
}
|
| 53 |
}
|
199 |
}
|