Merge branch 'MDL-54742-master' of git://github.com/mihailges/moodle
[moodle.git] / completion / tests / api_test.php
blobaf24c079a8416ee7c9f525f455bfea238b5b58d4
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 * Test completion API.
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
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Test completion API.
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
36 class core_completion_api_testcase extends advanced_testcase {
38 /**
39 * Test setup.
41 public function setUp() {
42 $this->resetAfterTest();
45 public function test_update_completion_date_event() {
46 global $CFG, $DB;
48 $this->setAdminUser();
50 // Create a course.
51 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
53 // Create an assign activity.
54 $time = time();
55 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
57 // Create the completion event.
58 $CFG->enablecompletion = true;
59 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
61 // Check that there is now an event in the database.
62 $events = $DB->get_records('event');
63 $this->assertCount(1, $events);
65 // Get the event.
66 $event = reset($events);
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);
76 require_once($CFG->dirroot . '/course/lib.php');
77 // Delete the module.
78 course_delete_module($assign->cmid);
80 // Check we don't get a failure when called on a deleted module.
81 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', null, $time);
84 public function test_update_completion_date_event_update() {
85 global $CFG, $DB;
87 $this->setAdminUser();
89 // Create a course.
90 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
92 // Create an assign activity.
93 $time = time();
94 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
96 // Create the event.
97 $CFG->enablecompletion = true;
98 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
100 // Call it again, but this time with a different time.
101 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time + DAYSECS);
103 // Check that there is still only one event in the database.
104 $events = $DB->get_records('event');
105 $this->assertCount(1, $events);
107 // Get the event.
108 $event = reset($events);
110 // Confirm that the event has been updated.
111 $this->assertEquals('assign', $event->modulename);
112 $this->assertEquals($assign->id, $event->instance);
113 $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
114 $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
115 $this->assertEquals($time + DAYSECS, $event->timestart);
116 $this->assertEquals($time + DAYSECS, $event->timesort);
119 public function test_update_completion_date_event_delete() {
120 global $CFG, $DB;
122 $this->setAdminUser();
124 // Create a course.
125 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
127 // Create an assign activity.
128 $time = time();
129 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
131 // Create the event.
132 $CFG->enablecompletion = true;
133 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
135 // Call it again, but the time specified as null.
136 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, null);
138 // Check that there is no event in the database.
139 $this->assertEquals(0, $DB->count_records('event'));
142 public function test_update_completion_date_event_completion_disabled() {
143 global $CFG, $DB;
145 $this->setAdminUser();
147 // Create a course.
148 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
150 // Create an assign activity.
151 $time = time();
152 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
154 // Try and create the completion event with completion disabled.
155 $CFG->enablecompletion = false;
156 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
158 // Check that there is no event in the database.
159 $this->assertEquals(0, $DB->count_records('event'));
162 public function test_update_completion_date_event_update_completion_disabled() {
163 global $CFG, $DB;
165 $this->setAdminUser();
167 // Create a course.
168 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
170 // Create an assign activity.
171 $time = time();
172 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
174 // Create the completion event.
175 $CFG->enablecompletion = true;
176 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
178 // Disable completion.
179 $CFG->enablecompletion = false;
181 // Try and update the completion date.
182 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time + DAYSECS);
184 // Check that there is an event in the database.
185 $events = $DB->get_records('event');
186 $this->assertCount(1, $events);
188 // Get the event.
189 $event = reset($events);
191 // Confirm the event has not changed.
192 $this->assertEquals('assign', $event->modulename);
193 $this->assertEquals($assign->id, $event->instance);
194 $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
195 $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
196 $this->assertEquals($time, $event->timestart);
197 $this->assertEquals($time, $event->timesort);
200 public function test_update_completion_date_event_delete_completion_disabled() {
201 global $CFG, $DB;
203 $this->setAdminUser();
205 // Create a course.
206 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
208 // Create an assign activity.
209 $time = time();
210 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
212 // Create the completion event.
213 $CFG->enablecompletion = true;
214 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
216 // Disable completion.
217 $CFG->enablecompletion = false;
219 // Should still be able to delete completion events even when completion is disabled.
220 \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, null);
222 // Check that there is now no event in the database.
223 $this->assertEquals(0, $DB->count_records('event'));