Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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