Automatically generated installer lang files
[moodle.git] / calendar / tests / event_mapper_test.php
blob0428de004a4cfcc66fa1781544c581105a2ea638
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 namespace core_calendar;
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;
38 defined('MOODLE_INTERNAL') || die();
40 global $CFG;
41 require_once($CFG->dirroot . '/calendar/lib.php');
43 /**
44 * Event mapper test.
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
50 class event_mapper_test extends \advanced_testcase {
51 /**
52 * Test legacy event -> event.
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()
61 $event = $mapper->from_legacy_event_to_event($legacyevent);
62 $this->assertInstanceOf(event_interface::class, $event);
65 /**
66 * Test event -> legacy event.
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()
76 $legacyevent = $mapper->from_event_to_legacy_event($event);
77 $this->assertInstanceOf(\calendar_event::class, $legacyevent);
80 /**
81 * Test event -> stdClass.
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()
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());
98 /**
99 * Test event -> array.
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()
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());
117 * Test for action event -> legacy event.
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)
126 $mapper = new event_mapper(
127 new event_mapper_test_event_factory()
129 $legacyevent = $mapper->from_event_to_legacy_event($event);
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());
139 * Helper function to create calendar events using the old code.
141 * @param array $properties A list of calendar event properties to set
142 * @return calendar_event
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;
155 foreach ($properties as $name => $value) {
156 $record->$name = $value;
159 $event = new \calendar_event($record);
160 return $event->create($record, false);
165 * A test event factory.
167 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
168 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
170 class event_mapper_test_event_factory implements event_factory_interface {
172 public function create_instance(\stdClass $dbrow) {
173 return new event_mapper_test_event();
178 * A test action event
180 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
181 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
183 class event_mapper_test_action_event implements action_event_interface {
185 * @var event_interface $event The event to delegate to.
187 protected $event;
190 * event_mapper_test_action_event constructor.
191 * @param event_interface $event
193 public function __construct(event_interface $event) {
194 $this->event = $event;
197 public function get_id() {
198 return $this->event->get_id();
201 public function get_name() {
202 return $this->event->get_name();
205 public function get_description() {
206 return $this->event->get_description();
209 public function get_location() {
210 return $this->event->get_location();
213 public function get_category() {
214 return $this->event->get_category();
217 public function get_course() {
218 return $this->event->get_course();
221 public function get_course_module() {
222 return $this->event->get_course_module();
225 public function get_group() {
226 return $this->event->get_group();
229 public function get_user() {
230 return $this->event->get_user();
233 public function get_type() {
234 return $this->event->get_type();
237 public function get_times() {
238 return $this->event->get_times();
241 public function get_repeats() {
242 return $this->event->get_repeats();
245 public function get_subscription() {
246 return $this->event->get_subscription();
249 public function is_visible() {
250 return $this->event->is_visible();
253 public function get_action() {
254 return new action(
255 'test action',
256 new \moodle_url('http://example.com'),
257 1729,
258 true
263 * Component
264 * @return string|null
266 public function get_component() {
267 return $this->event->get_component();
272 * A test event.
274 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
275 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
277 class event_mapper_test_event implements event_interface {
279 * @var proxy_interface $categoryproxy Category proxy.
281 protected $categoryproxy;
284 * @var proxy_interface $courseproxy Course proxy.
286 protected $courseproxy;
289 * @var proxy_interface $cmproxy Course module proxy.
291 protected $cmproxy;
294 * @var proxy_interface $groupproxy Group proxy.
296 protected $groupproxy;
299 * @var proxy_interface $userproxy User proxy.
301 protected $userproxy;
304 * @var proxy_interface $subscriptionproxy Subscription proxy.
306 protected $subscriptionproxy;
309 * Constructor.
311 * @param calendar_event $legacyevent Legacy event to extract IDs etc from.
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,
318 'modname' => $legacyevent->modname,
319 'instance' => $legacyevent->instance
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);
328 public function get_id() {
329 return 1729;
332 public function get_name() {
333 return 'Jeff';
336 public function get_description() {
337 return new event_description('asdf', 1);
340 public function get_location() {
341 return 'Cube office';
344 public function get_category() {
345 return $this->categoryproxy;
348 public function get_course() {
349 return $this->courseproxy;
352 public function get_course_module() {
353 return $this->cmproxy;
356 public function get_group() {
357 return $this->groupproxy;
360 public function get_user() {
361 return $this->userproxy;
364 public function get_type() {
365 return 'asdf';
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)
378 public function get_repeats() {
379 return new core_calendar_event_mapper_test_event_collection();
382 public function get_subscription() {
383 return $this->subscriptionproxy;
386 public function is_visible() {
387 return true;
391 * Component
392 * @return string|null
394 public function get_component() {
395 return null;
400 * A test proxy.
402 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
403 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
405 class event_mapper_test_proxy implements proxy_interface {
407 * @var int $id Proxied ID.
409 protected $id;
412 * @var array $params Params to proxy.
414 protected $params;
417 * Constructor.
419 * @param int $id Proxied ID.
420 * @param array $params Params to proxy.
422 public function __construct($id, $params = []) {
423 $this->params = $params;
426 public function get($member) {
427 if ($member === 'id') {
428 return $this->id;
430 return isset($params[$member]) ? $params[$member] : null;
433 public function get_proxied_instance() {
438 * A test event.
440 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
441 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
443 class core_calendar_event_mapper_test_event_collection implements event_collection_interface {
445 * @var array $events Array of events.
447 protected $events;
450 * Constructor.
452 public function __construct() {
453 $this->events = [
454 'not really an event hahaha',
455 'also not really. gottem.'
459 public function get_id() {
460 return 1729;
463 public function get_num() {
464 return 2;
467 public function getIterator() {
468 foreach ($this->events as $event) {
469 yield $event;