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 |
/**
|
|
|
18 |
* Unit tests for base datasource
|
|
|
19 |
*
|
|
|
20 |
* @package core_reportbuilder
|
|
|
21 |
* @copyright 2023 Paul Holden <paulh@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
declare(strict_types=1);
|
|
|
26 |
|
|
|
27 |
namespace core_reportbuilder;
|
|
|
28 |
|
|
|
29 |
use advanced_testcase;
|
|
|
30 |
use core_reportbuilder_generator;
|
|
|
31 |
use core_reportbuilder\local\entities\base;
|
|
|
32 |
use core_reportbuilder\local\filters\text;
|
|
|
33 |
use core_reportbuilder\local\report\{column, filter};
|
|
|
34 |
use lang_string;
|
|
|
35 |
use ReflectionClass;
|
|
|
36 |
|
|
|
37 |
defined('MOODLE_INTERNAL') || die();
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Unit tests for base datasource
|
|
|
41 |
*
|
|
|
42 |
* @package core_reportbuilder
|
|
|
43 |
* @coversDefaultClass \core_reportbuilder\datasource
|
|
|
44 |
* @copyright 2023 Paul Holden <paulh@moodle.com>
|
|
|
45 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
46 |
*/
|
|
|
47 |
final class datasource_test extends advanced_testcase {
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Data provider for {@see test_add_columns_from_entity}
|
|
|
51 |
*
|
|
|
52 |
* @return array[]
|
|
|
53 |
*/
|
|
|
54 |
public static function add_columns_from_entity_provider(): array {
|
|
|
55 |
return [
|
|
|
56 |
'All columns' => [
|
|
|
57 |
[],
|
|
|
58 |
[],
|
|
|
59 |
4,
|
|
|
60 |
],
|
|
|
61 |
'Include columns (first, extra1, extra2)' => [
|
|
|
62 |
['first', 'extra*'],
|
|
|
63 |
[],
|
|
|
64 |
3,
|
|
|
65 |
],
|
|
|
66 |
'Exclude columns (first, extra1, extra2)' => [
|
|
|
67 |
[],
|
|
|
68 |
['first', 'extra*'],
|
|
|
69 |
1,
|
|
|
70 |
],
|
|
|
71 |
];
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Test adding columns from entity
|
|
|
76 |
*
|
|
|
77 |
* @param string[] $include
|
|
|
78 |
* @param string[] $exclude
|
|
|
79 |
* @param int $expectedcount
|
|
|
80 |
*
|
|
|
81 |
* @covers ::add_columns_from_entity
|
|
|
82 |
*
|
|
|
83 |
* @dataProvider add_columns_from_entity_provider
|
|
|
84 |
*/
|
|
|
85 |
public function test_add_columns_from_entity(
|
|
|
86 |
array $include,
|
|
|
87 |
array $exclude,
|
|
|
88 |
int $expectedcount,
|
|
|
89 |
): void {
|
|
|
90 |
$instance = $this->get_datasource_test_source();
|
|
|
91 |
|
|
|
92 |
$method = (new ReflectionClass($instance))->getMethod('add_columns_from_entity');
|
|
|
93 |
$method->invoke($instance, 'datasource_test_entity', $include, $exclude);
|
|
|
94 |
|
|
|
95 |
// Get all our entity columns.
|
|
|
96 |
$columns = array_filter(
|
|
|
97 |
$instance->get_columns(),
|
|
|
98 |
fn(string $columnname) => strpos($columnname, 'datasource_test_entity:') === 0,
|
|
|
99 |
ARRAY_FILTER_USE_KEY,
|
|
|
100 |
);
|
|
|
101 |
|
|
|
102 |
$this->assertCount($expectedcount, $columns);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Data provider for {@see test_add_filters_from_entity}
|
|
|
107 |
*
|
|
|
108 |
* @return array[]
|
|
|
109 |
*/
|
|
|
110 |
public static function add_filters_from_entity_provider(): array {
|
|
|
111 |
return [
|
|
|
112 |
'All filters' => [
|
|
|
113 |
[],
|
|
|
114 |
[],
|
|
|
115 |
4,
|
|
|
116 |
],
|
|
|
117 |
'Include filters (first, extra1, extra2)' => [
|
|
|
118 |
['first', 'extra*'],
|
|
|
119 |
[],
|
|
|
120 |
3,
|
|
|
121 |
],
|
|
|
122 |
'Exclude filters (first, extra1, extra2)' => [
|
|
|
123 |
[],
|
|
|
124 |
['first', 'extra*'],
|
|
|
125 |
1,
|
|
|
126 |
],
|
|
|
127 |
];
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Test adding filters from entity
|
|
|
132 |
*
|
|
|
133 |
* @param string[] $include
|
|
|
134 |
* @param string[] $exclude
|
|
|
135 |
* @param int $expectedcount
|
|
|
136 |
*
|
|
|
137 |
* @covers ::add_filters_from_entity
|
|
|
138 |
*
|
|
|
139 |
* @dataProvider add_filters_from_entity_provider
|
|
|
140 |
*/
|
|
|
141 |
public function test_add_filters_from_entity(
|
|
|
142 |
array $include,
|
|
|
143 |
array $exclude,
|
|
|
144 |
int $expectedcount,
|
|
|
145 |
): void {
|
|
|
146 |
$instance = $this->get_datasource_test_source();
|
|
|
147 |
|
|
|
148 |
$method = (new ReflectionClass($instance))->getMethod('add_filters_from_entity');
|
|
|
149 |
$method->invoke($instance, 'datasource_test_entity', $include, $exclude);
|
|
|
150 |
|
|
|
151 |
// Get all our entity filters.
|
|
|
152 |
$filters = array_filter(
|
|
|
153 |
$instance->get_filters(),
|
|
|
154 |
fn(string $filtername) => strpos($filtername, 'datasource_test_entity:') === 0,
|
|
|
155 |
ARRAY_FILTER_USE_KEY,
|
|
|
156 |
);
|
|
|
157 |
|
|
|
158 |
$this->assertCount($expectedcount, $filters);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Data provider for {@see test_add_conditions_from_entity}
|
|
|
163 |
*
|
|
|
164 |
* @return array[]
|
|
|
165 |
*/
|
|
|
166 |
public static function add_conditions_from_entity_provider(): array {
|
|
|
167 |
return [
|
|
|
168 |
'All conditions' => [
|
|
|
169 |
[],
|
|
|
170 |
[],
|
|
|
171 |
4,
|
|
|
172 |
],
|
|
|
173 |
'Include conditions (first, extra1, extra2)' => [
|
|
|
174 |
['first', 'extra*'],
|
|
|
175 |
[],
|
|
|
176 |
3,
|
|
|
177 |
],
|
|
|
178 |
'Exclude conditions (first, extra1, extra2)' => [
|
|
|
179 |
[],
|
|
|
180 |
['first', 'extra*'],
|
|
|
181 |
1,
|
|
|
182 |
],
|
|
|
183 |
];
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Test adding conditions from entity
|
|
|
188 |
*
|
|
|
189 |
* @param string[] $include
|
|
|
190 |
* @param string[] $exclude
|
|
|
191 |
* @param int $expectedcount
|
|
|
192 |
*
|
|
|
193 |
* @covers ::add_conditions_from_entity
|
|
|
194 |
*
|
|
|
195 |
* @dataProvider add_conditions_from_entity_provider
|
|
|
196 |
*/
|
|
|
197 |
public function test_add_conditions_from_entity(
|
|
|
198 |
array $include,
|
|
|
199 |
array $exclude,
|
|
|
200 |
int $expectedcount,
|
|
|
201 |
): void {
|
|
|
202 |
$instance = $this->get_datasource_test_source();
|
|
|
203 |
|
|
|
204 |
$method = (new ReflectionClass($instance))->getMethod('add_conditions_from_entity');
|
|
|
205 |
$method->invoke($instance, 'datasource_test_entity', $include, $exclude);
|
|
|
206 |
|
|
|
207 |
// Get all our entity conditions.
|
|
|
208 |
$conditions = array_filter(
|
|
|
209 |
$instance->get_conditions(),
|
|
|
210 |
fn(string $conditionname) => strpos($conditionname, 'datasource_test_entity:') === 0,
|
|
|
211 |
ARRAY_FILTER_USE_KEY,
|
|
|
212 |
);
|
|
|
213 |
|
|
|
214 |
$this->assertCount($expectedcount, $conditions);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Test adding all from entity
|
|
|
219 |
*
|
|
|
220 |
* @covers ::add_all_from_entity
|
|
|
221 |
*/
|
|
|
222 |
public function test_add_all_from_entity(): void {
|
|
|
223 |
$instance = $this->get_datasource_test_source();
|
|
|
224 |
|
|
|
225 |
$method = (new ReflectionClass($instance))->getMethod('add_all_from_entity');
|
|
|
226 |
$method->invoke($instance, 'datasource_test_entity', ['first'], ['second'], ['extra1']);
|
|
|
227 |
|
|
|
228 |
// Assert the column we added (plus one we didn't).
|
|
|
229 |
$this->assertInstanceOf(column::class, $instance->get_column('datasource_test_entity:first'));
|
|
|
230 |
$this->assertNull($instance->get_column('datasource_test_entity:second'));
|
|
|
231 |
|
|
|
232 |
// Assert the filter we added (plus one we didn't).
|
|
|
233 |
$this->assertInstanceOf(filter::class, $instance->get_filter('datasource_test_entity:second'));
|
|
|
234 |
$this->assertNull($instance->get_filter('datasource_test_entity:first'));
|
|
|
235 |
|
|
|
236 |
// Assert the condition we added (plus one we didn't).
|
|
|
237 |
$this->assertInstanceOf(filter::class, $instance->get_condition('datasource_test_entity:extra1'));
|
|
|
238 |
$this->assertNull($instance->get_condition('datasource_test_entity:extra2'));
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Create and return our test datasource instance
|
|
|
243 |
*
|
|
|
244 |
* @return datasource_test_source
|
|
|
245 |
*/
|
|
|
246 |
protected function get_datasource_test_source(): datasource_test_source {
|
|
|
247 |
$this->resetAfterTest();
|
|
|
248 |
|
|
|
249 |
/** @var core_reportbuilder_generator $generator */
|
|
|
250 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
251 |
$report = $generator->create_report(['name' => 'Test', 'source' => datasource_test_source::class, 'default' => 0]);
|
|
|
252 |
|
|
|
253 |
/** @var datasource_test_source $instance */
|
|
|
254 |
$instance = manager::get_report_from_persistent($report);
|
|
|
255 |
return $instance;
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* Simple implementation of the base datasource
|
|
|
261 |
*/
|
|
|
262 |
class datasource_test_source extends datasource {
|
|
|
263 |
|
|
|
264 |
protected function initialise(): void {
|
|
|
265 |
$this->set_main_table('user', 'u');
|
|
|
266 |
$this->annotate_entity('dummy', new lang_string('yes'));
|
|
|
267 |
$this->add_column(new column('test', null, 'dummy'));
|
|
|
268 |
|
|
|
269 |
// This is the entity from which we'll add our report elements.
|
|
|
270 |
$this->add_entity(new datasource_test_entity());
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
public static function get_name(): string {
|
|
|
274 |
return self::class;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
public function get_default_columns(): array {
|
|
|
278 |
return [];
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
public function get_default_filters(): array {
|
|
|
282 |
return [];
|
|
|
283 |
}
|
|
|
284 |
|
|
|
285 |
public function get_default_conditions(): array {
|
|
|
286 |
return [];
|
|
|
287 |
}
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
/**
|
|
|
291 |
* Simple implementation of the base entity
|
|
|
292 |
*/
|
|
|
293 |
class datasource_test_entity extends base {
|
|
|
294 |
|
|
|
295 |
protected function get_default_tables(): array {
|
|
|
296 |
return ['course'];
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
protected function get_default_entity_title(): lang_string {
|
|
|
300 |
return new lang_string('course');
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* We're going to add multiple columns/filters/conditions, each named as following:
|
|
|
305 |
*
|
|
|
306 |
* [first, second, extra1, extra2]
|
|
|
307 |
*
|
|
|
308 |
* @return base
|
|
|
309 |
*/
|
|
|
310 |
public function initialise(): base {
|
|
|
311 |
foreach (['first', 'second', 'extra1', 'extra2'] as $field) {
|
|
|
312 |
$name = new lang_string('customfieldcolumn', 'core_reportbuilder', $field);
|
|
|
313 |
|
|
|
314 |
$this->add_column(new column($field, $name, $this->get_entity_name()));
|
|
|
315 |
$this->add_filter(new filter(text::class, $field, $name, $this->get_entity_name()));
|
|
|
316 |
$this->add_condition(new filter(text::class, $field, $name, $this->get_entity_name()));
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
return $this;
|
|
|
320 |
}
|
|
|
321 |
}
|