Proyectos de Subversion Moodle

Rev

| 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
/**
18
 * Unit tests for the relativedate condition.
19
 *
20
 * @package   availability_relativedate
21
 * @copyright 2022 eWallah.net
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace availability_relativedate;
26
 
27
use availability_relativedate\condition;
28
use context_module;
29
use core\event\course_module_completion_updated;
30
use core_availability\{tree, mock_info, info_module};
31
use stdClass;
32
 
33
/**
34
 * Unit tests for the relativedate condition.
35
 *
36
 * @package   availability_relativedate
37
 * @copyright 2022 eWallah.net
38
 * @author    Renaat Debleu <info@eWallah.net>
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 * @coversDefaultClass \availability_relativedate\condition
41
 */
42
final class condition_test extends \advanced_testcase {
43
    /** @var stdClass course. */
44
    private $course;
45
 
46
    /** @var stdClass user. */
47
    private $user;
48
 
49
    /**
50
     * Create course and page.
51
     */
52
    public function setUp(): void {
53
        global $CFG;
54
        require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
55
        require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info_module.php');
56
        require_once($CFG->libdir . '/completionlib.php');
57
        $this->resetAfterTest();
58
        $this->setAdminUser();
59
        $CFG->enablecompletion = true;
60
        $CFG->enableavailability = true;
61
        set_config('enableavailability', true);
62
        $dg = $this->getDataGenerator();
63
        $now = time();
64
        $this->course = $dg->create_course(['startdate' => $now, 'enddate' => $now + 7 * WEEKSECS, 'enablecompletion' => 1]);
65
        $this->user = $dg->create_user(['timezone' => 'UTC']);
66
        $dg->enrol_user($this->user->id, $this->course->id, 5, time());
67
    }
68
 
69
    /**
70
     * Relative dates tree provider.
71
     */
72
    public static function tree_provider(): array {
73
        return [
74
            'After start course' => [2, 1, 1, "+2 hour", "From"],
75
            'Before end course' => [3, 2, 2, '-3 day', 'Until'],
76
            'After end enrol' => [4, 3, 3, '+4 week', 'From'],
77
            'After end method' => [4, 3, 4, '+4 week', 'From'],
78
            'After end course' => [3, 2, 5, '+3 day', 'From'],
79
            'Before start course' => [2, 1, 6, "-2 hour", "Until"],
80
        ];
81
    }
82
 
83
    /**
84
     * Test tree.
85
     *
86
     * @dataProvider tree_provider
87
     * @param int $n number to skip
88
     * @param int $d Minute - hour - day - week  - month
89
     * @param int $s relative to
90
     * @param string $str
91
     * @param string $result
92
     * @covers \availability_relativedate\condition
93
     */
94
    public function test_tree($n, $d, $s, $str, $result): void {
95
        $arr = (object)['type' => 'relativedate', 'n' => $n, 'd' => $d, 's' => $s, 'm' => 9999999];
96
        $stru = (object)['op' => '|', 'show' => true, 'c' => [$arr]];
97
        $tree = new tree($stru);
98
        $this->assertFalse($tree->is_available_for_all());
99
        $this->setUser($this->user);
100
        $info = new mock_info($this->course, $this->user->id);
101
        $strf = get_string('strftimedatetime', 'langconfig');
102
        $nau = 'Not available unless:';
103
        $calc = userdate(strtotime($str, $this->get_reldate($s)), $strf, 0);
104
        $this->assertEquals("$nau $result $calc", $tree->get_full_information($info));
105
        $tree->check_available(false, $info, false, $this->user->id)->is_available();
106
    }
107
 
108
    /**
109
     * Tests relative module.
110
     * @covers \availability_relativedate\condition
111
     */
112
    public function test_relative_module(): void {
113
        $this->setTimezone('UTC');
114
        $dg = $this->getDataGenerator();
115
        $page = $dg->get_plugin_generator('mod_page')->create_instance(['course' => $this->course]);
116
        $stru = (object)['op' => '|', 'show' => true,
117
            'c' => [(object)['type' => 'relativedate', 'n' => 7, 'd' => 0, 's' => 7, 'm' => $page->cmid]],
118
        ];
119
        $tree = new tree($stru);
120
        $this->setUser($this->user);
121
        $info = new mock_info($this->course, $this->user->id);
122
        [$sql, $params] = $tree->get_user_list_sql(false, $info, false);
123
        $this->assertEquals('', $sql);
124
        $this->assertEquals([], $params);
125
        // 7 Minutes after completion of module.
126
        $this->assertStringContainsString('7 minutes after completion of activity', $tree->get_full_information($info));
127
        $this->do_cron();
128
        $this->assertFalse($tree->is_available_for_all());
129
    }
130
 
131
    /**
132
     * Relative dates description provider.
133
     */
134
    public static function description_provider(): array {
135
        return [
136
            'After start course' => [2, 1, 1, '+2 hour', 'From', 'Until', '2 hours after course start date'],
137
            'Before end course' => [3, 2, 2, '-3 day', 'Until', 'From', '3 days before course end date'],
138
            'After end enrol' => [4, 3, 3, '+4 week', 'From', 'Until', '4 weeks after user enrolment date'],
139
            'After end method' => [4, 3, 4, '+4 week', 'From', 'Until', '4 weeks after enrolment method end date'],
140
            'After end course' => [3, 2, 5, '+3 day', 'From', 'Until', '3 days after course end date'],
141
            'Before start course' => [2, 1, 6, '-2 hour', 'Until', 'From', '2 hours before course start date'],
142
        ];
143
    }
144
 
145
    /**
146
     * Test description.
147
     *
148
     * @dataProvider description_provider
149
     * @param int $n number to skip
150
     * @param int $d Minute - hour - day - week  - month
151
     * @param int $s relative to
152
     * @param string $str
153
     * @param string $result1
154
     * @param string $result2
155
     * @param string $result3
156
     * @covers \availability_relativedate\condition
157
     */
158
    public function test_description($n, $d, $s, $str, $result1, $result2, $result3): void {
159
        $strf = get_string('strftimedatetime', 'langconfig');
160
        $nau = 'Not available unless:';
161
        $info = new mock_info($this->course, $this->user->id);
162
        $this->setUser($this->user);
163
        $cond = new condition((object)['type' => 'relativedate', 'n' => $n, 'd' => $d, 's' => $s, 'm' => 99999]);
164
        $calc = userdate(strtotime($str, $this->get_reldate($s)), $strf);
165
        $this->assertEquals("$result1 $calc", $cond->get_description(true, false, $info));
166
        $this->assertEquals("$result2 $calc", $cond->get_description(true, true, $info));
167
        $this->assertEquals("$result1 $calc", $cond->get_description(false, false, $info));
168
        $this->assertEquals("$result2 $calc", $cond->get_description(false, true, $info));
169
        $this->assertEquals("$nau $result1 $calc", $cond->get_standalone_description(false, false, $info));
170
        $this->assertEquals("$nau $result2 $calc", $cond->get_standalone_description(false, true, $info));
171
 
172
        $this->setAdminUser();
173
        $this->assertStringContainsString($result3, $cond->get_description(true, false, $info));
174
        $this->assertNotEmpty($cond->get_standalone_description(false, false, $info));
175
    }
176
 
177
    /**
178
     * Tests the get_description and get_standalone_description functions.
179
     * @covers \availability_relativedate\condition
180
     */
181
    public function test_get_description(): void {
182
        global $DB;
183
        $this->get_reldate(4);
184
        $info = new mock_info($this->course, $this->user->id);
185
        $this->setUser($this->user);
186
 
187
        $pg = $this->getDataGenerator()->get_plugin_generator('mod_page');
188
        $page0 = $pg->create_instance(['course' => $this->course, 'completion' => COMPLETION_TRACKING_MANUAL]);
189
        $page1 = $pg->create_instance(['course' => $this->course, 'completion' => COMPLETION_TRACKING_MANUAL]);
190
 
191
        $str = '{"op":"|","show":true,"c":[{"type":"relativedate","n":4,"d":4,"s":7,"m":' . $page1->cmid . '}]}';
192
        $DB->set_field('course_modules', 'availability', $str, ['id' => $page0->cmid]);
193
        rebuild_course_cache($this->course->id, true);
194
        $cond = new condition((object)['type' => 'relativedate', 'n' => 4, 'd' => 4, 's' => 7, 'm' => $page1->cmid]);
195
        $this->assertStringContainsString("4 months after completion of activity", $cond->get_description(true, false, $info));
196
        $this->assertStringContainsString("4 months after completion of activity", $cond->get_description(true, true, $info));
197
        $this->assertStringContainsString("4 months after completion of activity", $cond->get_description(false, false, $info));
198
        $this->assertStringContainsString("4 months after completion of activity", $cond->get_description(false, true, $info));
199
        $this->assertFalse($cond->completion_value_used($this->course, $page0->cmid));
200
        $this->assertTrue($cond->completion_value_used($this->course, $page1->cmid));
201
 
202
        $modinfo = get_fast_modinfo($this->course);
203
        $str = '{"op":"|","show":true,"c":[{"type":"relativedate","n":4,"d":4,"s":7,"m":' . $page0->cmid . '}]}';
204
        foreach ($modinfo->get_section_info_all() as $section) {
205
            $DB->set_field('course_sections', 'availability', $str, ['id' => $section->id]);
206
        }
207
        $this->do_cron();
208
        $cond = new condition((object)['type' => 'relativedate', 'n' => 4, 'd' => 4, 's' => 7, 'm' => $page1->cmid]);
209
        $this->assertTrue($cond->completion_value_used($this->course, $page0->cmid));
210
        $this->assertTrue($cond->completion_value_used($this->course, $page1->cmid));
211
        $completion = new \completion_info($this->course);
212
        $completion->reset_all_state($modinfo->get_cm($page1->cmid));
213
 
214
        $cond = new condition((object)['type' => 'relativedate', 'n' => 4, 'd' => 4, 's' => 7, 'm' => $page0->cmid]);
215
        $this->assertTrue($cond->update_dependency_id('course_sections', $page0->cmid, 3));
216
        $this->assertFalse($cond->update_dependency_id('course_modules', $page1->cmid, 3));
217
        $cond = new condition((object)['type' => 'relativedate', 'n' => 4, 'd' => 4, 's' => 7, 'm' => $page1->cmid]);
218
        $this->assertTrue($cond->update_dependency_id('course_modules', $page1->cmid, 4));
219
        $this->assertFalse($cond->update_dependency_id('course_modules', $page1->cmid, -1));
220
    }
221
 
222
    /**
223
     * Tests a course with no enddate.
224
     * @covers \availability_relativedate\condition
225
     */
226
    public function test_no_enddate(): void {
227
        global $DB, $USER;
228
        $dg = $this->getDataGenerator();
229
        $now = time();
230
        $course1 = $dg->create_course(['enablecompletion' => 1]);
231
        $course2 = $dg->create_course(['enddate' => $now + 14 * WEEKSECS, 'enablecompletion' => 1]);
232
        $user = $dg->create_user();
233
        $roleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
234
        $dg->enrol_user($user->id, $course1->id, $roleid);
235
        $dg->enrol_user($user->id, $course2->id, $roleid);
236
        $pg = $this->getDataGenerator()->get_plugin_generator('mod_page');
237
        $page1 = $pg->create_instance(['course' => $course1, 'completion' => COMPLETION_TRACKING_MANUAL]);
238
        $page2 = $pg->create_instance(['course' => $course2, 'completion' => COMPLETION_TRACKING_MANUAL]);
239
        $modinfo1 = get_fast_modinfo($course1);
240
        $modinfo2 = get_fast_modinfo($course2);
241
        $cm1 = $modinfo1->get_cm($page1->cmid);
242
        $cm2 = $modinfo2->get_cm($page2->cmid);
243
        $info = new info_module($cm1);
244
        $cond = new condition((object)['type' => 'relativedate', 'n' => 7, 'd' => 2, 's' => 2, 'm' => 1]);
245
        $information = $cond->get_description(true, false, $info);
246
        $this->assertEquals('This course has no end date', $information);
247
        $this->assertEquals('{relativedate: 7 days before course end date}', "$cond");
248
        // No enddate => Never available.
249
        $this->assertFalse($cond->is_available(false, $info, false, $user->id));
250
        $this->assertFalse($cond->is_available(true, $info, false, $user->id));
251
        $info = new info_module($cm2);
252
        $information = $cond->get_description(true, false, $info);
253
        $strf = get_string('strftimedatetime', 'langconfig');
254
        $this->assertStringNotContainsString('(No course enddate)', $information);
255
        $str = userdate($course2->enddate - (7 * 24 * 3600), $strf);
256
        $this->assertEquals("Until $str (7 days before course end date)", $information);
257
        $this->assertEquals('{relativedate: 7 days before course end date}', "$cond");
258
        $this->assertFalse($cond->is_available(false, $info, false, $user->id));
259
        $this->assertTrue($cond->is_available(true, $info, false, $user->id));
260
        $this->assertFalse($cond->is_available(false, $info, false, null));
261
        $this->assertTrue($cond->is_available(true, $info, false, null));
262
 
263
        $cond = new condition((object)['type' => 'relativedate', 'n' => 7, 'd' => 2, 's' => 3, 'm' => 1]);
264
        $information = $cond->get_description(true, false, $info);
265
        $this->assertEquals('(7 days after user enrolment date)', $information);
266
        $this->assertFalse($cond->is_available(false, $info, false, $USER->id));
267
        $this->assertFalse($cond->is_available(true, $info, false, $USER->id));
268
 
269
        $cond = new condition((object)['type' => 'relativedate', 'n' => 7, 'd' => 2, 's' => 4, 'm' => 1]);
270
        $information = $cond->get_description(false, false, $info);
271
        $this->assertEquals('(7 days after enrolment method end date)', $information);
272
 
273
        $info = new info_module($cm1);
274
        $cond = new condition((object)['type' => 'relativedate', 'n' => 7, 'd' => 2, 's' => 5, 'm' => 1]);
275
        $information = $cond->get_description(false, false, $info);
276
        $this->assertEquals('This course has no end date', $information);
277
 
278
        $cond = new condition((object)['type' => 'relativedate', 'n' => 7, 'd' => 2, 's' => 6, 'm' => 1]);
279
        $information = $cond->get_description(false, false, $info);
280
        $str = userdate($course2->startdate - (7 * 24 * 3600), $strf);
281
        $this->assertEquals("Until $str", $information);
282
        $this->assertEquals('{relativedate: 7 days before course start date}', "$cond");
283
 
284
        $cond = new condition((object)['type' => 'relativedate', 'n' => 7, 'd' => 2, 's' => 6, 'm' => 9999999]);
285
        $information = $cond->get_description(false, false, $info);
286
        $this->assertEquals("Until $str", $information);
287
        $this->assertEquals('{relativedate: 7 days before course start date}', "$cond");
288
 
289
        $cond = new condition((object)['type' => 'relativedate', 'n' => 7, 'd' => 2, 's' => 6, 'm' => -1]);
290
        $information = $cond->get_description(false, false, $info);
291
        $this->assertEquals("Until $str", $information);
292
        $this->assertEquals('{relativedate: 7 days before course start date}', "$cond");
293
    }
294
 
295
    /**
296
     * Tests debug strings (reflection).
297
     * @covers \availability_relativedate\condition
298
     */
299
    public function test_reflection_debug_strings(): void {
300
        $name = 'availability_relativedate\condition';
301
        $daybefore = ' 1 ' . get_string('day', 'availability_relativedate');
302
        $pg = self::getDataGenerator()->get_plugin_generator('mod_page');
303
        $page0 = $pg->create_instance(['course' => $this->course, 'completion' => COMPLETION_TRACKING_MANUAL]);
304
 
305
        $condition = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 7, 'm' => $page0->cmid]);
306
        $result = \phpunit_util::call_internal_method($condition, 'get_debug_string', [], $name);
307
        $this->assertEquals(
308
            $daybefore . ' ' .
309
            get_string('datecompletion', 'availability_relativedate')
310
            . ' ' . condition::description_cm_name($page0->cmid),
311
            $result
312
        );
313
 
314
        $condition = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 7, 'm' => 999999]);
315
        $result = \phpunit_util::call_internal_method($condition, 'get_debug_string', [], $name);
316
        $this->assertStringContainsString(get_string('missing', 'availability_relativedate'), $result);
317
    }
318
 
319
    /**
320
     * Tests a reflection.
321
     * @covers \availability_relativedate\condition
322
     */
323
    public function test_reflection_calc(): void {
324
        global $DB;
325
        $name = 'availability_relativedate\condition';
326
        $pg = self::getDataGenerator()->get_plugin_generator('mod_page');
327
        $page0 = $pg->create_instance(['course' => $this->course, 'completion' => COMPLETION_TRACKING_MANUAL]);
328
        $context = context_module::instance($page0->cmid);
329
        $activitycompletion = $this->create_course_module_completion($page0->cmid);
330
        $arr = [
331
            'objectid' => $activitycompletion->id,
332
            'relateduserid' => $this->user->id,
333
            'context' => $context,
334
        ];
335
        course_module_completion_updated::create($arr)->trigger();
336
 
337
        $condition1 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 1, 'm' => 999999]);
338
        $result1 = \phpunit_util::call_internal_method($condition1, 'calc', [$this->course, $this->user->id], $name);
339
        $this->assertEquals($this->course->startdate + DAYSECS, $result1);
340
 
341
        $condition2 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 2, 'm' => 999999]);
342
        $result2 = \phpunit_util::call_internal_method($condition2, 'calc', [$this->course, $this->user->id], $name);
343
        $this->assertEquals($this->course->enddate - DAYSECS, $result2);
344
 
345
        self::getDataGenerator()->enrol_user($this->user->id, $this->course->id);
346
        $enrol1 = $DB->get_record('user_enrolments', ['userid' => $this->user->id]);
347
        $condition31 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 3, 'm' => 999999]);
348
        $result31 = \phpunit_util::call_internal_method($condition31, 'calc', [$this->course, $this->user->id], $name);
349
        $this->assertEquals($enrol1->timecreated + DAYSECS, $result31);
350
 
351
        $user2 = self::getDataGenerator()->create_user(['timezone' => 'UTC']);
352
        self::getDataGenerator()->enrol_user(
353
            $user2->id,
354
            $this->course->id,
355
            null,
356
            'manual',
357
            (int)$this->course->startdate + HOURSECS,
358
            (int)$this->course->startdate + HOURSECS * 24
359
        );
360
        $enrol2 = $DB->get_record('user_enrolments', ['userid' => $user2->id]);
361
        $condition32 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 3, 'm' => 999999]);
362
        $result32 = \phpunit_util::call_internal_method($condition32, 'calc', [$this->course, $user2->id], $name);
363
        $this->assertEquals((int)$enrol2->timestart + DAYSECS, $result32);
364
        $this->assertEquals((int)$this->course->startdate + HOURSECS * 25, $result32);
365
 
366
        $courseself = $DB->get_record('enrol', ['courseid' => $this->course->id, 'enrol' => 'manual']);
367
        $courseself->enrolenddate = $this->course->enddate - 12 * HOURSECS;
368
        $DB->update_record('enrol', $courseself);
369
        $condition4 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 4, 'm' => 999999]);
370
        $result4 = \phpunit_util::call_internal_method($condition4, 'calc', [$this->course, $this->user->id], $name);
371
        $this->assertEquals($courseself->enrolenddate + DAYSECS, $result4);
372
 
373
        $condition5 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 5, 'm' => 999999]);
374
        $result5 = \phpunit_util::call_internal_method($condition5, 'calc', [$this->course, $this->user->id], $name);
375
        $this->assertEquals($this->course->enddate + DAYSECS, $result5);
376
 
377
        $condition6 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 6, 'm' => 999999]);
378
        $result6 = \phpunit_util::call_internal_method($condition6, 'calc', [$this->course, $this->user->id], $name);
379
        $this->assertEquals($this->course->startdate - DAYSECS, $result6);
380
 
381
        $condition71 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 7, 'm' => 0]);
382
        $result71 = \phpunit_util::call_internal_method($condition71, 'calc', [$this->course, $this->user->id], $name);
383
        $this->assertEquals(0, $result71);
384
 
385
        $condition72 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 7, 'm' => $page0->cmid]);
386
        $result72 = \phpunit_util::call_internal_method($condition72, 'calc', [$this->course, $this->user->id], $name);
387
        $this->assertEquals($activitycompletion->timemodified + DAYSECS, $result72);
388
 
389
        $condition73 = new condition((object)['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 7, 'm' => 999999]);
390
        $result73 = \phpunit_util::call_internal_method($condition73, 'calc', [$this->course, $this->user->id], $name);
391
        $this->assertEquals(0, $result73);
392
    }
393
 
394
    /**
395
     * Create course module completion.
396
     *
397
     * @param int $cmid course module id
398
     * @return stdClass
399
     */
400
    private function create_course_module_completion(int $cmid): stdClass {
401
        global $DB;
402
        $activitycompletion = new stdClass();
403
        $activitycompletion->coursemoduleid = $cmid;
404
        $activitycompletion->userid = $this->user->id;
405
        $activitycompletion->viewed = null;
406
        $activitycompletion->overrideby = null;
407
        $activitycompletion->completionstate = 1;
408
        $activitycompletion->timemodified = time();
409
        $activitycompletion->id = $DB->insert_record('course_modules_completion', $activitycompletion);
410
        return $activitycompletion;
411
    }
412
 
413
    /**
414
     * Tests the autoupdate event.
415
     * @covers \availability_relativedate\autoupdate
416
     */
417
    public function test_autoupdate(): void {
418
        global $DB;
419
        $pg = $this->getDataGenerator()->get_plugin_generator('mod_page');
420
        $page0 = $pg->create_instance(['course' => $this->course, 'completion' => COMPLETION_TRACKING_MANUAL]);
421
        $page1 = $pg->create_instance(['course' => $this->course, 'completion' => COMPLETION_TRACKING_MANUAL]);
422
        $str = '{"op":"|","show":true,"c":[{"type":"relativedate","n":4,"d":4,"s":7,"m":' . $page0->cmid . '}]}';
423
        $DB->set_field('course_modules', 'availability', $str, ['id' => $page1->cmid]);
424
        $this->do_cron();
425
        $event = \core\event\course_module_deleted::create([
426
            'objectid' => $page0->cmid,
427
            'relateduserid' => 1,
428
            'context' => \context_course::instance($this->course->id),
429
            'courseid' => $this->course->id,
430
            'other' => [
431
                'relateduserid' => 1,
432
                'modulename' => 'page',
433
                'instanceid' => $page0->cmid,
434
                'name' => $page0->name,
435
            ],
436
        ]);
437
        $event->trigger();
438
 
439
        $actual = $DB->get_record('course_modules', ['id' => $page1->cmid]);
440
        self::assertEquals(
441
            '{"op":"|","show":true,"c":[{"type":"relativedate","n":4,"d":4,"s":7,"m":-1}]}',
442
            $actual->availability
443
        );
444
    }
445
 
446
    /**
447
     * Cron function.
448
     * @coversNothing
449
     */
450
    private function do_cron(): void {
451
        $task = new \core\task\completion_regular_task();
452
        ob_start();
453
        $task->execute();
454
        sleep(1);
455
        $task->execute();
456
        \phpunit_util::run_all_adhoc_tasks();
457
        ob_end_clean();
458
        get_fast_modinfo(0, 0, true);
459
        rebuild_course_cache($this->course->id);
460
    }
461
 
462
    /**
463
     * Which date.
464
     * @coversNothing
465
     *
466
     * @param int $s
467
     * @return int
468
     */
469
    private function get_reldate($s): int {
470
        global $DB;
471
        switch ($s) {
472
            case 1:
473
            case 6:
474
                return $this->course->startdate;
475
            case 2:
476
            case 5:
477
                return $this->course->enddate;
478
            case 3:
479
            case 4:
480
                $now = time();
481
                $selfplugin = enrol_get_plugin('self');
482
                $instance = $DB->get_record('enrol', ['courseid' => $this->course->id, 'enrol' => 'self'], '*', MUST_EXIST);
483
                $DB->set_field('enrol', 'enrolenddate', $now + 1000, ['id' => $instance->id]);
484
                $instance = $DB->get_record('enrol', ['courseid' => $this->course->id, 'enrol' => 'self'], '*', MUST_EXIST);
485
                $selfplugin->enrol_user($instance, $this->user->id, 5, $now);
486
                return ($s === 3) ? $now : $now + 1000;
487
        }
488
        return 0;
489
    }
490
}