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