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 * Calendar Ical unit tests
20 * @package core_calendar
21 * @copyright 2013 Ankit Agarwal
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
31 * Unit tests for ical APIs.
33 * @package core_calendar
34 * @copyright 2013 Ankit Agarwal
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_calendar_ical_testcase extends advanced_testcase {
43 protected function setUp() {
45 require_once($CFG->dirroot . '/calendar/lib.php');
49 * @expectedException coding_exception
51 public function test_calendar_update_subscription() {
52 $this->resetAfterTest(true);
54 $subscription = new stdClass();
55 $subscription->eventtype = 'site';
56 $subscription->name = 'test';
57 $id = calendar_add_subscription($subscription);
59 $subscription = new stdClass();
60 $subscription->id = $id;
61 $subscription->name = 'awesome';
62 calendar_update_subscription($subscription);
63 $sub = calendar_get_subscription($id);
64 $this->assertEquals($subscription->name, $sub->name);
66 $subscription = new stdClass();
67 $subscription->id = $id;
68 $subscription->name = 'awesome2';
69 $subscription->pollinterval = 604800;
70 calendar_update_subscription($subscription);
71 $sub = calendar_get_subscription($id);
72 $this->assertEquals($subscription->name, $sub->name);
73 $this->assertEquals($subscription->pollinterval, $sub->pollinterval);
75 $subscription = new stdClass();
76 $subscription->name = 'awesome4';
77 calendar_update_subscription($subscription);
80 public function test_calendar_add_subscription() {
83 require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
85 $this->resetAfterTest(true);
87 // Test for Microsoft Outlook 2010.
88 $subscription = new stdClass();
89 $subscription->name = 'Microsoft Outlook 2010';
90 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
91 $subscription->eventtype = 'site';
92 $id = calendar_add_subscription($subscription);
94 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
95 $ical = new iCalendar();
96 $ical->unserialize($calendar);
97 $this->assertEquals($ical->parser_errors, array());
99 $sub = calendar_get_subscription($id);
100 $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id);
101 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
102 $this->assertEquals($count, 1);
104 // Test for OSX Yosemite.
105 $subscription = new stdClass();
106 $subscription->name = 'OSX Yosemite';
107 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
108 $subscription->eventtype = 'site';
109 $id = calendar_add_subscription($subscription);
111 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
112 $ical = new iCalendar();
113 $ical->unserialize($calendar);
114 $this->assertEquals($ical->parser_errors, array());
116 $sub = calendar_get_subscription($id);
117 $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id);
118 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
119 $this->assertEquals($count, 1);
121 // Test for Google Gmail.
122 $subscription = new stdClass();
123 $subscription->name = 'Google Gmail';
124 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
125 $subscription->eventtype = 'site';
126 $id = calendar_add_subscription($subscription);
128 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
129 $ical = new iCalendar();
130 $ical->unserialize($calendar);
131 $this->assertEquals($ical->parser_errors, array());
133 $sub = calendar_get_subscription($id);
134 $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id);
135 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
136 $this->assertEquals($count, 1);