Merge branch 'MDL-73711_310' of https://github.com/stronk7/moodle into MOODLE_310_STABLE
[moodle.git] / calendar / tests / event_mapper_test.php
blobe69456338b948f0740b2a72207ea8b6e19d0b926
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 * Event mapper test.
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
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/calendar/lib.php');
30 use core_calendar\local\event\mappers\event_mapper;
31 use core_calendar\local\event\value_objects\action;
32 use core_calendar\local\event\value_objects\event_description;
33 use core_calendar\local\event\value_objects\event_times;
34 use core_calendar\local\event\factories\action_factory_interface;
35 use core_calendar\local\event\entities\event_collection_interface;
36 use core_calendar\local\event\factories\event_factory_interface;
37 use core_calendar\local\event\entities\event_interface;
38 use core_calendar\local\event\entities\action_event_interface;
39 use core_calendar\local\event\proxies\proxy_interface;
41 /**
42 * Event mapper testcase.
44 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 class core_calendar_event_mapper_testcase extends advanced_testcase {
48 /**
49 * Test legacy event -> event.
51 public function test_from_legacy_event_to_event() {
52 $this->resetAfterTest(true);
53 $this->setAdminUser();
54 $legacyevent = $this->create_event();
55 $mapper = new event_mapper(
56 new event_mapper_test_event_factory()
58 $event = $mapper->from_legacy_event_to_event($legacyevent);
59 $this->assertInstanceOf(event_interface::class, $event);
62 /**
63 * Test event -> legacy event.
65 public function test_from_event_to_legacy_event() {
66 $this->resetAfterTest(true);
67 $this->setAdminUser();
68 $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
69 $event = new event_mapper_test_event($legacyevent);
70 $mapper = new event_mapper(
71 new event_mapper_test_event_factory()
73 $legacyevent = $mapper->from_event_to_legacy_event($event);
74 $this->assertInstanceOf(calendar_event::class, $legacyevent);
77 /**
78 * Test event -> stdClass.
80 public function test_from_event_to_stdclass() {
81 $this->resetAfterTest(true);
82 $this->setAdminUser();
83 $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
84 $event = new event_mapper_test_event($legacyevent);
85 $mapper = new event_mapper(
86 new event_mapper_test_event_factory()
88 $obj = $mapper->from_event_to_stdclass($event);
89 $this->assertInstanceOf(\stdClass::class, $obj);
90 $this->assertEquals($obj->name, $event->get_name());
91 $this->assertEquals($obj->eventtype, $event->get_type());
92 $this->assertEquals($obj->timestart, $event->get_times()->get_start_time()->getTimestamp());
95 /**
96 * Test event -> array.
98 public function test_from_event_to_assoc_array() {
99 $this->resetAfterTest(true);
100 $this->setAdminUser();
101 $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
102 $event = new event_mapper_test_event($legacyevent);
103 $mapper = new event_mapper(
104 new event_mapper_test_event_factory()
106 $arr = $mapper->from_event_to_assoc_array($event);
107 $this->assertTrue(is_array($arr));
108 $this->assertEquals($arr['name'], $event->get_name());
109 $this->assertEquals($arr['eventtype'], $event->get_type());
110 $this->assertEquals($arr['timestart'], $event->get_times()->get_start_time()->getTimestamp());
114 * Test for action event -> legacy event.
116 public function test_from_action_event_to_legacy_event() {
117 $this->resetAfterTest(true);
118 $this->setAdminUser();
119 $legacyevent = $this->create_event(['modname' => 'assign', 'instance' => 1]);
120 $event = new event_mapper_test_action_event(
121 new event_mapper_test_event($legacyevent)
123 $mapper = new event_mapper(
124 new event_mapper_test_event_factory()
126 $legacyevent = $mapper->from_event_to_legacy_event($event);
128 $this->assertInstanceOf(calendar_event::class, $legacyevent);
129 $this->assertEquals($legacyevent->actionname, 'test action');
130 $this->assertInstanceOf(\moodle_url::class, $legacyevent->actionurl);
131 $this->assertEquals($legacyevent->actionnum, 1729);
132 $this->assertEquals($legacyevent->actionactionable, $event->get_action()->is_actionable());
136 * Helper function to create calendar events using the old code.
138 * @param array $properties A list of calendar event properties to set
139 * @return calendar_event
141 protected function create_event($properties = []) {
142 $record = new \stdClass();
143 $record->name = 'event name';
144 $record->eventtype = 'site';
145 $record->timestart = time();
146 $record->timeduration = 0;
147 $record->timesort = 0;
148 $record->type = 1;
149 $record->courseid = 0;
150 $record->categoryid = 0;
152 foreach ($properties as $name => $value) {
153 $record->$name = $value;
156 $event = new calendar_event($record);
157 return $event->create($record, false);
162 * A test event factory.
164 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
165 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
167 class event_mapper_test_event_factory implements event_factory_interface {
169 public function create_instance(\stdClass $dbrow) {
170 return new event_mapper_test_event();
175 * A test action event
177 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
178 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
180 class event_mapper_test_action_event implements action_event_interface {
182 * @var event_interface $event The event to delegate to.
184 protected $event;
187 * event_mapper_test_action_event constructor.
188 * @param event_interface $event
190 public function __construct(event_interface $event) {
191 $this->event = $event;
194 public function get_id() {
195 return $this->event->get_id();
198 public function get_name() {
199 return $this->event->get_name();
202 public function get_description() {
203 return $this->event->get_description();
206 public function get_location() {
207 return $this->event->get_location();
210 public function get_category() {
211 return $this->event->get_category();
214 public function get_course() {
215 return $this->event->get_course();
218 public function get_course_module() {
219 return $this->event->get_course_module();
222 public function get_group() {
223 return $this->event->get_group();
226 public function get_user() {
227 return $this->event->get_user();
230 public function get_type() {
231 return $this->event->get_type();
234 public function get_times() {
235 return $this->event->get_times();
238 public function get_repeats() {
239 return $this->event->get_repeats();
242 public function get_subscription() {
243 return $this->event->get_subscription();
246 public function is_visible() {
247 return $this->event->is_visible();
250 public function get_action() {
251 return new action(
252 'test action',
253 new \moodle_url('http://example.com'),
254 1729,
255 true
260 * Component
261 * @return string|null
263 public function get_component() {
264 return $this->event->get_component();
269 * A test event.
271 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
272 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
274 class event_mapper_test_event implements event_interface {
276 * @var proxy_interface $categoryproxy Category proxy.
278 protected $categoryproxy;
281 * @var proxy_interface $courseproxy Course proxy.
283 protected $courseproxy;
286 * @var proxy_interface $cmproxy Course module proxy.
288 protected $cmproxy;
291 * @var proxy_interface $groupproxy Group proxy.
293 protected $groupproxy;
296 * @var proxy_interface $userproxy User proxy.
298 protected $userproxy;
301 * @var proxy_interface $subscriptionproxy Subscription proxy.
303 protected $subscriptionproxy;
306 * Constructor.
308 * @param calendar_event $legacyevent Legacy event to extract IDs etc from.
310 public function __construct($legacyevent = null) {
311 if ($legacyevent) {
312 $this->courseproxy = new event_mapper_test_proxy($legacyevent->courseid);
313 $this->cmproxy = new event_mapper_test_proxy(1729,
315 'modname' => $legacyevent->modname,
316 'instance' => $legacyevent->instance
319 $this->groupproxy = new event_mapper_test_proxy(0);
320 $this->userproxy = new event_mapper_test_proxy($legacyevent->userid);
321 $this->subscriptionproxy = new event_mapper_test_proxy(null);
325 public function get_id() {
326 return 1729;
329 public function get_name() {
330 return 'Jeff';
333 public function get_description() {
334 return new event_description('asdf', 1);
337 public function get_location() {
338 return 'Cube office';
341 public function get_category() {
342 return $this->categoryproxy;
345 public function get_course() {
346 return $this->courseproxy;
349 public function get_course_module() {
350 return $this->cmproxy;
353 public function get_group() {
354 return $this->groupproxy;
357 public function get_user() {
358 return $this->userproxy;
361 public function get_type() {
362 return 'asdf';
365 public function get_times() {
366 return new event_times(
367 (new \DateTimeImmutable())->setTimestamp(-386380800),
368 (new \DateTimeImmutable())->setTimestamp(115776000),
369 (new \DateTimeImmutable())->setTimestamp(115776000),
370 (new \DateTimeImmutable())->setTimestamp(time()),
371 (new \DateTimeImmutable())->setTimestamp(115776000)
375 public function get_repeats() {
376 return new core_calendar_event_mapper_test_event_collection();
379 public function get_subscription() {
380 return $this->subscriptionproxy;
383 public function is_visible() {
384 return true;
388 * Component
389 * @return string|null
391 public function get_component() {
392 return null;
397 * A test proxy.
399 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
400 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
402 class event_mapper_test_proxy implements proxy_interface {
404 * @var int $id Proxied ID.
406 protected $id;
409 * @var array $params Params to proxy.
411 protected $params;
414 * Constructor.
416 * @param int $id Proxied ID.
417 * @param array $params Params to proxy.
419 public function __construct($id, $params = []) {
420 $this->params = $params;
423 public function get($member) {
424 if ($member === 'id') {
425 return $this->id;
427 return isset($params[$member]) ? $params[$member] : null;
430 public function get_proxied_instance() {
435 * A test event.
437 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
438 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
440 class core_calendar_event_mapper_test_event_collection implements event_collection_interface {
442 * @var array $events Array of events.
444 protected $events;
447 * Constructor.
449 public function __construct() {
450 $this->events = [
451 'not really an event hahaha',
452 'also not really. gottem.'
456 public function get_id() {
457 return 1729;
460 public function get_num() {
461 return 2;
464 public function getIterator() {
465 foreach ($this->events as $event) {
466 yield $event;