Commit | Line | Data |
---|---|---|
c61b3b4a MN |
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 event observers used by the weeks course format. | |
19 | * | |
20 | * @package format_weeks | |
21 | * @copyright 2017 Mark Nelson <markn@moodle.com> | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | defined('MOODLE_INTERNAL') || die(); | |
26 | ||
27 | /** | |
28 | * Unit tests for the event observers used by the weeks course format. | |
29 | * | |
30 | * @package format_weeks | |
31 | * @copyright 2017 Mark Nelson <markn@moodle.com> | |
32 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
33 | */ | |
34 | class format_weeks_observer_testcase extends advanced_testcase { | |
35 | ||
36 | /** | |
37 | * Test setup. | |
38 | */ | |
39 | public function setUp() { | |
40 | $this->resetAfterTest(); | |
41 | } | |
42 | ||
43 | /** | |
44 | * Tests when we update a course with automatic end date set. | |
45 | */ | |
46 | public function test_course_updated_with_automatic_end_date() { | |
47 | global $DB; | |
48 | ||
49 | // Generate a course with some sections. | |
50 | $numsections = 6; | |
51 | $startdate = time(); | |
52 | $course = $this->getDataGenerator()->create_course(array( | |
53 | 'numsections' => $numsections, | |
54 | 'format' => 'weeks', | |
55 | 'startdate' => $startdate, | |
56 | 'automaticenddate' => 1)); | |
57 | ||
58 | // Ok, let's update the course start date. | |
7aa3925c MG |
59 | $newstartdate = $startdate + WEEKSECS; |
60 | update_course((object)['id' => $course->id, 'startdate' => $newstartdate]); | |
c61b3b4a MN |
61 | |
62 | // Get the updated course end date. | |
63 | $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id)); | |
64 | ||
65 | $format = course_get_format($course->id); | |
7aa3925c MG |
66 | $this->assertEquals($numsections, $format->get_last_section_number()); |
67 | $this->assertEquals($newstartdate, $format->get_course()->startdate); | |
68 | $dates = $format->get_section_dates($numsections); | |
c61b3b4a MN |
69 | $this->assertEquals($dates->end, $enddate); |
70 | } | |
71 | ||
72 | /** | |
73 | * Tests when we update a course with automatic end date set but no actual change is made. | |
74 | */ | |
75 | public function test_course_updated_with_automatic_end_date_no_change() { | |
76 | global $DB; | |
77 | ||
78 | // Generate a course with some sections. | |
79 | $course = $this->getDataGenerator()->create_course(array( | |
80 | 'numsections' => 6, | |
81 | 'format' => 'weeks', | |
82 | 'startdate' => time(), | |
83 | 'automaticenddate' => 1)); | |
84 | ||
85 | // Get the end date from the DB as the results will have changed from $course above after observer processing. | |
86 | $createenddate = $DB->get_field('course', 'enddate', array('id' => $course->id)); | |
87 | ||
88 | // Ok, let's update the course - but actually not change anything. | |
7aa3925c | 89 | update_course((object)['id' => $course->id]); |
c61b3b4a MN |
90 | |
91 | // Get the updated course end date. | |
92 | $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id)); | |
93 | ||
94 | // Confirm nothing changed. | |
95 | $this->assertEquals($createenddate, $updateenddate); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Tests when we update a course without automatic end date set. | |
100 | */ | |
101 | public function test_course_updated_without_automatic_end_date() { | |
102 | global $DB; | |
103 | ||
104 | // Generate a course with some sections. | |
105 | $startdate = time(); | |
106 | $enddate = $startdate + WEEKSECS; | |
107 | $course = $this->getDataGenerator()->create_course(array( | |
108 | 'numsections' => 6, | |
109 | 'format' => 'weeks', | |
110 | 'startdate' => $startdate, | |
111 | 'enddate' => $enddate, | |
112 | 'automaticenddate' => 0)); | |
113 | ||
114 | // Ok, let's update the course start date. | |
7aa3925c MG |
115 | $newstartdate = $startdate + WEEKSECS; |
116 | update_course((object)['id' => $course->id, 'startdate' => $newstartdate]); | |
c61b3b4a MN |
117 | |
118 | // Get the updated course end date. | |
119 | $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id)); | |
120 | ||
121 | // Confirm nothing changed. | |
122 | $this->assertEquals($enddate, $updateenddate); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Tests when we adding a course section with automatic end date set. | |
127 | */ | |
128 | public function test_course_section_created_with_automatic_end_date() { | |
129 | global $DB; | |
130 | ||
131 | $numsections = 6; | |
132 | $course = $this->getDataGenerator()->create_course(array( | |
133 | 'numsections' => $numsections, | |
134 | 'format' => 'weeks', | |
135 | 'startdate' => time(), | |
136 | 'automaticenddate' => 1)); | |
137 | ||
138 | // Add a section to the course. | |
139 | course_create_section($course->id); | |
140 | ||
141 | // Get the updated course end date. | |
142 | $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id)); | |
143 | ||
144 | $format = course_get_format($course->id); | |
7aa3925c | 145 | $dates = $format->get_section_dates($numsections + 1); |
c61b3b4a MN |
146 | |
147 | // Confirm end date was updated. | |
148 | $this->assertEquals($enddate, $dates->end); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Tests when we deleting a course section with automatic end date set. | |
153 | */ | |
154 | public function test_course_section_deleted_with_automatic_end_date() { | |
155 | global $DB; | |
156 | ||
157 | // Generate a course with some sections. | |
158 | $numsections = 6; | |
159 | $course = $this->getDataGenerator()->create_course(array( | |
160 | 'numsections' => $numsections, | |
161 | 'format' => 'weeks', | |
162 | 'startdate' => time(), | |
163 | 'automaticenddate' => 1)); | |
164 | ||
165 | // Add a section to the course. | |
166 | course_delete_section($course, $numsections); | |
167 | ||
168 | // Get the updated course end date. | |
169 | $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id)); | |
170 | ||
171 | $format = course_get_format($course->id); | |
7aa3925c | 172 | $dates = $format->get_section_dates($numsections - 1); |
c61b3b4a MN |
173 | |
174 | // Confirm end date was updated. | |
175 | $this->assertEquals($enddate, $dates->end); | |
176 | } | |
177 | } |