Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 17... Línea 17...
17
declare(strict_types=1);
17
declare(strict_types=1);
Línea 18... Línea 18...
18
 
18
 
Línea 19... Línea 19...
19
namespace core_reportbuilder\local\helpers;
19
namespace core_reportbuilder\local\helpers;
20
 
-
 
21
use core_reportbuilder_generator;
20
 
22
use core_reportbuilder_testcase;
21
use core_reportbuilder_generator;
23
use core_reportbuilder\local\entities\user;
-
 
24
use core_reportbuilder\local\filters\boolean_select;
-
 
25
use core_reportbuilder\local\filters\date;
-
 
26
use core_reportbuilder\local\filters\select;
22
use core_reportbuilder\local\entities\user;
27
use core_reportbuilder\local\filters\text;
23
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
28
use core_reportbuilder\local\report\column;
24
use core_reportbuilder\local\report\{column, filter};
Línea 29... Línea -...
29
use core_reportbuilder\local\report\filter;
-
 
30
use core_user\reportbuilder\datasource\users;
-
 
31
 
-
 
32
defined('MOODLE_INTERNAL') || die();
-
 
33
 
-
 
34
global $CFG;
25
use core_reportbuilder\tests\core_reportbuilder_testcase;
35
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
26
use core_user\reportbuilder\datasource\users;
36
 
27
 
37
/**
28
/**
38
 * Unit tests for user profile fields helper
29
 * Unit tests for user profile fields helper
39
 *
30
 *
40
 * @package     core_reportbuilder
31
 * @package     core_reportbuilder
41
 * @covers      \core_reportbuilder\local\helpers\user_profile_fields
32
 * @covers      \core_reportbuilder\local\helpers\user_profile_fields
42
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
33
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
Línea 43... Línea 34...
43
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 */
35
 */
45
class user_profile_fields_test extends core_reportbuilder_testcase {
36
final class user_profile_fields_test extends core_reportbuilder_testcase {
46
 
37
 
47
    /**
38
    /**
48
     * Generate custom profile fields, one of each type
39
     * Generate custom profile fields, one of each type
49
     *
40
     *
50
     * @return user_profile_fields
41
     * @return user_profile_fields
Línea -... Línea 42...
-
 
42
     */
51
     */
43
    private function generate_userprofilefields(): user_profile_fields {
52
    private function generate_userprofilefields(): user_profile_fields {
44
        $this->getDataGenerator()->create_custom_profile_field([
-
 
45
            'shortname' => 'checkbox', 'name' => 'Checkbox field', 'datatype' => 'checkbox', 'defaultdata' => 1]);
Línea 53... Línea 46...
53
        $this->getDataGenerator()->create_custom_profile_field([
46
 
54
            'shortname' => 'checkbox', 'name' => 'Checkbox field', 'datatype' => 'checkbox']);
47
        // This field is available only to admins.
-
 
48
        $this->getDataGenerator()->create_custom_profile_field([
Línea 55... Línea 49...
55
 
49
            'shortname' => 'datetime', 'name' => 'Date field', 'datatype' => 'datetime', 'param2' => 2022, 'param3' => 0,
56
        $this->getDataGenerator()->create_custom_profile_field([
50
                'defaultdata' => 0, 'visible' => PROFILE_VISIBLE_NONE]);
Línea 57... Línea 51...
57
            'shortname' => 'datetime', 'name' => 'Date field', 'datatype' => 'datetime', 'param2' => 2022, 'param3' => 0]);
51
 
58
 
52
        $this->getDataGenerator()->create_custom_profile_field([
Línea 59... Línea 53...
59
        $this->getDataGenerator()->create_custom_profile_field([
53
            'shortname' => 'menu', 'name' => 'Menu field', 'datatype' => 'menu', 'param1' => "Cat\nDog\nFish",
60
            'shortname' => 'menu', 'name' => 'Menu field', 'datatype' => 'menu', 'param1' => "Cat\nDog"]);
54
                'defaultdata' => 'Cat']);
Línea 61... Línea 55...
61
 
55
 
62
        $this->getDataGenerator()->create_custom_profile_field([
56
        $this->getDataGenerator()->create_custom_profile_field([
Línea 63... Línea 57...
63
            'shortname' => 'Social', 'name' => 'msn', 'datatype' => 'social', 'param1' => 'msn']);
57
            'shortname' => 'Social', 'name' => 'msn', 'datatype' => 'social', 'param1' => 'msn']);
64
 
58
 
65
        $this->getDataGenerator()->create_custom_profile_field([
59
        $this->getDataGenerator()->create_custom_profile_field([
Línea 66... Línea 60...
66
            'shortname' => 'text', 'name' => 'Text field', 'datatype' => 'text']);
60
            'shortname' => 'text', 'name' => 'Text field', 'datatype' => 'text', 'defaultdata' => 'default']);
67
 
61
 
68
        $this->getDataGenerator()->create_custom_profile_field([
62
        $this->getDataGenerator()->create_custom_profile_field([
69
            'shortname' => 'textarea', 'name' => 'Textarea field', 'datatype' => 'textarea']);
63
            'shortname' => 'textarea', 'name' => 'Textarea field', 'datatype' => 'textarea', 'defaultdata' => 'Default']);
70
 
64
 
-
 
65
        $userentity = new user();
Línea -... Línea 66...
-
 
66
        $useralias = $userentity->get_table_alias('user');
71
        $userentity = new user();
67
 
-
 
68
        // Create an instance of the userprofilefield helper.
72
        $useralias = $userentity->get_table_alias('user');
69
        return new user_profile_fields("{$useralias}.id", $userentity->get_entity_name());
-
 
70
    }
-
 
71
 
Línea 73... Línea -...
73
 
-
 
74
        // Create an instance of the userprofilefield helper.
72
    /**
75
        return new user_profile_fields("$useralias.id", $userentity->get_entity_name());
73
     * Test for get_columns
76
    }
-
 
77
 
74
     */
78
    /**
75
    public function test_get_columns(): void {
79
     * Test for get_columns
-
 
80
     */
76
        $this->resetAfterTest();
81
    public function test_get_columns(): void {
77
        $this->setAdminUser();
Línea 82... Línea 78...
82
        $this->resetAfterTest();
78
 
83
 
79
        // Get pre-existing user profile fields.
84
        $userentity = new user();
-
 
Línea 85... Línea 80...
85
        $useralias = $userentity->get_table_alias('user');
80
        $userentity = new user();
-
 
81
        $initialcolumns = (new user_profile_fields(
86
 
82
            $userentity->get_table_alias('user') . '.id',
87
        // Get pre-existing user profile fields.
83
            $userentity->get_entity_name(),
Línea 88... Línea 84...
88
        $initialuserprofilefields = new user_profile_fields("$useralias.id", $userentity->get_entity_name());
84
        ))->get_columns();
89
        $initialcolumns = $initialuserprofilefields->get_columns();
-
 
90
        $initialcolumntitles = array_map(static function(column $column): string {
-
 
91
            return $column->get_title();
85
 
92
        }, $initialcolumns);
-
 
93
        $initialcolumntypes = array_map(static function(column $column): int {
86
        // Create a field which will duplicate one of the subsequently generated fields (case-insensitive shortname).
94
            return $column->get_type();
87
        $this->getDataGenerator()->create_custom_profile_field([
95
        }, $initialcolumns);
88
            'shortname' => 'CHECKBOX',
96
 
89
            'name' => 'Duplicate checkbox field',
97
        // Add new custom profile fields.
90
            'datatype' => 'checkbox',
98
        $userprofilefields = $this->generate_userprofilefields();
91
        ]);
99
        $columns = $userprofilefields->get_columns();
92
 
100
 
93
        // Add new custom profile fields.
-
 
94
        $userprofilefields = $this->generate_userprofilefields();
-
 
95
 
Línea 101... Línea 96...
101
        // Columns count should be equal to start + 6.
96
        // Ensure pre-existing fields are ignored in subsequent assertions.
102
        $this->assertCount(count($initialcolumns) + 6, $columns);
-
 
103
        $this->assertContainsOnlyInstancesOf(column::class, $columns);
-
 
104
 
97
        $columns = array_slice($userprofilefields->get_columns(), count($initialcolumns));
105
        // Assert column titles.
-
 
106
        $columntitles = array_map(static function(column $column): string {
98
        $this->assertCount(6, $columns);
107
            return $column->get_title();
99
        $this->assertContainsOnlyInstancesOf(column::class, $columns);
108
        }, $columns);
100
 
109
        $expectedcolumntitles = array_merge($initialcolumntitles, [
101
        // Column titles.
110
            'Checkbox field',
102
        $this->assertEquals([
111
            'Date field',
103
            'Checkbox field',
112
            'Menu field',
104
            'Date field',
113
            'MSN ID',
105
            'Menu field',
-
 
106
            'MSN ID',
114
            'Text field',
107
            'Text field',
Línea -... Línea 108...
-
 
108
            'Textarea field',
-
 
109
        ], array_map(
-
 
110
            fn(column $column): string => $column->get_title(),
115
            'Textarea field',
111
            $columns,
116
        ]);
112
        ));
-
 
113
 
117
        $this->assertEquals($expectedcolumntitles, $columntitles);
114
        // Column types.
-
 
115
        $this->assertEquals([
-
 
116
            column::TYPE_BOOLEAN,
118
 
117
            column::TYPE_TIMESTAMP,
119
        // Assert column types.
118
            column::TYPE_TEXT,
-
 
119
            column::TYPE_TEXT,
Línea -... Línea 120...
-
 
120
            column::TYPE_TEXT,
120
        $columntypes = array_map(static function(column $column): int {
121
            column::TYPE_LONGTEXT,
-
 
122
        ], array_map(
-
 
123
            fn(column $column): int => $column->get_type(),
-
 
124
            $columns,
-
 
125
        ));
-
 
126
 
-
 
127
        // Column sortable.
-
 
128
        $this->assertEquals([
121
            return $column->get_type();
129
            true,
122
        }, $columns);
130
            true,
-
 
131
            true,
Línea 123... Línea 132...
123
        $expectedcolumntypes = array_merge($initialcolumntypes, [
132
            true,
-
 
133
            true,
-
 
134
            true,
-
 
135
        ], array_map(
-
 
136
            fn(column $column): bool => $column->get_is_sortable(),
-
 
137
            $columns,
-
 
138
        ));
-
 
139
 
-
 
140
        // Column available.
-
 
141
        $this->assertEquals([
124
            column::TYPE_BOOLEAN,
142
            true,
125
            column::TYPE_TIMESTAMP,
143
            true,
-
 
144
            true,
126
            column::TYPE_TEXT,
145
            true,
Línea 127... Línea 146...
127
            column::TYPE_TEXT,
146
            true,
128
            column::TYPE_TEXT,
147
            true,
129
            column::TYPE_LONGTEXT,
148
        ], array_map(
130
        ]);
149
            fn(column $column): bool => $column->get_is_available(),
131
        $this->assertEquals($expectedcolumntypes, $columntypes);
150
            $columns,
Línea 132... Línea 151...
132
    }
151
        ));
133
 
-
 
134
    /**
-
 
Línea -... Línea 152...
-
 
152
 
-
 
153
        // Column available, for non-privileged user.
-
 
154
        $this->setUser(null);
-
 
155
        $this->assertEquals([
-
 
156
            true,
-
 
157
            false,
-
 
158
            true,
-
 
159
            true,
-
 
160
            true,
-
 
161
            true,
135
     * Test for add_join
162
        ], array_map(
-
 
163
            fn(column $column): bool => $column->get_is_available(),
136
     */
164
            array_slice($userprofilefields->get_columns(), count($initialcolumns)),
137
    public function test_add_join(): void {
165
        ));
-
 
166
    }
-
 
167
 
-
 
168
    /**
-
 
169
     * Test that joins added to the profile fields helper are present in its columns/filters
-
 
170
     */
-
 
171
    public function test_add_join(): void {
-
 
172
        $this->resetAfterTest();
138
        $this->resetAfterTest();
173
 
Línea 139... Línea 174...
139
 
174
        $userprofilefields = $this->generate_userprofilefields();
140
        $userprofilefields = $this->generate_userprofilefields();
175
 
141
        $columns = $userprofilefields->get_columns();
176
        // We always join on the user info data table.
142
        $this->assertCount(1, ($columns[0])->get_joins());
177
        $columnjoins = $userprofilefields->get_columns()[0]->get_joins();
143
 
178
        $this->assertCount(1, $columnjoins);
-
 
179
        $this->assertStringStartsWith('LEFT JOIN {user_info_data}', $columnjoins[0]);
Línea -... Línea 180...
-
 
180
 
144
        $userprofilefields->add_join('JOIN {test} t ON t.id = id');
181
        $filterjoins = $userprofilefields->get_filters()[0]->get_joins();
-
 
182
        $this->assertCount(1, $filterjoins);
145
        $columns = $userprofilefields->get_columns();
183
        $this->assertStringStartsWith('LEFT JOIN {user_info_data}', $filterjoins[0]);
-
 
184
 
-
 
185
        // Add additional join.
Línea 146... Línea 186...
146
        $this->assertCount(2, ($columns[0])->get_joins());
186
        $userprofilefields->add_join('JOIN {test} t ON t.id = id');
147
    }
187
 
148
 
188
        $columnjoins = $userprofilefields->get_columns()[0]->get_joins();
149
    /**
189
        $this->assertCount(2, $columnjoins);
150
     * Test for add_joins
190
        $this->assertEquals('JOIN {test} t ON t.id = id', $columnjoins[0]);
151
     */
191
        $this->assertStringStartsWith('LEFT JOIN {user_info_data}', $columnjoins[1]);
Línea 152... Línea 192...
152
    public function test_add_joins(): void {
192
 
153
        $this->resetAfterTest();
193
        $filterjoins = $userprofilefields->get_filters()[0]->get_joins();
154
 
-
 
Línea 155... Línea 194...
155
        $userprofilefields = $this->generate_userprofilefields();
194
        $this->assertCount(2, $filterjoins);
-
 
195
        $this->assertEquals('JOIN {test} t ON t.id = id', $filterjoins[0]);
156
        $columns = $userprofilefields->get_columns();
196
        $this->assertStringStartsWith('LEFT JOIN {user_info_data}', $filterjoins[1]);
157
        $this->assertCount(1, ($columns[0])->get_joins());
197
    }
Línea 158... Línea 198...
158
 
198
 
159
        $userprofilefields->add_joins(['JOIN {test} t ON t.id = id', 'JOIN {test2} t2 ON t2.id = id']);
-
 
160
        $columns = $userprofilefields->get_columns();
-
 
161
        $this->assertCount(3, ($columns[0])->get_joins());
199
    /**
162
    }
-
 
163
 
200
     * Test for get_filters
164
    /**
201
     */
165
     * Test for get_filters
202
    public function test_get_filters(): void {
166
     */
203
        $this->resetAfterTest();
167
    public function test_get_filters(): void {
204
        $this->setAdminUser();
168
        $this->resetAfterTest();
205
 
-
 
206
        // Get pre-existing user profile fields.
-
 
207
        $userentity = new user();
-
 
208
        $initialfilters = (new user_profile_fields(
169
 
209
            $userentity->get_table_alias('user') . '.id',
-
 
210
            $userentity->get_entity_name(),
-
 
211
        ))->get_filters();
-
 
212
 
-
 
213
        // Create a field which will duplicate one of the subsequently generated fields (case-insensitive shortname).
-
 
214
        $this->getDataGenerator()->create_custom_profile_field([
-
 
215
            'shortname' => 'CHECKBOX',
-
 
216
            'name' => 'Duplicate checkbox field',
-
 
217
            'datatype' => 'checkbox',
-
 
218
        ]);
-
 
219
 
170
        $userentity = new user();
220
        // Add new custom profile fields.
-
 
221
        $userprofilefields = $this->generate_userprofilefields();
-
 
222
 
-
 
223
        // Ensure pre-existing fields are ignored in subsequent assertions.
-
 
224
        $filters = array_slice($userprofilefields->get_filters(), count($initialfilters));
-
 
225
        $this->assertCount(6, $filters);
-
 
226
        $this->assertContainsOnlyInstancesOf(filter::class, $filters);
-
 
227
 
-
 
228
        // Filter headers.
-
 
229
        $this->assertEquals([
-
 
230
            'Checkbox field',
-
 
231
            'Date field',
-
 
232
            'Menu field',
-
 
233
            'MSN ID',
-
 
234
            'Text field',
-
 
235
            'Textarea field',
-
 
236
        ], array_map(
-
 
237
            fn(filter $filter): string => $filter->get_header(),
-
 
238
            $filters,
-
 
239
        ));
-
 
240
 
-
 
241
        // Filter types.
-
 
242
        $this->assertEquals([
-
 
243
            boolean_select::class,
-
 
244
            date::class,
-
 
245
            select::class,
-
 
246
            text::class,
-
 
247
            text::class,
-
 
248
            text::class,
-
 
249
        ], array_map(
171
        $useralias = $userentity->get_table_alias('user');
250
            fn(filter $filter) => $filter->get_filter_class(),
Línea 172... Línea 251...
172
 
251
            $filters,
173
        // Get pre-existing user profile fields.
252
        ));
174
        $initialuserprofilefields = new user_profile_fields("$useralias.id", $userentity->get_entity_name());
253
 
175
        $initialfilters = $initialuserprofilefields->get_filters();
254
        // Filter available.
176
        $initialfilterheaders = array_map(static function(filter $filter): string {
255
        $this->assertEquals([
177
            return $filter->get_header();
-
 
178
        }, $initialfilters);
256
            true,
Línea 179... Línea 257...
179
 
257
            true,
-
 
258
            true,
180
        // Add new custom profile fields.
259
            true,
181
        $userprofilefields = $this->generate_userprofilefields();
260
            true,
182
        $filters = $userprofilefields->get_filters();
261
            true,
183
 
262
        ], array_map(
184
        // Filters count should be equal to start + 6.
263
            fn(filter $filter): bool => $filter->get_is_available(),
185
        $this->assertCount(count($initialfilters) + 6, $filters);
264
            $filters,
186
        $this->assertContainsOnlyInstancesOf(filter::class, $filters);
265
        ));
187
 
266
 
188
        // Assert filter headers.
267
        // Filter available, for non-privileged user.
Línea 189... Línea 268...
189
        $filterheaders = array_map(static function(filter $filter): string {
268
        $this->setUser(null);
190
            return $filter->get_header();
269
        $this->assertEquals([
191
        }, $filters);
270
            true,
Línea 192... Línea 271...
192
        $expectedfilterheaders = array_merge($initialfilterheaders, [
271
            false,
193
            'Checkbox field',
272
            true,
194
            'Date field',
273
            true,
195
            'Menu field',
274
            true,
196
            'MSN ID',
275
            true,
197
            'Text field',
276
        ], array_map(
198
            'Textarea field',
277
            fn(filter $filter): bool => $filter->get_is_available(),
199
        ]);
278
            array_slice($userprofilefields->get_filters(), count($initialfilters)),
Línea 200... Línea -...
200
        $this->assertEquals($expectedfilterheaders, $filterheaders);
-
 
201
    }
-
 
202
 
-
 
203
    /**
279
        ));
204
     * Test that adding user profile field columns to a report returns expected values
280
    }
205
     */
281
 
206
    public function test_custom_report_content(): void {
282
    /**
207
        $this->resetAfterTest();
283
     * Test that adding user profile field columns to a report returns expected values
208
 
284
     */
209
        $userprofilefields = $this->generate_userprofilefields();
285
    public function test_custom_report_content(): void {
210
 
286
        $this->resetAfterTest();
211
        // Create test subject with user profile fields content.
287
        $this->setAdminUser();
212
        $user = $this->getDataGenerator()->create_user([
288
 
213
            'firstname' => 'Zebedee',
289
        // Create test subject with user profile fields content.
214
            'profile_field_checkbox' => true,
290
        $this->generate_userprofilefields();
215
            'profile_field_datetime' => '2021-12-09',
291
        $this->getDataGenerator()->create_user([
216
            'profile_field_menu' => 'Cat',
292
            'firstname' => 'Zebedee',
217
            'profile_field_Social' => 12345,
293
            'profile_field_checkbox' => 0,
218
            'profile_field_text' => 'Hello',
294
            'profile_field_datetime' => '2021-12-09',
219
            'profile_field_textarea' => 'Goodbye',
295
            'profile_field_menu' => 'Dog',
220
        ]);
296
            'profile_field_Social' => 12345,
221
 
297
            'profile_field_text' => 'Hello',
222
        /** @var core_reportbuilder_generator $generator */
298
            'profile_field_textarea' => 'Goodbye',
223
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
299
        ]);
Línea 224... Línea 300...
224
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
300
 
225
 
301
        /** @var core_reportbuilder_generator $generator */
226
        // Add user profile field columns to the report.
302
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
227
        $firstname = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
303
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
228
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_checkbox']);
304
 
229
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_datetime']);
305
        // Add user profile field columns to the report.
230
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_menu']);
306
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
231
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_social']);
307
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_checkbox']);
232
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_text']);
-
 
233
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_textarea']);
-
 
234
 
-
 
235
        // Sort the report, Admin -> Zebedee for consistency.
308
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_datetime']);
-
 
309
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_menu']);
-
 
310
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_social']);
-
 
311
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_text']);
236
        report::toggle_report_column_sorting($report->get('id'), $firstname->get('id'), true);
312
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_textarea']);
237
 
313
 
238
        $content = $this->get_custom_report_content($report->get('id'));
314
        $content = $this->get_custom_report_content($report->get('id'));
239
        $this->assertEquals([
315
        $this->assertEquals([
240
            [
316
            [
241
                'c0_firstname' => 'Admin',
317
                'Admin',
242
                'c1_data' => '',
318
                'Yes',
-
 
319
                'Not set',
243
                'c2_data' => '',
320
                'Cat',
244
                'c3_data' => '',
321
                '',
245
                'c4_data' => '',
322
                'default',
246
                'c5_data' => '',
323
                format_text('Default', options: ['overflowdiv' => true]),
247
                'c6_data' => '',
324
            ], [
248
            ], [
325
                'Zebedee',
249
                'c0_firstname' => 'Zebedee',
326
                'No',
250
                'c1_data' => 'Yes',
327
                '9 December 2021',
251
                'c2_data' => '9 December 2021',
328
                'Dog',
-
 
329
                '12345',
-
 
330
                'Hello',
-
 
331
                format_text('Goodbye', options: ['overflowdiv' => true]),
-
 
332
            ],
252
                'c3_data' => 'Cat',
333
        ], array_map('array_values', $content));
253
                'c4_data' => '12345',
334
    }
254
                'c5_data' => 'Hello',
335
 
255
                'c6_data' => '<div class="no-overflow">Goodbye</div>',
336
    /**
256
            ],
337
     * Data provider for {@see test_custom_report_filter}
257
        ], $content);
338
     *
-
 
339
     * @return array[]
258
    }
340
     */
259
 
341
    public static function custom_report_filter_provider(): array {
260
    /**
342
        return [
261
     * Data provider for {@see test_custom_report_filter}
343
            'Filter by checkbox profile field' => ['user:profilefield_checkbox', [
262
     *
344
                'user:profilefield_checkbox_operator' => boolean_select::NOT_CHECKED,
263
     * @return array[]
345
            ], 'testuser'],
264
     */
346
            'Filter by checkbox profile field (default)' => ['user:profilefield_checkbox', [
265
    public function custom_report_filter_provider(): array {
347
                'user:profilefield_checkbox_operator' => boolean_select::CHECKED,
266
        return [
348
            ], 'admin'],
-
 
349
            'Filter by datetime profile field' => ['user:profilefield_datetime', [
-
 
350
                'user:profilefield_datetime_operator' => date::DATE_RANGE,
-
 
351
                'user:profilefield_datetime_from' => 1622502000,
-
 
352
            ], 'testuser'],
267
            'Filter by checkbox profile field' => ['user:profilefield_checkbox', [
353
            'Filter by datetime profile field (no match)' => ['user:profilefield_datetime', [
268
                'user:profilefield_checkbox_operator' => boolean_select::CHECKED,
354
                'user:profilefield_datetime_operator' => date::DATE_RANGE,
269
            ], 'testuser'],
355
                'user:profilefield_datetime_from' => 1672531200,
270
            'Filter by checkbox profile field (empty)' => ['user:profilefield_checkbox', [
356
            ]],
271
                'user:profilefield_checkbox_operator' => boolean_select::NOT_CHECKED,
357
            'Filter by menu profile field' => ['user:profilefield_menu', [
272
            ], 'admin'],
358
                'user:profilefield_menu_operator' => select::EQUAL_TO,
273
            'Filter by datetime profile field' => ['user:profilefield_datetime', [
359
                'user:profilefield_menu_value' => 'Dog',
274
                'user:profilefield_datetime_operator' => date::DATE_RANGE,
360
            ], 'testuser'],
-
 
361
            'Filter by menu profile field (default)' => ['user:profilefield_menu', [
-
 
362
                'user:profilefield_menu_operator' => select::EQUAL_TO,
-
 
363
                'user:profilefield_menu_value' => 'Cat',
-
 
364
            ], 'admin'],
275
                'user:profilefield_datetime_from' => 1622502000,
365
            'Filter by menu profile field (no match)' => ['user:profilefield_menu', [
276
            ], 'testuser'],
366
                'user:profilefield_menu_operator' => select::EQUAL_TO,
Línea 277... Línea 367...
277
            'Filter by datetime profile field (empty)' => ['user:profilefield_datetime', [
367
                'user:profilefield_menu_value' => 'Fish',
278
                'user:profilefield_datetime_operator' => date::DATE_EMPTY,
368
            ]],
279
            ], 'admin'],
369
            'Filter by social profile field' => ['user:profilefield_social', [
280
            'Filter by menu profile field' => ['user:profilefield_menu', [
370
                'user:profilefield_social_operator' => text::IS_EQUAL_TO,
281
                'user:profilefield_menu_operator' => select::EQUAL_TO,
371
                'user:profilefield_social_value' => '12345',
282
                'user:profilefield_menu_value' => 'Dog',
372
            ], 'testuser'],
283
            ], 'testuser'],
373
            'Filter by social profile field (no match)' => ['user:profilefield_social', [
284
            'Filter by menu profile field (empty)' => ['user:profilefield_menu', [
374
                'user:profilefield_social_operator' => text::IS_EQUAL_TO,
285
                'user:profilefield_menu_operator' => select::NOT_EQUAL_TO,
375
                'user:profilefield_social_value' => '54321',
286
                'user:profilefield_menu_value' => 'Dog',
376
            ]],
287
            ], 'admin'],
377
            'Filter by text profile field' => ['user:profilefield_text', [
288
            'Filter by social profile field' => ['user:profilefield_social', [
-
 
289
                'user:profilefield_social_operator' => text::IS_EQUAL_TO,
378
                'user:profilefield_text_operator' => text::IS_EQUAL_TO,
Línea 290... Línea 379...
290
                'user:profilefield_social_value' => '12345',
379
                'user:profilefield_text_value' => 'Hello',
-
 
380
            ], 'testuser'],
291
            ], 'testuser'],
381
            'Filter by text profile field (default)' => ['user:profilefield_text', [
292
            'Filter by social profile field (empty)' => ['user:profilefield_social', [
382
                'user:profilefield_text_operator' => text::IS_EQUAL_TO,
293
                'user:profilefield_social_operator' => text::IS_EMPTY,
383
                'user:profilefield_text_value' => 'default',
294
            ], 'admin'],
384
            ], 'admin'],
295
            'Filter by text profile field' => ['user:profilefield_text', [
385
            'Filter by text profile field (no match)' => ['user:profilefield_text', [
296
                'user:profilefield_text_operator' => text::IS_EQUAL_TO,
386
                'user:profilefield_text_operator' => text::IS_EQUAL_TO,
297
                'user:profilefield_text_value' => 'Hello',
387
                'user:profilefield_text_value' => 'hola',
298
            ], 'testuser'],
388
            ]],
Línea 345... Línea 435...
345
 
435
 
346
        // Add filter, set it's values.
436
        // Add filter, set it's values.
347
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
437
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
Línea -... Línea 438...
-
 
438
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
348
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
439
 
349
 
440
        if ($expectmatch !== null) {
-
 
441
            $this->assertCount(1, $content);
-
 
442
            $this->assertEquals($expectmatch, reset($content[0]));
-
 
443
        } else {
350
        $this->assertCount(1, $content);
444
            $this->assertEmpty($content);
Línea 351... Línea 445...
351
        $this->assertEquals($expectmatchuser, reset($content[0]));
445
        }
352
    }
446
    }
353
 
447
 
Línea 361... Línea 455...
361
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
455
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
362
        }
456
        }
Línea 363... Línea 457...
363
 
457
 
Línea 364... Línea 458...
364
        $this->resetAfterTest();
458
        $this->resetAfterTest();
365
 
459
 
366
        $userprofilefields = $this->generate_userprofilefields();
460
        $this->generate_userprofilefields();
367
        $user = $this->getDataGenerator()->create_user([
461
        $this->getDataGenerator()->create_user([
368
            'profile_field_checkbox' => true,
462
            'profile_field_checkbox' => true,
369
            'profile_field_datetime' => '2021-12-09',
463
            'profile_field_datetime' => '2021-12-09',
370
            'profile_field_menu' => 'Dog',
464
            'profile_field_menu' => 'Dog',