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 core_availability;
18
 
19
/**
20
 * Unit tests for info and subclasses.
21
 *
22
 * @package core_availability
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 info_test extends \advanced_testcase {
1 efrain 27
    public function setUp(): void {
28
        // Load the mock condition so that it can be used.
29
        require_once(__DIR__ . '/fixtures/mock_condition.php');
1441 ariadna 30
        parent::setUp();
1 efrain 31
    }
32
 
33
    /**
34
     * Tests the info_module class (is_available, get_full_information).
35
     */
11 efrain 36
    public function test_info_module(): void {
1 efrain 37
        global $DB, $CFG;
38
 
39
        // Create a course and pages.
40
        $CFG->enableavailability = 0;
41
        $this->setAdminUser();
42
        $this->resetAfterTest();
43
        $generator = $this->getDataGenerator();
44
        $course = $generator->create_course();
45
        $rec = array('course' => $course);
46
        $page1 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
47
        $page2 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
48
        $page3 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
49
        $page4 = $generator->get_plugin_generator('mod_page')->create_instance($rec);
50
 
51
        // Set up the availability option for the pages to mock options.
52
        $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[' .
53
                '{"type":"mock","a":false,"m":"grandmaster flash"}]}', array('id' => $page1->cmid));
54
        $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[' .
55
                '{"type":"mock","a":true,"m":"the furious five"}]}', array('id' => $page2->cmid));
56
 
57
        // Third page is invalid. (Fourth has no availability settings.)
58
        $DB->set_field('course_modules', 'availability', '{{{', array('id' => $page3->cmid));
59
 
60
        $modinfo = get_fast_modinfo($course);
61
        $cm1 = $modinfo->get_cm($page1->cmid);
62
        $cm2 = $modinfo->get_cm($page2->cmid);
63
        $cm3 = $modinfo->get_cm($page3->cmid);
64
        $cm4 = $modinfo->get_cm($page4->cmid);
65
 
66
        // Do availability and full information checks.
67
        $info = new info_module($cm1);
68
        $information = '';
69
        $this->assertFalse($info->is_available($information));
70
        $this->assertEquals('SA: grandmaster flash', $information);
71
        $this->assertEquals('SA: [FULL]grandmaster flash', $info->get_full_information());
72
        $info = new info_module($cm2);
73
        $this->assertTrue($info->is_available($information));
74
        $this->assertEquals('', $information);
75
        $this->assertEquals('SA: [FULL]the furious five', $info->get_full_information());
76
 
77
        // Check invalid one.
78
        $info = new info_module($cm3);
79
        $this->assertFalse($info->is_available($information));
80
        $debugging = $this->getDebuggingMessages();
81
        $this->resetDebugging();
82
        $this->assertEquals(1, count($debugging));
83
        $this->assertStringContainsString('Invalid availability', $debugging[0]->message);
84
 
85
        // Check empty one.
86
        $info = new info_module($cm4);
87
        $this->assertTrue($info->is_available($information));
88
        $this->assertEquals('', $information);
89
        $this->assertEquals('', $info->get_full_information());
90
    }
91
 
92
    /**
93
     * Tests the info_section class (is_available, get_full_information).
94
     */
11 efrain 95
    public function test_info_section(): void {
1 efrain 96
        global $DB;
97
 
98
        // Create a course.
99
        $this->setAdminUser();
100
        $this->resetAfterTest();
101
        $generator = $this->getDataGenerator();
102
        $course = $generator->create_course(
103
                array('numsections' => 4), array('createsections' => true));
104
 
105
        // Set up the availability option for the sections to mock options.
106
        $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' .
107
                '{"type":"mock","a":false,"m":"public"}]}',
108
                array('course' => $course->id, 'section' => 1));
109
        $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[' .
110
                '{"type":"mock","a":true,"m":"enemy"}]}',
111
                array('course' => $course->id, 'section' => 2));
112
 
113
        // Third section is invalid. (Fourth has no availability setting.)
114
        $DB->set_field('course_sections', 'availability', '{{{',
115
                array('course' => $course->id, 'section' => 3));
116
 
117
        $modinfo = get_fast_modinfo($course);
118
        $sections = $modinfo->get_section_info_all();
119
 
120
        // Do availability and full information checks.
121
        $info = new info_section($sections[1]);
122
        $information = '';
123
        $this->assertFalse($info->is_available($information));
124
        $this->assertEquals('SA: public', $information);
125
        $this->assertEquals('SA: [FULL]public', $info->get_full_information());
126
        $info = new info_section($sections[2]);
127
        $this->assertTrue($info->is_available($information));
128
        $this->assertEquals('', $information);
129
        $this->assertEquals('SA: [FULL]enemy', $info->get_full_information());
130
 
131
        // Check invalid one.
132
        $info = new info_section($sections[3]);
133
        $this->assertFalse($info->is_available($information));
134
        $debugging = $this->getDebuggingMessages();
135
        $this->resetDebugging();
136
        $this->assertEquals(1, count($debugging));
137
        $this->assertStringContainsString('Invalid availability', $debugging[0]->message);
138
 
139
        // Check empty one.
140
        $info = new info_section($sections[4]);
141
        $this->assertTrue($info->is_available($information));
142
        $this->assertEquals('', $information);
143
        $this->assertEquals('', $info->get_full_information());
144
    }
145
 
146
    /**
147
     * Tests the is_user_visible() static function in info_module.
148
     */
11 efrain 149
    public function test_is_user_visible(): void {
1 efrain 150
        global $CFG, $DB;
151
        require_once($CFG->dirroot . '/course/lib.php');
152
        $this->resetAfterTest();
153
        $CFG->enableavailability = 0;
154
 
155
        // Create a course and some pages:
156
        // 0. Invisible due to visible=0.
157
        // 1. Availability restriction (mock, set to fail).
158
        // 2. Availability restriction on section (mock, set to fail).
159
        // 3. Actually visible.
160
        $generator = $this->getDataGenerator();
161
        $course = $generator->create_course(
162
                array('numsections' => 1), array('createsections' => true));
163
        $rec = array('course' => $course, );
164
        $pages = array();
165
        $pagegen = $generator->get_plugin_generator('mod_page');
166
        $pages[0] = $pagegen->create_instance($rec, array('visible' => 0));
167
        $pages[1] = $pagegen->create_instance($rec);
168
        $pages[2] = $pagegen->create_instance($rec);
169
        $pages[3] = $pagegen->create_instance($rec);
170
        $modinfo = get_fast_modinfo($course);
171
        $section = $modinfo->get_section_info(1);
172
        $cm = $modinfo->get_cm($pages[2]->cmid);
173
        moveto_module($cm, $section);
174
 
175
        // Set the availability restrictions in database. The enableavailability
176
        // setting is off so these do not take effect yet.
177
        $notavailable = '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}';
178
        $DB->set_field('course_sections', 'availability',
179
                $notavailable, array('id' => $section->id));
180
        $DB->set_field('course_modules', 'availability',
181
                $notavailable, array('id' => $pages[1]->cmid));
182
        get_fast_modinfo($course, 0, true);
183
 
184
        // Set up 4 users - a teacher and student plus somebody who isn't even
185
        // on the course. Also going to use admin user and a spare student to
186
        // avoid cache problems.
187
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
188
        $teacher = $generator->create_user();
189
        $student = $generator->create_user();
190
        $student2 = $generator->create_user();
191
        $other = $generator->create_user();
192
        $admin = $DB->get_record('user', array('username' => 'admin'));
193
        $generator->enrol_user($teacher->id, $course->id, $roleids['teacher']);
194
        $generator->enrol_user($student->id, $course->id, $roleids['student']);
195
        $generator->enrol_user($student2->id, $course->id, $roleids['student']);
196
 
197
        // Basic case when availability disabled, for visible item.
198
        $this->assertTrue(info_module::is_user_visible($pages[3]->cmid, $student->id, false));
199
 
200
        // Specifying as an object should not make any queries.
201
        $cm = $DB->get_record('course_modules', array('id' => $pages[3]->cmid));
202
        $beforequeries = $DB->perf_get_queries();
203
        $this->assertTrue(info_module::is_user_visible($cm, $student->id, false));
204
        $this->assertEquals($beforequeries, $DB->perf_get_queries());
205
 
206
        // Specifying as cm_info for correct user should not make any more queries
207
        // if we have already obtained dynamic data.
208
        $modinfo = get_fast_modinfo($course, $student->id);
209
        $cminfo = $modinfo->get_cm($cm->id);
210
        // This will obtain dynamic data.
211
        $name = $cminfo->name;
212
        $beforequeries = $DB->perf_get_queries();
213
        $this->assertTrue(info_module::is_user_visible($cminfo, $student->id, false));
214
        $this->assertEquals($beforequeries, $DB->perf_get_queries());
215
 
216
        // Function does not care if you are in the course (unless $checkcourse).
217
        $this->assertTrue(info_module::is_user_visible($cm, $other->id, false));
218
 
219
        // With $checkcourse, check for enrolled, not enrolled, and admin user.
220
        $this->assertTrue(info_module::is_user_visible($cm, $student->id, true));
221
        $this->assertFalse(info_module::is_user_visible($cm, $other->id, true));
222
        $this->assertTrue(info_module::is_user_visible($cm, $admin->id, true));
223
 
224
        // With availability off, the student can access all except the
225
        // visible=0 one.
226
        $this->assertFalse(info_module::is_user_visible($pages[0]->cmid, $student->id, false));
227
        $this->assertTrue(info_module::is_user_visible($pages[1]->cmid, $student->id, false));
228
        $this->assertTrue(info_module::is_user_visible($pages[2]->cmid, $student->id, false));
229
 
230
        // Teacher and admin can even access the visible=0 one.
231
        $this->assertTrue(info_module::is_user_visible($pages[0]->cmid, $teacher->id, false));
232
        $this->assertTrue(info_module::is_user_visible($pages[0]->cmid, $admin->id, false));
233
 
234
        // Now enable availability (and clear cache).
235
        $CFG->enableavailability = true;
236
        get_fast_modinfo($course, 0, true);
237
 
238
        // Student cannot access the activity restricted by its own or by the
239
        // section's availability.
240
        $this->assertFalse(info_module::is_user_visible($pages[1]->cmid, $student->id, false));
241
        $this->assertFalse(info_module::is_user_visible($pages[2]->cmid, $student->id, false));
242
    }
243
 
244
    /**
245
     * Tests the convert_legacy_fields function used in restore.
246
     */
11 efrain 247
    public function test_convert_legacy_fields(): void {
1 efrain 248
        // Check with no availability conditions first.
249
        $rec = (object)array('availablefrom' => 0, 'availableuntil' => 0,
250
                'groupingid' => 7, 'showavailability' => 1);
251
        $this->assertNull(info::convert_legacy_fields($rec, false));
252
 
253
        // Check same list for a section.
254
        $this->assertEquals(
255
                '{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}',
256
                info::convert_legacy_fields($rec, true));
257
 
258
        // Check groupmembersonly with grouping.
259
        $rec->groupmembersonly = 1;
260
        $this->assertEquals(
261
                '{"op":"&","showc":[false],"c":[{"type":"grouping","id":7}]}',
262
                info::convert_legacy_fields($rec, false));
263
 
264
        // Check groupmembersonly without grouping.
265
        $rec->groupingid = 0;
266
        $this->assertEquals(
267
                '{"op":"&","showc":[false],"c":[{"type":"group"}]}',
268
                info::convert_legacy_fields($rec, false));
269
 
270
        // Check start date.
271
        $rec->groupmembersonly = 0;
272
        $rec->availablefrom = 123;
273
        $this->assertEquals(
274
                '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":123}]}',
275
                info::convert_legacy_fields($rec, false));
276
 
277
        // Start date with show = false.
278
        $rec->showavailability = 0;
279
        $this->assertEquals(
280
                '{"op":"&","showc":[false],"c":[{"type":"date","d":">=","t":123}]}',
281
                info::convert_legacy_fields($rec, false));
282
 
283
        // End date.
284
        $rec->showavailability = 1;
285
        $rec->availablefrom = 0;
286
        $rec->availableuntil = 456;
287
        $this->assertEquals(
288
                '{"op":"&","showc":[false],"c":[{"type":"date","d":"<","t":456}]}',
289
                info::convert_legacy_fields($rec, false));
290
 
291
        // All together now.
292
        $rec->groupingid = 7;
293
        $rec->groupmembersonly = 1;
294
        $rec->availablefrom = 123;
295
        $this->assertEquals(
296
                '{"op":"&","showc":[false,true,false],"c":[' .
297
                '{"type":"grouping","id":7},' .
298
                '{"type":"date","d":">=","t":123},' .
299
                '{"type":"date","d":"<","t":456}' .
300
                ']}',
301
                info::convert_legacy_fields($rec, false));
302
        $this->assertEquals(
303
                '{"op":"&","showc":[false,true,false],"c":[' .
304
                '{"type":"grouping","id":7},' .
305
                '{"type":"date","d":">=","t":123},' .
306
                '{"type":"date","d":"<","t":456}' .
307
                ']}',
308
                info::convert_legacy_fields($rec, false, true));
309
    }
310
 
311
    /**
312
     * Tests the add_legacy_availability_condition function used in restore.
313
     */
11 efrain 314
    public function test_add_legacy_availability_condition(): void {
1 efrain 315
        // Completion condition tests.
316
        $rec = (object)array('sourcecmid' => 7, 'requiredcompletion' => 1);
317
        // No previous availability, show = true.
318
        $this->assertEquals(
319
                '{"op":"&","showc":[true],"c":[{"type":"completion","cm":7,"e":1}]}',
320
                info::add_legacy_availability_condition(null, $rec, true));
321
        // No previous availability, show = false.
322
        $this->assertEquals(
323
                '{"op":"&","showc":[false],"c":[{"type":"completion","cm":7,"e":1}]}',
324
                info::add_legacy_availability_condition(null, $rec, false));
325
 
326
        // Existing availability.
327
        $before = '{"op":"&","showc":[true],"c":[{"type":"date","d":">=","t":70}]}';
328
        $this->assertEquals(
329
                '{"op":"&","showc":[true,true],"c":['.
330
                '{"type":"date","d":">=","t":70},' .
331
                '{"type":"completion","cm":7,"e":1}' .
332
                ']}',
333
                info::add_legacy_availability_condition($before, $rec, true));
334
 
335
        // Grade condition tests.
336
        $rec = (object)array('gradeitemid' => 3, 'grademin' => 7, 'grademax' => null);
337
        $this->assertEquals(
338
                '{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000}]}',
339
                info::add_legacy_availability_condition(null, $rec, true));
340
        $rec->grademax = 8;
341
        $this->assertEquals(
342
                '{"op":"&","showc":[true],"c":[{"type":"grade","id":3,"min":7.00000,"max":8.00000}]}',
343
                info::add_legacy_availability_condition(null, $rec, true));
344
        unset($rec->grademax);
345
        unset($rec->grademin);
346
        $this->assertEquals(
347
                '{"op":"&","showc":[true],"c":[{"type":"grade","id":3}]}',
348
                info::add_legacy_availability_condition(null, $rec, true));
349
 
350
        // Note: There is no need to test the grade condition with show
351
        // true/false and existing availability, because this uses the same
352
        // function.
353
    }
354
 
355
    /**
356
     * Tests the add_legacy_availability_field_condition function used in restore.
357
     */
11 efrain 358
    public function test_add_legacy_availability_field_condition(): void {
1 efrain 359
        // User field, normal operator.
360
        $rec = (object)array('userfield' => 'email', 'shortname' => null,
361
                'operator' => 'contains', 'value' => '@');
362
        $this->assertEquals(
363
                '{"op":"&","showc":[true],"c":[' .
364
                '{"type":"profile","op":"contains","sf":"email","v":"@"}]}',
365
                info::add_legacy_availability_field_condition(null, $rec, true));
366
 
367
        // User field, non-value operator.
368
        $rec = (object)array('userfield' => 'email', 'shortname' => null,
369
                'operator' => 'isempty', 'value' => '');
370
        $this->assertEquals(
371
                '{"op":"&","showc":[true],"c":[' .
372
                '{"type":"profile","op":"isempty","sf":"email"}]}',
373
                info::add_legacy_availability_field_condition(null, $rec, true));
374
 
375
        // Custom field.
376
        $rec = (object)array('userfield' => null, 'shortname' => 'frogtype',
377
                'operator' => 'isempty', 'value' => '');
378
        $this->assertEquals(
379
                '{"op":"&","showc":[true],"c":[' .
380
                '{"type":"profile","op":"isempty","cf":"frogtype"}]}',
381
                info::add_legacy_availability_field_condition(null, $rec, true));
382
    }
383
 
384
    /**
385
     * Tests the filter_user_list() and get_user_list_sql() functions.
386
     */
11 efrain 387
    public function test_filter_user_list(): void {
1 efrain 388
        global $CFG, $DB;
389
        require_once($CFG->dirroot . '/course/lib.php');
390
        $this->resetAfterTest();
391
        $CFG->enableavailability = true;
392
 
393
        // Create a course with 2 sections and 2 pages and 3 users.
394
        // Availability is set up initially on the 'page/section 2' items.
395
        $generator = $this->getDataGenerator();
396
        $course = $generator->create_course(
397
                array('numsections' => 2), array('createsections' => true));
398
        $u1 = $generator->create_user();
399
        $u2 = $generator->create_user();
400
        $u3 = $generator->create_user();
401
        $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'), MUST_EXIST);
402
        $allusers = array($u1->id => $u1, $u2->id => $u2, $u3->id => $u3);
403
        $generator->enrol_user($u1->id, $course->id, $studentroleid);
404
        $generator->enrol_user($u2->id, $course->id, $studentroleid);
405
        $generator->enrol_user($u3->id, $course->id, $studentroleid);
406
 
407
        // Page 2 allows access to users 2 and 3, while section 2 allows access
408
        // to users 1 and 2.
409
        $pagegen = $generator->get_plugin_generator('mod_page');
410
        $page = $pagegen->create_instance(array('course' => $course));
411
        $page2 = $pagegen->create_instance(array('course' => $course,
412
                'availability' => '{"op":"|","show":true,"c":[{"type":"mock","filter":[' .
413
                $u2->id . ',' . $u3->id . ']}]}'));
414
        $modinfo = get_fast_modinfo($course);
415
        $section = $modinfo->get_section_info(1);
416
        $section2 = $modinfo->get_section_info(2);
417
        $DB->set_field('course_sections', 'availability',
418
                '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u1->id . ',' . $u2->id .']}]}',
419
                array('id' => $section2->id));
420
        moveto_module($modinfo->get_cm($page2->cmid), $section2);
421
 
422
        // With no restrictions, returns full list.
423
        $info = new info_module($modinfo->get_cm($page->cmid));
424
        $this->assertEquals(array($u1->id, $u2->id, $u3->id),
425
                array_keys($info->filter_user_list($allusers)));
426
        $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
427
 
428
        // Set an availability restriction in database for section 1.
429
        // For the section we set it so it doesn't support filters; for the
430
        // module we have a filter.
431
        $DB->set_field('course_sections', 'availability',
432
                '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}',
433
                array('id' => $section->id));
434
        $DB->set_field('course_modules', 'availability',
435
                '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u3->id .']}]}',
436
                array('id' => $page->cmid));
437
        rebuild_course_cache($course->id, true);
438
        $modinfo = get_fast_modinfo($course);
439
 
440
        // Now it should work (for the module).
441
        $info = new info_module($modinfo->get_cm($page->cmid));
442
        $expected = array($u3->id);
443
        $this->assertEquals($expected,
444
                array_keys($info->filter_user_list($allusers)));
445
        list ($sql, $params) = $info->get_user_list_sql();
446
        $result = $DB->get_fieldset_sql($sql, $params);
447
        sort($result);
448
        $this->assertEquals($expected, $result);
449
        $info = new info_section($modinfo->get_section_info(1));
450
        $this->assertEquals(array($u1->id, $u2->id, $u3->id),
451
                array_keys($info->filter_user_list($allusers)));
452
        $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
453
 
454
        // With availability disabled, module returns full list too.
455
        $CFG->enableavailability = false;
456
        $info = new info_module($modinfo->get_cm($page->cmid));
457
        $this->assertEquals(array($u1->id, $u2->id, $u3->id),
458
                array_keys($info->filter_user_list($allusers)));
459
        $this->assertEquals(array('', array()), $info->get_user_list_sql(true));
460
 
461
        // Check the other section...
462
        $CFG->enableavailability = true;
463
        $info = new info_section($modinfo->get_section_info(2));
464
        $expected = array($u1->id, $u2->id);
465
        $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
466
        list ($sql, $params) = $info->get_user_list_sql(true);
467
        $result = $DB->get_fieldset_sql($sql, $params);
468
        sort($result);
469
        $this->assertEquals($expected, $result);
470
 
471
        // And the module in that section - which has combined the section and
472
        // module restrictions.
473
        $info = new info_module($modinfo->get_cm($page2->cmid));
474
        $expected = array($u2->id);
475
        $this->assertEquals($expected, array_keys($info->filter_user_list($allusers)));
476
        list ($sql, $params) = $info->get_user_list_sql(true);
477
        $result = $DB->get_fieldset_sql($sql, $params);
478
        sort($result);
479
        $this->assertEquals($expected, $result);
480
 
481
        // If the students have viewhiddenactivities, they get past the module
482
        // restriction.
483
        role_change_permission($studentroleid, \context_module::instance($page2->cmid),
484
                'moodle/course:ignoreavailabilityrestrictions', CAP_ALLOW);
485
        $expected = array($u1->id, $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);
491
 
492
        // If they have viewhiddensections, they also get past the section
493
        // restriction.
494
        role_change_permission($studentroleid, \context_course::instance($course->id),
495
                'moodle/course:ignoreavailabilityrestrictions', CAP_ALLOW);
496
        $expected = array($u1->id, $u2->id, $u3->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);
502
    }
503
 
504
    /**
505
     * Tests the info_module class when involved in a recursive call to $cm->name.
506
     */
11 efrain 507
    public function test_info_recursive_name_call(): void {
1 efrain 508
        global $DB;
509
 
510
        $this->resetAfterTest();
511
 
512
        // Create a course and page.
513
        $generator = $this->getDataGenerator();
514
        $course = $generator->create_course();
515
        $page1 = $generator->create_module('page', ['course' => $course->id, 'name' => 'Page1']);
516
 
517
        // Set invalid availability.
518
        $DB->set_field('course_modules', 'availability', 'not valid', ['id' => $page1->cmid]);
519
 
520
        // Get the cm_info object.
521
        $this->setAdminUser();
522
        $modinfo = get_fast_modinfo($course);
523
        $cm1 = $modinfo->get_cm($page1->cmid);
524
 
525
        // At this point we will generate dynamic data for $cm1, which will cause the debugging
526
        // call below.
527
        $this->assertEquals('Page1', $cm1->name);
528
 
529
        $this->assertDebuggingCalled('Error processing availability data for ' .
530
                '&lsquo;Page1&rsquo;: Invalid availability text');
531
    }
532
 
533
    /**
534
     * Test for the is_available_for_all() method of the info base class.
535
     * @covers \core_availability\info_module::is_available_for_all
536
     */
11 efrain 537
    public function test_is_available_for_all(): void {
1 efrain 538
        global $CFG, $DB;
539
        $this->resetAfterTest();
540
        $CFG->enableavailability = 0;
541
 
542
        $generator = $this->getDataGenerator();
543
        $course = $generator->create_course();
544
        $page = $generator->get_plugin_generator('mod_page')->create_instance(['course' => $course]);
545
 
546
        // Set an availability restriction and reset the modinfo cache.
547
        // The enableavailability setting is disabled so this does not take effect yet.
548
        $notavailable = '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}';
549
        $DB->set_field('course_modules', 'availability', $notavailable, ['id' => $page->cmid]);
550
        get_fast_modinfo($course, 0, true);
551
 
552
        // Availability is disabled, so we expect this module to be available for everyone.
553
        $modinfo = get_fast_modinfo($course);
554
        $info = new info_module($modinfo->get_cm($page->cmid));
555
        $this->assertTrue($info->is_available_for_all());
556
 
557
        // Now, enable availability restrictions, and check again.
558
        // This time, we expect it to return false, because of the access restriction.
559
        $CFG->enableavailability = 1;
560
        $this->assertFalse($info->is_available_for_all());
561
    }
1441 ariadna 562
 
563
    /**
564
     * Test update_display_mode function.
565
     *
566
     * @covers \core\plugininfo\availability::update_display_mode
567
     * @dataProvider update_display_mode_provider
568
     *
569
     * @param string $plugin The plugin name.
570
     * @param string $expected The expected data.
571
     */
572
    public function test_update_display_mode(string $plugin, string $expected): void {
573
        global $DB;
574
 
575
        $this->resetAfterTest();
576
 
577
        // Get default value for default display mode.
578
        $availabilityvalue = $DB->get_field('config_plugins', 'value',
579
            ['name' => 'defaultdisplaymode', 'plugin' => "availability_$plugin"]);
580
        $updatedisplaymode = \core\plugininfo\availability::update_display_mode($plugin, true);
581
 
582
        // The default value is not inserted into the table.
583
        // Or the display is updated but the display mode is the same value as the default.
584
        $this->assertFalse($availabilityvalue);
585
        $this->assertFalse($updatedisplaymode);
586
 
587
        // Update display mode for plugins.
588
        $updatedisplaymode = \core\plugininfo\availability::update_display_mode($plugin, false);
589
 
590
        // The function should return true because the display mode value has changed.
591
        $this->assertTrue($updatedisplaymode);
592
 
593
        // Get the updated value for the default display mode.
594
        $availabilityvalue = $DB->get_field('config_plugins', 'value',
595
            ['name' => 'defaultdisplaymode', 'plugin' => "availability_$plugin"]);
596
        $this->assertEquals($expected, $availabilityvalue);
597
    }
598
 
599
    /**
600
     * Data provider for test_update_display_mode().
601
     *
602
     * @return array
603
     */
604
    public static function update_display_mode_provider(): array {
605
        return [
606
            'Update display mode for completion' => [
607
                'completion',
608
                '1',
609
            ],
610
            'Update display mode for grade' => [
611
                'grade',
612
                '1',
613
            ],
614
            'Update display mode for group' => [
615
                'group',
616
                '1',
617
            ],
618
        ];
619
    }
1 efrain 620
}