Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
namespace availability_group;
18
 
19
/**
20
 * Unit tests for the condition.
21
 *
22
 * @package availability_group
23
 * @copyright 2014 The Open University
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
1441 ariadna 26
final class condition_test extends \advanced_testcase {
1 efrain 27
    /**
28
     * Load required classes.
29
     */
30
    public function setUp(): void {
31
        // Load the mock info class so that it can be used.
32
        global $CFG;
33
        require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
1441 ariadna 34
        parent::setUp();
1 efrain 35
    }
36
 
37
    /**
38
     * Tests constructing and using condition.
39
     */
11 efrain 40
    public function test_usage(): void {
1 efrain 41
        global $CFG, $USER;
42
        $this->resetAfterTest();
43
        $CFG->enableavailability = true;
44
 
45
        // Erase static cache before test.
46
        condition::wipe_static_cache();
47
 
48
        // Make a test course and user.
49
        $generator = $this->getDataGenerator();
50
        $course = $generator->create_course();
51
        $user = $generator->create_and_enrol($course);
52
        $usertwo = $generator->create_and_enrol($course);
53
 
54
        $info = new \core_availability\mock_info($course, $user->id);
55
 
56
        // Make 2 test groups, one in a grouping and one not.
57
        $grouping = $generator->create_grouping(array('courseid' => $course->id));
58
        $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'G1!'));
59
        groups_assign_grouping($grouping->id, $group1->id);
60
        $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'G2!'));
61
 
62
        // Do test (not in group).
63
        $cond = new condition((object)array('id' => (int)$group1->id));
64
 
65
        // Check if available (when not available).
66
        $this->assertFalse($cond->is_available(false, $info, true, $user->id));
67
        $information = $cond->get_description(false, false, $info);
68
        $information = \core_availability\info::format_info($information, $course);
69
        $this->assertMatchesRegularExpression('~You belong to.*G1!~', $information);
70
        $this->assertTrue($cond->is_available(true, $info, true, $user->id));
71
 
72
        // Add user to groups and refresh cache.
73
        groups_add_member($group1, $user);
74
        groups_add_member($group2, $user);
75
        $info = new \core_availability\mock_info($course, $user->id);
76
 
77
        // Recheck.
78
        $this->assertTrue($cond->is_available(false, $info, true, $user->id));
79
        $this->assertFalse($cond->is_available(true, $info, true, $user->id));
80
        $this->assertFalse($cond->is_available(false, $info, true, $usertwo->id));
81
        $this->assertTrue($cond->is_available(true, $info, true, $usertwo->id));
82
        $information = $cond->get_description(false, true, $info);
83
        $information = \core_availability\info::format_info($information, $course);
84
        $this->assertMatchesRegularExpression('~do not belong to.*G1!~', $information);
85
 
86
        // Check group 2 works also.
87
        $cond = new condition((object)array('id' => (int)$group2->id));
88
        $this->assertTrue($cond->is_available(false, $info, true, $user->id));
89
        $this->assertFalse($cond->is_available(false, $info, true, $usertwo->id));
90
 
91
        // What about an 'any group' condition?
92
        $cond = new condition((object)array());
93
        $this->assertTrue($cond->is_available(false, $info, true, $user->id));
94
        $this->assertFalse($cond->is_available(true, $info, true, $user->id));
95
        $this->assertFalse($cond->is_available(false, $info, true, $usertwo->id));
96
        $this->assertTrue($cond->is_available(true, $info, true, $usertwo->id));
97
        $information = $cond->get_description(false, true, $info);
98
        $information = \core_availability\info::format_info($information, $course);
99
        $this->assertMatchesRegularExpression('~do not belong to any~', $information);
100
 
101
        // Admin user doesn't belong to a group, but they can access it
102
        // either way (positive or NOT).
103
        $this->setAdminUser();
104
        $this->assertTrue($cond->is_available(false, $info, true, $USER->id));
105
        $this->assertTrue($cond->is_available(true, $info, true, $USER->id));
106
 
107
        // Group that doesn't exist uses 'missing' text.
108
        $cond = new condition((object)array('id' => $group2->id + 1000));
109
        $this->assertFalse($cond->is_available(false, $info, true, $user->id));
110
        $information = $cond->get_description(false, false, $info);
111
        $information = \core_availability\info::format_info($information, $course);
112
        $this->assertMatchesRegularExpression('~You belong to.*\(Missing group\)~', $information);
113
    }
114
 
115
    /**
116
     * Tests the constructor including error conditions. Also tests the
117
     * string conversion feature (intended for debugging only).
118
     */
11 efrain 119
    public function test_constructor(): void {
1 efrain 120
        // Invalid id (not int).
121
        $structure = (object)array('id' => 'bourne');
122
        try {
123
            $cond = new condition($structure);
124
            $this->fail();
125
        } catch (\coding_exception $e) {
126
            $this->assertStringContainsString('Invalid ->id', $e->getMessage());
127
        }
128
 
129
        // Valid (with id).
130
        $structure->id = 123;
131
        $cond = new condition($structure);
132
        $this->assertEquals('{group:#123}', (string)$cond);
133
 
134
        // Valid (no id).
135
        unset($structure->id);
136
        $cond = new condition($structure);
137
        $this->assertEquals('{group:any}', (string)$cond);
138
    }
139
 
140
    /**
141
     * Tests the save() function.
142
     */
11 efrain 143
    public function test_save(): void {
1 efrain 144
        $structure = (object)array('id' => 123);
145
        $cond = new condition($structure);
146
        $structure->type = 'group';
147
        $this->assertEquals($structure, $cond->save());
148
 
149
        $structure = (object)array();
150
        $cond = new condition($structure);
151
        $structure->type = 'group';
152
        $this->assertEquals($structure, $cond->save());
153
    }
154
 
155
    /**
156
     * Tests the update_dependency_id() function.
157
     */
11 efrain 158
    public function test_update_dependency_id(): void {
1 efrain 159
        $cond = new condition((object)array('id' => 123));
160
        $this->assertFalse($cond->update_dependency_id('frogs', 123, 456));
161
        $this->assertFalse($cond->update_dependency_id('groups', 12, 34));
162
        $this->assertTrue($cond->update_dependency_id('groups', 123, 456));
163
        $after = $cond->save();
164
        $this->assertEquals(456, $after->id);
165
    }
166
 
167
    /**
168
     * Tests the filter_users (bulk checking) function. Also tests the SQL
169
     * variant get_user_list_sql.
170
     */
11 efrain 171
    public function test_filter_users(): void {
1 efrain 172
        global $DB;
173
        $this->resetAfterTest();
174
 
175
        // Erase static cache before test.
176
        condition::wipe_static_cache();
177
 
178
        // Make a test course and some users.
179
        $generator = $this->getDataGenerator();
180
        $course = $generator->create_course();
181
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
182
        $teacher = $generator->create_user();
183
        $generator->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);
184
        $allusers = array($teacher->id => $teacher);
185
        $students = array();
186
        for ($i = 0; $i < 3; $i++) {
187
            $student = $generator->create_user();
188
            $students[$i] = $student;
189
            $generator->enrol_user($student->id, $course->id, $roleids['student']);
190
            $allusers[$student->id] = $student;
191
        }
192
        $info = new \core_availability\mock_info($course);
193
 
194
        // Make test groups.
195
        $group1 = $generator->create_group(array('courseid' => $course->id));
196
        $group2 = $generator->create_group(array('courseid' => $course->id));
197
 
198
        // Assign students to groups as follows (teacher is not in a group):
199
        // 0: no groups.
200
        // 1: in group 1.
201
        // 2: in group 2.
202
        groups_add_member($group1, $students[1]);
203
        groups_add_member($group2, $students[2]);
204
 
205
        // Test 'any group' condition.
206
        $checker = new \core_availability\capability_checker($info->get_context());
207
        $cond = new condition((object)array());
208
        $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
209
        ksort($result);
210
        $expected = array($teacher->id, $students[1]->id, $students[2]->id);
211
        $this->assertEquals($expected, $result);
212
 
213
        // Test it with get_user_list_sql.
214
        list ($sql, $params) = $cond->get_user_list_sql(false, $info, true);
215
        $result = $DB->get_fieldset_sql($sql, $params);
216
        sort($result);
217
        $this->assertEquals($expected, $result);
218
 
219
        // Test NOT version (note that teacher can still access because AAG works
220
        // both ways).
221
        $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
222
        ksort($result);
223
        $expected = array($teacher->id, $students[0]->id);
224
        $this->assertEquals($expected, $result);
225
 
226
        // Test with get_user_list_sql.
227
        list ($sql, $params) = $cond->get_user_list_sql(true, $info, true);
228
        $result = $DB->get_fieldset_sql($sql, $params);
229
        sort($result);
230
        $this->assertEquals($expected, $result);
231
 
232
        // Test specific group.
233
        $cond = new condition((object)array('id' => (int)$group1->id));
234
        $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
235
        ksort($result);
236
        $expected = array($teacher->id, $students[1]->id);
237
        $this->assertEquals($expected, $result);
238
 
239
        list ($sql, $params) = $cond->get_user_list_sql(false, $info, true);
240
        $result = $DB->get_fieldset_sql($sql, $params);
241
        sort($result);
242
        $this->assertEquals($expected, $result);
243
 
244
        $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
245
        ksort($result);
246
        $expected = array($teacher->id, $students[0]->id, $students[2]->id);
247
        $this->assertEquals($expected, $result);
248
 
249
        list ($sql, $params) = $cond->get_user_list_sql(true, $info, true);
250
        $result = $DB->get_fieldset_sql($sql, $params);
251
        sort($result);
252
        $this->assertEquals($expected, $result);
253
    }
254
}