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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core_reportbuilder\local\helpers;
|
|
|
20 |
|
|
|
21 |
use advanced_testcase;
|
|
|
22 |
use core_reportbuilder_generator;
|
|
|
23 |
use invalid_parameter_exception;
|
|
|
24 |
use core_reportbuilder\datasource;
|
|
|
25 |
use core_reportbuilder\local\models\column;
|
|
|
26 |
use core_reportbuilder\local\models\filter;
|
|
|
27 |
use core_tag_tag;
|
|
|
28 |
use core_user\reportbuilder\datasource\users;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Unit tests for the report helper class
|
|
|
32 |
*
|
|
|
33 |
* @package core_reportbuilder
|
|
|
34 |
* @covers \core_reportbuilder\local\helpers\report
|
|
|
35 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class report_test extends advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Test creation report
|
|
|
42 |
*/
|
|
|
43 |
public function test_create_report(): void {
|
|
|
44 |
$this->resetAfterTest();
|
|
|
45 |
$this->setAdminUser();
|
|
|
46 |
|
|
|
47 |
$report = report::create_report((object) [
|
|
|
48 |
'name' => 'My report with tags',
|
|
|
49 |
'source' => users::class,
|
|
|
50 |
'tags' => ['cat', 'dog'],
|
|
|
51 |
]);
|
|
|
52 |
|
|
|
53 |
$this->assertEquals('My report with tags', $report->get('name'));
|
|
|
54 |
$this->assertEquals(datasource::TYPE_CUSTOM_REPORT, $report->get('type'));
|
|
|
55 |
$this->assertEqualsCanonicalizing(['cat', 'dog'],
|
|
|
56 |
core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id')));
|
|
|
57 |
|
|
|
58 |
$report = report::create_report((object) [
|
|
|
59 |
'name' => 'My report without tags',
|
|
|
60 |
'source' => users::class,
|
|
|
61 |
]);
|
|
|
62 |
|
|
|
63 |
$this->assertEquals('My report without tags', $report->get('name'));
|
|
|
64 |
$this->assertEquals(datasource::TYPE_CUSTOM_REPORT, $report->get('type'));
|
|
|
65 |
$this->assertEmpty(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report',
|
|
|
66 |
$report->get('id')));
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Test updating report
|
|
|
71 |
*/
|
|
|
72 |
public function test_update_report(): void {
|
|
|
73 |
$this->resetAfterTest();
|
|
|
74 |
$this->setAdminUser();
|
|
|
75 |
|
|
|
76 |
/** @var core_reportbuilder_generator $generator */
|
|
|
77 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
78 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'uniquerows' => 0]);
|
|
|
79 |
|
|
|
80 |
$reportupdated = report::update_report((object) [
|
|
|
81 |
'id' => $report->get('id'),
|
|
|
82 |
'name' => 'My renamed report without add tags',
|
|
|
83 |
'uniquerows' => 1,
|
|
|
84 |
]);
|
|
|
85 |
|
|
|
86 |
$this->assertEquals('My renamed report without add tags', $reportupdated->get('name'));
|
|
|
87 |
$this->assertTrue($reportupdated->get('uniquerows'));
|
|
|
88 |
$this->assertEmpty(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report',
|
|
|
89 |
$reportupdated->get('id')));
|
|
|
90 |
|
|
|
91 |
$reportupdated = report::update_report((object) [
|
|
|
92 |
'id' => $report->get('id'),
|
|
|
93 |
'name' => 'My renamed report adding tags',
|
|
|
94 |
'uniquerows' => 1,
|
|
|
95 |
'tags' => ['cat', 'dog'],
|
|
|
96 |
]);
|
|
|
97 |
|
|
|
98 |
$this->assertEquals('My renamed report adding tags', $reportupdated->get('name'));
|
|
|
99 |
$this->assertTrue($reportupdated->get('uniquerows'));
|
|
|
100 |
$this->assertEqualsCanonicalizing(['cat', 'dog'],
|
|
|
101 |
core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $reportupdated->get('id')));
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Test deleting report
|
|
|
106 |
*/
|
|
|
107 |
public function test_delete_report(): void {
|
|
|
108 |
$this->resetAfterTest();
|
|
|
109 |
$this->setAdminUser();
|
|
|
110 |
|
|
|
111 |
/** @var core_reportbuilder_generator $generator */
|
|
|
112 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
113 |
|
|
|
114 |
// Create Report1 and add some elements.
|
|
|
115 |
$report1 = $generator->create_report(['name' => 'My report 1', 'source' => users::class, 'default' => false,
|
|
|
116 |
'tags' => ['cat', 'dog']]);
|
|
|
117 |
$column1 = $generator->create_column(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
118 |
$filter1 = $generator->create_filter(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
119 |
$condition1 = $generator->create_condition(['reportid' => $report1->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
120 |
|
|
|
121 |
// Create Report2 and add some elements.
|
|
|
122 |
$report2 = $generator->create_report(['name' => 'My report 2', 'source' => users::class, 'default' => false]);
|
|
|
123 |
$column2 = $generator->create_column(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
124 |
$filter2 = $generator->create_filter(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
125 |
$condition2 = $generator->create_condition(['reportid' => $report2->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
126 |
|
|
|
127 |
// Delete Report1.
|
|
|
128 |
$result = report::delete_report($report1->get('id'));
|
|
|
129 |
$this->assertTrue($result);
|
|
|
130 |
|
|
|
131 |
// Make sure Report1, and all it's elements are deleted.
|
|
|
132 |
$this->assertFalse($report1::record_exists($report1->get('id')));
|
|
|
133 |
$this->assertFalse($column1::record_exists($column1->get('id')));
|
|
|
134 |
$this->assertFalse($filter1::record_exists($filter1->get('id')));
|
|
|
135 |
$this->assertFalse($condition1::record_exists($condition1->get('id')));
|
|
|
136 |
$this->assertEmpty(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report1->get('id')));
|
|
|
137 |
|
|
|
138 |
// Make sure Report2, and all it's elements still exist.
|
|
|
139 |
$this->assertTrue($report2::record_exists($report2->get('id')));
|
|
|
140 |
$this->assertTrue($column2::record_exists($column2->get('id')));
|
|
|
141 |
$this->assertTrue($filter2::record_exists($filter2->get('id')));
|
|
|
142 |
$this->assertTrue($condition2::record_exists($condition2->get('id')));
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Testing adding report column
|
|
|
147 |
*/
|
|
|
148 |
public function test_add_report_column(): void {
|
|
|
149 |
$this->resetAfterTest();
|
|
|
150 |
$this->setAdminUser();
|
|
|
151 |
|
|
|
152 |
/** @var core_reportbuilder_generator $generator */
|
|
|
153 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
154 |
|
|
|
155 |
$report = $generator->create_report([
|
|
|
156 |
'name' => 'My report',
|
|
|
157 |
'source' => users::class,
|
|
|
158 |
'default' => false,
|
|
|
159 |
]);
|
|
|
160 |
|
|
|
161 |
// Add first column.
|
|
|
162 |
$columnfullname = report::add_report_column($report->get('id'), 'user:fullname');
|
|
|
163 |
$this->assertTrue(column::record_exists($columnfullname->get('id')));
|
|
|
164 |
|
|
|
165 |
$this->assertEquals($report->get('id'), $columnfullname->get('reportid'));
|
|
|
166 |
$this->assertEquals('user:fullname', $columnfullname->get('uniqueidentifier'));
|
|
|
167 |
$this->assertEquals(1, $columnfullname->get('columnorder'));
|
|
|
168 |
$this->assertEquals(1, $columnfullname->get('sortorder'));
|
|
|
169 |
|
|
|
170 |
// Add second column.
|
|
|
171 |
$columnemail = report::add_report_column($report->get('id'), 'user:email');
|
|
|
172 |
$this->assertTrue(column::record_exists($columnemail->get('id')));
|
|
|
173 |
|
|
|
174 |
$this->assertEquals($report->get('id'), $columnemail->get('reportid'));
|
|
|
175 |
$this->assertEquals('user:email', $columnemail->get('uniqueidentifier'));
|
|
|
176 |
$this->assertEquals(2, $columnemail->get('columnorder'));
|
|
|
177 |
$this->assertEquals(2, $columnemail->get('sortorder'));
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Test adding invalid report column
|
|
|
182 |
*/
|
|
|
183 |
public function test_add_report_column_invalid(): void {
|
|
|
184 |
$this->resetAfterTest();
|
|
|
185 |
$this->setAdminUser();
|
|
|
186 |
|
|
|
187 |
/** @var core_reportbuilder_generator $generator */
|
|
|
188 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
189 |
|
|
|
190 |
$report = $generator->create_report([
|
|
|
191 |
'name' => 'My report',
|
|
|
192 |
'source' => users::class,
|
|
|
193 |
'default' => false,
|
|
|
194 |
]);
|
|
|
195 |
|
|
|
196 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
197 |
$this->expectExceptionMessage('Invalid column');
|
|
|
198 |
report::add_report_column($report->get('id'), 'user:invalid');
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Testing deleting report column
|
|
|
203 |
*/
|
|
|
204 |
public function test_delete_report_column(): void {
|
|
|
205 |
$this->resetAfterTest();
|
|
|
206 |
$this->setAdminUser();
|
|
|
207 |
|
|
|
208 |
/** @var core_reportbuilder_generator $generator */
|
|
|
209 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
210 |
|
|
|
211 |
$report = $generator->create_report([
|
|
|
212 |
'name' => 'My report',
|
|
|
213 |
'source' => users::class,
|
|
|
214 |
'default' => false,
|
|
|
215 |
]);
|
|
|
216 |
|
|
|
217 |
// Add two columns.
|
|
|
218 |
$columnfullname = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
|
|
|
219 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
220 |
|
|
|
221 |
// Delete the first column.
|
|
|
222 |
$result = report::delete_report_column($report->get('id'), $columnfullname->get('id'));
|
|
|
223 |
$this->assertTrue($result);
|
|
|
224 |
|
|
|
225 |
// Assert report columns.
|
|
|
226 |
$columns = column::get_records(['reportid' => $report->get('id')]);
|
|
|
227 |
$this->assertCount(1, $columns);
|
|
|
228 |
|
|
|
229 |
$column = reset($columns);
|
|
|
230 |
$this->assertEquals('user:email', $column->get('uniqueidentifier'));
|
|
|
231 |
$this->assertEquals(1, $column->get('columnorder'));
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Testing deleting invalid report column
|
|
|
236 |
*/
|
|
|
237 |
public function test_delete_report_column_invalid(): void {
|
|
|
238 |
$this->resetAfterTest();
|
|
|
239 |
$this->setAdminUser();
|
|
|
240 |
|
|
|
241 |
/** @var core_reportbuilder_generator $generator */
|
|
|
242 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
243 |
|
|
|
244 |
$report = $generator->create_report([
|
|
|
245 |
'name' => 'My report',
|
|
|
246 |
'source' => users::class,
|
|
|
247 |
'default' => false,
|
|
|
248 |
]);
|
|
|
249 |
|
|
|
250 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
251 |
$this->expectExceptionMessage('Invalid column');
|
|
|
252 |
report::delete_report_column($report->get('id'), 42);
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Testing re-ordering report column
|
|
|
257 |
*/
|
|
|
258 |
public function test_reorder_report_column(): void {
|
|
|
259 |
$this->resetAfterTest();
|
|
|
260 |
$this->setAdminUser();
|
|
|
261 |
|
|
|
262 |
/** @var core_reportbuilder_generator $generator */
|
|
|
263 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
264 |
|
|
|
265 |
$report = $generator->create_report([
|
|
|
266 |
'name' => 'My report',
|
|
|
267 |
'source' => users::class,
|
|
|
268 |
'default' => false,
|
|
|
269 |
]);
|
|
|
270 |
|
|
|
271 |
// Add four columns.
|
|
|
272 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
|
|
|
273 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
274 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
|
|
|
275 |
$columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
|
|
|
276 |
|
|
|
277 |
// Move the city column to second position.
|
|
|
278 |
$result = report::reorder_report_column($report->get('id'), $columncity->get('id'), 2);
|
|
|
279 |
$this->assertTrue($result);
|
|
|
280 |
|
|
|
281 |
// Assert report columns order.
|
|
|
282 |
$columns = column::get_records(['reportid' => $report->get('id')], 'columnorder');
|
|
|
283 |
|
|
|
284 |
$columnidentifiers = array_map(static function(column $column): string {
|
|
|
285 |
return $column->get('uniqueidentifier');
|
|
|
286 |
}, $columns);
|
|
|
287 |
|
|
|
288 |
$this->assertEquals([
|
|
|
289 |
'user:fullname',
|
|
|
290 |
'user:city',
|
|
|
291 |
'user:email',
|
|
|
292 |
'user:country',
|
|
|
293 |
], $columnidentifiers);
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
/**
|
|
|
297 |
* Testing re-ordering invalid report column
|
|
|
298 |
*/
|
|
|
299 |
public function test_reorder_report_column_invalid(): void {
|
|
|
300 |
$this->resetAfterTest();
|
|
|
301 |
$this->setAdminUser();
|
|
|
302 |
|
|
|
303 |
/** @var core_reportbuilder_generator $generator */
|
|
|
304 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
305 |
|
|
|
306 |
$report = $generator->create_report([
|
|
|
307 |
'name' => 'My report',
|
|
|
308 |
'source' => users::class,
|
|
|
309 |
'default' => false,
|
|
|
310 |
]);
|
|
|
311 |
|
|
|
312 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
313 |
$this->expectExceptionMessage('Invalid column');
|
|
|
314 |
report::reorder_report_column($report->get('id'), 42, 1);
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
/**
|
|
|
318 |
* Testing re-ordering report column sorting
|
|
|
319 |
*/
|
|
|
320 |
public function test_reorder_report_column_sorting(): void {
|
|
|
321 |
$this->resetAfterTest();
|
|
|
322 |
$this->setAdminUser();
|
|
|
323 |
|
|
|
324 |
/** @var core_reportbuilder_generator $generator */
|
|
|
325 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
326 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
327 |
|
|
|
328 |
// Add four columns.
|
|
|
329 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
|
|
|
330 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
331 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
|
|
|
332 |
$columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
|
|
|
333 |
|
|
|
334 |
// Move the city column to second position.
|
|
|
335 |
$result = report::reorder_report_column_sorting($report->get('id'), $columncity->get('id'), 2);
|
|
|
336 |
$this->assertTrue($result);
|
|
|
337 |
|
|
|
338 |
// Assert report columns order.
|
|
|
339 |
$columns = column::get_records(['reportid' => $report->get('id')], 'sortorder');
|
|
|
340 |
|
|
|
341 |
$columnidentifiers = array_map(static function(column $column): string {
|
|
|
342 |
return $column->get('uniqueidentifier');
|
|
|
343 |
}, $columns);
|
|
|
344 |
|
|
|
345 |
$this->assertEquals([
|
|
|
346 |
'user:fullname',
|
|
|
347 |
'user:city',
|
|
|
348 |
'user:email',
|
|
|
349 |
'user:country',
|
|
|
350 |
], $columnidentifiers);
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* Testing re-ordering invalid report column sorting
|
|
|
355 |
*/
|
|
|
356 |
public function test_reorder_report_column_sorting_invalid(): void {
|
|
|
357 |
$this->resetAfterTest();
|
|
|
358 |
$this->setAdminUser();
|
|
|
359 |
|
|
|
360 |
/** @var core_reportbuilder_generator $generator */
|
|
|
361 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
362 |
|
|
|
363 |
$report = $generator->create_report([
|
|
|
364 |
'name' => 'My report',
|
|
|
365 |
'source' => users::class,
|
|
|
366 |
'default' => false,
|
|
|
367 |
]);
|
|
|
368 |
|
|
|
369 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
370 |
$this->expectExceptionMessage('Invalid column');
|
|
|
371 |
report::reorder_report_column_sorting($report->get('id'), 42, 1);
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
/**
|
|
|
375 |
* Test toggling of report column sorting
|
|
|
376 |
*/
|
|
|
377 |
public function test_toggle_report_column_sorting(): void {
|
|
|
378 |
$this->resetAfterTest();
|
|
|
379 |
$this->setAdminUser();
|
|
|
380 |
|
|
|
381 |
/** @var core_reportbuilder_generator $generator */
|
|
|
382 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
383 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
|
|
|
384 |
$column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
385 |
|
|
|
386 |
// Toggle sort descending.
|
|
|
387 |
$result = report::toggle_report_column_sorting($report->get('id'), $column->get('id'), true, SORT_DESC);
|
|
|
388 |
$this->assertTrue($result);
|
|
|
389 |
|
|
|
390 |
// Confirm column was updated.
|
|
|
391 |
$columnupdated = new column($column->get('id'));
|
|
|
392 |
$this->assertTrue($columnupdated->get('sortenabled'));
|
|
|
393 |
$this->assertEquals(SORT_DESC, $columnupdated->get('sortdirection'));
|
|
|
394 |
}
|
|
|
395 |
|
|
|
396 |
/**
|
|
|
397 |
* Test toggling of report column sorting with invalid column
|
|
|
398 |
*/
|
|
|
399 |
public function test_toggle_report_column_sorting_invalid(): void {
|
|
|
400 |
$this->resetAfterTest();
|
|
|
401 |
$this->setAdminUser();
|
|
|
402 |
|
|
|
403 |
/** @var core_reportbuilder_generator $generator */
|
|
|
404 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
405 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
|
|
|
406 |
|
|
|
407 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
408 |
$this->expectExceptionMessage('Invalid column');
|
|
|
409 |
report::toggle_report_column_sorting($report->get('id'), 42, false);
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
/**
|
|
|
413 |
* Test adding report condition
|
|
|
414 |
*/
|
|
|
415 |
public function test_add_report_condition(): void {
|
|
|
416 |
$this->resetAfterTest();
|
|
|
417 |
$this->setAdminUser();
|
|
|
418 |
|
|
|
419 |
/** @var core_reportbuilder_generator $generator */
|
|
|
420 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
421 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
422 |
|
|
|
423 |
// Add first condition.
|
|
|
424 |
$conditionfullname = report::add_report_condition($report->get('id'), 'user:fullname');
|
|
|
425 |
$this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 1',
|
|
|
426 |
['id' => $conditionfullname->get('id')]));
|
|
|
427 |
|
|
|
428 |
$this->assertEquals($report->get('id'), $conditionfullname->get('reportid'));
|
|
|
429 |
$this->assertEquals('user:fullname', $conditionfullname->get('uniqueidentifier'));
|
|
|
430 |
$this->assertEquals(1, $conditionfullname->get('filterorder'));
|
|
|
431 |
|
|
|
432 |
// Add second condition.
|
|
|
433 |
$conditionemail = report::add_report_condition($report->get('id'), 'user:email');
|
|
|
434 |
$this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 1',
|
|
|
435 |
['id' => $conditionemail->get('id')]));
|
|
|
436 |
|
|
|
437 |
$this->assertEquals($report->get('id'), $conditionemail->get('reportid'));
|
|
|
438 |
$this->assertEquals('user:email', $conditionemail->get('uniqueidentifier'));
|
|
|
439 |
$this->assertEquals(2, $conditionemail->get('filterorder'));
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
/**
|
|
|
443 |
* Test adding invalid report condition
|
|
|
444 |
*/
|
|
|
445 |
public function test_add_report_condition_invalid(): void {
|
|
|
446 |
$this->resetAfterTest();
|
|
|
447 |
$this->setAdminUser();
|
|
|
448 |
|
|
|
449 |
/** @var core_reportbuilder_generator $generator */
|
|
|
450 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
451 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
452 |
|
|
|
453 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
454 |
$this->expectExceptionMessage('Invalid condition');
|
|
|
455 |
report::add_report_condition($report->get('id'), 'user:invalid');
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
/**
|
|
|
459 |
* Test adding duplicate report condition
|
|
|
460 |
*/
|
|
|
461 |
public function test_add_report_condition_duplicate(): void {
|
|
|
462 |
$this->resetAfterTest();
|
|
|
463 |
$this->setAdminUser();
|
|
|
464 |
|
|
|
465 |
/** @var core_reportbuilder_generator $generator */
|
|
|
466 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
467 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
468 |
|
|
|
469 |
// First one is fine.
|
|
|
470 |
report::add_report_condition($report->get('id'), 'user:email');
|
|
|
471 |
|
|
|
472 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
473 |
$this->expectExceptionMessage('Duplicate condition');
|
|
|
474 |
report::add_report_condition($report->get('id'), 'user:email');
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* Test deleting report condition
|
|
|
479 |
*/
|
|
|
480 |
public function test_delete_report_condition(): void {
|
|
|
481 |
$this->resetAfterTest();
|
|
|
482 |
$this->setAdminUser();
|
|
|
483 |
|
|
|
484 |
/** @var core_reportbuilder_generator $generator */
|
|
|
485 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
486 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
487 |
|
|
|
488 |
// Add two conditions.
|
|
|
489 |
$conditionfullname = $generator->create_condition([
|
|
|
490 |
'reportid' => $report->get('id'),
|
|
|
491 |
'uniqueidentifier' => 'user:fullname',
|
|
|
492 |
]);
|
|
|
493 |
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
494 |
|
|
|
495 |
// Delete the first condition.
|
|
|
496 |
$result = report::delete_report_condition($report->get('id'), $conditionfullname->get('id'));
|
|
|
497 |
$this->assertTrue($result);
|
|
|
498 |
|
|
|
499 |
// Assert report conditions.
|
|
|
500 |
$conditions = filter::get_condition_records($report->get('id'));
|
|
|
501 |
$this->assertCount(1, $conditions);
|
|
|
502 |
|
|
|
503 |
$condition = reset($conditions);
|
|
|
504 |
$this->assertEquals('user:email', $condition->get('uniqueidentifier'));
|
|
|
505 |
$this->assertEquals(1, $condition->get('filterorder'));
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
/**
|
|
|
509 |
* Test deleting invalid report condition
|
|
|
510 |
*/
|
|
|
511 |
public function test_delete_report_condition_invalid(): void {
|
|
|
512 |
$this->resetAfterTest();
|
|
|
513 |
$this->setAdminUser();
|
|
|
514 |
|
|
|
515 |
/** @var core_reportbuilder_generator $generator */
|
|
|
516 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
517 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
518 |
|
|
|
519 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
520 |
$this->expectExceptionMessage('Invalid condition');
|
|
|
521 |
report::delete_report_condition($report->get('id'), 42);
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
/**
|
|
|
525 |
* Test re-ordering report condition
|
|
|
526 |
*/
|
|
|
527 |
public function test_reorder_report_condition(): void {
|
|
|
528 |
$this->resetAfterTest();
|
|
|
529 |
$this->setAdminUser();
|
|
|
530 |
|
|
|
531 |
/** @var core_reportbuilder_generator $generator */
|
|
|
532 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
533 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
534 |
|
|
|
535 |
// Add four conditions.
|
|
|
536 |
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
|
|
|
537 |
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
538 |
$generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
|
|
|
539 |
$conditioncity = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
|
|
|
540 |
|
|
|
541 |
// Move the city condition to second position.
|
|
|
542 |
$result = report::reorder_report_condition($report->get('id'), $conditioncity->get('id'), 2);
|
|
|
543 |
$this->assertTrue($result);
|
|
|
544 |
|
|
|
545 |
// Assert report conditions order.
|
|
|
546 |
$conditions = filter::get_condition_records($report->get('id'), 'filterorder');
|
|
|
547 |
|
|
|
548 |
$conditionidentifiers = array_map(static function(filter $condition): string {
|
|
|
549 |
return $condition->get('uniqueidentifier');
|
|
|
550 |
}, $conditions);
|
|
|
551 |
|
|
|
552 |
$this->assertEquals([
|
|
|
553 |
'user:fullname',
|
|
|
554 |
'user:city',
|
|
|
555 |
'user:email',
|
|
|
556 |
'user:country',
|
|
|
557 |
], $conditionidentifiers);
|
|
|
558 |
}
|
|
|
559 |
|
|
|
560 |
/**
|
|
|
561 |
* Test re-ordering invalid report condition
|
|
|
562 |
*/
|
|
|
563 |
public function test_reorder_report_condition_invalid(): void {
|
|
|
564 |
$this->resetAfterTest();
|
|
|
565 |
$this->setAdminUser();
|
|
|
566 |
|
|
|
567 |
/** @var core_reportbuilder_generator $generator */
|
|
|
568 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
569 |
|
|
|
570 |
$report = $generator->create_report([
|
|
|
571 |
'name' => 'My report',
|
|
|
572 |
'source' => users::class,
|
|
|
573 |
'default' => false,
|
|
|
574 |
]);
|
|
|
575 |
|
|
|
576 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
577 |
$this->expectExceptionMessage('Invalid condition');
|
|
|
578 |
report::reorder_report_condition($report->get('id'), 42, 1);
|
|
|
579 |
}
|
|
|
580 |
|
|
|
581 |
/**
|
|
|
582 |
* Test adding report filter
|
|
|
583 |
*/
|
|
|
584 |
public function test_add_report_filter(): void {
|
|
|
585 |
$this->resetAfterTest();
|
|
|
586 |
$this->setAdminUser();
|
|
|
587 |
|
|
|
588 |
/** @var core_reportbuilder_generator $generator */
|
|
|
589 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
590 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
591 |
|
|
|
592 |
// Add first filter.
|
|
|
593 |
$filterfullname = report::add_report_filter($report->get('id'), 'user:fullname');
|
|
|
594 |
$this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0',
|
|
|
595 |
['id' => $filterfullname->get('id')]));
|
|
|
596 |
|
|
|
597 |
$this->assertEquals($report->get('id'), $filterfullname->get('reportid'));
|
|
|
598 |
$this->assertEquals('user:fullname', $filterfullname->get('uniqueidentifier'));
|
|
|
599 |
$this->assertEquals(1, $filterfullname->get('filterorder'));
|
|
|
600 |
|
|
|
601 |
// Add second filter.
|
|
|
602 |
$filteremail = report::add_report_filter($report->get('id'), 'user:email');
|
|
|
603 |
$this->assertTrue(filter::record_exists_select('id = :id AND iscondition = 0',
|
|
|
604 |
['id' => $filteremail->get('id')]));
|
|
|
605 |
|
|
|
606 |
$this->assertEquals($report->get('id'), $filteremail->get('reportid'));
|
|
|
607 |
$this->assertEquals('user:email', $filteremail->get('uniqueidentifier'));
|
|
|
608 |
$this->assertEquals(2, $filteremail->get('filterorder'));
|
|
|
609 |
}
|
|
|
610 |
|
|
|
611 |
/**
|
|
|
612 |
* Test adding invalid report filter
|
|
|
613 |
*/
|
|
|
614 |
public function test_add_report_filter_invalid(): void {
|
|
|
615 |
$this->resetAfterTest();
|
|
|
616 |
$this->setAdminUser();
|
|
|
617 |
|
|
|
618 |
/** @var core_reportbuilder_generator $generator */
|
|
|
619 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
620 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
621 |
|
|
|
622 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
623 |
$this->expectExceptionMessage('Invalid filter');
|
|
|
624 |
report::add_report_filter($report->get('id'), 'user:invalid');
|
|
|
625 |
}
|
|
|
626 |
|
|
|
627 |
/**
|
|
|
628 |
* Test adding duplicate report filter
|
|
|
629 |
*/
|
|
|
630 |
public function test_add_report_filter_duplicate(): void {
|
|
|
631 |
$this->resetAfterTest();
|
|
|
632 |
$this->setAdminUser();
|
|
|
633 |
|
|
|
634 |
/** @var core_reportbuilder_generator $generator */
|
|
|
635 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
636 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
637 |
|
|
|
638 |
// First one is fine.
|
|
|
639 |
report::add_report_filter($report->get('id'), 'user:email');
|
|
|
640 |
|
|
|
641 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
642 |
$this->expectExceptionMessage('Duplicate filter');
|
|
|
643 |
report::add_report_filter($report->get('id'), 'user:email');
|
|
|
644 |
}
|
|
|
645 |
|
|
|
646 |
/**
|
|
|
647 |
* Test deleting report filter
|
|
|
648 |
*/
|
|
|
649 |
public function test_delete_report_filter(): void {
|
|
|
650 |
$this->resetAfterTest();
|
|
|
651 |
$this->setAdminUser();
|
|
|
652 |
|
|
|
653 |
/** @var core_reportbuilder_generator $generator */
|
|
|
654 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
655 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
656 |
|
|
|
657 |
// Add two filters.
|
|
|
658 |
$filterfullname = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
|
|
|
659 |
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
660 |
|
|
|
661 |
// Delete the first filter.
|
|
|
662 |
$result = report::delete_report_filter($report->get('id'), $filterfullname->get('id'));
|
|
|
663 |
$this->assertTrue($result);
|
|
|
664 |
|
|
|
665 |
// Assert report filters.
|
|
|
666 |
$filters = filter::get_filter_records($report->get('id'));
|
|
|
667 |
$this->assertCount(1, $filters);
|
|
|
668 |
|
|
|
669 |
$filter = reset($filters);
|
|
|
670 |
$this->assertEquals('user:email', $filter->get('uniqueidentifier'));
|
|
|
671 |
$this->assertEquals(1, $filter->get('filterorder'));
|
|
|
672 |
}
|
|
|
673 |
|
|
|
674 |
/**
|
|
|
675 |
* Test deleting invalid report filter
|
|
|
676 |
*/
|
|
|
677 |
public function test_delete_report_filter_invalid(): void {
|
|
|
678 |
$this->resetAfterTest();
|
|
|
679 |
$this->setAdminUser();
|
|
|
680 |
|
|
|
681 |
/** @var core_reportbuilder_generator $generator */
|
|
|
682 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
683 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
684 |
|
|
|
685 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
686 |
$this->expectExceptionMessage('Invalid filter');
|
|
|
687 |
report::delete_report_filter($report->get('id'), 42);
|
|
|
688 |
}
|
|
|
689 |
|
|
|
690 |
/**
|
|
|
691 |
* Test re-ordering report filter
|
|
|
692 |
*/
|
|
|
693 |
public function test_reorder_report_filter(): void {
|
|
|
694 |
$this->resetAfterTest();
|
|
|
695 |
$this->setAdminUser();
|
|
|
696 |
|
|
|
697 |
/** @var core_reportbuilder_generator $generator */
|
|
|
698 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
699 |
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
|
|
|
700 |
|
|
|
701 |
// Add four filters.
|
|
|
702 |
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
|
|
|
703 |
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
|
|
|
704 |
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
|
|
|
705 |
$filtercity = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
|
|
|
706 |
|
|
|
707 |
// Move the city filter to second position.
|
|
|
708 |
$result = report::reorder_report_filter($report->get('id'), $filtercity->get('id'), 2);
|
|
|
709 |
$this->assertTrue($result);
|
|
|
710 |
|
|
|
711 |
// Assert report filters order.
|
|
|
712 |
$filters = filter::get_filter_records($report->get('id'), 'filterorder');
|
|
|
713 |
|
|
|
714 |
$filteridentifiers = array_map(static function(filter $filter): string {
|
|
|
715 |
return $filter->get('uniqueidentifier');
|
|
|
716 |
}, $filters);
|
|
|
717 |
|
|
|
718 |
$this->assertEquals([
|
|
|
719 |
'user:fullname',
|
|
|
720 |
'user:city',
|
|
|
721 |
'user:email',
|
|
|
722 |
'user:country',
|
|
|
723 |
], $filteridentifiers);
|
|
|
724 |
}
|
|
|
725 |
|
|
|
726 |
/**
|
|
|
727 |
* Test re-ordering invalid report filter
|
|
|
728 |
*/
|
|
|
729 |
public function test_reorder_report_filter_invalid(): void {
|
|
|
730 |
$this->resetAfterTest();
|
|
|
731 |
$this->setAdminUser();
|
|
|
732 |
|
|
|
733 |
/** @var core_reportbuilder_generator $generator */
|
|
|
734 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
735 |
|
|
|
736 |
$report = $generator->create_report([
|
|
|
737 |
'name' => 'My report',
|
|
|
738 |
'source' => users::class,
|
|
|
739 |
'default' => false,
|
|
|
740 |
]);
|
|
|
741 |
|
|
|
742 |
$this->expectException(invalid_parameter_exception::class);
|
|
|
743 |
$this->expectExceptionMessage('Invalid filter');
|
|
|
744 |
report::reorder_report_filter($report->get('id'), 42, 1);
|
|
|
745 |
}
|
|
|
746 |
}
|