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 * This file contains the class that handles testing of the calendar events.
20 * @package core_calendar
21 * @copyright 2014 Ankit Agarwal <ankit.agrr@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot . '/calendar/tests/externallib_test.php');
30 * This file contains the class that handles testing of the calendar events.
32 * @package core_calendar
33 * @copyright 2014 Ankit Agarwal <ankit.agrr@gmail.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_calendar_events_testcase extends advanced_testcase {
51 protected function setUp() {
53 // The user we are going to test this on.
54 $this->setAdminUser();
56 $this->course = self::getDataGenerator()->create_course();
60 * Tests for calendar_event_created event.
62 public function test_calendar_event_created() {
64 $this->resetAfterTest();
67 $sink = $this->redirectEvents();
69 // Create a calendar event.
70 $record = new stdClass();
71 $record->courseid = 0;
73 $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time,
74 $record); // User event.
77 $events = $sink->get_events();
80 // Validate the event.
82 $this->assertInstanceOf('\core\event\calendar_event_created', $event);
83 $this->assertEquals('event', $event->objecttable);
84 $this->assertEquals(0, $event->courseid);
85 $this->assertEquals($calevent->context, $event->get_context());
86 $expectedlog = array(0, 'calendar', 'add', 'event.php?action=edit&id=' . $calevent->id , $calevent->name);
87 $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event');
88 $this->assertEquals($other, $event->other);
89 $this->assertEventLegacyLogData($expectedlog, $event);
90 $this->assertEventContextNotUsed($event);
92 // Now we create a repeated course event.
93 $record = new stdClass();
94 $record->courseid = $this->course->id;
95 $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, $time,
97 $events = $sink->get_events();
100 $this->assertEquals(10, count($events));
101 foreach ($events as $event) {
102 $this->assertInstanceOf('\core\event\calendar_event_created', $event);
103 $this->assertEquals('event', $event->objecttable);
104 $this->assertEquals($this->course->id, $event->courseid);
105 $this->assertEquals($calevent->context, $event->get_context());
110 * Tests for event validations related to calendar_event_created event.
112 public function test_calendar_event_created_validations() {
113 $this->resetAfterTest();
114 $context = context_user::instance($this->user->id);
116 // Test not setting other['repeatid'].
118 \core\event\calendar_event_created::create(array(
119 'context' => $context,
122 'timestart' => time(),
126 $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without
128 } catch (coding_exception $e) {
129 $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
132 // Test not setting other['name'].
134 \core\event\calendar_event_created::create(array(
135 'context' => $context,
139 'timestart' => time(),
142 $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without
144 } catch (coding_exception $e) {
145 $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
148 // Test not setting other['timestart'].
150 \core\event\calendar_event_created::create(array(
151 'context' => $context,
158 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
159 other['timestart']");
160 } catch (coding_exception $e) {
161 $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
166 * Tests for calendar_event_updated event.
168 public function test_calendar_event_updated() {
170 $this->resetAfterTest();
172 // Create a calendar event.
173 $record = new stdClass();
174 $record->courseid = 0;
176 $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time,
177 $record); // User event.
180 $sink = $this->redirectEvents();
181 $prop = new stdClass();
182 $prop->name = 'new event';
183 $calevent->update($prop); // Update calender event.
184 // Capture the event.
185 $events = $sink->get_events();
187 // Validate the event.
189 $this->assertInstanceOf('\core\event\calendar_event_updated', $event);
190 $this->assertEquals('event', $event->objecttable);
191 $this->assertEquals(0, $event->courseid);
192 $this->assertEquals($calevent->context, $event->get_context());
193 $expectedlog = array(0, 'calendar', 'edit', 'event.php?action=edit&id=' . $calevent->id , $calevent->name);
194 $this->assertEventLegacyLogData($expectedlog, $event);
195 $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'new event');
196 $this->assertEquals($other, $event->other);
197 $this->assertEventContextNotUsed($event);
199 // Now we create a repeated course event and update it.
200 $record = new stdClass();
201 $record->courseid = $this->course->id;
202 $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(),
206 $prop = new stdClass();
207 $prop->name = 'new event';
208 $prop->repeateditall = true;
209 $calevent->update($prop); // Update calender event.
210 $events = $sink->get_events();
213 $this->assertEquals(10, count($events));
214 foreach ($events as $event) {
215 $this->assertInstanceOf('\core\event\calendar_event_updated', $event);
216 $this->assertEquals('event', $event->objecttable);
217 $this->assertEquals($this->course->id, $event->courseid);
218 $this->assertEquals($calevent->context, $event->get_context());
223 * Tests for event validations related to calendar_event_created event.
225 public function test_calendar_event_updated_validations() {
226 $this->resetAfterTest();
227 $context = context_user::instance($this->user->id);
229 // Test not setting other['repeatid'].
231 \core\event\calendar_event_updated::create(array(
232 'context' => $context,
235 'timestart' => time(),
239 $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
241 } catch (coding_exception $e) {
242 $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
245 // Test not setting other['name'].
247 \core\event\calendar_event_updated::create(array(
248 'context' => $context,
252 'timestart' => time(),
255 $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without
257 } catch (coding_exception $e) {
258 $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
261 // Test not setting other['timestart'].
263 \core\event\calendar_event_updated::create(array(
264 'context' => $context,
271 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
272 other['timestart']");
273 } catch (coding_exception $e) {
274 $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());
279 * Tests for calendar_event_deleted event.
281 public function test_calendar_event_deleted() {
284 $this->resetAfterTest();
286 // Create a calendar event.
287 $record = new stdClass();
288 $record->courseid = 0;
289 $record->repeatid = 0;
291 $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time,
292 $record); // User event.
293 $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST);
296 $sink = $this->redirectEvents();
297 $calevent->delete(false);
298 $events = $sink->get_events();
300 // Validate the event.
302 $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
303 $this->assertEquals('event', $event->objecttable);
304 $this->assertEquals(0, $event->courseid);
305 $this->assertEquals($calevent->context, $event->get_context());
306 $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event');
307 $this->assertEquals($other, $event->other);
308 $this->assertEventContextNotUsed($event);
309 $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid));
311 // Now we create a repeated course event and delete it.
312 $record = new stdClass();
313 $record->courseid = $this->course->id;
314 $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(),
318 $prop = new stdClass();
319 $prop->name = 'new event';
320 $prop->repeateditall = true;
321 $calevent->delete(true);
322 $events = $sink->get_events();
325 $this->assertEquals(10, count($events));
326 foreach ($events as $event) {
327 $this->assertInstanceOf('\core\event\calendar_event_deleted', $event);
328 $this->assertEquals('event', $event->objecttable);
329 $this->assertEquals($this->course->id, $event->courseid);
330 $this->assertEquals($calevent->context, $event->get_context());
335 * Tests for event validations related to calendar_event_deleted event.
337 public function test_calendar_event_deleted_validations() {
338 $this->resetAfterTest();
339 $context = context_user::instance($this->user->id);
341 // Test not setting other['repeatid'].
343 \core\event\calendar_event_deleted::create(array(
344 'context' => $context,
347 'timestart' => time(),
351 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
353 } catch (coding_exception $e) {
354 $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage());
357 // Test not setting other['name'].
359 \core\event\calendar_event_deleted::create(array(
360 'context' => $context,
364 'timestart' => time(),
367 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
369 } catch (coding_exception $e) {
370 $this->assertContains('The \'name\' value must be set in other.', $e->getMessage());
373 // Test not setting other['timestart'].
375 \core\event\calendar_event_deleted::create(array(
376 'context' => $context,
383 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without
384 other['timestart']");
385 } catch (coding_exception $e) {
386 $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage());