Automatically generated installer lang files
[moodle.git] / availability / tests / info_test.php
blobe8044df13801373417e417d6e7f60eab233f6e6a
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.
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/>.
17 /**
18 * Unit tests for info and subclasses.
20 * @package core_availability
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 use core_availability\info;
28 use core_availability\info_module;
29 use core_availability\info_section;
31 /**
32 * Unit tests for info and subclasses.
34 * @package core_availability
35 * @copyright 2014 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class info_testcase extends advanced_testcase {
39 public function setUp() {
40 // Load the mock condition so that it can be used.
41 require_once(__DIR__ . '/fixtures/mock_condition.php');
44 /**
45 * Tests the info_module class (is_available, get_full_information).
47 public function test_info_module() {
48 global $DB, $CFG;
50 // Create a course and pages.
51 $CFG->enableavailability = 0;
52 $this->setAdminUser();
53 $this->resetAfterTest();
54 $generator = $this->getDataGenerator();
55 $course = $generator->create_course();
56 $rec = array('course' => $course);
57 $page1 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
58 $page2 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
59 $page3 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
60 $page4 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
62 // Set up the availability option for the pages to mock options.
63 $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[' .
64 '{"type":"mock","a":false,"m":"grandmaster flash"}]}', array('id' => $page1->cmid));
65 $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[' .
66 '{"type":"mock","a":true,"m":"the furious five"}]}', array('id' => $page2->cmid));
68 // Third page is invalid. (Fourth has no availability settings.)
69 $DB->set_field('course_modules', 'availability', '{{{', array('id' => $page3->cmid));
71 $modinfo = get_fast_modinfo($course);
72 $cm1 = $modinfo->get_cm($page1->cmid);
73 $cm2 = $modinfo->get_cm($page2->cmid);
74 $cm3 = $modinfo->get_cm($page3->cmid);
75 $cm4 = $modinfo->get_cm($page4->cmid);
77 // Do availability and full information checks.
78 $info = new info_module($cm1);
79 $information = '';
80 $this->assertFalse($info->is_available($information));
81 $this->assertEquals('SA: grandmaster flash', $information);
82 $this->assertEquals('SA: [FULL]grandmaster flash', $info->get_full_information());
83 $info = new info_module($cm2);
84 $this->assertTrue($info->is_available($information));
85 $this->assertEquals('', $information);
86 $this->assertEquals('SA: [FULL]the furious five', $info->get_full_information());
88 // Check invalid one.
89 $info = new info_module($cm3);
90 $this->assertFalse($info->is_available($information));
91 $debugging = $this->getDebuggingMessages();
92 $this->resetDebugging();
93 $this->assertEquals(1, count($debugging));
94 $this->assertContains('Invalid availability', $debugging[0]->message);
96 // Check empty one.
97 $info = new info_module($cm4);
98 $this->assertTrue($info->is_available($information));
99 $this->assertEquals('', $information);
100 $this->assertEquals('', $info->get_full_information());
104 * Tests the info_section class (is_available, get_full_information).
106 public function test_info_section() {
107 global $DB;
109 // Create a course.
110 $this->setAdminUser();
111 $this->resetAfterTest();
112 $generator = $this->getDataGenerator();
113 $course = $generator->create_course(
114 array('numsections' => 4), array('createsections' => true));
116 // Set up the availability option for the sections to mock options.
117 $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' .
118 '{"type":"mock","a":false,"m":"public"}]}',
119 array('course' => $course->id, 'section' => 1));
120 $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' .
121 '{"type":"mock","a":true,"m":"enemy"}]}',
122 array('course' => $course->id, 'section' => 2));
124 // Third section is invalid. (Fourth has no availability setting.)
125 $DB->set_field('course_sections', 'availability', '{{{',
126 array('course' => $course->id, 'section' => 3));
128 $modinfo = get_fast_modinfo($course);
129 $sections = $modinfo->get_section_info_all();
131 // Do availability and full information checks.
132 $info = new info_section($sections[1]);
133 $information = '';
134 $this->assertFalse($info->is_available($information));
135 $this->assertEquals('SA: public', $information);
136 $this->assertEquals('SA: [FULL]public', $info->get_full_information());
137 $info = new info_section($sections[2]);
138 $this->assertTrue($info->is_available($information));
139 $this->assertEquals('', $information);
140 $this->assertEquals('SA: [FULL]enemy', $info->get_full_information());
142 // Check invalid one.
143 $info = new info_section($sections[3]);
144 $this->assertFalse($info->is_available($information));
145 $debugging = $this->getDebuggingMessages();
146 $this->resetDebugging();
147 $this->assertEquals(1, count($debugging));
148 $this->assertContains('Invalid availability', $debugging[0]->message);
150 // Check empty one.
151 $info = new info_section($sections[4]);
152 $this->assertTrue($info->is_available($information));
153 $this->assertEquals('', $information);
154 $this->assertEquals('', $info->get_full_information());
158 * Tests the is_user_visible() static function in info_module.
160 public function test_is_user_visible() {
161 global $CFG, $DB;
162 require_once($CFG->dirroot . '/course/lib.php');
163 $this->resetAfterTest();
164 $CFG->enableavailability = 0;
166 // Create a course and some pages:
167 // 0. Invisible due to visible=0.
168 // 1. Availability restriction (mock, set to fail).
169 // 2. Availability restriction on section (mock, set to fail).
170 // 3. Actually visible.
171 $generator = $this->getDataGenerator();
172 $course = $generator->create_course(
173 array('numsections' => 1), array('createsections' => true));
174 $rec = array('course' => $course, );
175 $pages = array();
176 $pagegen = $generator->get_plugin_generator('mod_page');
177 $pages[0] = $pagegen->create_instance($rec, array('visible' => 0));
178 $pages[1] = $pagegen->create_instance($rec);
179 $pages[2] = $pagegen->create_instance($rec);
180 $pages[3] = $pagegen->create_instance($rec);
181 $modinfo = get_fast_modinfo($course);
182 $section = $modinfo->get_section_info(1);
183 $cm = $modinfo->get_cm($pages[2]->cmid);
184 moveto_module($cm, $section);
186 // Set the availability restrictions in database. The enableavailability
187 // setting is off so these do not take effect yet.
188 $notavailable = '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}';
189 $DB->set_field('course_sections', 'availability',
190 $notavailable, array('id' => $section->id));
191 $DB->set_field('course_modules', 'availability',
192 $notavailable, array('id' => $pages[1]->cmid));
193 get_fast_modinfo($course, 0, true);
195 // Set up 4 users - a teacher and student plus somebody who isn't even
196 // on the course. Also going to use admin user and a spare student to
197 // avoid cache problems.
198 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
199 $teacher = $generator->create_user();
200 $student = $generator->create_user();
201 $student2 = $generator->create_user();
202 $other = $generator->create_user();
203 $admin = $DB->get_record('user', array('username' => 'admin'));
204 $generator->enrol_user($teacher->id, $course->id, $roleids['teacher']);
205 $generator->enrol_user($student->id, $course->id, $roleids['student']);
206 $generator->enrol_user($student2->id, $course->id, $roleids['student']);
208 // Basic case when availability disabled, for visible item.
209 $this->assertTrue(info_module::is_user_visible($pages[3]->cmid, $student->id, false));
211 // Specifying as an object should not make any queries.
212 $cm = $DB->get_record('course_modules', array('id' => $pages[3]->cmid));
213 $beforequeries = $DB->perf_get_queries();
214 $this->assertTrue(info_module::is_user_visible($cm, $student->id, false));
215 $this->assertEquals($beforequeries, $DB->perf_get_queries());
217 // Specifying as cm_info for correct user should not make any more queries
218 // if we have already obtained dynamic data.
219 $modinfo = get_fast_modinfo($course, $student->id);
220 $cminfo = $modinfo->get_cm($cm->id);
221 // This will obtain dynamic data.
222 $name = $cminfo->name;
223 $beforequeries = $DB->perf_get_queries();
224 $this->assertTrue(info_module::is_user_visible($cminfo, $student->id, false));
225 $this->assertEquals($beforequeries, $DB->perf_get_queries());
227 // Function does not care if you are in the course (unless $checkcourse).
228 $this->assertTrue(info_module::is_user_visible($cm, $other->id, false));
230 // With $checkcourse, check for enrolled, not enrolled, and admin user.
231 $this->assertTrue(info_module::is_user_visible($cm, $student->id, true));
232 $this->assertFalse(info_module::is_user_visible($cm, $other->id, true));
233 $this->assertTrue(info_module::is_user_visible($cm, $admin->id, true));
235 // With availability off, the student can access all except the
236 // visible=0 one.
237 $this->assertFalse(info_module::is_user_visible($pages[0]->cmid, $student->id, false));
238 $this->assertTrue(info_module::is_user_visible($pages[1]->cmid, $student->id, false));
239 $this->assertTrue(info_module::is_user_visible($pages[2]->cmid, $student->id, false));
241 // Teacher and admin can even access the visible=0 one.
242 $this->assertTrue(info_module::is_user_visible($pages[0]->cmid, $teacher->id, false));
243 $this->assertTrue(info_module::is_user_visible($pages[0]->cmid, $admin->id, false));
245 // Now enable availability (and clear cache).
246 $CFG->enableavailability = true;
247 get_fast_modinfo($course, 0, true);
249 // Student cannot access the activity restricted by its own or by the
250 // section's availability.
251 $this->assertFalse(info_module::is_user_visible($pages[1]->cmid, $student->id, false));
252 $this->assertFalse(info_module::is_user_visible($pages[2]->cmid, $student->id, false));
256 * Tests the convert_legacy_fields function used in restore.
258 public function test_convert_legacy_fields() {
259 // Check with no availability conditions first.
260 $rec = (object)array('availablefrom' => 0, 'availableuntil' => 0,
261 'groupingid' => 7, 'showavailability' => 1);
262 $this->assertNull(info::convert_legacy_fields($rec, false));
264 // Check same list for a section.
265 $this->assertEquals(
266 '{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}',
267 info::convert_legacy_fields($rec, true));
269 // Check groupmembersonly with grouping.
270 $rec->groupmembersonly = 1;
271 $this->assertEquals(
272 '{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}',
273 info::convert_legacy_fields($rec, false));
275 // Check groupmembersonly without grouping.
276 $rec->groupingid = 0;
277 $this->assertEquals(
278 '{"op":"&","showc":[false],"c":[{"type":"group"}]}',
279 info::convert_legacy_fields($rec, false));
281 // Check start date.
282 $rec->groupmembersonly = 0;
283 $rec->availablefrom = 123;
284 $this->assertEquals(
285 '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":123}]}',
286 info::convert_legacy_fields($rec, false));
288 // Start date with show = false.
289 $rec->showavailability = 0;
290 $this->assertEquals(
291 '{"op":"&","showc":[false],"c":[{"type":"date","d":">=","t":123}]}',
292 info::convert_legacy_fields($rec, false));
294 // End date.
295 $rec->showavailability = 1;
296 $rec->availablefrom = 0;
297 $rec->availableuntil = 456;
298 $this->assertEquals(
299 '{"op":"&","showc":[false],"c":[{"type":"date","d":"<","t":456}]}',
300 info::convert_legacy_fields($rec, false));
302 // All together now.
303 $rec->groupingid = 7;
304 $rec->groupmembersonly = 1;
305 $rec->availablefrom = 123;
306 $this->assertEquals(
307 '{"op":"&","showc":[false,true,false],"c":[' .
308 '{"type":"grouping","id":7},' .
309 '{"type":"date","d":">=","t":123},' .
310 '{"type":"date","d":"<","t":456}' .
311 ']}',
312 info::convert_legacy_fields($rec, false));
313 $this->assertEquals(
314 '{"op":"&","showc":[false,true,false],"c":[' .
315 '{"type":"grouping","id":7},' .
316 '{"type":"date","d":">=","t":123},' .
317 '{"type":"date","d":"<","t":456}' .
318 ']}',
319 info::convert_legacy_fields($rec, false, true));
323 * Tests the add_legacy_availability_condition function used in restore.
325 public function test_add_legacy_availability_condition() {
326 // Completion condition tests.
327 $rec = (object)array('sourcecmid' => 7, 'requiredcompletion' => 1);
328 // No previous availability, show = true.
329 $this->assertEquals(
330 '{"op":"&","showc":[true],"c":[{"type":"completion","cm":7,"e":1}]}',
331 info::add_legacy_availability_condition(null, $rec, true));
332 // No previous availability, show = false.
333 $this->assertEquals(
334 '{"op":"&","showc":[false],"c":[{"type":"completion","cm":7,"e":1}]}',
335 info::add_legacy_availability_condition(null, $rec, false));
337 // Existing availability.
338 $before = '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":70}]}';
339 $this->assertEquals(
340 '{"op":"&","showc":[true,true],"c":['.
341 '{"type":"date","d":">=","t":70},' .
342 '{"type":"completion","cm":7,"e":1}' .
343 ']}',
344 info::add_legacy_availability_condition($before, $rec, true));
346 // Grade condition tests.
347 $rec = (object)array('gradeitemid' => 3, 'grademin' => 7, 'grademax' => null);
348 $this->assertEquals(
349 '{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000}]}',
350 info::add_legacy_availability_condition(null, $rec, true));
351 $rec->grademax = 8;
352 $this->assertEquals(
353 '{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000,"max":8.00000}]}',
354 info::add_legacy_availability_condition(null, $rec, true));
355 unset($rec->grademax);
356 unset($rec->grademin);
357 $this->assertEquals(
358 '{"op":"&","showc":[true],"c":[{"type":"grade","id":3}]}',
359 info::add_legacy_availability_condition(null, $rec, true));
361 // Note: There is no need to test the grade condition with show
362 // true/false and existing availability, because this uses the same
363 // function.
367 * Tests the add_legacy_availability_field_condition function used in restore.
369 public function test_add_legacy_availability_field_condition() {
370 // User field, normal operator.
371 $rec = (object)array('userfield' => 'email', 'shortname' => null,
372 'operator' => 'contains', 'value' => '@');
373 $this->assertEquals(
374 '{"op":"&","showc":[true],"c":[' .
375 '{"type":"profile","op":"contains","sf":"email","v":"@"}]}',
376 info::add_legacy_availability_field_condition(null, $rec, true));
378 // User field, non-value operator.
379 $rec = (object)array('userfield' => 'email', 'shortname' => null,
380 'operator' => 'isempty', 'value' => '');
381 $this->assertEquals(
382 '{"op":"&","showc":[true],"c":[' .
383 '{"type":"profile","op":"isempty","sf":"email"}]}',
384 info::add_legacy_availability_field_condition(null, $rec, true));
386 // Custom field.
387 $rec = (object)array('userfield' => null, 'shortname' => 'frogtype',
388 'operator' => 'isempty', 'value' => '');
389 $this->assertEquals(
390 '{"op":"&","showc":[true],"c":[' .
391 '{"type":"profile","op":"isempty","cf":"frogtype"}]}',
392 info::add_legacy_availability_field_condition(null, $rec, true));
396 * Tests the filter_user_list() and get_user_list_sql() functions.
398 public function test_filter_user_list() {
399 global $CFG, $DB;
400 require_once($CFG->dirroot . '/course/lib.php');
401 $this->resetAfterTest();
402 $CFG->enableavailability = true;
404 // Create a course with 2 sections and 2 pages and 3 users.
405 // Availability is set up initially on the 'page/section 2' items.
406 $generator = $this->getDataGenerator();
407 $course = $generator->create_course(
408 array('numsections' => 2), array('createsections' => true));
409 $u1 = $generator->create_user();
410 $u2 = $generator->create_user();
411 $u3 = $generator->create_user();
412 $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'), MUST_EXIST);
413 $allusers = array($u1->id => $u1, $u2->id => $u2, $u3->id => $u3);
414 $generator->enrol_user($u1->id, $course->id, $studentroleid);
415 $generator->enrol_user($u2->id, $course->id, $studentroleid);
416 $generator->enrol_user($u3->id, $course->id, $studentroleid);
418 // Page 2 allows access to users 2 and 3, while section 2 allows access
419 // to users 1 and 2.
420 $pagegen = $generator->get_plugin_generator('mod_page');
421 $page = $pagegen->create_instance(array('course' => $course));
422 $page2 = $pagegen->create_instance(array('course' => $course,
423 'availability' => '{"op":"|","show":true,"c":[{"type":"mock","filter":[' .
424 $u2->id . ',' . $u3->id . ']}]}'));
425 $modinfo = get_fast_modinfo($course);
426 $section = $modinfo->get_section_info(1);
427 $section2 = $modinfo->get_section_info(2);
428 $DB->set_field('course_sections', 'availability',
429 '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u1->id . ',' . $u2->id .']}]}',
430 array('id' => $section2->id));
431 moveto_module($modinfo->get_cm($page2->cmid), $section2);
433 // With no restrictions, returns full list.
434 $info = new info_module($modinfo->get_cm($page->cmid));
435 $this->assertEquals(array($u1->id, $u2->id, $u3->id),
436 array_keys($info->filter_user_list($allusers)));
437 $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
439 // Set an availability restriction in database for section 1.
440 // For the section we set it so it doesn't support filters; for the
441 // module we have a filter.
442 $DB->set_field('course_sections', 'availability',
443 '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}',
444 array('id' => $section->id));
445 $DB->set_field('course_modules', 'availability',
446 '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u3->id .']}]}',
447 array('id' => $page->cmid));
448 rebuild_course_cache($course->id, true);
449 $modinfo = get_fast_modinfo($course);
451 // Now it should work (for the module).
452 $info = new info_module($modinfo->get_cm($page->cmid));
453 $expected = array($u3->id);
454 $this->assertEquals($expected,
455 array_keys($info->filter_user_list($allusers)));
456 list ($sql, $params) = $info->get_user_list_sql();
457 $result = $DB->get_fieldset_sql($sql, $params);
458 sort($result);
459 $this->assertEquals($expected, $result);
460 $info = new info_section($modinfo->get_section_info(1));
461 $this->assertEquals(array($u1->id, $u2->id, $u3->id),
462 array_keys($info->filter_user_list($allusers)));
463 $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
465 // With availability disabled, module returns full list too.
466 $CFG->enableavailability = false;
467 $info = new info_module($modinfo->get_cm($page->cmid));
468 $this->assertEquals(array($u1->id, $u2->id, $u3->id),
469 array_keys($info->filter_user_list($allusers)));
470 $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
472 // Check the other section...
473 $CFG->enableavailability = true;
474 $info = new info_section($modinfo->get_section_info(2));
475 $expected = array($u1->id, $u2->id);
476 $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
477 list ($sql, $params) = $info->get_user_list_sql(true);
478 $result = $DB->get_fieldset_sql($sql, $params);
479 sort($result);
480 $this->assertEquals($expected, $result);
482 // And the module in that section - which has combined the section and
483 // module restrictions.
484 $info = new info_module($modinfo->get_cm($page2->cmid));
485 $expected = array($u2->id);
486 $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
487 list ($sql, $params) = $info->get_user_list_sql(true);
488 $result = $DB->get_fieldset_sql($sql, $params);
489 sort($result);
490 $this->assertEquals($expected, $result);
492 // If the students have viewhiddenactivities, they get past the module
493 // restriction.
494 role_change_permission($studentroleid, context_module::instance($page2->cmid),
495 'moodle/course:ignoreavailabilityrestrictions', CAP_ALLOW);
496 $expected = array($u1->id, $u2->id);
497 $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
498 list ($sql, $params) = $info->get_user_list_sql(true);
499 $result = $DB->get_fieldset_sql($sql, $params);
500 sort($result);
501 $this->assertEquals($expected, $result);
503 // If they have viewhiddensections, they also get past the section
504 // restriction.
505 role_change_permission($studentroleid, context_course::instance($course->id),
506 'moodle/course:ignoreavailabilityrestrictions', CAP_ALLOW);
507 $expected = array($u1->id, $u2->id, $u3->id);
508 $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
509 list ($sql, $params) = $info->get_user_list_sql(true);
510 $result = $DB->get_fieldset_sql($sql, $params);
511 sort($result);
512 $this->assertEquals($expected, $result);