Proyectos de Subversion Moodle

Rev

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