1 |
efrain |
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 mapper 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 |
|
|
|
25 |
namespace core_calendar;
|
|
|
26 |
|
|
|
27 |
use core_calendar\local\event\mappers\event_mapper;
|
|
|
28 |
use core_calendar\local\event\value_objects\action;
|
|
|
29 |
use core_calendar\local\event\value_objects\event_description;
|
|
|
30 |
use core_calendar\local\event\value_objects\event_times;
|
|
|
31 |
use core_calendar\local\event\factories\action_factory_interface;
|
|
|
32 |
use core_calendar\local\event\entities\event_collection_interface;
|
|
|
33 |
use core_calendar\local\event\factories\event_factory_interface;
|
|
|
34 |
use core_calendar\local\event\entities\event_interface;
|
|
|
35 |
use core_calendar\local\event\entities\action_event_interface;
|
|
|
36 |
use core_calendar\local\event\proxies\proxy_interface;
|
|
|
37 |
|
|
|
38 |
defined('MOODLE_INTERNAL') || die();
|
|
|
39 |
|
|
|
40 |
global $CFG;
|
|
|
41 |
require_once($CFG->dirroot . '/calendar/lib.php');
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Event mapper test.
|
|
|
45 |
*
|
|
|
46 |
* @package core_calendar
|
|
|
47 |
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
|
|
|
48 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
49 |
*/
|
|
|
50 |
class event_mapper_test extends \advanced_testcase {
|
|
|
51 |
/**
|
|
|
52 |
* Test legacy event -> event.
|
|
|
53 |
*/
|
|
|
54 |
public function test_from_legacy_event_to_event() {
|
|
|
55 |
$this->resetAfterTest(true);
|
|
|
56 |
$this->setAdminUser();
|
|
|
57 |
$legacyevent = $this->create_event();
|
|
|
58 |
$mapper = new event_mapper(
|
|
|
59 |
new event_mapper_test_event_factory()
|
|
|
60 |
);
|
|
|
61 |
$event = $mapper->from_legacy_event_to_event($legacyevent);
|
|
|
62 |
$this->assertInstanceOf(event_interface::class, $event);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Test event -> legacy event.
|
|
|
67 |
*/
|
|
|
68 |
public function test_from_event_to_legacy_event() {
|
|
|
69 |
$this->resetAfterTest(true);
|
|
|
70 |
$this->setAdminUser();
|
|
|
71 |
$legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
|
|
|
72 |
$event = new event_mapper_test_event($legacyevent);
|
|
|
73 |
$mapper = new event_mapper(
|
|
|
74 |
new event_mapper_test_event_factory()
|
|
|
75 |
);
|
|
|
76 |
$legacyevent = $mapper->from_event_to_legacy_event($event);
|
|
|
77 |
$this->assertInstanceOf(\calendar_event::class, $legacyevent);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Test event -> stdClass.
|
|
|
82 |
*/
|
|
|
83 |
public function test_from_event_to_stdclass() {
|
|
|
84 |
$this->resetAfterTest(true);
|
|
|
85 |
$this->setAdminUser();
|
|
|
86 |
$legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
|
|
|
87 |
$event = new event_mapper_test_event($legacyevent);
|
|
|
88 |
$mapper = new event_mapper(
|
|
|
89 |
new event_mapper_test_event_factory()
|
|
|
90 |
);
|
|
|
91 |
$obj = $mapper->from_event_to_stdClass($event);
|
|
|
92 |
$this->assertInstanceOf(\stdClass::class, $obj);
|
|
|
93 |
$this->assertEquals($obj->name, $event->get_name());
|
|
|
94 |
$this->assertEquals($obj->eventtype, $event->get_type());
|
|
|
95 |
$this->assertEquals($obj->timestart, $event->get_times()->get_start_time()->getTimestamp());
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Test event -> array.
|
|
|
100 |
*/
|
|
|
101 |
public function test_from_event_to_assoc_array() {
|
|
|
102 |
$this->resetAfterTest(true);
|
|
|
103 |
$this->setAdminUser();
|
|
|
104 |
$legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
|
|
|
105 |
$event = new event_mapper_test_event($legacyevent);
|
|
|
106 |
$mapper = new event_mapper(
|
|
|
107 |
new event_mapper_test_event_factory()
|
|
|
108 |
);
|
|
|
109 |
$arr = $mapper->from_event_to_assoc_array($event);
|
|
|
110 |
$this->assertTrue(is_array($arr));
|
|
|
111 |
$this->assertEquals($arr['name'], $event->get_name());
|
|
|
112 |
$this->assertEquals($arr['eventtype'], $event->get_type());
|
|
|
113 |
$this->assertEquals($arr['timestart'], $event->get_times()->get_start_time()->getTimestamp());
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Test for action event -> legacy event.
|
|
|
118 |
*/
|
|
|
119 |
public function test_from_action_event_to_legacy_event() {
|
|
|
120 |
$this->resetAfterTest(true);
|
|
|
121 |
$this->setAdminUser();
|
|
|
122 |
$legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
|
|
|
123 |
$event = new event_mapper_test_action_event(
|
|
|
124 |
new event_mapper_test_event($legacyevent)
|
|
|
125 |
);
|
|
|
126 |
$mapper = new event_mapper(
|
|
|
127 |
new event_mapper_test_event_factory()
|
|
|
128 |
);
|
|
|
129 |
$legacyevent = $mapper->from_event_to_legacy_event($event);
|
|
|
130 |
|
|
|
131 |
$this->assertInstanceOf(\calendar_event::class, $legacyevent);
|
|
|
132 |
$this->assertEquals($legacyevent->actionname, 'test action');
|
|
|
133 |
$this->assertInstanceOf(\moodle_url::class, $legacyevent->actionurl);
|
|
|
134 |
$this->assertEquals($legacyevent->actionnum, 1729);
|
|
|
135 |
$this->assertEquals($legacyevent->actionactionable, $event->get_action()->is_actionable());
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Helper function to create calendar events using the old code.
|
|
|
140 |
*
|
|
|
141 |
* @param array $properties A list of calendar event properties to set
|
|
|
142 |
* @return calendar_event
|
|
|
143 |
*/
|
|
|
144 |
protected function create_event($properties = []) {
|
|
|
145 |
$record = new \stdClass();
|
|
|
146 |
$record->name = 'event name';
|
|
|
147 |
$record->eventtype = 'site';
|
|
|
148 |
$record->timestart = time();
|
|
|
149 |
$record->timeduration = 0;
|
|
|
150 |
$record->timesort = 0;
|
|
|
151 |
$record->type = 1;
|
|
|
152 |
$record->courseid = 0;
|
|
|
153 |
$record->categoryid = 0;
|
|
|
154 |
|
|
|
155 |
foreach ($properties as $name => $value) {
|
|
|
156 |
$record->$name = $value;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$event = new \calendar_event($record);
|
|
|
160 |
return $event->create($record, false);
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* A test event factory.
|
|
|
166 |
*
|
|
|
167 |
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
|
|
|
168 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
169 |
*/
|
|
|
170 |
class event_mapper_test_event_factory implements event_factory_interface {
|
|
|
171 |
|
|
|
172 |
public function create_instance(\stdClass $dbrow) {
|
|
|
173 |
return new event_mapper_test_event();
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* A test action event
|
|
|
179 |
*
|
|
|
180 |
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
|
|
|
181 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
182 |
*/
|
|
|
183 |
class event_mapper_test_action_event implements action_event_interface {
|
|
|
184 |
/**
|
|
|
185 |
* @var event_interface $event The event to delegate to.
|
|
|
186 |
*/
|
|
|
187 |
protected $event;
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* event_mapper_test_action_event constructor.
|
|
|
191 |
* @param event_interface $event
|
|
|
192 |
*/
|
|
|
193 |
public function __construct(event_interface $event) {
|
|
|
194 |
$this->event = $event;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
public function get_id() {
|
|
|
198 |
return $this->event->get_id();
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
public function get_name() {
|
|
|
202 |
return $this->event->get_name();
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
public function get_description() {
|
|
|
206 |
return $this->event->get_description();
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
public function get_location() {
|
|
|
210 |
return $this->event->get_location();
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
public function get_category() {
|
|
|
214 |
return $this->event->get_category();
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
public function get_course() {
|
|
|
218 |
return $this->event->get_course();
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
public function get_course_module() {
|
|
|
222 |
return $this->event->get_course_module();
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
public function get_group() {
|
|
|
226 |
return $this->event->get_group();
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
public function get_user() {
|
|
|
230 |
return $this->event->get_user();
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
public function get_type() {
|
|
|
234 |
return $this->event->get_type();
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
public function get_times() {
|
|
|
238 |
return $this->event->get_times();
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
public function get_repeats() {
|
|
|
242 |
return $this->event->get_repeats();
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
public function get_subscription() {
|
|
|
246 |
return $this->event->get_subscription();
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
public function is_visible() {
|
|
|
250 |
return $this->event->is_visible();
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
public function get_action() {
|
|
|
254 |
return new action(
|
|
|
255 |
'test action',
|
|
|
256 |
new \moodle_url('http://example.com'),
|
|
|
257 |
1729,
|
|
|
258 |
true
|
|
|
259 |
);
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
/**
|
|
|
263 |
* Component
|
|
|
264 |
* @return string|null
|
|
|
265 |
*/
|
|
|
266 |
public function get_component() {
|
|
|
267 |
return $this->event->get_component();
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* A test event.
|
|
|
273 |
*
|
|
|
274 |
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
|
|
|
275 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
276 |
*/
|
|
|
277 |
class event_mapper_test_event implements event_interface {
|
|
|
278 |
/**
|
|
|
279 |
* @var proxy_interface $categoryproxy Category proxy.
|
|
|
280 |
*/
|
|
|
281 |
protected $categoryproxy;
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* @var proxy_interface $courseproxy Course proxy.
|
|
|
285 |
*/
|
|
|
286 |
protected $courseproxy;
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* @var proxy_interface $cmproxy Course module proxy.
|
|
|
290 |
*/
|
|
|
291 |
protected $cmproxy;
|
|
|
292 |
|
|
|
293 |
/**
|
|
|
294 |
* @var proxy_interface $groupproxy Group proxy.
|
|
|
295 |
*/
|
|
|
296 |
protected $groupproxy;
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* @var proxy_interface $userproxy User proxy.
|
|
|
300 |
*/
|
|
|
301 |
protected $userproxy;
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* @var proxy_interface $subscriptionproxy Subscription proxy.
|
|
|
305 |
*/
|
|
|
306 |
protected $subscriptionproxy;
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* Constructor.
|
|
|
310 |
*
|
|
|
311 |
* @param calendar_event $legacyevent Legacy event to extract IDs etc from.
|
|
|
312 |
*/
|
|
|
313 |
public function __construct($legacyevent = null) {
|
|
|
314 |
if ($legacyevent) {
|
|
|
315 |
$this->courseproxy = new event_mapper_test_proxy($legacyevent->courseid);
|
|
|
316 |
$this->cmproxy = new event_mapper_test_proxy(1729,
|
|
|
317 |
[
|
|
|
318 |
'modname' => $legacyevent->modname,
|
|
|
319 |
'instance' => $legacyevent->instance
|
|
|
320 |
]
|
|
|
321 |
);
|
|
|
322 |
$this->groupproxy = new event_mapper_test_proxy(0);
|
|
|
323 |
$this->userproxy = new event_mapper_test_proxy($legacyevent->userid);
|
|
|
324 |
$this->subscriptionproxy = new event_mapper_test_proxy(null);
|
|
|
325 |
}
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
public function get_id() {
|
|
|
329 |
return 1729;
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
public function get_name() {
|
|
|
333 |
return 'Jeff';
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
public function get_description() {
|
|
|
337 |
return new event_description('asdf', 1);
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
public function get_location() {
|
|
|
341 |
return 'Cube office';
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
public function get_category() {
|
|
|
345 |
return $this->categoryproxy;
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
public function get_course() {
|
|
|
349 |
return $this->courseproxy;
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
public function get_course_module() {
|
|
|
353 |
return $this->cmproxy;
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
public function get_group() {
|
|
|
357 |
return $this->groupproxy;
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
public function get_user() {
|
|
|
361 |
return $this->userproxy;
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
public function get_type() {
|
|
|
365 |
return 'asdf';
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
public function get_times() {
|
|
|
369 |
return new event_times(
|
|
|
370 |
(new \DateTimeImmutable())->setTimestamp(-386380800),
|
|
|
371 |
(new \DateTimeImmutable())->setTimestamp(115776000),
|
|
|
372 |
(new \DateTimeImmutable())->setTimestamp(115776000),
|
|
|
373 |
(new \DateTimeImmutable())->setTimestamp(time()),
|
|
|
374 |
(new \DateTimeImmutable())->setTimestamp(115776000)
|
|
|
375 |
);
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
public function get_repeats() {
|
|
|
379 |
return new core_calendar_event_mapper_test_event_collection();
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
public function get_subscription() {
|
|
|
383 |
return $this->subscriptionproxy;
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
public function is_visible() {
|
|
|
387 |
return true;
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
/**
|
|
|
391 |
* Component
|
|
|
392 |
* @return string|null
|
|
|
393 |
*/
|
|
|
394 |
public function get_component() {
|
|
|
395 |
return null;
|
|
|
396 |
}
|
|
|
397 |
}
|
|
|
398 |
|
|
|
399 |
/**
|
|
|
400 |
* A test proxy.
|
|
|
401 |
*
|
|
|
402 |
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
|
|
|
403 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
404 |
*/
|
|
|
405 |
class event_mapper_test_proxy implements proxy_interface {
|
|
|
406 |
/**
|
|
|
407 |
* @var int $id Proxied ID.
|
|
|
408 |
*/
|
|
|
409 |
protected $id;
|
|
|
410 |
|
|
|
411 |
/**
|
|
|
412 |
* @var array $params Params to proxy.
|
|
|
413 |
*/
|
|
|
414 |
protected $params;
|
|
|
415 |
|
|
|
416 |
/**
|
|
|
417 |
* Constructor.
|
|
|
418 |
*
|
|
|
419 |
* @param int $id Proxied ID.
|
|
|
420 |
* @param array $params Params to proxy.
|
|
|
421 |
*/
|
|
|
422 |
public function __construct($id, $params = []) {
|
|
|
423 |
$this->params = $params;
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
public function get($member) {
|
|
|
427 |
if ($member === 'id') {
|
|
|
428 |
return $this->id;
|
|
|
429 |
}
|
|
|
430 |
return isset($params[$member]) ? $params[$member] : null;
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
public function get_proxied_instance() {
|
|
|
434 |
}
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
/**
|
|
|
438 |
* A test event.
|
|
|
439 |
*
|
|
|
440 |
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
|
|
|
441 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
442 |
*/
|
|
|
443 |
class core_calendar_event_mapper_test_event_collection implements event_collection_interface {
|
|
|
444 |
/**
|
|
|
445 |
* @var array $events Array of events.
|
|
|
446 |
*/
|
|
|
447 |
protected $events;
|
|
|
448 |
|
|
|
449 |
/**
|
|
|
450 |
* Constructor.
|
|
|
451 |
*/
|
|
|
452 |
public function __construct() {
|
|
|
453 |
$this->events = [
|
|
|
454 |
'not really an event hahaha',
|
|
|
455 |
'also not really. gottem.'
|
|
|
456 |
];
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
public function get_id() {
|
|
|
460 |
return 1729;
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
public function get_num() {
|
|
|
464 |
return 2;
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
public function getIterator(): \Traversable {
|
|
|
468 |
foreach ($this->events as $event) {
|
|
|
469 |
yield $event;
|
|
|
470 |
}
|
|
|
471 |
}
|
|
|
472 |
}
|