MDL-58211 core_calendar: fixed incompatibility with PHP 5.x
[moodle.git] / calendar / tests / event_factory_test.php
CommitLineData
5ca71c2d
CB
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 * Event factory test.
19 *
20 * @package core_calendar
21 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use core_calendar\local\event\factories\event_factory;
28use core_calendar\local\interfaces\event_interface;
29
30/**
31 * Event factory testcase.
32 *
33 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class core_calendar_event_factory_testcase extends advanced_testcase {
37 /**
38 * Test event class getters.
39 *
40 * @dataProvider create_instance_testcases()
2a5cce61
CB
41 * @param \stdClass $dbrow Row from the event table.
42 * @param callable $actioncallbackapplier Action callback applier.
43 * @param callable $visibilitycallbackapplier Visibility callback applier.
44 * @param string $expectedclass Class the factory is expected to produce.
45 * @param mixed $expectedattributevalue Expected value of the modified attribute.
5ca71c2d
CB
46 */
47 public function test_create_instance(
48 $dbrow,
2a5cce61
CB
49 callable $actioncallbackapplier,
50 callable $visibilitycallbackapplier,
51 $expectedclass,
5ca71c2d
CB
52 $expectedattributevalue
53 ) {
54 $this->resetAfterTest(true);
55 $this->setAdminUser();
56 $event = $this->create_event();
1d364a89
CB
57 $coursecache = [];
58 $factory = new event_factory($actioncallbackapplier, $visibilitycallbackapplier, $coursecache);
5ca71c2d
CB
59 $dbrow->id = $event->id;
60 $instance = $factory->create_instance($dbrow);
61
2a5cce61
CB
62 if ($expectedclass) {
63 $this->assertInstanceOf($expectedclass, $instance);
64 }
65
66 if (is_null($expectedclass)) {
67 $this->assertNull($instance);
68 }
69
70 if ($expectedattributevalue) {
71 $this->assertEquals($instance->testattribute, $expectedattributevalue);
72 }
73 }
74
75 /**
76 * Test invalid callback exception.
77 *
78 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
79 */
80 public function test_invalid_action_callback() {
81 $this->resetAfterTest(true);
82 $this->setAdminUser();
83 $event = $this->create_event();
1d364a89 84 $coursecache = [];
2a5cce61
CB
85 $factory = new event_factory(
86 function () {
87 return 'hello';
88 },
89 function () {
90 return true;
1d364a89
CB
91 },
92 $coursecache
2a5cce61
CB
93 );
94
95 $factory->create_instance(
96 (object)[
97 'id' => $event->id,
98 'name' => 'test',
99 'description' => 'Test description',
100 'format' => 2,
101 'courseid' => 1,
102 'groupid' => 1,
103 'userid' => 1,
104 'repeatid' => 1,
105 'modulename' => 'assign',
106 'instance' => 1,
107 'eventtype' => 'due',
108 'timestart' => 123456789,
109 'timeduration' => 12,
110 'timemodified' => 123456789,
111 'timesort' => 123456789,
112 'visible' => 1,
113 'subscriptionid' => 1
114 ]
115 );
5ca71c2d
CB
116 }
117
118 /**
119 * Test invalid callback exception.
120 *
121 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
122 */
2a5cce61 123 public function test_invalid_visibility_callback() {
5ca71c2d
CB
124 $this->resetAfterTest(true);
125 $this->setAdminUser();
126 $event = $this->create_event();
1d364a89 127 $coursecache = [];
2a5cce61
CB
128 $factory = new event_factory(
129 function ($event) {
130 return $event;
131 },
132 function () {
133 return 'asdf';
1d364a89
CB
134 },
135 $coursecache
2a5cce61 136 );
5ca71c2d
CB
137
138 $factory->create_instance(
139 (object)[
140 'id' => $event->id,
141 'name' => 'test',
142 'description' => 'Test description',
143 'format' => 2,
144 'courseid' => 1,
145 'groupid' => 1,
146 'userid' => 1,
147 'repeatid' => 1,
148 'modulename' => 'assign',
149 'instance' => 1,
150 'eventtype' => 'due',
151 'timestart' => 123456789,
152 'timeduration' => 12,
153 'timemodified' => 123456789,
154 'timesort' => 123456789,
155 'visible' => 1,
156 'subscriptionid' => 1
157 ]
158 );
159 }
160
1d364a89
CB
161 /**
162 * Test the factory's course cache.
163 */
164 public function test_course_cache() {
165 $this->resetAfterTest(true);
166 $this->setAdminUser();
167 $course = self::getDataGenerator()->create_course();
168 $event = $this->create_event(['courseid' => $course->id]);
169 $coursecache = [];
170 $factory = new event_factory(
171 function ($event) {
172 return $event;
173 },
174 function () {
175 return true;
176 },
177 $coursecache
178 );
179
180 $instance = $factory->create_instance(
181 (object)[
182 'id' => $event->id,
183 'name' => 'test',
184 'description' => 'Test description',
185 'format' => 2,
186 'courseid' => $course->id,
187 'groupid' => 1,
188 'userid' => 1,
189 'repeatid' => 1,
190 'modulename' => 'assign',
191 'instance' => 1,
192 'eventtype' => 'due',
193 'timestart' => 123456789,
194 'timeduration' => 12,
195 'timemodified' => 123456789,
196 'timesort' => 123456789,
197 'visible' => 1,
198 'subscriptionid' => 1
199 ]
200 );
201
202 $instance->get_course()->get('fullname');
203 $this->assertArrayHasKey($course->id, $coursecache);
204 }
205
206 /**
207 * Testcases for the create instance test.
208 *
209 * @return array Array of testcases.
210 */
5ca71c2d
CB
211 public function create_instance_testcases() {
212 return [
2a5cce61 213 'Sample event record with event exposed' => [
5ca71c2d
CB
214 'dbrow' => (object)[
215 'name' => 'Test event',
216 'description' => 'Hello',
217 'format' => 1,
218 'courseid' => 1,
219 'groupid' => 1,
220 'userid' => 1,
221 'repeatid' => null,
222 'modulename' => 'Test module',
223 'instance' => 1,
224 'eventtype' => 'Due',
225 'timestart' => 123456789,
226 'timeduration' => 123456789,
227 'timemodified' => 123456789,
228 'timesort' => 123456789,
229 'visible' => true,
230 'subscriptionid' => 1
231 ],
2a5cce61 232 'actioncallbackapplier' => function(event_interface $event) {
5ca71c2d
CB
233 $event->testattribute = 'Hello';
234 return $event;
235 },
2a5cce61
CB
236 'visibilitycallbackapplier' => function(event_interface $event) {
237 return true;
238 },
239 event_interface::class,
5ca71c2d 240 'Hello'
2a5cce61
CB
241 ],
242 'Sample event record with event hidden' => [
243 'dbrow' => (object)[
244 'name' => 'Test event',
245 'description' => 'Hello',
246 'format' => 1,
247 'courseid' => 1,
248 'groupid' => 1,
249 'userid' => 1,
250 'repeatid' => null,
251 'modulename' => 'Test module',
252 'instance' => 1,
253 'eventtype' => 'Due',
254 'timestart' => 123456789,
255 'timeduration' => 123456789,
256 'timemodified' => 123456789,
257 'timesort' => 123456789,
258 'visible' => true,
259 'subscriptionid' => 1
260 ],
261 'actioncallbackapplier' => function(event_interface $event) {
262 $event->testattribute = 'Hello';
263 return $event;
264 },
265 'visibilitycallbackapplier' => function(event_interface $event) {
266 return false;
267 },
268 null,
269 null
5ca71c2d
CB
270 ]
271 ];
272 }
273
274 /**
275 * Helper function to create calendar events using the old code.
276 *
277 * @param array $properties A list of calendar event properties to set
278 * @return event
279 */
280 protected function create_event($properties = []) {
281 $record = new \stdClass();
282 $record->name = 'event name';
283 $record->eventtype = 'global';
284 $record->timestart = time();
285 $record->timeduration = 0;
286 $record->timesort = 0;
287 $record->type = 1;
288 $record->courseid = 0;
289
290 foreach ($properties as $name => $value) {
291 $record->$name = $value;
292 }
293
294 $event = new \core_calendar\event($record);
295 return $event->create($record, false);
296 }
297}