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
/**
18
 * Unit tests for base entity
19
 *
20
 * @package     core_reportbuilder
21
 * @copyright   2021 David Matamoros <davidmc@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\local\entities;
28
 
29
use advanced_testcase;
30
use coding_exception;
31
use lang_string;
32
use core_reportbuilder\local\filters\text;
33
use core_reportbuilder\local\report\column;
34
use core_reportbuilder\local\report\filter;
35
 
36
defined('MOODLE_INTERNAL') || die();
37
 
38
/**
39
 * Unit tests for base entity
40
 *
41
 * @package     core_reportbuilder
42
 * @covers      \core_reportbuilder\local\entities\base
43
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
44
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 */
1441 ariadna 46
final class base_test extends advanced_testcase {
1 efrain 47
 
48
    /**
49
     * Test entity table alias
50
     */
51
    public function test_get_table_alias(): void {
52
        $entity = new base_test_entity();
53
 
54
        $mytablealias = $entity->get_table_alias('mytable');
55
        $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $mytablealias);
56
 
57
        $myothertablealias = $entity->get_table_alias('myothertable');
58
        $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $myothertablealias);
59
 
60
        // They must differ.
61
        $this->assertNotEquals($mytablealias, $myothertablealias);
62
 
63
        // Re-request both, ensure they are identical to what we previously received.
64
        $this->assertEquals($mytablealias, $entity->get_table_alias('mytable'));
65
        $this->assertEquals($myothertablealias, $entity->get_table_alias('myothertable'));
66
    }
67
 
68
    /**
1441 ariadna 69
     * Test for deprecated get table alias
70
     */
71
    public function test_get_table_alias_deprecated(): void {
72
        $entity = new base_test_entity();
73
 
74
        $entity->get_table_alias('mydeprecatedtable');
75
 
76
        // Debugging called twice, as get_table_alias internally calls set_table_alias for undefined alias.
77
        $this->assertDebuggingCalledCount(2, [
78
            'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
79
            'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
80
        ]);
81
    }
82
 
83
    /**
84
     * Test for deprecated get table alias replacement
85
     */
86
    public function test_get_table_alias_deprecated_replacement(): void {
87
        $entity = new base_test_entity_second();
88
 
89
        $mydeprecatedtable = $entity->get_table_alias('mydeprecatedtable');
90
        $this->assertDebuggingCalled();
91
 
92
        // We should get back the same alias for the replacement table.
93
        $this->assertEquals($mydeprecatedtable, $entity->get_table_alias('mytable'));
94
    }
95
 
96
    /**
1 efrain 97
     * Test for invalid get table alias
98
     */
99
    public function test_get_table_alias_invalid(): void {
100
        $entity = new base_test_entity();
101
 
102
        $this->expectException(coding_exception::class);
103
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
104
            'Invalid table name (nonexistingalias)');
105
        $entity->get_table_alias('nonexistingalias');
106
    }
107
 
108
    /**
109
     * Test getting all table aliases
110
     */
111
    public function test_get_table_aliases(): void {
112
        $entity = new base_test_entity();
113
 
114
        [
115
            'mytable' => $mytablealias,
116
            'myothertable' => $myothertablealias,
117
        ] = $entity->get_table_aliases();
118
 
119
        $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $mytablealias);
120
        $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $myothertablealias);
121
 
122
        // They must differ.
123
        $this->assertNotEquals($mytablealias, $myothertablealias);
124
    }
125
 
126
    /**
127
     * Test setting table alias
128
     */
129
    public function test_set_table_alias(): void {
130
        $entity = new base_test_entity();
131
 
132
        $entity->set_table_alias('mytable', 'newalias');
133
        $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
134
    }
135
 
136
    /**
1441 ariadna 137
     * Test for deprecated set table alias
138
     */
139
    public function test_set_table_alias_deprecated(): void {
140
        $entity = new base_test_entity();
141
 
142
        $entity->set_table_alias('mydeprecatedtable', 'newalias');
143
        $this->assertEquals('newalias', $entity->get_table_alias('mydeprecatedtable'));
144
 
145
        // Debugging called twice, once for set_table_alias and once for subsequent get_table_alias.
146
        $this->assertDebuggingCalledCount(2, [
147
            'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
148
            'The table \'mydeprecatedtable\' is deprecated, please do not use it any more.',
149
        ]);
150
    }
151
 
152
    /**
153
     * Test for deprecated get table alias replacement
154
     */
155
    public function test_set_table_alias_deprecated_replacement(): void {
156
        $entity = new base_test_entity_second();
157
 
158
        $entity->set_table_alias('mydeprecatedtable', 'newalias');
159
        $this->assertDebuggingCalled();
160
 
161
        // We should get back the same alias for the replacement table.
162
        $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
163
    }
164
 
165
    /**
1 efrain 166
     * Test invalid entity set table alias
167
     */
168
    public function test_set_table_alias_invalid(): void {
169
        $entity = new base_test_entity();
170
 
171
        $this->expectException(coding_exception::class);
172
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)');
173
        $entity->set_table_alias('nonexistent', 'newalias');
174
    }
175
 
176
    /**
177
     * Test setting multiple table aliases
178
     */
179
    public function test_set_table_aliases(): void {
180
        $entity = new base_test_entity();
181
 
182
        $entity->set_table_aliases([
183
            'mytable' => 'newalias',
184
            'myothertable' => 'newalias2',
185
        ]);
186
        $this->assertEquals([
187
            'mytable' => 'newalias',
188
            'myothertable' => 'newalias2',
189
        ], $entity->get_table_aliases());
190
    }
191
 
192
    /**
193
     * Test setting multiple table aliases, containing an invalid table
194
     */
195
    public function test_set_table_aliases_invalid(): void {
196
        $entity = new base_test_entity();
197
 
198
        $this->expectException(coding_exception::class);
199
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)');
200
        $entity->set_table_aliases([
201
            'mytable' => 'newalias',
202
            'nonexistent' => 'newalias2',
203
        ]);
204
    }
205
 
206
    /**
207
     * Test setting table join alias
208
     */
209
    public function test_set_table_join_alias(): void {
210
        $entity = new base_test_entity();
211
 
212
        $entity->set_table_join_alias('mytable', 'newalias');
213
        $this->assertTrue($entity->has_table_join_alias('mytable'));
214
        $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
215
    }
216
 
217
    /**
218
     * Test that entity doesn't have table join alias by default
219
     *
220
     * {@see test_set_table_join_alias} for assertion where it does
221
     */
222
    public function test_has_table_join_alias(): void {
223
        $entity = new base_test_entity();
224
        $this->assertFalse($entity->has_table_join_alias('mytable'));
225
    }
226
 
227
    /**
228
     * Test entity name
229
     */
230
    public function test_set_entity_name(): void {
231
        $entity = new base_test_entity();
232
 
233
        $this->assertEquals('base_test_entity', $entity->get_entity_name());
234
 
235
        $entity->set_entity_name('newentityname');
236
        $this->assertEquals('newentityname', $entity->get_entity_name());
237
    }
238
 
239
    /**
240
     * Test entity title
241
     */
242
    public function test_set_entity_title(): void {
243
        $entity = new base_test_entity();
244
 
245
        $this->assertEquals(new lang_string('yes'), $entity->get_entity_title());
246
 
247
        $newtitle = new lang_string('fullname');
248
        $entity->set_entity_title($newtitle);
249
        $this->assertEquals($newtitle, $entity->get_entity_title());
250
    }
251
 
252
    /**
253
     * Test getting column
254
     */
255
    public function test_get_column(): void {
256
        $entity = (new base_test_entity())->initialise();
257
 
258
        $column = $entity->get_column('test');
259
        $this->assertEquals('base_test_entity:test', $column->get_unique_identifier());
260
    }
261
 
262
    /**
263
     * Test for invalid get column
264
     */
265
    public function test_get_column_invalid(): void {
266
        $entity = (new base_test_entity())->initialise();
267
 
268
        $this->expectException(coding_exception::class);
269
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
270
            'Invalid column name (nonexistingcolumn)');
271
        $entity->get_column('nonexistingcolumn');
272
    }
273
 
274
    /**
275
     * Test getting columns
276
     */
277
    public function test_get_columns(): void {
278
        $entity = (new base_test_entity())->initialise();
279
 
280
        $columns = $entity->get_columns();
281
        $this->assertCount(1, $columns);
282
        $this->assertContainsOnlyInstancesOf(column::class, $columns);
283
    }
284
 
285
    /**
286
     * Test getting filter
287
     */
288
    public function test_get_filter(): void {
289
        $entity = (new base_test_entity())->initialise();
290
 
291
        $filter = $entity->get_filter('test');
292
        $this->assertEquals('base_test_entity:test', $filter->get_unique_identifier());
293
    }
294
 
295
    /**
296
     * Test for invalid get filter
297
     */
298
    public function test_get_filter_invalid(): void {
299
        $entity = (new base_test_entity())->initialise();
300
 
301
        $this->expectException(coding_exception::class);
302
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
303
            'Invalid filter name (nonexistingfilter)');
304
        $entity->get_filter('nonexistingfilter');
305
    }
306
 
307
    /**
308
     * Test getting filters
309
     */
310
    public function test_get_filters(): void {
311
        $entity = (new base_test_entity())->initialise();
312
 
313
        $filters = $entity->get_filters();
314
        $this->assertCount(1, $filters);
315
        $this->assertContainsOnlyInstancesOf(filter::class, $filters);
316
    }
317
 
318
    /**
319
     * Test getting condition
320
     */
321
    public function test_get_condition(): void {
322
        $entity = (new base_test_entity())->initialise();
323
 
324
        $condition = $entity->get_condition('test');
325
        $this->assertEquals('base_test_entity:test', $condition->get_unique_identifier());
326
    }
327
 
328
    /**
329
     * Test for invalid get condition
330
     */
331
    public function test_get_condition_invalid(): void {
332
        $entity = (new base_test_entity())->initialise();
333
 
334
        $this->expectException(coding_exception::class);
335
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
336
            'Invalid condition name (nonexistingcondition)');
337
        $entity->get_condition('nonexistingcondition');
338
    }
339
 
340
    /**
341
     * Test getting conditions
342
     */
343
    public function test_get_conditions(): void {
344
        $entity = (new base_test_entity())->initialise();
345
 
346
        $conditions = $entity->get_conditions();
347
        $this->assertCount(1, $conditions);
348
        $this->assertContainsOnlyInstancesOf(filter::class, $conditions);
349
    }
350
}
351
 
352
/**
353
 * Simple implementation of the base entity
354
 */
355
class base_test_entity extends base {
356
 
357
    /**
1441 ariadna 358
     * Database tables that this entity uses
1 efrain 359
     *
1441 ariadna 360
     * @return string[]
1 efrain 361
     */
362
    protected function get_default_tables(): array {
363
        return [
364
            'mytable',
365
            'myothertable',
366
        ];
367
    }
368
 
369
    /**
1441 ariadna 370
     * Database tables that this entity no longer uses
371
     *
372
     * @return string[]
373
     */
374
    protected function get_deprecated_tables(): array {
375
        return [
376
            'mydeprecatedtable',
377
        ];
378
    }
379
 
380
    /**
1 efrain 381
     * Entity title
382
     *
383
     * @return lang_string
384
     */
385
    protected function get_default_entity_title(): lang_string {
386
        return new lang_string('yes');
387
    }
388
 
389
    /**
390
     * Initialise entity
391
     *
392
     * @return base
393
     */
394
    public function initialise(): base {
395
        $column = (new column(
396
            'test',
397
            new lang_string('no'),
398
            $this->get_entity_name()
399
        ))
400
            ->add_field('no');
401
 
402
        $filter = (new filter(
403
            text::class,
404
            'test',
405
            new lang_string('no'),
406
            $this->get_entity_name(),
407
        ))
408
            ->set_field_sql('no');
409
 
410
        return $this
411
            ->add_column($column)
412
            ->add_filter($filter)
413
            ->add_condition($filter);
414
    }
415
}
1441 ariadna 416
 
417
/**
418
 * Another simple implementation of the base entity
419
 */
420
class base_test_entity_second extends base_test_entity {
421
 
422
    /**
423
     * Database tables that this entity no longer uses
424
     *
425
     * @return string[]
426
     */
427
    protected function get_deprecated_tables(): array {
428
        return [
429
            'mytable' => 'mydeprecatedtable',
430
        ];
431
    }
432
}