MDL-59287 core_completion: Update to unit tests.
[moodle.git] / completion / tests / api_test.php
CommitLineData
7925aab9
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 * Test completion API.
19 *
20 * @package core_completion
21 * @category test
22 * @copyright 2017 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Test completion API.
30 *
31 * @package core_completion
32 * @category test
33 * @copyright 2017 Mark Nelson <markn@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class core_completion_api_testcase extends advanced_testcase {
37
38 /**
39 * Test setup.
40 */
41 public function setUp() {
42 $this->resetAfterTest();
43 }
44
45 public function test_update_completion_date_event() {
46 global $CFG, $DB;
47
48 $this->setAdminUser();
49
50 // Create a course.
51 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
52
53 // Create an assign activity.
54 $time = time();
55 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
56
57 // Create the completion event.
58 $CFG->enablecompletion = true;
190646bd 59 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
7925aab9
MN
60
61 // Check that there is now an event in the database.
62 $events = $DB->get_records('event');
63 $this->assertCount(1, $events);
64
65 // Get the event.
66 $event = reset($events);
67
68 // Confirm the event is correct.
69 $this->assertEquals('assign', $event->modulename);
70 $this->assertEquals($assign->id, $event->instance);
71 $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
72 $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
73 $this->assertEquals($time, $event->timestart);
74 $this->assertEquals($time, $event->timesort);
75 }
76
77 public function test_update_completion_date_event_update() {
78 global $CFG, $DB;
79
80 $this->setAdminUser();
81
82 // Create a course.
83 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
84
85 // Create an assign activity.
86 $time = time();
87 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
88
89 // Create the event.
90 $CFG->enablecompletion = true;
190646bd 91 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
7925aab9
MN
92
93 // Call it again, but this time with a different time.
190646bd 94 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time + DAYSECS);
7925aab9
MN
95
96 // Check that there is still only one event in the database.
97 $events = $DB->get_records('event');
98 $this->assertCount(1, $events);
99
100 // Get the event.
101 $event = reset($events);
102
103 // Confirm that the event has been updated.
104 $this->assertEquals('assign', $event->modulename);
105 $this->assertEquals($assign->id, $event->instance);
106 $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
107 $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
108 $this->assertEquals($time + DAYSECS, $event->timestart);
109 $this->assertEquals($time + DAYSECS, $event->timesort);
110 }
111
112 public function test_update_completion_date_event_delete() {
113 global $CFG, $DB;
114
115 $this->setAdminUser();
116
117 // Create a course.
118 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
119
120 // Create an assign activity.
121 $time = time();
122 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
123
124 // Create the event.
125 $CFG->enablecompletion = true;
190646bd 126 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
7925aab9
MN
127
128 // Call it again, but the time specified as null.
190646bd 129 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, null);
7925aab9
MN
130
131 // Check that there is no event in the database.
132 $this->assertEquals(0, $DB->count_records('event'));
133 }
134
135 public function test_update_completion_date_event_completion_disabled() {
136 global $CFG, $DB;
137
138 $this->setAdminUser();
139
140 // Create a course.
141 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
142
143 // Create an assign activity.
144 $time = time();
145 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
146
147 // Try and create the completion event with completion disabled.
148 $CFG->enablecompletion = false;
190646bd 149 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
7925aab9
MN
150
151 // Check that there is no event in the database.
152 $this->assertEquals(0, $DB->count_records('event'));
153 }
154
155 public function test_update_completion_date_event_update_completion_disabled() {
156 global $CFG, $DB;
157
158 $this->setAdminUser();
159
160 // Create a course.
161 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
162
163 // Create an assign activity.
164 $time = time();
165 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
166
167 // Create the completion event.
168 $CFG->enablecompletion = true;
190646bd 169 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
7925aab9
MN
170
171 // Disable completion.
172 $CFG->enablecompletion = false;
173
174 // Try and update the completion date.
190646bd 175 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time + DAYSECS);
7925aab9
MN
176
177 // Check that there is an event in the database.
178 $events = $DB->get_records('event');
179 $this->assertCount(1, $events);
180
181 // Get the event.
182 $event = reset($events);
183
184 // Confirm the event has not changed.
185 $this->assertEquals('assign', $event->modulename);
186 $this->assertEquals($assign->id, $event->instance);
187 $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
188 $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
189 $this->assertEquals($time, $event->timestart);
190 $this->assertEquals($time, $event->timesort);
191 }
192
193 public function test_update_completion_date_event_delete_completion_disabled() {
194 global $CFG, $DB;
195
196 $this->setAdminUser();
197
198 // Create a course.
199 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
200
201 // Create an assign activity.
202 $time = time();
203 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
204
205 // Create the completion event.
206 $CFG->enablecompletion = true;
190646bd 207 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
7925aab9
MN
208
209 // Disable completion.
210 $CFG->enablecompletion = false;
211
212 // Should still be able to delete completion events even when completion is disabled.
190646bd 213 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, null);
7925aab9
MN
214
215 // Check that there is now no event in the database.
216 $this->assertEquals(0, $DB->count_records('event'));
217 }
218}