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_user\reportbuilder\datasource;
20
 
21
use core_reportbuilder_generator;
1441 ariadna 22
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text, user as user_filter};
23
use core_reportbuilder\tests\core_reportbuilder_testcase;
1 efrain 24
 
25
/**
26
 * Unit tests for users datasource
27
 *
28
 * @package     core_user
29
 * @covers      \core_user\reportbuilder\datasource\users
30
 * @copyright   2022 Paul Holden <paulh@moodle.com>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
1441 ariadna 33
final class users_test extends core_reportbuilder_testcase {
1 efrain 34
 
35
    /**
36
     * Test default datasource
37
     */
38
    public function test_datasource_default(): void {
39
        $this->resetAfterTest();
40
 
41
        $user2 = $this->getDataGenerator()->create_user(['firstname' => 'Charles']);
42
        $user3 = $this->getDataGenerator()->create_user(['firstname' => 'Brian']);
43
 
44
        /** @var core_reportbuilder_generator $generator */
45
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
46
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 1]);
47
 
48
        $content = $this->get_custom_report_content($report->get('id'));
49
        $this->assertCount(3, $content);
50
 
51
        // Default columns are fullname, username, email. Results are sorted by the fullname.
52
        [$adminrow, $userrow1, $userrow2] = array_map('array_values', $content);
53
 
54
        $this->assertEquals(['Admin User', 'admin', 'admin@example.com'], $adminrow);
55
        $this->assertEquals([fullname($user3), $user3->username, $user3->email], $userrow1);
56
        $this->assertEquals([fullname($user2), $user2->username, $user2->email], $userrow2);
57
    }
58
 
59
    /**
60
     * Test datasource columns that aren't added by default
61
     */
62
    public function test_datasource_non_default_columns(): void {
63
        $this->resetAfterTest();
64
 
65
        $user = $this->getDataGenerator()->create_user([
66
            'firstname' => 'Zoe',
67
            'idnumber' => 'U0001',
68
            'city' => 'London',
69
            'country' => 'GB',
1441 ariadna 70
            'lang' => 'en',
71
            'timezone' => 'Europe/London',
1 efrain 72
            'theme' => 'boost',
73
            'interests' => ['Horses'],
74
        ]);
75
 
76
        $cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
77
        cohort_add_member($cohort->id, $user->id);
78
 
79
        /** @var core_reportbuilder_generator $generator */
80
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
81
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
82
 
83
        // User.
84
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithlink',
85
            'sortenabled' => 1]);
86
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithpicture']);
87
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithpicturelink']);
88
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:picture']);
89
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
90
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
91
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
92
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
1441 ariadna 93
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lang']);
94
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:timezone']);
1 efrain 95
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:description']);
96
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstnamephonetic']);
97
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastnamephonetic']);
98
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:middlename']);
99
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:alternatename']);
100
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:idnumber']);
101
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:institution']);
102
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:department']);
103
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:phone1']);
104
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:phone2']);
105
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:address']);
106
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastaccess']);
107
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended']);
108
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:confirmed']);
109
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:auth']);
110
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:moodlenetprofile']);
111
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:timecreated']);
112
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:timemodified']);
113
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastip']);
114
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:theme']);
115
 
116
        // Tags.
117
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
118
 
119
        // Cohort.
120
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
121
 
122
        $content = $this->get_custom_report_content($report->get('id'));
123
        $this->assertCount(2, $content);
124
 
125
        // Admin row.
126
        [
127
            $fullnamewithlink,
128
            $fullnamewithpicture,
129
            $fullnamewithpicturelink,
130
            $picture,
131
            $lastname,
132
            $firstname,
133
        ] = array_values($content[0]);
134
 
135
        $this->assertStringContainsString('Admin User', $fullnamewithlink);
136
        $this->assertStringContainsString('Admin User', $fullnamewithpicture);
137
        $this->assertStringContainsString('Admin User', $fullnamewithpicturelink);
138
        $this->assertNotEmpty($picture);
139
        $this->assertEquals('Admin', $lastname);
140
        $this->assertEquals('User', $firstname);
141
 
142
        // User row.
143
        [
144
            $fullnamewithlink,
145
            $fullnamewithpicture,
146
            $fullnamewithpicturelink,
147
            $picture,
148
            $firstname,
149
            $lastname,
150
            $city,
151
            $country,
1441 ariadna 152
            $lang,
153
            $timezone,
1 efrain 154
            $description,
155
            $firstnamephonetic,
156
            $lastnamephonetic,
157
            $middlename,
158
            $alternatename,
159
            $idnumber,
160
            $institution,
161
            $department,
162
            $phone1,
163
            $phone2,
164
            $address,
165
            $lastaccess,
166
            $suspended,
167
            $confirmed,
168
            $auth,
169
            $moodlenetprofile,
170
            $timecreated,
171
            $timemodified,
172
            $lastip,
173
            $theme,
174
            $tag,
175
            $cohortname,
176
        ] = array_values($content[1]);
177
 
178
        $this->assertStringContainsString(fullname($user), $fullnamewithlink);
179
        $this->assertStringContainsString(fullname($user), $fullnamewithpicture);
180
        $this->assertStringContainsString(fullname($user), $fullnamewithpicturelink);
181
        $this->assertNotEmpty($picture);
182
        $this->assertEquals($user->firstname, $firstname);
183
        $this->assertEquals($user->lastname, $lastname);
184
        $this->assertEquals($user->city, $city);
185
        $this->assertEquals('United Kingdom', $country);
1441 ariadna 186
        $this->assertEquals('English ‎(en)‎', $lang);
187
        $this->assertEquals('Europe/London', $timezone);
1 efrain 188
        $this->assertEquals($user->description, $description);
189
        $this->assertEquals($user->firstnamephonetic, $firstnamephonetic);
190
        $this->assertEquals($user->lastnamephonetic, $lastnamephonetic);
191
        $this->assertEquals($user->middlename, $middlename);
192
        $this->assertEquals($user->alternatename, $alternatename);
193
        $this->assertEquals($user->idnumber, $idnumber);
194
        $this->assertEquals($user->institution, $institution);
195
        $this->assertEquals($user->department, $department);
196
        $this->assertEquals($user->phone1, $phone1);
197
        $this->assertEquals($user->phone2, $phone2);
198
        $this->assertEquals($user->address, $address);
199
        $this->assertEmpty($lastaccess);
200
        $this->assertEquals('No', $suspended);
201
        $this->assertEquals('Yes', $confirmed);
202
        $this->assertEquals('Manual accounts', $auth);
203
        $this->assertEquals($user->moodlenetprofile, $moodlenetprofile);
204
        $this->assertNotEmpty($timecreated);
205
        $this->assertNotEmpty($timemodified);
206
        $this->assertEquals('0.0.0.0', $lastip);
207
        $this->assertEquals('Boost', $theme);
208
        $this->assertEquals('Horses', $tag);
209
        $this->assertEquals($cohort->name, $cohortname);
210
    }
211
 
212
    /**
1441 ariadna 213
     * Test fullname columns when alternative fullname format is configured
214
     */
215
    public function test_datasource_alternative_fullname_columns(): void {
216
        $this->resetAfterTest();
217
        $this->setAdminUser();
218
 
219
        set_config('alternativefullnameformat', '(alternatename) firstname lastname');
220
 
221
        $this->getDataGenerator()->create_user(['firstname' => 'John', 'lastname' => 'Smith', 'alternatename' => 'JS']);
222
 
223
        /** @var core_reportbuilder_generator $generator */
224
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
225
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
226
 
227
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]);
228
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithlink']);
229
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithpicture']);
230
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullnamewithpicturelink']);
231
 
232
        $content = $this->get_custom_report_content($report->get('id'));
233
        $this->assertCount(2, $content);
234
 
235
        // Admin row.
236
        [
237
            $fullname,
238
            $fullnamewithlink,
239
            $fullnamewithpicture,
240
            $fullnamewithpicturelink
241
        ] = array_values($content[0]);
242
 
243
        $this->assertEquals('Admin User', $fullname);
244
        $this->assertStringContainsString('Admin User', $fullnamewithlink);
245
        $this->assertStringContainsString('Admin User', $fullnamewithpicture);
246
        $this->assertStringContainsString('Admin User', $fullnamewithpicturelink);
247
 
248
        // User row.
249
        [
250
            $fullname,
251
            $fullnamewithlink,
252
            $fullnamewithpicture,
253
            $fullnamewithpicturelink
254
        ] = array_values($content[1]);
255
 
256
        $this->assertEquals('(JS) John Smith', $fullname);
257
        $this->assertStringContainsString('(JS) John Smith', $fullnamewithlink);
258
        $this->assertStringContainsString('(JS) John Smith', $fullnamewithpicture);
259
        $this->assertStringContainsString('(JS) John Smith', $fullnamewithpicturelink);
260
    }
261
 
262
    /**
1 efrain 263
     * Data provider for {@see test_datasource_filters}
264
     *
265
     * @return array[]
266
     */
1441 ariadna 267
    public static function datasource_filters_provider(): array {
1 efrain 268
        return [
269
            // User.
270
            'Filter user' => ['user:userselect', [
271
                'user:userselect_operator' => user_filter::USER_SELECT,
272
                'user:userselect_value' => [-1],
273
            ], false],
274
            'Filter fullname' => ['user:fullname', [
275
                'user:fullname_operator' => text::CONTAINS,
276
                'user:fullname_value' => 'Zoe',
277
            ], true],
278
            'Filter fullname (no match)' => ['user:fullname', [
279
                'user:fullname_operator' => text::CONTAINS,
280
                'user:fullname_value' => 'Alfie',
281
            ], false],
1441 ariadna 282
            'Filter picture' => ['user:picture', [
283
                'user:picture_operator' => boolean_select::NOT_CHECKED,
284
            ], true],
285
            'Filter picture (no match)' => ['user:picture', [
286
                'user:picture_operator' => boolean_select::CHECKED,
287
            ], false],
1 efrain 288
            'Filter firstname' => ['user:firstname', [
289
                'user:firstname_operator' => text::IS_EQUAL_TO,
290
                'user:firstname_value' => 'Zoe',
291
            ], true],
292
            'Filter firstname (no match)' => ['user:firstname', [
293
                'user:firstname_operator' => text::IS_EQUAL_TO,
294
                'user:firstname_value' => 'Alfie',
295
            ], false],
296
            'Filter middlename' => ['user:middlename', [
297
                'user:middlename_operator' => text::IS_EQUAL_TO,
298
                'user:middlename_value' => 'Zebediah',
299
            ], true],
300
            'Filter middlename (no match)' => ['user:middlename', [
301
                'user:middlename_operator' => text::IS_EQUAL_TO,
302
                'user:middlename_value' => 'Aardvark',
303
            ], false],
304
            'Filter lastname' => ['user:lastname', [
305
                'user:lastname_operator' => text::IS_EQUAL_TO,
306
                'user:lastname_value' => 'Zebra',
307
            ], true],
308
            'Filter lastname (no match)' => ['user:lastname', [
309
                'user:lastname_operator' => text::IS_EQUAL_TO,
310
                'user:lastname_value' => 'Aardvark',
311
            ], false],
312
            'Filter firstnamephonetic' => ['user:firstnamephonetic', [
313
                'user:firstnamephonetic_operator' => text::IS_EQUAL_TO,
314
                'user:firstnamephonetic_value' => 'Eoz',
315
            ], true],
316
            'Filter firstnamephonetic (no match)' => ['user:firstnamephonetic', [
317
                'user:firstnamephonetic_operator' => text::IS_EQUAL_TO,
318
                'user:firstnamephonetic_value' => 'Alfie',
319
            ], false],
320
            'Filter lastnamephonetic' => ['user:lastnamephonetic', [
321
                'user:lastnamephonetic_operator' => text::IS_EQUAL_TO,
322
                'user:lastnamephonetic_value' => 'Arbez',
323
            ], true],
324
            'Filter lastnamephonetic (no match)' => ['user:lastnamephonetic', [
325
                'user:lastnamephonetic_operator' => text::IS_EQUAL_TO,
326
                'user:lastnamephonetic_value' => 'Aardvark',
327
            ], false],
328
            'Filter alternatename' => ['user:alternatename', [
329
                'user:alternatename_operator' => text::IS_EQUAL_TO,
330
                'user:alternatename_value' => 'Zee',
331
            ], true],
332
            'Filter alternatename (no match)' => ['user:alternatename', [
333
                'user:alternatename_operator' => text::IS_EQUAL_TO,
334
                'user:alternatename_value' => 'Aardvark',
335
            ], false],
336
            'Filter email' => ['user:email', [
337
                'user:email_operator' => text::CONTAINS,
338
                'user:email_value' => 'zoe1',
339
            ], true],
340
            'Filter email (no match)' => ['user:email', [
341
                'user:email_operator' => text::CONTAINS,
342
                'user:email_value' => 'alfie1',
343
            ], false],
344
            'Filter phone1' => ['user:phone1', [
345
                'user:phone1_operator' => text::IS_EQUAL_TO,
346
                'user:phone1_value' => '111',
347
            ], true],
348
            'Filter phone1 (no match)' => ['user:phone1', [
349
                'user:phone1_operator' => text::IS_EQUAL_TO,
350
                'user:phone1_value' => '119',
351
            ], false],
352
            'Filter phone2' => ['user:phone2', [
353
                'user:phone2_operator' => text::IS_EQUAL_TO,
354
                'user:phone2_value' => '222',
355
            ], true],
356
            'Filter phone2 (no match)' => ['user:phone2', [
357
                'user:phone2_operator' => text::IS_EQUAL_TO,
358
                'user:phone2_value' => '229',
359
            ], false],
360
            'Filter address' => ['user:address', [
361
                'user:address_operator' => text::IS_EQUAL_TO,
362
                'user:address_value' => 'Big Farm',
363
            ], true],
364
            'Filter address (no match)' => ['user:address', [
365
                'user:address_operator' => text::IS_EQUAL_TO,
366
                'user:address_value' => 'Small Farm',
367
            ], false],
368
            'Filter city' => ['user:city', [
369
                'user:city_operator' => text::IS_EQUAL_TO,
370
                'user:city_value' => 'Barcelona',
371
            ], true],
372
            'Filter city (no match)' => ['user:city', [
373
                'user:city_operator' => text::IS_EQUAL_TO,
374
                'user:city_value' => 'Perth',
375
            ], false],
376
            'Filter country' => ['user:country', [
377
                'user:country_operator' => select::EQUAL_TO,
378
                'user:country_value' => 'ES',
379
            ], true],
380
            'Filter country (no match)' => ['user:country', [
381
                'user:country_operator' => select::EQUAL_TO,
382
                'user:country_value' => 'AU',
383
            ], false],
1441 ariadna 384
            'Filter lang' => ['user:lang', [
385
                'user:lang_operator' => select::EQUAL_TO,
386
                'user:lang_value' => 'en',
387
            ], true],
388
            'Filter lang (no match)' => ['user:lang', [
389
                'user:lang_operator' => select::NOT_EQUAL_TO,
390
                'user:lang_value' => 'en',
391
            ], false],
392
            'Filter timezone' => ['user:timezone', [
393
                'user:timezone_operator' => select::EQUAL_TO,
394
                'user:timezone_value' => 'Europe/Barcelona',
395
            ], true],
396
            'Filter timezone (no match)' => ['user:timezone', [
397
                'user:timezone_operator' => select::EQUAL_TO,
398
                'user:timezone_value' => 'Australia/Perth',
399
            ], false],
1 efrain 400
            'Filter theme' => ['user:theme', [
401
                'user:theme_operator' => select::EQUAL_TO,
402
                'user:theme_value' => 'boost',
403
            ], true],
404
            'Filter theme (no match)' => ['user:theme', [
405
                'user:theme_operator' => select::EQUAL_TO,
406
                'user:theme_value' => 'classic',
407
            ], false],
408
            'Filter description' => ['user:description', [
409
                'user:description_operator' => text::CONTAINS,
410
                'user:description_value' => 'Hello there',
411
            ], true],
412
            'Filter description (no match)' => ['user:description', [
413
                'user:description_operator' => text::CONTAINS,
414
                'user:description_value' => 'Goodbye',
415
            ], false],
416
            'Filter auth' => ['user:auth', [
417
                'user:auth_operator' => select::EQUAL_TO,
418
                'user:auth_value' => 'manual',
419
            ], true],
420
            'Filter auth (no match)' => ['user:auth', [
421
                'user:auth_operator' => select::EQUAL_TO,
422
                'user:auth_value' => 'ldap',
423
            ], false],
424
            'Filter username' => ['user:username', [
425
                'user:username_operator' => text::IS_EQUAL_TO,
426
                'user:username_value' => 'zoe1',
427
            ], true],
428
            'Filter username (no match)' => ['user:username', [
429
                'user:username_operator' => text::IS_EQUAL_TO,
430
                'user:username_value' => 'alfie1',
431
            ], false],
432
            'Filter idnumber' => ['user:idnumber', [
433
                'user:idnumber_operator' => text::IS_EQUAL_TO,
434
                'user:idnumber_value' => 'Z0001',
435
            ], true],
436
            'Filter idnumber (no match)' => ['user:idnumber', [
437
                'user:idnumber_operator' => text::IS_EQUAL_TO,
438
                'user:idnumber_value' => 'A0001',
439
            ], false],
440
            'Filter institution' => ['user:institution', [
441
                'user:institution_operator' => text::IS_EQUAL_TO,
442
                'user:institution_value' => 'Farm',
443
            ], true],
444
            'Filter institution (no match)' => ['user:institution', [
445
                'user:institution_operator' => text::IS_EQUAL_TO,
446
                'user:institution_value' => 'University',
447
            ], false],
448
            'Filter department' => ['user:department', [
449
                'user:department_operator' => text::IS_EQUAL_TO,
450
                'user:department_value' => 'Stable',
451
            ], true],
452
            'Filter department (no match)' => ['user:department', [
453
                'user:department_operator' => text::IS_EQUAL_TO,
454
                'user:department_value' => 'Office',
455
            ], false],
456
            'Filter moodlenetprofile' => ['user:moodlenetprofile', [
457
                'user:moodlenetprofile_operator' => text::IS_EQUAL_TO,
458
                'user:moodlenetprofile_value' => '@zoe1@example.com',
459
            ], true],
460
            'Filter moodlenetprofile (no match)' => ['user:moodlenetprofile', [
461
                'user:moodlenetprofile_operator' => text::IS_EQUAL_TO,
462
                'user:moodlenetprofile_value' => '@alfie1@example.com',
463
            ], false],
464
            'Filter suspended' => ['user:suspended', [
465
                'user:suspended_operator' => boolean_select::NOT_CHECKED,
466
            ], true],
467
            'Filter suspended (no match)' => ['user:suspended', [
468
                'user:suspended_operator' => boolean_select::CHECKED,
469
            ], false],
470
            'Filter confirmed' => ['user:confirmed', [
471
                'user:confirmed_operator' => boolean_select::CHECKED,
472
            ], true],
473
            'Filter confirmed (no match)' => ['user:confirmed', [
474
                'user:confirmed_operator' => boolean_select::NOT_CHECKED,
475
            ], false],
476
            'Filter timecreated' => ['user:timecreated', [
477
                'user:timecreated_operator' => date::DATE_RANGE,
478
                'user:timecreated_from' => 1622502000,
479
            ], true],
480
            'Filter timecreated (no match)' => ['user:timecreated', [
481
                'user:timecreated_operator' => date::DATE_RANGE,
482
                'user:timecreated_from' => 1619823600,
483
                'user:timecreated_to' => 1622502000,
484
            ], false],
485
            'Filter timemodified' => ['user:timemodified', [
486
                'user:timemodified_operator' => date::DATE_RANGE,
487
                'user:timemodified_from' => 1622502000,
488
            ], true],
489
            'Filter timemodified (no match)' => ['user:timemodified', [
490
                'user:timemodified_operator' => date::DATE_RANGE,
491
                'user:timemodified_to' => 1622502000,
492
            ], false],
493
            'Filter lastaccess' => ['user:lastaccess', [
494
                'user:lastaccess_operator' => date::DATE_EMPTY,
495
            ], true],
496
            'Filter lastaccess (no match)' => ['user:lastaccess', [
497
                'user:lastaccess_operator' => date::DATE_RANGE,
498
                'user:lastaccess_from' => 1619823600,
499
                'user:lastaccess_to' => 1622502000,
500
            ], false],
501
            'Filter lastip' => ['user:lastip', [
502
                'user:lastip_operator' => text::IS_EQUAL_TO,
503
                'user:lastip_value' => '0.0.0.0',
504
            ], true],
505
            'Filter lastip (no match)' => ['user:lastip', [
506
                'user:lastip_operator' => text::IS_EQUAL_TO,
507
                'user:lastip_value' => '1.2.3.4',
508
            ], false],
509
 
510
            // Tags.
511
            'Filter tag name' => ['tag:name', [
512
                'tag:name_operator' => tags::EQUAL_TO,
513
                'tag:name_value' => [-1],
514
            ], false],
515
            'Filter tag name not empty' => ['tag:name', [
516
                'tag:name_operator' => tags::NOT_EMPTY,
517
            ], true],
518
 
519
            // Cohort.
520
            'Filter cohort name' => ['cohort:name', [
521
                'cohort:name_operator' => text::IS_EQUAL_TO,
522
                'cohort:name_value' => 'My cohort',
523
            ], true],
524
            'Filter cohort name (no match)' => ['cohort:name', [
525
                'cohort:name_operator' => text::IS_EQUAL_TO,
526
                'cohort:name_value' => 'Not my cohort',
527
            ], false],
528
        ];
529
    }
530
 
531
    /**
532
     * Test datasource filters
533
     *
534
     * @param string $filtername
535
     * @param array $filtervalues
536
     * @param bool $expectmatch
537
     *
538
     * @dataProvider datasource_filters_provider
539
     */
540
    public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void {
541
        $this->resetAfterTest();
542
 
543
        $user = $this->getDataGenerator()->create_user([
544
            'username' => 'zoe1',
545
            'email' => 'zoe1@example.com',
546
            'firstname' => 'Zoe',
547
            'middlename' => 'Zebediah',
548
            'lastname' => 'Zebra',
549
            'firstnamephonetic' => 'Eoz',
550
            'lastnamephonetic' => 'Arbez',
551
            'alternatename' => 'Zee',
552
            'idnumber' => 'Z0001',
553
            'institution' => 'Farm',
554
            'department' => 'Stable',
555
            'phone1' => '111',
556
            'phone2' => '222',
557
            'address' => 'Big Farm',
558
            'city' => 'Barcelona',
559
            'country' => 'ES',
1441 ariadna 560
            'lang' => 'en',
561
            'timezone' => 'Europe/Barcelona',
1 efrain 562
            'theme' => 'boost',
563
            'description' => 'Hello there',
564
            'moodlenetprofile' => '@zoe1@example.com',
565
            'interests' => ['Horses'],
566
            'lastip' => '0.0.0.0',
567
        ]);
568
 
569
        $cohort = $this->getDataGenerator()->create_cohort(['name' => 'My cohort']);
570
        cohort_add_member($cohort->id, $user->id);
571
 
572
        /** @var core_reportbuilder_generator $generator */
573
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
574
 
575
        // Create report containing single column, and given filter.
1441 ariadna 576
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
1 efrain 577
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
578
 
579
        // Add filter, set it's values.
580
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
581
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
582
 
583
        if ($expectmatch) {
584
            $this->assertNotEmpty($content);
585
 
586
            // Merge report usernames into easily traversable array.
587
            $usernames = array_merge(...array_map('array_values', $content));
588
            $this->assertContains($user->username, $usernames);
589
        } else {
590
            $this->assertEmpty($content);
591
        }
592
    }
593
 
594
    /**
595
     * Stress test datasource
596
     *
597
     * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
598
     */
599
    public function test_stress_datasource(): void {
600
        if (!PHPUNIT_LONGTEST) {
601
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
602
        }
603
 
604
        $this->resetAfterTest();
605
 
606
        $user = $this->getDataGenerator()->create_user();
607
 
608
        $this->datasource_stress_test_columns(users::class);
609
        $this->datasource_stress_test_columns_aggregation(users::class);
610
        $this->datasource_stress_test_conditions(users::class, 'user:username');
611
    }
612
}