Proyectos de Subversion Moodle

Rev

| 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
 */
46
class base_test extends advanced_testcase {
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
    /**
69
     * Test for invalid get table alias
70
     */
71
    public function test_get_table_alias_invalid(): void {
72
        $entity = new base_test_entity();
73
 
74
        $this->expectException(coding_exception::class);
75
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
76
            'Invalid table name (nonexistingalias)');
77
        $entity->get_table_alias('nonexistingalias');
78
    }
79
 
80
    /**
81
     * Test getting all table aliases
82
     */
83
    public function test_get_table_aliases(): void {
84
        $entity = new base_test_entity();
85
 
86
        [
87
            'mytable' => $mytablealias,
88
            'myothertable' => $myothertablealias,
89
        ] = $entity->get_table_aliases();
90
 
91
        $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $mytablealias);
92
        $this->assertMatchesRegularExpression('/^rbalias(\d+)$/', $myothertablealias);
93
 
94
        // They must differ.
95
        $this->assertNotEquals($mytablealias, $myothertablealias);
96
    }
97
 
98
    /**
99
     * Test setting table alias
100
     */
101
    public function test_set_table_alias(): void {
102
        $entity = new base_test_entity();
103
 
104
        $entity->set_table_alias('mytable', 'newalias');
105
        $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
106
    }
107
 
108
    /**
109
     * Test invalid entity set table alias
110
     */
111
    public function test_set_table_alias_invalid(): void {
112
        $entity = new base_test_entity();
113
 
114
        $this->expectException(coding_exception::class);
115
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)');
116
        $entity->set_table_alias('nonexistent', 'newalias');
117
    }
118
 
119
    /**
120
     * Test setting multiple table aliases
121
     */
122
    public function test_set_table_aliases(): void {
123
        $entity = new base_test_entity();
124
 
125
        $entity->set_table_aliases([
126
            'mytable' => 'newalias',
127
            'myothertable' => 'newalias2',
128
        ]);
129
        $this->assertEquals([
130
            'mytable' => 'newalias',
131
            'myothertable' => 'newalias2',
132
        ], $entity->get_table_aliases());
133
    }
134
 
135
    /**
136
     * Test setting multiple table aliases, containing an invalid table
137
     */
138
    public function test_set_table_aliases_invalid(): void {
139
        $entity = new base_test_entity();
140
 
141
        $this->expectException(coding_exception::class);
142
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)');
143
        $entity->set_table_aliases([
144
            'mytable' => 'newalias',
145
            'nonexistent' => 'newalias2',
146
        ]);
147
    }
148
 
149
    /**
150
     * Test setting table join alias
151
     */
152
    public function test_set_table_join_alias(): void {
153
        $entity = new base_test_entity();
154
 
155
        $entity->set_table_join_alias('mytable', 'newalias');
156
        $this->assertTrue($entity->has_table_join_alias('mytable'));
157
        $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
158
    }
159
 
160
    /**
161
     * Test that entity doesn't have table join alias by default
162
     *
163
     * {@see test_set_table_join_alias} for assertion where it does
164
     */
165
    public function test_has_table_join_alias(): void {
166
        $entity = new base_test_entity();
167
        $this->assertFalse($entity->has_table_join_alias('mytable'));
168
    }
169
 
170
    /**
171
     * Test entity name
172
     */
173
    public function test_set_entity_name(): void {
174
        $entity = new base_test_entity();
175
 
176
        $this->assertEquals('base_test_entity', $entity->get_entity_name());
177
 
178
        $entity->set_entity_name('newentityname');
179
        $this->assertEquals('newentityname', $entity->get_entity_name());
180
    }
181
 
182
    /**
183
     * Test entity title
184
     */
185
    public function test_set_entity_title(): void {
186
        $entity = new base_test_entity();
187
 
188
        $this->assertEquals(new lang_string('yes'), $entity->get_entity_title());
189
 
190
        $newtitle = new lang_string('fullname');
191
        $entity->set_entity_title($newtitle);
192
        $this->assertEquals($newtitle, $entity->get_entity_title());
193
    }
194
 
195
    /**
196
     * Test adding single join
197
     */
198
    public function test_add_join(): void {
199
        $entity = new base_test_entity();
200
 
201
        $tablejoin = "JOIN {course} c2 ON c2.id = c1.id";
202
        $entity->add_join($tablejoin);
203
 
204
        $this->assertEquals([$tablejoin], $entity->get_joins());
205
    }
206
 
207
    /**
208
     * Test adding multiple joins
209
     */
210
    public function test_add_joins(): void {
211
        $entity = new base_test_entity();
212
 
213
        $tablejoins = [
214
            "JOIN {course} c2 ON c2.id = c1.id",
215
            "JOIN {course} c3 ON c3.id = c1.id",
216
        ];
217
        $entity->add_joins($tablejoins);
218
 
219
        $this->assertEquals($tablejoins, $entity->get_joins());
220
    }
221
 
222
    /**
223
     * Test adding duplicate joins
224
     */
225
    public function test_add_duplicate_joins(): void {
226
        $entity = new base_test_entity();
227
 
228
        $tablejoins = [
229
            "JOIN {course} c2 ON c2.id = c1.id",
230
            "JOIN {course} c3 ON c3.id = c1.id",
231
        ];
232
        $entity
233
            ->add_joins($tablejoins)
234
            ->add_joins($tablejoins);
235
 
236
        $this->assertEquals($tablejoins, $entity->get_joins());
237
    }
238
 
239
    /**
240
     * Test getting column
241
     */
242
    public function test_get_column(): void {
243
        $entity = (new base_test_entity())->initialise();
244
 
245
        $column = $entity->get_column('test');
246
        $this->assertEquals('base_test_entity:test', $column->get_unique_identifier());
247
    }
248
 
249
    /**
250
     * Test for invalid get column
251
     */
252
    public function test_get_column_invalid(): void {
253
        $entity = (new base_test_entity())->initialise();
254
 
255
        $this->expectException(coding_exception::class);
256
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
257
            'Invalid column name (nonexistingcolumn)');
258
        $entity->get_column('nonexistingcolumn');
259
    }
260
 
261
    /**
262
     * Test getting columns
263
     */
264
    public function test_get_columns(): void {
265
        $entity = (new base_test_entity())->initialise();
266
 
267
        $columns = $entity->get_columns();
268
        $this->assertCount(1, $columns);
269
        $this->assertContainsOnlyInstancesOf(column::class, $columns);
270
    }
271
 
272
    /**
273
     * Test getting filter
274
     */
275
    public function test_get_filter(): void {
276
        $entity = (new base_test_entity())->initialise();
277
 
278
        $filter = $entity->get_filter('test');
279
        $this->assertEquals('base_test_entity:test', $filter->get_unique_identifier());
280
    }
281
 
282
    /**
283
     * Test for invalid get filter
284
     */
285
    public function test_get_filter_invalid(): void {
286
        $entity = (new base_test_entity())->initialise();
287
 
288
        $this->expectException(coding_exception::class);
289
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
290
            'Invalid filter name (nonexistingfilter)');
291
        $entity->get_filter('nonexistingfilter');
292
    }
293
 
294
    /**
295
     * Test getting filters
296
     */
297
    public function test_get_filters(): void {
298
        $entity = (new base_test_entity())->initialise();
299
 
300
        $filters = $entity->get_filters();
301
        $this->assertCount(1, $filters);
302
        $this->assertContainsOnlyInstancesOf(filter::class, $filters);
303
    }
304
 
305
    /**
306
     * Test getting condition
307
     */
308
    public function test_get_condition(): void {
309
        $entity = (new base_test_entity())->initialise();
310
 
311
        $condition = $entity->get_condition('test');
312
        $this->assertEquals('base_test_entity:test', $condition->get_unique_identifier());
313
    }
314
 
315
    /**
316
     * Test for invalid get condition
317
     */
318
    public function test_get_condition_invalid(): void {
319
        $entity = (new base_test_entity())->initialise();
320
 
321
        $this->expectException(coding_exception::class);
322
        $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
323
            'Invalid condition name (nonexistingcondition)');
324
        $entity->get_condition('nonexistingcondition');
325
    }
326
 
327
    /**
328
     * Test getting conditions
329
     */
330
    public function test_get_conditions(): void {
331
        $entity = (new base_test_entity())->initialise();
332
 
333
        $conditions = $entity->get_conditions();
334
        $this->assertCount(1, $conditions);
335
        $this->assertContainsOnlyInstancesOf(filter::class, $conditions);
336
    }
337
}
338
 
339
/**
340
 * Simple implementation of the base entity
341
 */
342
class base_test_entity extends base {
343
 
344
    /**
345
     * Table aliases
346
     *
347
     * @return array
348
     */
349
    protected function get_default_tables(): array {
350
        return [
351
            'mytable',
352
            'myothertable',
353
        ];
354
    }
355
 
356
    /**
357
     * Entity title
358
     *
359
     * @return lang_string
360
     */
361
    protected function get_default_entity_title(): lang_string {
362
        return new lang_string('yes');
363
    }
364
 
365
    /**
366
     * Initialise entity
367
     *
368
     * @return base
369
     */
370
    public function initialise(): base {
371
        $column = (new column(
372
            'test',
373
            new lang_string('no'),
374
            $this->get_entity_name()
375
        ))
376
            ->add_field('no');
377
 
378
        $filter = (new filter(
379
            text::class,
380
            'test',
381
            new lang_string('no'),
382
            $this->get_entity_name(),
383
        ))
384
            ->set_field_sql('no');
385
 
386
        return $this
387
            ->add_column($column)
388
            ->add_filter($filter)
389
            ->add_condition($filter);
390
    }
391
}