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\report;
|
|
|
20 |
|
|
|
21 |
use advanced_testcase;
|
|
|
22 |
use coding_exception;
|
|
|
23 |
use context_system;
|
|
|
24 |
use core_reportbuilder\local\helpers\database;
|
|
|
25 |
use core_reportbuilder\system_report_available;
|
|
|
26 |
use core_reportbuilder\system_report_factory;
|
|
|
27 |
use lang_string;
|
|
|
28 |
use ReflectionClass;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Unit tests for report base class
|
|
|
32 |
*
|
|
|
33 |
* @package core_reportbuilder
|
|
|
34 |
* @covers \core_reportbuilder\local\report\base
|
|
|
35 |
* @copyright 2021 David Matamoros <davidmc@moodle.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class base_test extends advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Load required class
|
|
|
42 |
*/
|
|
|
43 |
public static function setUpBeforeClass(): void {
|
|
|
44 |
global $CFG;
|
|
|
45 |
require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Test for add_base_condition_simple
|
|
|
50 |
*/
|
|
|
51 |
public function test_add_base_condition_simple(): void {
|
|
|
52 |
$this->resetAfterTest();
|
|
|
53 |
|
|
|
54 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
55 |
$systemreport->add_base_condition_simple('username', 'admin');
|
|
|
56 |
[$where, $params] = $systemreport->get_base_condition();
|
|
|
57 |
$this->assertStringMatchesFormat('username = :%a', $where);
|
|
|
58 |
$this->assertEqualsCanonicalizing(['admin'], $params);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Test for add_base_condition_simple null
|
|
|
63 |
*/
|
|
|
64 |
public function test_add_base_condition_simple_null(): void {
|
|
|
65 |
$this->resetAfterTest();
|
|
|
66 |
|
|
|
67 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
68 |
$systemreport->add_base_condition_simple('username', null);
|
|
|
69 |
[$where, $params] = $systemreport->get_base_condition();
|
|
|
70 |
$this->assertEquals('username IS NULL', $where);
|
|
|
71 |
$this->assertEmpty($params);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Test for adding SQL base condition to a report
|
|
|
76 |
*/
|
|
|
77 |
public function test_add_base_condition_sql(): void {
|
|
|
78 |
$this->resetAfterTest();
|
|
|
79 |
|
|
|
80 |
$parameter = database::generate_param_name();
|
|
|
81 |
|
|
|
82 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
83 |
$systemreport->add_base_condition_sql("username = :{$parameter}", [$parameter => 'admin']);
|
|
|
84 |
|
|
|
85 |
[$where, $params] = $systemreport->get_base_condition();
|
|
|
86 |
$this->assertEquals("username = :{$parameter}", $where);
|
|
|
87 |
$this->assertEquals([$parameter => 'admin'], $params);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Test for adding multiple SQL base condition to a report
|
|
|
92 |
*/
|
|
|
93 |
public function test_add_base_condition_sql_multiple(): void {
|
|
|
94 |
$this->resetAfterTest();
|
|
|
95 |
|
|
|
96 |
[$paramusername, $paramemail] = database::generate_param_names(2);
|
|
|
97 |
|
|
|
98 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
99 |
$systemreport->add_base_condition_sql("username = :{$paramusername}", [$paramusername => 'admin']);
|
|
|
100 |
$systemreport->add_base_condition_sql("email = :{$paramemail}", [$paramemail => 'admin@example.com']);
|
|
|
101 |
|
|
|
102 |
[$where, $params] = $systemreport->get_base_condition();
|
|
|
103 |
$this->assertEquals("username = :{$paramusername} AND email = :{$paramemail}", $where);
|
|
|
104 |
$this->assertEquals([$paramusername => 'admin', $paramemail => 'admin@example.com'], $params);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Test for adding empty SQL base condition to a report
|
|
|
109 |
*/
|
|
|
110 |
public function test_add_base_condition_sql_empty_clause(): void {
|
|
|
111 |
$this->resetAfterTest();
|
|
|
112 |
|
|
|
113 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
114 |
$systemreport->add_base_condition_sql('username IS NOT NULL');
|
|
|
115 |
$systemreport->add_base_condition_sql('');
|
|
|
116 |
|
|
|
117 |
[$where, $params] = $systemreport->get_base_condition();
|
|
|
118 |
$this->assertEquals("username IS NOT NULL", $where);
|
|
|
119 |
$this->assertEmpty($params);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Test for adding SQL base condition to a report with invalid parameter
|
|
|
124 |
*/
|
|
|
125 |
public function test_add_base_condition_sql_invalid_parameter(): void {
|
|
|
126 |
$this->resetAfterTest();
|
|
|
127 |
|
|
|
128 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
129 |
|
|
|
130 |
$this->expectException(coding_exception::class);
|
|
|
131 |
$this->expectExceptionMessage('Invalid parameter names');
|
|
|
132 |
$systemreport->add_base_condition_sql("username = :param", ['param' => 'admin']);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Test getting report base conditions, where none have been set
|
|
|
137 |
*/
|
|
|
138 |
public function test_get_base_condition_default(): void {
|
|
|
139 |
$this->resetAfterTest();
|
|
|
140 |
|
|
|
141 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
142 |
|
|
|
143 |
[$where, $params] = $systemreport->get_base_condition();
|
|
|
144 |
$this->assertEmpty($where);
|
|
|
145 |
$this->assertEmpty($params);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Test for get_filter_instances
|
|
|
150 |
*/
|
|
|
151 |
public function test_get_filter_instances(): void {
|
|
|
152 |
$this->resetAfterTest();
|
|
|
153 |
|
|
|
154 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance(),
|
|
|
155 |
'', '', 0, ['withfilters' => true]);
|
|
|
156 |
$filters = $systemreport->get_filter_instances();
|
|
|
157 |
$this->assertCount(1, $filters);
|
|
|
158 |
$this->assertInstanceOf(\core_reportbuilder\local\filters\text::class, reset($filters));
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Test for set_downloadable
|
|
|
163 |
*/
|
|
|
164 |
public function test_set_downloadable(): void {
|
|
|
165 |
$this->resetAfterTest();
|
|
|
166 |
|
|
|
167 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
168 |
$systemreport->set_downloadable(true, 'testfilename');
|
|
|
169 |
$this->assertTrue($systemreport->is_downloadable());
|
|
|
170 |
$this->assertEquals('testfilename', $systemreport->get_downloadfilename());
|
|
|
171 |
|
|
|
172 |
$systemreport->set_downloadable(false, 'anothertestfilename');
|
|
|
173 |
$this->assertFalse($systemreport->is_downloadable());
|
|
|
174 |
$this->assertEquals('anothertestfilename', $systemreport->get_downloadfilename());
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Test for get_context
|
|
|
179 |
*/
|
|
|
180 |
public function test_get_context(): void {
|
|
|
181 |
$this->resetAfterTest();
|
|
|
182 |
|
|
|
183 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
184 |
$this->assertEquals(context_system::instance(), $systemreport->get_context());
|
|
|
185 |
|
|
|
186 |
$course = $this->getDataGenerator()->create_course();
|
|
|
187 |
$contextcourse = \context_course::instance($course->id);
|
|
|
188 |
$systemreport2 = system_report_factory::create(system_report_available::class, $contextcourse);
|
|
|
189 |
$this->assertEquals($contextcourse, $systemreport2->get_context());
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* Test entity annotation
|
|
|
194 |
*/
|
|
|
195 |
public function test_annotate_entity(): void {
|
|
|
196 |
$this->resetAfterTest();
|
|
|
197 |
|
|
|
198 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
199 |
|
|
|
200 |
$method = (new ReflectionClass($systemreport))->getMethod('annotate_entity');
|
|
|
201 |
|
|
|
202 |
$method->invoke($systemreport, 'test', new lang_string('yes'));
|
|
|
203 |
$this->assertEquals(new lang_string('yes'), $systemreport->get_entity_title('test'));
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Test entity annotation for invalid entity name
|
|
|
208 |
*/
|
|
|
209 |
public function test_annotate_entity_invalid(): void {
|
|
|
210 |
$this->resetAfterTest();
|
|
|
211 |
|
|
|
212 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
213 |
|
|
|
214 |
$method = (new ReflectionClass($systemreport))->getMethod('annotate_entity');
|
|
|
215 |
|
|
|
216 |
$this->expectException(coding_exception::class);
|
|
|
217 |
$this->expectExceptionMessage('Entity name must be comprised of alphanumeric character, underscore or dash');
|
|
|
218 |
$method->invoke($systemreport, '', new lang_string('yes'));
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Test entity annotation for duplicated entity name
|
|
|
223 |
*/
|
|
|
224 |
public function test_annotate_entity_duplicate(): void {
|
|
|
225 |
$this->resetAfterTest();
|
|
|
226 |
|
|
|
227 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
228 |
|
|
|
229 |
$method = (new ReflectionClass($systemreport))->getMethod('annotate_entity');
|
|
|
230 |
|
|
|
231 |
$method->invoke($systemreport, 'test', new lang_string('yes'));
|
|
|
232 |
|
|
|
233 |
// Adding a second time with the same name should trigger exception.
|
|
|
234 |
$this->expectException(coding_exception::class);
|
|
|
235 |
$this->expectExceptionMessage('Duplicate entity name (test)');
|
|
|
236 |
$method->invoke($systemreport, 'test', new lang_string('no'));
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Test for get_column
|
|
|
241 |
*/
|
|
|
242 |
public function test_get_column(): void {
|
|
|
243 |
$this->resetAfterTest();
|
|
|
244 |
|
|
|
245 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
246 |
$column = $systemreport->get_column('user:username');
|
|
|
247 |
$this->assertInstanceOf(column::class, $column);
|
|
|
248 |
|
|
|
249 |
$column = $systemreport->get_column('user:nonexistingcolumn');
|
|
|
250 |
$this->assertNull($column);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* Test for get_filter
|
|
|
255 |
*/
|
|
|
256 |
public function test_get_filter(): void {
|
|
|
257 |
$this->resetAfterTest();
|
|
|
258 |
|
|
|
259 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance(),
|
|
|
260 |
'', '', 0, ['withfilters' => true]);
|
|
|
261 |
$filter = $systemreport->get_filter('user:username');
|
|
|
262 |
$this->assertInstanceOf(filter::class, $filter);
|
|
|
263 |
|
|
|
264 |
$filter = $systemreport->get_filter('user:nonexistingfilter');
|
|
|
265 |
$this->assertNull($filter);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* Test for get_report_persistent
|
|
|
270 |
*/
|
|
|
271 |
public function test_get_report_persistent(): void {
|
|
|
272 |
$this->resetAfterTest();
|
|
|
273 |
|
|
|
274 |
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
|
|
275 |
$persistent = $systemreport->get_report_persistent();
|
|
|
276 |
$this->assertEquals(system_report_available::class, $persistent->get('source'));
|
|
|
277 |
}
|
|
|
278 |
}
|