2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for the event observers used by the weeks course format.
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
25 defined('MOODLE_INTERNAL') ||
die();
28 * Unit tests for the event observers used by the weeks course format.
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
34 class format_weeks_observer_testcase
extends advanced_testcase
{
39 public function setUp() {
40 $this->resetAfterTest();
44 * Tests when we update a course with automatic end date set.
46 public function test_course_updated_with_automatic_end_date() {
49 // Generate a course with some sections.
52 $course = $this->getDataGenerator()->create_course(array(
53 'numsections' => $numsections,
55 'startdate' => $startdate,
56 'automaticenddate' => 1));
58 // Ok, let's update the course start date.
59 $course->startdate
= $startdate + WEEKSECS
;
60 update_course($course);
62 // Get the updated course end date.
63 $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id
));
65 $format = course_get_format($course->id
);
66 $dates = $format->get_section_dates($numsections, $course->startdate
);
68 // Confirm the end date is the number of weeks ahead of the start date.
69 $this->assertEquals($dates->end
, $enddate);
73 * Tests when we update a course with automatic end date set but no actual change is made.
75 public function test_course_updated_with_automatic_end_date_no_change() {
78 // Generate a course with some sections.
79 $course = $this->getDataGenerator()->create_course(array(
82 'startdate' => time(),
83 'automaticenddate' => 1));
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
));
88 // Ok, let's update the course - but actually not change anything.
89 update_course($course);
91 // Get the updated course end date.
92 $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id
));
94 // Confirm nothing changed.
95 $this->assertEquals($createenddate, $updateenddate);
99 * Tests when we update a course without automatic end date set.
101 public function test_course_updated_without_automatic_end_date() {
104 // Generate a course with some sections.
106 $enddate = $startdate + WEEKSECS
;
107 $course = $this->getDataGenerator()->create_course(array(
110 'startdate' => $startdate,
111 'enddate' => $enddate,
112 'automaticenddate' => 0));
114 // Ok, let's update the course start date.
115 $course->startdate
= $startdate + WEEKSECS
;
116 update_course($course);
118 // Get the updated course end date.
119 $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id
));
121 // Confirm nothing changed.
122 $this->assertEquals($enddate, $updateenddate);
126 * Tests when we adding a course section with automatic end date set.
128 public function test_course_section_created_with_automatic_end_date() {
132 $course = $this->getDataGenerator()->create_course(array(
133 'numsections' => $numsections,
135 'startdate' => time(),
136 'automaticenddate' => 1));
138 // Add a section to the course.
139 course_create_section($course->id
);
141 // Get the updated course end date.
142 $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id
));
144 $format = course_get_format($course->id
);
145 $dates = $format->get_section_dates($numsections +
1, $course->startdate
);
147 // Confirm end date was updated.
148 $this->assertEquals($enddate, $dates->end
);
152 * Tests when we deleting a course section with automatic end date set.
154 public function test_course_section_deleted_with_automatic_end_date() {
157 // Generate a course with some sections.
159 $course = $this->getDataGenerator()->create_course(array(
160 'numsections' => $numsections,
162 'startdate' => time(),
163 'automaticenddate' => 1));
165 // Add a section to the course.
166 course_delete_section($course, $numsections);
168 // Get the updated course end date.
169 $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id
));
171 $format = course_get_format($course->id
);
172 $dates = $format->get_section_dates($numsections - 1, $course->startdate
);
174 // Confirm end date was updated.
175 $this->assertEquals($enddate, $dates->end
);