Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea 17...
17
declare(strict_types=1);
17
declare(strict_types=1);
Línea 18... Línea -...
18
 
-
 
19
namespace core_user\reportbuilder\datasource;
18
 
20
 
19
namespace core_user\reportbuilder\datasource;
21
use core_reportbuilder_testcase;
-
 
22
use core_reportbuilder_generator;
-
 
23
use core_reportbuilder\local\filters\boolean_select;
-
 
24
use core_reportbuilder\local\filters\date;
-
 
25
use core_reportbuilder\local\filters\select;
20
 
26
use core_reportbuilder\local\filters\tags;
-
 
27
use core_reportbuilder\local\filters\text;
-
 
28
use core_reportbuilder\local\filters\user as user_filter;
-
 
29
 
-
 
30
defined('MOODLE_INTERNAL') || die();
-
 
Línea 31... Línea 21...
31
 
21
use core_reportbuilder_generator;
32
global $CFG;
22
use core_reportbuilder\local\filters\{boolean_select, date, select, tags, text, user as user_filter};
33
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
23
use core_reportbuilder\tests\core_reportbuilder_testcase;
34
 
24
 
35
/**
25
/**
36
 * Unit tests for users datasource
26
 * Unit tests for users datasource
37
 *
27
 *
38
 * @package     core_user
28
 * @package     core_user
39
 * @covers      \core_user\reportbuilder\datasource\users
29
 * @covers      \core_user\reportbuilder\datasource\users
Línea 40... Línea 30...
40
 * @copyright   2022 Paul Holden <paulh@moodle.com>
30
 * @copyright   2022 Paul Holden <paulh@moodle.com>
41
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
32
 */
43
class users_test extends core_reportbuilder_testcase {
33
final class users_test extends core_reportbuilder_testcase {
Línea 75... Línea 65...
75
        $user = $this->getDataGenerator()->create_user([
65
        $user = $this->getDataGenerator()->create_user([
76
            'firstname' => 'Zoe',
66
            'firstname' => 'Zoe',
77
            'idnumber' => 'U0001',
67
            'idnumber' => 'U0001',
78
            'city' => 'London',
68
            'city' => 'London',
79
            'country' => 'GB',
69
            'country' => 'GB',
-
 
70
            'lang' => 'en',
-
 
71
            'timezone' => 'Europe/London',
80
            'theme' => 'boost',
72
            'theme' => 'boost',
81
            'interests' => ['Horses'],
73
            'interests' => ['Horses'],
82
        ]);
74
        ]);
Línea 83... Línea 75...
83
 
75
 
Línea 96... Línea 88...
96
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:picture']);
88
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:picture']);
97
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
89
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
98
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
90
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
99
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
91
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
100
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
92
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
-
 
93
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lang']);
-
 
94
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:timezone']);
101
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:description']);
95
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:description']);
102
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstnamephonetic']);
96
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstnamephonetic']);
103
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastnamephonetic']);
97
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastnamephonetic']);
104
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:middlename']);
98
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:middlename']);
105
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:alternatename']);
99
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:alternatename']);
Línea 153... Línea 147...
153
            $picture,
147
            $picture,
154
            $firstname,
148
            $firstname,
155
            $lastname,
149
            $lastname,
156
            $city,
150
            $city,
157
            $country,
151
            $country,
-
 
152
            $lang,
-
 
153
            $timezone,
158
            $description,
154
            $description,
159
            $firstnamephonetic,
155
            $firstnamephonetic,
160
            $lastnamephonetic,
156
            $lastnamephonetic,
161
            $middlename,
157
            $middlename,
162
            $alternatename,
158
            $alternatename,
Línea 185... Línea 181...
185
        $this->assertNotEmpty($picture);
181
        $this->assertNotEmpty($picture);
186
        $this->assertEquals($user->firstname, $firstname);
182
        $this->assertEquals($user->firstname, $firstname);
187
        $this->assertEquals($user->lastname, $lastname);
183
        $this->assertEquals($user->lastname, $lastname);
188
        $this->assertEquals($user->city, $city);
184
        $this->assertEquals($user->city, $city);
189
        $this->assertEquals('United Kingdom', $country);
185
        $this->assertEquals('United Kingdom', $country);
-
 
186
        $this->assertEquals('English ‎(en)‎', $lang);
-
 
187
        $this->assertEquals('Europe/London', $timezone);
190
        $this->assertEquals($user->description, $description);
188
        $this->assertEquals($user->description, $description);
191
        $this->assertEquals($user->firstnamephonetic, $firstnamephonetic);
189
        $this->assertEquals($user->firstnamephonetic, $firstnamephonetic);
192
        $this->assertEquals($user->lastnamephonetic, $lastnamephonetic);
190
        $this->assertEquals($user->lastnamephonetic, $lastnamephonetic);
193
        $this->assertEquals($user->middlename, $middlename);
191
        $this->assertEquals($user->middlename, $middlename);
194
        $this->assertEquals($user->alternatename, $alternatename);
192
        $this->assertEquals($user->alternatename, $alternatename);
Línea 210... Línea 208...
210
        $this->assertEquals('Horses', $tag);
208
        $this->assertEquals('Horses', $tag);
211
        $this->assertEquals($cohort->name, $cohortname);
209
        $this->assertEquals($cohort->name, $cohortname);
212
    }
210
    }
Línea 213... Línea 211...
213
 
211
 
-
 
212
    /**
-
 
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
 
214
    /**
262
    /**
215
     * Data provider for {@see test_datasource_filters}
263
     * Data provider for {@see test_datasource_filters}
216
     *
264
     *
217
     * @return array[]
265
     * @return array[]
218
     */
266
     */
219
    public function datasource_filters_provider(): array {
267
    public static function datasource_filters_provider(): array {
220
        return [
268
        return [
221
            // User.
269
            // User.
222
            'Filter user' => ['user:userselect', [
270
            'Filter user' => ['user:userselect', [
223
                'user:userselect_operator' => user_filter::USER_SELECT,
271
                'user:userselect_operator' => user_filter::USER_SELECT,
Línea 229... Línea 277...
229
            ], true],
277
            ], true],
230
            'Filter fullname (no match)' => ['user:fullname', [
278
            'Filter fullname (no match)' => ['user:fullname', [
231
                'user:fullname_operator' => text::CONTAINS,
279
                'user:fullname_operator' => text::CONTAINS,
232
                'user:fullname_value' => 'Alfie',
280
                'user:fullname_value' => 'Alfie',
233
            ], false],
281
            ], false],
-
 
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],
234
            'Filter firstname' => ['user:firstname', [
288
            'Filter firstname' => ['user:firstname', [
235
                'user:firstname_operator' => text::IS_EQUAL_TO,
289
                'user:firstname_operator' => text::IS_EQUAL_TO,
236
                'user:firstname_value' => 'Zoe',
290
                'user:firstname_value' => 'Zoe',
237
            ], true],
291
            ], true],
238
            'Filter firstname (no match)' => ['user:firstname', [
292
            'Filter firstname (no match)' => ['user:firstname', [
Línea 325... Línea 379...
325
            ], true],
379
            ], true],
326
            'Filter country (no match)' => ['user:country', [
380
            'Filter country (no match)' => ['user:country', [
327
                'user:country_operator' => select::EQUAL_TO,
381
                'user:country_operator' => select::EQUAL_TO,
328
                'user:country_value' => 'AU',
382
                'user:country_value' => 'AU',
329
            ], false],
383
            ], false],
-
 
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],
330
            'Filter theme' => ['user:theme', [
400
            'Filter theme' => ['user:theme', [
331
                'user:theme_operator' => select::EQUAL_TO,
401
                'user:theme_operator' => select::EQUAL_TO,
332
                'user:theme_value' => 'boost',
402
                'user:theme_value' => 'boost',
333
            ], true],
403
            ], true],
334
            'Filter theme (no match)' => ['user:theme', [
404
            'Filter theme (no match)' => ['user:theme', [
Línea 485... Línea 555...
485
            'phone1' => '111',
555
            'phone1' => '111',
486
            'phone2' => '222',
556
            'phone2' => '222',
487
            'address' => 'Big Farm',
557
            'address' => 'Big Farm',
488
            'city' => 'Barcelona',
558
            'city' => 'Barcelona',
489
            'country' => 'ES',
559
            'country' => 'ES',
-
 
560
            'lang' => 'en',
-
 
561
            'timezone' => 'Europe/Barcelona',
490
            'theme' => 'boost',
562
            'theme' => 'boost',
491
            'description' => 'Hello there',
563
            'description' => 'Hello there',
492
            'moodlenetprofile' => '@zoe1@example.com',
564
            'moodlenetprofile' => '@zoe1@example.com',
493
            'interests' => ['Horses'],
565
            'interests' => ['Horses'],
494
            'lastip' => '0.0.0.0',
566
            'lastip' => '0.0.0.0',
Línea 499... Línea 571...
499
 
571
 
500
        /** @var core_reportbuilder_generator $generator */
572
        /** @var core_reportbuilder_generator $generator */
Línea 501... Línea 573...
501
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
573
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
502
 
574
 
503
        // Create report containing single column, and given filter.
575
        // Create report containing single column, and given filter.
Línea 504... Línea 576...
504
        $report = $generator->create_report(['name' => 'Tasks', 'source' => users::class, 'default' => 0]);
576
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
505
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
577
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']);
506
 
578