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
 
1441 ariadna 17
namespace core;
18
 
1 efrain 19
/**
1441 ariadna 20
 * Tests the report_helper class.
1 efrain 21
 *
1441 ariadna 22
 * @covers \core\report_helper
23
 * @package core
24
 * @category test
25
 * @copyright 2024 The Open University
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1 efrain 27
 */
1441 ariadna 28
final class report_helper_test extends \advanced_testcase {
29
    /** @var int[] Array of created user ids */
30
    protected array $userids;
1 efrain 31
 
1441 ariadna 32
    /**
33
     * Tests {@see report_helper::get_group_filter()}.
34
     */
35
    public function test_get_group_filter(): void {
36
        $this->resetAfterTest();
1 efrain 37
 
1441 ariadna 38
        // Create some test course, groups, and users.
39
        $generator = self::getDataGenerator();
1 efrain 40
 
1441 ariadna 41
        $vgcourse = $generator->create_course(['groupmode' => VISIBLEGROUPS]);
42
        $sgcourse = $generator->create_course(['groupmode' => SEPARATEGROUPS]);
43
 
44
        $vg1 = $generator->create_group(['courseid' => $vgcourse->id]);
45
        $vg2 = $generator->create_group(['courseid' => $vgcourse->id]);
46
        $sg1 = $generator->create_group(['courseid' => $sgcourse->id]);
47
        $sg2 = $generator->create_group(['courseid' => $sgcourse->id]);
48
 
49
        $this->userids = [];
50
        for ($i = 0; $i < 10; $i++) {
51
            $this->userids[$i] = $generator->create_user()->id;
52
            $generator->enrol_user($this->userids[$i], ($i < 5) ? $vgcourse->id : $sgcourse->id, 'student');
53
        }
54
 
55
        groups_add_member($vg1, $this->userids[0]);
56
        groups_add_member($vg1, $this->userids[1]);
57
        groups_add_member($vg2, $this->userids[0]);
58
        groups_add_member($vg2, $this->userids[2]);
59
        groups_add_member($vg2, $this->userids[3]);
60
 
61
        groups_add_member($sg1, $this->userids[5]);
62
        groups_add_member($sg1, $this->userids[6]);
63
        groups_add_member($sg2, $this->userids[5]);
64
        groups_add_member($sg2, $this->userids[7]);
65
        groups_add_member($sg2, $this->userids[8]);
66
 
67
        // Teacher user has access all groups.
68
        $teacher = $generator->create_user();
69
        $generator->enrol_user($teacher->id, $vgcourse->id, 'editingteacher');
70
        $generator->enrol_user($teacher->id, $sgcourse->id, 'editingteacher');
71
 
72
        // With specified groups on either course (does not matter who user is).
73
        $this->assert_group_filter([0, 1], ['courseid' => $vgcourse->id, 'groupid' => $vg1->id]);
74
        $this->assert_group_filter([0, 2, 3], ['courseid' => $vgcourse->id, 'groupid' => $vg2->id]);
75
        $this->assert_group_filter([5, 6], ['courseid' => $sgcourse->id, 'groupid' => $sg1->id]);
76
        $this->assert_group_filter([5, 7, 8], ['courseid' => $sgcourse->id, 'groupid' => $sg2->id]);
77
 
78
        // With specified group and user.
79
        $this->assert_group_filter([2], [
80
            'courseid' => $vgcourse->id,
81
            'groupid' => $vg2->id,
82
            'userid' => $this->userids[2],
83
        ]);
84
        $this->assert_group_filter([6], [
85
            'courseid' => $sgcourse->id,
86
            'groupid' => $sg2->id,
87
            'userid' => $this->userids[6],
88
        ]);
89
 
90
        // No restrictions, user belongs to a group or to both groups on VG course.
91
        $this->setUser($this->userids[1]);
92
        $all = array_keys($this->userids);
93
        $this->assert_group_filter($all, ['courseid' => $vgcourse->id]);
94
        $this->setUser($this->userids[0]);
95
        $this->assert_group_filter($all, ['courseid' => $vgcourse->id]);
96
 
97
        // No restrictions, user belongs to a group or to both groups on SG course.
98
        $this->setUser($this->userids[6]);
99
        $this->assert_group_filter([5, 6], ['courseid' => $sgcourse->id]);
100
        $this->setUser($this->userids[5]);
101
        $this->assert_group_filter([5, 6, 7, 8], ['courseid' => $sgcourse->id]);
102
 
103
        // No restrictions, user has access all groups on either course.
104
        $this->setUser($teacher);
105
        $this->assert_group_filter($all, ['courseid' => $vgcourse->id]);
106
        $this->assert_group_filter($all, ['courseid' => $sgcourse->id]);
107
 
108
        // There was a performance issue for users with access all groups where it listed all users
109
        // in the system in the 'filter' list, now it doesn't.
110
        $this->assertNull(report_helper::get_group_filter(
111
            (object)['courseid' => $sgcourse->id],
112
        )['useridfilter']);
113
 
114
        // Specified group even if you have AAG.
115
        $this->assert_group_filter([0, 1], ['courseid' => $vgcourse->id, 'groupid' => $vg1->id]);
116
        $this->assert_group_filter([5, 6], ['courseid' => $sgcourse->id, 'groupid' => $sg1->id]);
117
 
118
        // No restrictions, user does not belong to a group on course. Makes no difference in VG.
119
        $this->setUser($this->userids[5]);
120
        $this->assert_group_filter($all, ['courseid' => $vgcourse->id]);
121
 
122
        // In SG user can now view all users across system who are not in a group on course.
123
        // Strange but true.
124
        $this->setUser($this->userids[0]);
125
        $this->assert_group_filter([0, 1, 2, 3, 4, 9], ['courseid' => $sgcourse->id]);
126
    }
127
 
1 efrain 128
    /**
1441 ariadna 129
     * Calls {@see report_helper::get_group_filter()} and checks which of the users created by this
130
     * unit test are returned.
1 efrain 131
     *
1441 ariadna 132
     * @param int[] $expecteduserindexes Expected user indexes
133
     * @param array $filterparams Array of filter parameters to pass to get_group_filter
1 efrain 134
     */
1441 ariadna 135
    protected function assert_group_filter(array $expecteduserindexes, array $filterparams): void {
136
        global $DB;
137
 
138
        $result = report_helper::get_group_filter((object)$filterparams);
139
 
140
        // Combine the joins (if any). 'TRUE' is not allowed in SQL Server, you must use '1 = 1'.
141
        $where = '1 = 1';
142
        foreach ($result['joins'] as $join) {
143
            $where .= ' AND ' . $join;
144
        }
145
 
146
        // The joins use field 'userid' so we make a subselect table with that field name.
147
        $userids = $DB->get_fieldset_sql("
148
            SELECT userid
149
              FROM (SELECT id AS userid FROM {user}) userdata
150
             WHERE $where", $result['params']);
151
 
152
        if ($result['useridfilter'] !== null) {
153
            $userids = array_filter($userids, fn($userid) => array_key_exists($userid, $result['useridfilter']));
154
        }
155
 
156
        // Convert user ids to expected indexes, exclude any results not in our test user list, and sort.
157
        $indexes = array_map(fn($userid) => array_search($userid, $this->userids), $userids);
158
        $indexes = array_filter($indexes, fn($index) => $index !== false);
159
        sort($indexes);
160
        $this->assertEquals($expecteduserindexes, $indexes);
1 efrain 161
    }
162
 
163
    /**
1441 ariadna 164
     * Tests {@see report_helper::has_valid_group()}.
1 efrain 165
     *
1441 ariadna 166
     * @param int $groupmode Group mode for the course
167
     * @param string $username Username of the user to check
168
     * @param array $expected Expected result of the check, with 3 boolean values depending on the context:
169
     * - Course context
170
     * - Module context
171
     * - System context
172
     *
173
     * @covers       \core\report_helper::has_valid_group
174
     * @dataProvider  has_valid_group_provider
1 efrain 175
     */
1441 ariadna 176
    public function test_has_valid_group(int $groupmode, string $username, array $expected): void {
177
        $this->resetAfterTest();
1 efrain 178
 
1441 ariadna 179
        // Create some test course, groups, and users.
180
        $generator = self::getDataGenerator();
181
        $course = $generator->create_course(['groupmode' => $groupmode, 'groupmodeforce' => 1]);
182
        $assign = $generator->create_module('assign', ['course' => $course->id]);
183
        $g1 = $generator->create_group(['courseid' => $course->id]);
1 efrain 184
 
1441 ariadna 185
        $this->userids = [];
186
        $data = [
187
            's1' => ['role' => 'student', 'group' => $g1->id],
188
            's2' => ['role' => 'student', 'group' => null],
189
            't1' => ['role' => 'teacher', 'group' => $g1->id],
190
            't2' => ['role' => 'teacher', 'group' => null],
191
            'et1' => ['role' => 'editingteacher', 'group' => null],
192
        ];
193
        foreach ($data as $key => $value) {
194
            ['group' => $groupid, 'role' => $role] = $value;
195
            $this->userids[$key] = $generator->create_user(['username' => $key]);
196
            $generator->enrol_user($this->userids[$key]->id, $course->id, $role);
197
            if ($groupid) {
198
                groups_add_member($groupid, $this->userids[$key]->id);
199
            }
200
        }
201
        $coursecontext = \context_course::instance($course->id);
202
        [$course, $cm] = get_course_and_cm_from_instance($assign->id, 'assign');
203
        $modulecontext = \context_module::instance($cm->id);
204
        [$hasvalidgroupcourse, $hasvalidgroupmodule, $hasvalidgroupsystem] = $expected;
205
        $this->assertEquals(
206
            $hasvalidgroupcourse,
207
            report_helper::has_valid_group($coursecontext, $this->userids[$username]->id),
208
            "Failed for user $username in course context"
209
        );
210
        $this->assertEquals(
211
            $hasvalidgroupmodule,
212
            report_helper::has_valid_group($modulecontext, $this->userids[$username]->id),
213
            'Failed for user ' . $username . ' in module context'
214
        );
215
        $this->assertEquals(
216
            $hasvalidgroupsystem,
217
            report_helper::has_valid_group(\context_system::instance(), $this->userids[$username]->id),
218
            'Failed for user ' . $username . ' in system context'
219
        );
220
    }
1 efrain 221
 
1441 ariadna 222
    /**
223
     * Data provider for test_has_valid_group.
224
     *
225
     * @return array
226
     */
227
    public static function has_valid_group_provider(): array {
228
        return [
229
            'student 1 - g1 - separate group' => [
230
                'groupmode' => SEPARATEGROUPS,
231
                'username' => 's1',
232
                'expected' => [true, true, true],
233
            ],
234
            'student 2 - no group - separate group' => [
235
                'groupmode' => SEPARATEGROUPS,
236
                'username' => 's2',
237
                'expected' => [false, false, true],
238
            ],
239
            'teacher 1 - g1 - separate group' => [
240
                'groupmode' => SEPARATEGROUPS,
241
                'username' => 't1',
242
                'expected' => [true, true, true],
243
            ],
244
            'teacher 2 - no group - separate group' => [
245
                'groupmode' => SEPARATEGROUPS,
246
                'username' => 't2',
247
                'expected' => [false, false, true],
248
            ],
249
            'editing teacher - no group - separate group' => [
250
                'groupmode' => SEPARATEGROUPS,
251
                'username' => 'et1',
252
                'expected' => [true, true, true],
253
            ],
254
            'student 1 - g1 - no group' => [
255
                'groupmode' => NOGROUPS,
256
                'username' => 's1',
257
                'expected' => [true, true, true],
258
            ],
259
            'student 2 - no group - no group' => [
260
                'groupmode' => NOGROUPS,
261
                'username' => 's2',
262
                'expected' => [true, true, true],
263
            ],
264
            'teacher 1 - g1 - no group' => [
265
                'groupmode' => NOGROUPS,
266
                'username' => 't1',
267
                'expected' => [true, true, true],
268
            ],
269
            'teacher 2 - no group - no group' => [
270
                'groupmode' => NOGROUPS,
271
                'username' => 't2',
272
                'expected' => [true, true, true],
273
            ],
274
            'editing teacher - no group - no group' => [
275
                'groupmode' => NOGROUPS,
276
                'username' => 'et1',
277
                'expected' => [true, true, true],
278
            ],
279
            'student 1 - g1 - visible group' => [
280
                'groupmode' => VISIBLEGROUPS,
281
                'username' => 's1',
282
                'expected' => [true, true, true],
283
            ],
284
            'student 2 - no group - visible group' => [
285
                'groupmode' => VISIBLEGROUPS,
286
                'username' => 's2',
287
                'expected' => [true, true, true],
288
            ],
289
            'teacher 1 - g1 - visible group' => [
290
                'groupmode' => VISIBLEGROUPS,
291
                'username' => 't1',
292
                'expected' => [true, true, true],
293
            ],
294
            'teacher 2 - no group - visible group' => [
295
                'groupmode' => VISIBLEGROUPS,
296
                'username' => 't2',
297
                'expected' => [true, true, true],
298
            ],
299
            'editing teacher - visible group - no group' => [
300
                'groupmode' => VISIBLEGROUPS,
301
                'username' => 'et1',
302
                'expected' => [true, true, true],
303
            ],
304
        ];
1 efrain 305
    }
306
}