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_role\reportbuilder\datasource;
20
 
21
use core\context\course;
22
use core_reportbuilder_generator;
23
use core_reportbuilder\local\filters\{date, select, text};
1441 ariadna 24
use core_reportbuilder\tests\core_reportbuilder_testcase;
1 efrain 25
 
26
/**
27
 * Unit tests for roles datasource
28
 *
29
 * @package     core_role
1441 ariadna 30
 * @covers      \core_role\reportbuilder\datasource\roles
1 efrain 31
 * @copyright   2024 Paul Holden <paulh@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
final class roles_test extends core_reportbuilder_testcase {
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
        $context = course::instance($course->id);
44
 
45
        $studentone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
46
        $studenttwo = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Amy']);
47
        $manager = $this->getDataGenerator()->create_and_enrol($course, 'manager');
48
 
49
        /** @var core_reportbuilder_generator $generator */
50
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
51
        $report = $generator->create_report(['name' => 'Roles', 'source' => roles::class, 'default' => 1]);
52
 
53
        $content = $this->get_custom_report_content($report->get('id'));
54
        $this->assertCount(3, $content);
55
 
56
        // Default columns are context link, original role name and user link. Sorted by each.
57
        [$contextlink, $rolename, $userlink] = array_values($content[0]);
58
        $this->assertStringContainsString($context->get_context_name(), $contextlink);
59
        $this->assertEquals('Manager', $rolename);
60
        $this->assertStringContainsString(fullname($manager), $userlink);
61
 
62
        [$contextlink, $rolename, $userlink] = array_values($content[1]);
63
        $this->assertStringContainsString($context->get_context_name(), $contextlink);
64
        $this->assertEquals('Student', $rolename);
65
        $this->assertStringContainsString(fullname($studenttwo), $userlink);
66
 
67
        [$contextlink, $rolename, $userlink] = array_values($content[2]);
68
        $this->assertStringContainsString($context->get_context_name(), $contextlink);
69
        $this->assertEquals('Student', $rolename);
70
        $this->assertStringContainsString(fullname($studentone), $userlink);
71
    }
72
 
73
    /**
74
     * Test datasource columns that aren't added by default
75
     */
76
    public function test_datasource_non_default_columns(): void {
77
        global $DB;
78
 
79
        $this->resetAfterTest();
80
 
81
        $course = $this->getDataGenerator()->create_course();
82
        $context = course::instance($course->id);
83
 
84
        // Create an alias for our role.
85
        $roleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
86
        $DB->insert_record('role_names', (object) [
87
            'contextid' => $context->id,
88
            'roleid' => $roleid,
89
            'name' => 'Moocher',
90
        ]);
91
 
92
        $manager = $this->getDataGenerator()->create_user();
93
        $this->getDataGenerator()->enrol_user($manager->id, $course->id, $roleid);
94
 
95
        /** @var core_reportbuilder_generator $generator */
96
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
97
        $report = $generator->create_report(['name' => 'Roles', 'source' => roles::class, 'default' => 0]);
98
 
99
        // Role.
100
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:name']);
101
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:shortname']);
1441 ariadna 102
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:archetype']);
1 efrain 103
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:description']);
104
 
105
        // Role assignment.
106
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role_assignment:timemodified']);
107
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role_assignment:component']);
108
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role_assignment:itemid']);
109
 
110
        $content = $this->get_custom_report_content($report->get('id'));
111
        $this->assertCount(1, $content);
112
 
1441 ariadna 113
        [
114
            $rolename,
115
            $roleshortname,
116
            $rolearchetype,
117
            $roledescription,
118
            $timemodified,
119
            $component,
120
            $itemid,
121
        ] = array_values($content[0]);
1 efrain 122
 
123
        // Role.
124
        $this->assertEquals('Moocher (Manager)', $rolename);
125
        $this->assertEquals('manager', $roleshortname);
1441 ariadna 126
        $this->assertEquals('ARCHETYPE: Manager', $rolearchetype);
1 efrain 127
        $this->assertEquals('Managers can access courses and modify them, but usually do not participate in them.',
128
            $roledescription);
129
 
130
        // Role assignment.
131
        $this->assertNotEmpty($timemodified);
132
        $this->assertEquals('', $component);
133
        $this->assertEquals(0, $itemid);
134
    }
135
 
136
    /**
137
     * Data provider for {@see test_datasource_filters}
138
     *
139
     * @return array[]
140
     */
141
    public static function datasource_filters_provider(): array {
142
        global $DB;
143
 
144
        return [
145
            // Role.
146
            'Filter role name' => ['role:name', [
147
                'role:name_operator' => select::EQUAL_TO,
148
                'role:name_value' => $DB->get_field('role', 'id', ['shortname' => 'student']),
149
            ], true],
150
            'Filter role name (no match)' => ['role:name', [
151
                'role:name_operator' => select::EQUAL_TO,
1441 ariadna 152
                'role:name_value' => $DB->get_field('role', 'id', ['shortname' => 'teacher']),
1 efrain 153
            ], false],
1441 ariadna 154
            'Filter role archetype' => ['role:archetype', [
155
                'role:archetype_operator' => select::EQUAL_TO,
156
                'role:archetype_value' => 'student',
157
            ], true],
158
            'Filter role archetype (no match)' => ['role:archetype', [
159
                'role:archetype_operator' => select::EQUAL_TO,
160
                'role:archetype_value' => 'teacher',
161
            ], false],
1 efrain 162
 
163
            // Role assignment.
164
            'Filter role assignment time modified' => ['role_assignment:timemodified', [
165
                'role_assignment:timemodified_operator' => date::DATE_RANGE,
166
                'role_assignment:timemodified_from' => 1622502000,
167
            ], true],
168
            'Filter role assignment time modified (no match)' => ['role_assignment:timemodified', [
169
                'role_assignment:timemodified_operator' => date::DATE_RANGE,
170
                'role_assignment:timemodified_to' => 1622502000,
171
            ], false],
172
 
173
            // Context.
174
            'Filter context level' => ['context:level', [
175
                'context:level_operator' => select::EQUAL_TO,
176
                'context:level_value' => CONTEXT_COURSE,
177
            ], true],
178
            'Filter context level (no match)' => ['context:level', [
179
                'context:level_operator' => select::EQUAL_TO,
180
                'context:level_value' => CONTEXT_COURSECAT,
181
            ], false],
182
 
183
            // User.
184
            'Filter user firstname' => ['user:firstname', [
185
                'user:firstname_operator' => text::IS_EQUAL_TO,
186
                'user:firstname_value' => 'Zoe',
187
            ], true],
188
            'Filter user firstname (no match)' => ['user:firstname', [
189
                'user:firstname_operator' => text::IS_EQUAL_TO,
190
                'user:firstname_value' => 'Amy',
191
            ], false],
192
        ];
193
    }
194
 
195
    /**
196
     * Test datasource filters
197
     *
198
     * @param string $filtername
199
     * @param array $filtervalues
200
     * @param bool $expectmatch
201
     *
202
     * @dataProvider datasource_filters_provider
203
     */
204
    public function test_datasource_filters(
205
        string $filtername,
206
        array $filtervalues,
207
        bool $expectmatch,
208
    ): void {
209
        $this->resetAfterTest();
210
 
211
        $course = $this->getDataGenerator()->create_course();
212
        $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
213
 
214
        /** @var core_reportbuilder_generator $generator */
215
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
216
 
217
        // Create report containing single column, and given filter.
218
        $report = $generator->create_report(['name' => 'Roles', 'source' => roles::class, 'default' => 0]);
219
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:shortname']);
220
 
221
        // Add filter, set it's values.
222
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
223
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
224
 
225
        if ($expectmatch) {
226
            $this->assertCount(1, $content);
227
            $this->assertEquals('student', reset($content[0]));
228
        } else {
229
            $this->assertEmpty($content);
230
        }
231
    }
232
 
233
    /**
234
     * Stress test datasource
235
     *
236
     * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
237
     */
238
    public function test_stress_datasource(): void {
239
        if (!PHPUNIT_LONGTEST) {
240
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
241
        }
242
 
243
        $this->resetAfterTest();
244
 
245
        $course = $this->getDataGenerator()->create_course();
246
        $this->getDataGenerator()->create_and_enrol($course);
247
 
248
        $this->datasource_stress_test_columns(roles::class);
249
        $this->datasource_stress_test_columns_aggregation(roles::class);
250
        $this->datasource_stress_test_conditions(roles::class, 'role:shortname');
251
    }
252
}