1441 |
ariadna |
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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core_competency\reportbuilder\datasource;
|
|
|
20 |
|
|
|
21 |
use core_competency_generator;
|
|
|
22 |
use core_competency\user_competency;
|
|
|
23 |
use core_reportbuilder_generator;
|
|
|
24 |
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
|
|
|
25 |
use core_reportbuilder\tests\core_reportbuilder_testcase;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Unit tests for comptencies datasource
|
|
|
29 |
*
|
|
|
30 |
* @package core_competency
|
|
|
31 |
* @covers \core_competency\reportbuilder\datasource\competencies
|
|
|
32 |
* @copyright 2024 Paul Holden <paulh@moodle.com>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
final class competencies_test extends core_reportbuilder_testcase {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Test default datasource
|
|
|
39 |
*/
|
|
|
40 |
public function test_datasource_default(): void {
|
|
|
41 |
$this->resetAfterTest();
|
|
|
42 |
|
|
|
43 |
$user = $this->getDataGenerator()->create_user();
|
|
|
44 |
|
|
|
45 |
/** @var core_competency_generator $generator */
|
|
|
46 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_competency');
|
|
|
47 |
|
|
|
48 |
// First framework has two competencies.
|
|
|
49 |
$framework = $generator->create_framework(['shortname' => 'Zoology']);
|
|
|
50 |
$generator->create_competency(['competencyframeworkid' => $framework->get('id'), 'shortname' => 'Zebras']);
|
|
|
51 |
$competency = $generator->create_competency([
|
|
|
52 |
'competencyframeworkid' => $framework->get('id'),
|
|
|
53 |
'shortname' => 'Aardvarks',
|
|
|
54 |
]);
|
|
|
55 |
|
|
|
56 |
// Framework two has no competencies.
|
|
|
57 |
$generator->create_framework(['shortname' => 'Algebra']);
|
|
|
58 |
|
|
|
59 |
// Assign user to second competency.
|
|
|
60 |
$generator->create_user_competency([
|
|
|
61 |
'competencyid' => $competency->get('id'),
|
|
|
62 |
'userid' => $user->id,
|
|
|
63 |
'proficiency' => true,
|
|
|
64 |
'grade' => 1,
|
|
|
65 |
]);
|
|
|
66 |
|
|
|
67 |
/** @var core_reportbuilder_generator $generator */
|
|
|
68 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
69 |
$report = $generator->create_report(['name' => 'Competencies', 'source' => competencies::class, 'default' => 1]);
|
|
|
70 |
|
|
|
71 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
72 |
|
|
|
73 |
// Default columns are framework name, competency name, user fullname, proficient. Sorted by first three.
|
|
|
74 |
$this->assertEquals([
|
|
|
75 |
['Algebra', '', '', ''],
|
|
|
76 |
['Zoology', 'Aardvarks', fullname($user), 'Yes'],
|
|
|
77 |
['Zoology', 'Zebras', '', ''],
|
|
|
78 |
], array_map('array_values', $content));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Test datasource columns that aren't added by default
|
|
|
83 |
*/
|
|
|
84 |
public function test_datasource_non_default_columns(): void {
|
|
|
85 |
$this->resetAfterTest();
|
|
|
86 |
|
|
|
87 |
$user = $this->getDataGenerator()->create_user();
|
|
|
88 |
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
|
|
|
89 |
cohort_add_member($cohort->id, $user->id);
|
|
|
90 |
|
|
|
91 |
$scale = $this->getDataGenerator()->create_scale(['name' => 'My scale', 'scale' => 'A,B,C,D']);
|
|
|
92 |
|
|
|
93 |
/** @var core_competency_generator $generator */
|
|
|
94 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_competency');
|
|
|
95 |
|
|
|
96 |
$framework = $generator->create_framework(['description' => 'So cool', 'idnumber' => 'FRM101', 'scaleid' => $scale->id]);
|
|
|
97 |
$competency = $generator->create_competency(['competencyframeworkid' => $framework->get('id'), 'idnumber' => 'COM101']);
|
|
|
98 |
$generator->create_user_competency([
|
|
|
99 |
'competencyid' => $competency->get('id'),
|
|
|
100 |
'userid' => $user->id,
|
|
|
101 |
'proficiency' => true,
|
|
|
102 |
'grade' => 3,
|
|
|
103 |
]);
|
|
|
104 |
|
|
|
105 |
/** @var core_reportbuilder_generator $generator */
|
|
|
106 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
107 |
$report = $generator->create_report(['name' => 'Competencies', 'source' => competencies::class, 'default' => 0]);
|
|
|
108 |
|
|
|
109 |
// Framework.
|
|
|
110 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'framework:description']);
|
|
|
111 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'framework:idnumber']);
|
|
|
112 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'framework:scale']);
|
|
|
113 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'framework:visible']);
|
|
|
114 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'framework:timecreated']);
|
|
|
115 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'framework:timemodified']);
|
|
|
116 |
|
|
|
117 |
// Competency.
|
|
|
118 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'competency:idnumber']);
|
|
|
119 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'competency:timecreated']);
|
|
|
120 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'competency:timemodified']);
|
|
|
121 |
|
|
|
122 |
// User competency.
|
|
|
123 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'usercompetency:status']);
|
|
|
124 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'usercompetency:rating']);
|
|
|
125 |
|
|
|
126 |
// Cohort.
|
|
|
127 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
|
|
|
128 |
|
|
|
129 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
130 |
$this->assertCount(1, $content);
|
|
|
131 |
|
|
|
132 |
[
|
|
|
133 |
$frameworkdescription,
|
|
|
134 |
$frameworkidnumber,
|
|
|
135 |
$frameworkscale,
|
|
|
136 |
$frameworkvisible,
|
|
|
137 |
$frameworktimecreated,
|
|
|
138 |
$frameworktimemodified,
|
|
|
139 |
$competencyidnumber,
|
|
|
140 |
$competencytimecreated,
|
|
|
141 |
$competencytimemodified,
|
|
|
142 |
$usercompetencystatus,
|
|
|
143 |
$usercompetencyrating,
|
|
|
144 |
$cohortname,
|
|
|
145 |
] = array_values($content[0]);
|
|
|
146 |
|
|
|
147 |
$this->assertEquals('So cool', $frameworkdescription);
|
|
|
148 |
$this->assertEquals('FRM101', $frameworkidnumber);
|
|
|
149 |
$this->assertEquals('My scale', $frameworkscale);
|
|
|
150 |
$this->assertEquals('Yes', $frameworkvisible);
|
|
|
151 |
$this->assertNotEmpty($frameworktimecreated);
|
|
|
152 |
$this->assertNotEmpty($frameworktimemodified);
|
|
|
153 |
$this->assertEquals('COM101', $competencyidnumber);
|
|
|
154 |
$this->assertNotEmpty($competencytimecreated);
|
|
|
155 |
$this->assertNotEmpty($competencytimemodified);
|
|
|
156 |
$this->assertEquals('Idle', $usercompetencystatus);
|
|
|
157 |
$this->assertEquals('C', $usercompetencyrating);
|
|
|
158 |
$this->assertEquals('My cohort', $cohortname);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Data provider for {@see test_datasource_filters}
|
|
|
163 |
*
|
|
|
164 |
* @return array[]
|
|
|
165 |
*/
|
|
|
166 |
public static function datasource_filters_provider(): array {
|
|
|
167 |
return [
|
|
|
168 |
// Framework.
|
|
|
169 |
'Framework name' => ['framework:name', [
|
|
|
170 |
'framework:name_operator' => text::IS_EQUAL_TO,
|
|
|
171 |
'framework:name_value' => 'How much I care',
|
|
|
172 |
], true],
|
|
|
173 |
'Framework name (no match)' => ['framework:name', [
|
|
|
174 |
'framework:name_operator' => text::IS_EQUAL_TO,
|
|
|
175 |
'framework:name_value' => 'Something else',
|
|
|
176 |
], false],
|
|
|
177 |
'Framework idnumber' => ['framework:idnumber', [
|
|
|
178 |
'framework:idnumber_operator' => text::IS_EQUAL_TO,
|
|
|
179 |
'framework:idnumber_value' => 'FRM101',
|
|
|
180 |
], true],
|
|
|
181 |
'Framework idnumber (no match)' => ['framework:idnumber', [
|
|
|
182 |
'framework:idnumber_operator' => text::IS_EQUAL_TO,
|
|
|
183 |
'framework:idnumber_value' => 'FRM102',
|
|
|
184 |
], false],
|
|
|
185 |
'Framework scale' => ['framework:scale', [
|
|
|
186 |
'framework:scale_operator' => select::EQUAL_TO,
|
|
|
187 |
'framework:scale_value' => '<SCALEID>',
|
|
|
188 |
], true],
|
|
|
189 |
'Framework scale (no match)' => ['framework:scale', [
|
|
|
190 |
'framework:scale_operator' => select::NOT_EQUAL_TO,
|
|
|
191 |
'framework:scale_value' => '<SCALEID>',
|
|
|
192 |
], false],
|
|
|
193 |
'Framework visible' => ['framework:visible', [
|
|
|
194 |
'framework:visible_operator' => boolean_select::CHECKED,
|
|
|
195 |
], true],
|
|
|
196 |
'Framework visible (no match)' => ['framework:visible', [
|
|
|
197 |
'framework:visible_operator' => boolean_select::NOT_CHECKED,
|
|
|
198 |
], false],
|
|
|
199 |
'Framework time created' => ['framework:timecreated', [
|
|
|
200 |
'framework:timecreated_operator' => date::DATE_RANGE,
|
|
|
201 |
'framework:timecreated_from' => 1622502000,
|
|
|
202 |
], true],
|
|
|
203 |
'Framework time created (no match)' => ['framework:timecreated', [
|
|
|
204 |
'framework:timecreated_operator' => date::DATE_RANGE,
|
|
|
205 |
'framework:timecreated_to' => 1622502000,
|
|
|
206 |
], false],
|
|
|
207 |
'Framework time modified' => ['framework:timemodified', [
|
|
|
208 |
'framework:timemodified_operator' => date::DATE_RANGE,
|
|
|
209 |
'framework:timemodified_from' => 1622502000,
|
|
|
210 |
], true],
|
|
|
211 |
'Framework time modified (no match)' => ['framework:timemodified', [
|
|
|
212 |
'framework:timemodified_operator' => date::DATE_RANGE,
|
|
|
213 |
'framework:timemodified_to' => 1622502000,
|
|
|
214 |
], false],
|
|
|
215 |
|
|
|
216 |
// Competency.
|
|
|
217 |
'Competency name' => ['competency:name', [
|
|
|
218 |
'competency:name_operator' => text::IS_EQUAL_TO,
|
|
|
219 |
'competency:name_value' => 'My framework',
|
|
|
220 |
], true],
|
|
|
221 |
'Competency name (no match)' => ['competency:name', [
|
|
|
222 |
'competency:name_operator' => text::IS_EQUAL_TO,
|
|
|
223 |
'competency:name_value' => 'Something else',
|
|
|
224 |
], false],
|
|
|
225 |
'Competency idnumber' => ['competency:idnumber', [
|
|
|
226 |
'competency:idnumber_operator' => text::IS_EQUAL_TO,
|
|
|
227 |
'competency:idnumber_value' => 'COM101',
|
|
|
228 |
], true],
|
|
|
229 |
'Competency idnumber (no match)' => ['competency:idnumber', [
|
|
|
230 |
'competency:idnumber_operator' => text::IS_EQUAL_TO,
|
|
|
231 |
'competency:idnumber_value' => 'COM102',
|
|
|
232 |
], false],
|
|
|
233 |
'Competency time created' => ['competency:timecreated', [
|
|
|
234 |
'competency:timecreated_operator' => date::DATE_RANGE,
|
|
|
235 |
'competency:timecreated_from' => 1622502000,
|
|
|
236 |
], true],
|
|
|
237 |
'Competency time created (no match)' => ['competency:timecreated', [
|
|
|
238 |
'competency:timecreated_operator' => date::DATE_RANGE,
|
|
|
239 |
'competency:timecreated_to' => 1622502000,
|
|
|
240 |
], false],
|
|
|
241 |
'Competency time modified' => ['competency:timemodified', [
|
|
|
242 |
'competency:timemodified_operator' => date::DATE_RANGE,
|
|
|
243 |
'competency:timemodified_from' => 1622502000,
|
|
|
244 |
], true],
|
|
|
245 |
'Competency time modified (no match)' => ['competency:timemodified', [
|
|
|
246 |
'competency:timemodified_operator' => date::DATE_RANGE,
|
|
|
247 |
'competency:timemodified_to' => 1622502000,
|
|
|
248 |
], false],
|
|
|
249 |
|
|
|
250 |
// User competency.
|
|
|
251 |
'User competency status' => ['usercompetency:status', [
|
|
|
252 |
'usercompetency:status_operator' => SELECT::EQUAL_TO,
|
|
|
253 |
'usercompetency:status_value' => user_competency::STATUS_IDLE,
|
|
|
254 |
], true],
|
|
|
255 |
'User competency status (no match)' => ['usercompetency:status', [
|
|
|
256 |
'usercompetency:status_operator' => SELECT::EQUAL_TO,
|
|
|
257 |
'usercompetency:status_value' => user_competency::STATUS_WAITING_FOR_REVIEW,
|
|
|
258 |
], false],
|
|
|
259 |
'User competency proficient' => ['usercompetency:proficient', [
|
|
|
260 |
'usercompetency:proficient_operator' => boolean_select::CHECKED,
|
|
|
261 |
], true],
|
|
|
262 |
'User competency proficient (no match)' => ['usercompetency:proficient', [
|
|
|
263 |
'usercompetency:proficient_operator' => boolean_select::NOT_CHECKED,
|
|
|
264 |
], false],
|
|
|
265 |
|
|
|
266 |
// User.
|
|
|
267 |
'User username' => ['user:username', [
|
|
|
268 |
'user:username_operator' => text::IS_EQUAL_TO,
|
|
|
269 |
'user:username_value' => 'testuser',
|
|
|
270 |
], true],
|
|
|
271 |
'User username (no match)' => ['user:username', [
|
|
|
272 |
'user:username_operator' => text::IS_NOT_EQUAL_TO,
|
|
|
273 |
'user:username_value' => 'testuser',
|
|
|
274 |
], false],
|
|
|
275 |
|
|
|
276 |
// Cohort.
|
|
|
277 |
'Cohort name' => ['cohort:name', [
|
|
|
278 |
'cohort:name_operator' => text::IS_EQUAL_TO,
|
|
|
279 |
'cohort:name_value' => 'My cohort',
|
|
|
280 |
], true],
|
|
|
281 |
'Cohort name (no match)' => ['cohort:name', [
|
|
|
282 |
'cohort:name_operator' => text::IS_EQUAL_TO,
|
|
|
283 |
'cohort:name_value' => 'Another cohort',
|
|
|
284 |
], false],
|
|
|
285 |
];
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Test datasource filters
|
|
|
290 |
*
|
|
|
291 |
* @param string $filtername
|
|
|
292 |
* @param array $filtervalues
|
|
|
293 |
* @param bool $expectmatch
|
|
|
294 |
*
|
|
|
295 |
* @dataProvider datasource_filters_provider
|
|
|
296 |
*/
|
|
|
297 |
public function test_datasource_filters(
|
|
|
298 |
string $filtername,
|
|
|
299 |
array $filtervalues,
|
|
|
300 |
bool $expectmatch
|
|
|
301 |
): void {
|
|
|
302 |
$this->resetAfterTest();
|
|
|
303 |
|
|
|
304 |
$user = $this->getDataGenerator()->create_user(['username' => 'testuser']);
|
|
|
305 |
$cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
|
|
|
306 |
cohort_add_member($cohort->id, $user->id);
|
|
|
307 |
|
|
|
308 |
$scale = $this->getDataGenerator()->create_scale([]);
|
|
|
309 |
|
|
|
310 |
/** @var core_competency_generator $generator */
|
|
|
311 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_competency');
|
|
|
312 |
|
|
|
313 |
$framework = $generator->create_framework([
|
|
|
314 |
'shortname' => 'How much I care',
|
|
|
315 |
'description' => 'Sometimes I feel my heart will overflow',
|
|
|
316 |
'idnumber' => 'FRM101',
|
|
|
317 |
'scaleid' => $scale->id,
|
|
|
318 |
]);
|
|
|
319 |
$competency = $generator->create_competency([
|
|
|
320 |
'competencyframeworkid' => $framework->get('id'),
|
|
|
321 |
'shortname' => 'My framework',
|
|
|
322 |
'idnumber' => 'COM101',
|
|
|
323 |
]);
|
|
|
324 |
$generator->create_user_competency([
|
|
|
325 |
'competencyid' => $competency->get('id'),
|
|
|
326 |
'userid' => $user->id,
|
|
|
327 |
'proficiency' => true,
|
|
|
328 |
'grade' => 3,
|
|
|
329 |
]);
|
|
|
330 |
|
|
|
331 |
/** @var core_reportbuilder_generator $generator */
|
|
|
332 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
333 |
|
|
|
334 |
// Create report containing single column, and given filter.
|
|
|
335 |
$report = $generator->create_report(['name' => 'Competencies', 'source' => competencies::class, 'default' => 0]);
|
|
|
336 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'framework:idnumber']);
|
|
|
337 |
|
|
|
338 |
// Add filter, set it's values.
|
|
|
339 |
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
|
|
|
340 |
$content = $this->get_custom_report_content(
|
|
|
341 |
reportid: $report->get('id'),
|
|
|
342 |
filtervalues: str_replace('<SCALEID>', $scale->id, $filtervalues),
|
|
|
343 |
);
|
|
|
344 |
|
|
|
345 |
if ($expectmatch) {
|
|
|
346 |
$this->assertCount(1, $content);
|
|
|
347 |
$this->assertEquals('FRM101', reset($content[0]));
|
|
|
348 |
} else {
|
|
|
349 |
$this->assertEmpty($content);
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* Stress test datasource
|
|
|
355 |
*
|
|
|
356 |
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
|
|
|
357 |
*/
|
|
|
358 |
public function test_stress_datasource(): void {
|
|
|
359 |
if (!PHPUNIT_LONGTEST) {
|
|
|
360 |
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
$this->resetAfterTest();
|
|
|
364 |
|
|
|
365 |
$user = $this->getDataGenerator()->create_user();
|
|
|
366 |
|
|
|
367 |
/** @var core_competency_generator $generator */
|
|
|
368 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_competency');
|
|
|
369 |
|
|
|
370 |
$framework = $generator->create_framework();
|
|
|
371 |
$competency = $generator->create_competency(['competencyframeworkid' => $framework->get('id')]);
|
|
|
372 |
$generator->create_user_competency(['competencyid' => $competency->get('id'), 'userid' => $user->id]);
|
|
|
373 |
|
|
|
374 |
$this->datasource_stress_test_columns(competencies::class);
|
|
|
375 |
$this->datasource_stress_test_columns_aggregation(competencies::class);
|
|
|
376 |
$this->datasource_stress_test_conditions(competencies::class, 'framework:idnumber');
|
|
|
377 |
}
|
|
|
378 |
}
|