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_files\reportbuilder\datasource;
20
 
21
use core\context\{course, coursecat, user};
22
use core_reportbuilder_generator;
23
use core_reportbuilder\local\filters\{boolean_select, date, filesize, select, text};
1441 ariadna 24
use core_reportbuilder\tests\core_reportbuilder_testcase;
1 efrain 25
 
26
/**
27
 * Unit tests for files datasource
28
 *
29
 * @package     core_files
30
 * @covers      \core_files\reportbuilder\datasource\files
31
 * @copyright   2022 Paul Holden <paulh@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
1441 ariadna 34
final class files_test extends core_reportbuilder_testcase {
1 efrain 35
 
36
    /**
37
     * Test default datasource
38
     */
39
    public function test_datasource_default(): void {
40
        $this->resetAfterTest();
41
 
42
        $course = $this->getDataGenerator()->create_course();
43
        $coursecontext = course::instance($course->id);
44
 
45
        $user = $this->getDataGenerator()->create_user();
46
        $usercontext = user::instance($user->id);
47
 
48
        $this->setUser($user);
49
 
50
        $this->generate_test_files($coursecontext);
51
 
52
        /** @var core_reportbuilder_generator $generator */
53
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
54
        $report = $generator->create_report(['name' => 'Files', 'source' => files::class, 'default' => 1]);
55
 
56
        $content = $this->get_custom_report_content($report->get('id'));
57
        $content = $this->filter_custom_report_content($content, static function(array $row): bool {
58
            return $row['c0_ctxid'] !== 'System';
59
        });
60
 
61
        $this->assertCount(2, $content);
62
 
63
        // Default columns are context, user, name, type, size, time created. Sorted by context and time created.
64
        [$contextname, $userfullname, $filename, $mimetype, $filesize, $timecreated] = array_values($content[0]);
65
        $this->assertEquals($coursecontext->get_context_name(), $contextname);
66
        $this->assertEquals(fullname($user), $userfullname);
67
        $this->assertEquals('Hello.txt', $filename);
68
        $this->assertEquals('Text file', $mimetype);
69
        $this->assertEquals("5\xc2\xa0bytes", $filesize);
70
        $this->assertNotEmpty($timecreated);
71
 
72
        [$contextname, $userfullname, $filename, $mimetype, $filesize, $timecreated] = array_values($content[1]);
73
        $this->assertEquals($usercontext->get_context_name(), $contextname);
74
        $this->assertEquals(fullname($user), $userfullname);
75
        $this->assertEquals('Hello.txt', $filename);
76
        $this->assertEquals('Text file', $mimetype);
77
        $this->assertEquals("5\xc2\xa0bytes", $filesize);
78
        $this->assertNotEmpty($timecreated);
79
    }
80
 
81
    /**
82
     * Test datasource columns that aren't added by default
83
     */
84
    public function test_datasource_non_default_columns(): void {
85
        global $OUTPUT;
86
 
87
        $this->resetAfterTest();
88
 
89
        $category = $this->getDataGenerator()->create_category();
90
        $categorycontext = coursecat::instance($category->id);
91
 
92
        $course = $this->getDataGenerator()->create_course(['category' => $category->id]);
93
        $coursecontext = course::instance($course->id);
94
 
95
        $user = $this->getDataGenerator()->create_user();
96
        $usercontext = user::instance($user->id);
97
 
98
        $this->setUser($user);
99
 
100
        $draftitemid = $this->generate_test_files($coursecontext);
101
 
102
        /** @var core_reportbuilder_generator $generator */
103
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
104
        $report = $generator->create_report(['name' => 'Files', 'source' => files::class, 'default' => 0]);
105
 
106
        // Consistent order, sorted by context and content hash.
107
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:link',
108
            'sortenabled' => 1, 'sortorder' => 1]);
109
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:name']);
110
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:level']);
111
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:path']);
112
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:parent']);
113
 
114
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:icon']);
115
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:path']);
116
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:author']);
117
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:license']);
118
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:contenthash',
119
            'sortenabled' => 1, 'sortorder' => 2]);
120
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:component']);
121
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:area']);
122
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:itemid']);
123
 
124
        $content = $this->get_custom_report_content($report->get('id'));
125
        $content = $this->filter_custom_report_content($content, static function(array $row): bool {
126
            return stripos($row['c0_ctxid'], 'System') === false;
127
        });
128
 
129
        // There should be two entries (directory & file) for each context.
130
        $this->assertEquals([
131
            [
132
                "<a href=\"{$coursecontext->get_url()}\">{$coursecontext->get_context_name()}</a>",
133
                $coursecontext->get_context_name(),
134
                'Course',
135
                $coursecontext->path,
136
                $categorycontext->get_context_name(),
137
                '<img class="icon iconsize-medium" alt="Directory" title="Directory" src="' .
138
                    $OUTPUT->image_url('f/folder')->out() . '" />',
139
                '/',
140
                null,
141
                '',
142
                'da39a3ee5e6b4b0d3255bfef95601890afd80709',
143
                'course',
144
                'summary',
145
                0,
146
            ],
147
            [
148
                "<a href=\"{$coursecontext->get_url()}\">{$coursecontext->get_context_name()}</a>",
149
                $coursecontext->get_context_name(),
150
                'Course',
151
                $coursecontext->path,
152
                $categorycontext->get_context_name(),
153
                '<img class="icon iconsize-medium" alt="Text file" title="Text file" src="' .
154
                    $OUTPUT->image_url('f/text')->out() . '" />',
155
                '/',
156
                null,
157
                '',
158
                'f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0',
159
                'course',
160
                'summary',
161
                0,
162
            ],
163
            [
164
                "<a href=\"{$usercontext->get_url()}\">{$usercontext->get_context_name()}</a>",
165
                $usercontext->get_context_name(),
166
                'User',
167
                $usercontext->path,
168
                'System',
169
                '<img class="icon iconsize-medium" alt="Directory" title="Directory" src="' .
170
                    $OUTPUT->image_url('f/folder')->out() . '" />',
171
                '/',
172
                null,
173
                '',
174
                'da39a3ee5e6b4b0d3255bfef95601890afd80709',
175
                'user',
176
                'draft',
177
                $draftitemid,
178
            ],
179
            [
180
                "<a href=\"{$usercontext->get_url()}\">{$usercontext->get_context_name()}</a>",
181
                $usercontext->get_context_name(),
182
                'User',
183
                $usercontext->path,
184
                'System',
185
                '<img class="icon iconsize-medium" alt="Text file" title="Text file" src="' .
186
                    $OUTPUT->image_url('f/text')->out() . '" />',
187
                '/',
188
                null,
189
                '',
190
                'f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0',
191
                'user',
192
                'draft',
193
                $draftitemid,
194
            ],
195
        ], array_map('array_values', $content));
196
    }
197
 
198
    /**
199
     * Data provider for {@see test_datasource_filters}
200
     *
201
     * @return array[]
202
     */
1441 ariadna 203
    public static function datasource_filters_provider(): array {
1 efrain 204
        return [
205
            // File.
206
            'Filter directory' => ['file:directory', [
207
                'file:directory_operator' => boolean_select::CHECKED,
208
            ], 2],
209
            'Filter draft' => ['file:draft', [
210
                'file:draft_operator' => boolean_select::CHECKED,
211
            ], 2],
212
            'Filter name' => ['file:name', [
213
                'file:name_operator' => text::IS_EQUAL_TO,
214
                'file:name_value' => 'Hello.txt',
215
            ], 2],
216
            'Filter size' => ['file:size', [
217
                'file:size_operator' => filesize::GREATER_THAN,
218
                'file:size_value1' => 2,
219
                'file:size_unit' => filesize::SIZE_UNIT_BYTE,
220
            ], 2],
221
            'Filter type' => ['file:type', [
222
                'file:type_operator' => select::EQUAL_TO,
223
                'file:type_value' => 'text/plain',
224
            ], 2],
225
            'Filter type (non match)' => ['file:type', [
226
                'file:type_operator' => select::EQUAL_TO,
227
                'file:type_value' => 'image/png',
228
            ], 0],
1441 ariadna 229
            'Filter author' => ['file:author', [
230
                'file:author_operator' => text::IS_EMPTY,
231
            ], 4],
232
            'Filter author (non match)' => ['file:author', [
233
                'file:author_operator' => text::IS_NOT_EMPTY,
234
            ], 0],
1 efrain 235
            'Filter license' => ['file:license', [
236
                'file:license_operator' => select::EQUAL_TO,
237
                'file:license_value' => 'unknown',
238
            ], 4],
239
            'Filter license (non match)' => ['file:license', [
240
                'file:license_operator' => select::EQUAL_TO,
241
                'file:license_value' => 'public',
242
            ], 0],
243
            'Filter content hash' => ['file:contenthash', [
244
                'file:contenthash_operator' => text::IS_EQUAL_TO,
245
                'file:contenthash_value' => 'f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0',
246
            ], 2],
247
            'Filter content hash (no match)' => ['file:contenthash', [
248
                'file:contenthash_operator' => text::IS_EQUAL_TO,
249
                'file:contenthash_value' => 'f00f',
250
            ], 0],
1441 ariadna 251
            'Filter component' => ['file:component', [
252
                'file:component_operator' => text::IS_EQUAL_TO,
253
                'file:component_value' => 'course',
254
            ], 2],
255
            'Filter component (no match)' => ['file:component', [
256
                'file:component_operator' => text::IS_EQUAL_TO,
257
                'file:component_value' => 'reportbuilder',
258
            ], 0],
259
            'Filter area' => ['file:area', [
260
                'file:area_operator' => text::IS_EQUAL_TO,
261
                'file:area_value' => 'summary',
262
            ], 2],
263
            'Filter area (no match)' => ['file:area', [
264
                'file:area_operator' => text::IS_EQUAL_TO,
265
                'file:area_value' => 'report',
266
            ], 0],
1 efrain 267
            'Filter time created' => ['file:timecreated', [
268
                'file:timecreated_operator' => date::DATE_RANGE,
269
                'file:timecreated_from' => 1622502000,
270
            ], 4],
271
            'Filter time created (non match)' => ['file:timecreated', [
272
                'file:timecreated_operator' => date::DATE_RANGE,
273
                'file:timecreated_to' => 1622502000,
274
            ], 0],
275
 
276
            // Context.
277
            'Context level' => ['context:level', [
278
                'context:level_operator' => select::EQUAL_TO,
279
                'context:level_value' => CONTEXT_COURSE,
280
            ], 2],
281
            'Context level (no match)' => ['context:level', [
282
                'context:level_operator' => select::EQUAL_TO,
283
                'context:level_value' => CONTEXT_BLOCK,
284
            ], 0],
285
            'Context path' => ['context:path', [
286
                'context:path_operator' => text::STARTS_WITH,
287
                'context:path_value' => '/1/',
288
            ], 4],
289
            'Context path (no match)' => ['context:path', [
290
                'context:path_operator' => text::STARTS_WITH,
291
                'context:path_value' => '/1/2/3/',
292
            ], 0],
293
 
294
            // User.
295
            'Filter user' => ['user:username', [
296
                'user:username_operator' => text::IS_EQUAL_TO,
297
                'user:username_value' => 'alfie',
298
            ], 4],
299
            'Filter user (no match)' => ['user:username', [
300
                'user:username_operator' => text::IS_EQUAL_TO,
301
                'user:username_value' => 'lionel',
302
            ], 0],
303
        ];
304
    }
305
 
306
    /**
307
     * Test datasource filters
308
     *
309
     * @param string $filtername
310
     * @param array $filtervalues
311
     * @param int $expectmatchcount
312
     *
313
     * @dataProvider datasource_filters_provider
314
     */
315
    public function test_datasource_filters(
316
        string $filtername,
317
        array $filtervalues,
318
        int $expectmatchcount
319
    ): void {
320
        $this->resetAfterTest();
321
        $this->setAdminUser();
322
 
323
        $user = $this->getDataGenerator()->create_user(['username' => 'alfie']);
324
        $this->setUser($user);
325
 
326
        $course = $this->getDataGenerator()->create_course();
327
        $coursecontext = course::instance($course->id);
328
 
329
        $this->generate_test_files($coursecontext);
330
 
331
        /** @var core_reportbuilder_generator $generator */
332
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
333
 
334
        // Create report containing single column, and given filter.
335
        $report = $generator->create_report(['name' => 'Files', 'source' => files::class, 'default' => 0]);
336
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:name']);
337
 
338
        // Add filter, set it's values.
339
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
340
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
341
        $content = $this->filter_custom_report_content($content, static function(array $row): bool {
342
            return stripos($row['c0_ctxid'], 'System') === false;
343
        });
344
 
345
        $this->assertCount($expectmatchcount, $content);
346
    }
347
 
348
    /**
349
     * Stress test datasource
350
     *
351
     * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
352
     */
353
    public function test_stress_datasource(): void {
354
        if (!PHPUNIT_LONGTEST) {
355
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
356
        }
357
 
358
        $this->resetAfterTest();
359
        $this->setAdminUser();
360
 
361
        $course = $this->getDataGenerator()->create_course();
362
        $coursecontext = course::instance($course->id);
363
 
364
        $this->generate_test_files($coursecontext);
365
 
366
        $this->datasource_stress_test_columns(files::class);
367
        $this->datasource_stress_test_columns_aggregation(files::class);
368
        $this->datasource_stress_test_conditions(files::class, 'file:path');
369
    }
370
 
371
    /**
372
     * Ensuring report content only includes files we have explicitly created within the test
373
     *
374
     * @param array $content
375
     * @param callable $callback
376
     * @return array
377
     */
378
    protected function filter_custom_report_content(array $content, callable $callback): array {
379
        $content = array_filter($content, $callback);
380
        return array_values($content);
381
    }
382
 
383
    /**
384
     * Helper method to generate some test files (a user draft and course summary file) for reporting on
385
     *
386
     * @param course $context
387
     * @return int Draft item ID
388
     */
389
    protected function generate_test_files(course $context): int {
390
        global $USER;
391
 
392
        $draftitemid = file_get_unused_draft_itemid();
393
 
394
        // Populate user draft.
395
        get_file_storage()->create_file_from_string([
396
            'contextid' => user::instance($USER->id)->id,
397
            'userid' => $USER->id,
398
            'component' => 'user',
399
            'filearea' => 'draft',
400
            'itemid' => $draftitemid,
401
            'filepath' => '/',
402
            'filename' => 'Hello.txt',
403
        ], 'Hello');
404
 
405
        // Save draft to course summary file area.
406
        file_save_draft_area_files($draftitemid, $context->id, 'course', 'summary', 0);
407
 
408
        return $draftitemid;
409
    }
410
}