MDL-56251 format_weeks: added observers for updating the course end date
[moodle.git] / course / format / weeks / tests / observer_test.php
blob2bf8454c3870d02e36135310fb7d4e4898af6977
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 * 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();
27 /**
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 {
36 /**
37 * Test setup.
39 public function setUp() {
40 $this->resetAfterTest();
43 /**
44 * Tests when we update a course with automatic end date set.
46 public function test_course_updated_with_automatic_end_date() {
47 global $DB;
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));
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);
72 /**
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() {
76 global $DB;
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));
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);
98 /**
99 * Tests when we update a course without automatic end date set.
101 public function test_course_updated_without_automatic_end_date() {
102 global $DB;
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));
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() {
129 global $DB;
131 $numsections = 6;
132 $course = $this->getDataGenerator()->create_course(array(
133 'numsections' => $numsections,
134 'format' => 'weeks',
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() {
155 global $DB;
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));
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);