Merge branch 'MDL-79186-401' of https://github.com/paulholden/moodle into MOODLE_401_...
[moodle.git] / mod / lesson / tests / dates_test.php
blob4d4345cb665b886c49c5300064b0c24a2c368bcc
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 * Contains unit tests for mod_lesson\dates.
20 * @package mod_lesson
21 * @category test
22 * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 declare(strict_types=1);
28 namespace mod_lesson;
30 use advanced_testcase;
31 use cm_info;
32 use core\activity_dates;
34 /**
35 * Class for unit testing mod_lesson\dates.
37 * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class dates_test extends advanced_testcase {
42 /**
43 * Data provider for get_dates_for_module().
44 * @return array[]
46 public function get_dates_for_module_provider(): array {
47 $now = time();
48 $before = $now - DAYSECS;
49 $earlier = $before - DAYSECS;
50 $after = $now + DAYSECS;
51 $later = $after + DAYSECS;
53 return [
54 'without any dates' => [
55 null, null, null, null, null, null, []
57 'only with opening time' => [
58 $after, null, null, null, null, null, [
59 ['label' => get_string('activitydate:opens', 'course'), 'timestamp' => $after, 'dataid' => 'available'],
62 'only with closing time' => [
63 null, $after, null, null, null, null, [
64 ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $after, 'dataid' => 'deadline'],
67 'with both times' => [
68 $after, $later, null, null, null, null, [
69 ['label' => get_string('activitydate:opens', 'course'), 'timestamp' => $after, 'dataid' => 'available'],
70 ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
73 'between the dates' => [
74 $before, $after, null, null, null, null, [
75 ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $before, 'dataid' => 'available'],
76 ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $after, 'dataid' => 'deadline'],
79 'dates are past' => [
80 $earlier, $before, null, null, null, null, [
81 ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier, 'dataid' => 'available'],
82 ['label' => get_string('activitydate:closed', 'course'), 'timestamp' => $before, 'dataid' => 'deadline'],
85 'with user override' => [
86 $before, $after, $earlier, $later, null, null, [
87 ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier, 'dataid' => 'available'],
88 ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
91 'with group override' => [
92 $before, $after, null, null, $earlier, $later, [
93 ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier, 'dataid' => 'available'],
94 ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
97 'with both user and group overrides' => [
98 $before, $after, $earlier, $later, $earlier - DAYSECS, $later + DAYSECS, [
99 ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier, 'dataid' => 'available'],
100 ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
107 * Test for get_dates_for_module().
109 * @dataProvider get_dates_for_module_provider
110 * @param int|null $available The 'available from' value of the lesson.
111 * @param int|null $deadline The lesson's deadline.
112 * @param int|null $useravailable The user override for opening the lesson.
113 * @param int|null $userdeadline The user override for deadline of the lesson.
114 * @param int|null $groupavailable The group override for opening the lesson.
115 * @param int|null $groupuserdeadline The group override for deadline of the lesson.
116 * @param array $expected The expected value of calling get_dates_for_module()
118 public function test_get_dates_for_module(?int $available, ?int $deadline,
119 ?int $useravailable, ?int $userdeadline,
120 ?int $groupavailable, ?int $groupuserdeadline,
121 array $expected) {
122 $this->resetAfterTest();
123 $generator = $this->getDataGenerator();
124 /** @var \mod_lesson_generator $lessongenerator */
125 $lessongenerator = $generator->get_plugin_generator('mod_lesson');
127 $course = $generator->create_course();
128 $user = $generator->create_user();
129 $generator->enrol_user($user->id, $course->id);
131 $data = ['course' => $course->id];
132 if ($available) {
133 $data['available'] = $available;
135 if ($deadline) {
136 $data['deadline'] = $deadline;
138 $this->setAdminUser();
139 $lesson = $lessongenerator->create_instance($data);
141 if ($useravailable || $userdeadline || $groupavailable || $groupuserdeadline) {
142 $generator->enrol_user($user->id, $course->id);
143 $group = $generator->create_group(['courseid' => $course->id]);
144 $generator->create_group_member(['groupid' => $group->id, 'userid' => $user->id]);
146 if ($useravailable || $userdeadline) {
147 $lessongenerator->create_override([
148 'lessonid' => $lesson->id,
149 'userid' => $user->id,
150 'available' => $useravailable,
151 'deadline' => $userdeadline,
155 if ($groupavailable || $groupuserdeadline) {
156 $lessongenerator->create_override([
157 'lessonid' => $lesson->id,
158 'groupid' => $group->id,
159 'available' => $groupavailable,
160 'deadline' => $groupuserdeadline,
165 $this->setUser($user);
167 $cm = get_coursemodule_from_instance('lesson', $lesson->id);
168 // Make sure we're using a cm_info object.
169 $cm = cm_info::create($cm);
171 $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
173 $this->assertEquals($expected, $dates);