MDL-57750 calendar: calendar event vault tests
[moodle.git] / calendar / tests / helpers.php
CommitLineData
18a96325
RW
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 * This file contains helper classes and functions for testing.
19 *
20 * @package core_calendar
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once(__DIR__ . '/../lib.php');
28
29use core_calendar\local\data_access\event_vault;
30use core_calendar\local\event\entities\action_event;
31use core_calendar\local\event\entities\event;
32use core_calendar\local\event\entities\repeat_event_collection;
33use core_calendar\local\event\value_objects\action;
34use core_calendar\local\event\value_objects\event_description;
35use core_calendar\local\event\value_objects\event_times;
36use core_calendar\local\event\value_objects\event_course_module;
37use core_calendar\local\event\proxies\std_proxy;
38use core_calendar\local\interfaces\event_factory_interface;
39
40/**
41 * Create a calendar event with the given properties.
42 *
43 * @param array $properties The properties to set on the event
44 * @return \core_calendar\event
45 */
46function create_event($properties) {
47 $record = new \stdClass();
48 $record->name = 'event name';
49 $record->eventtype = 'global';
50 $record->repeat = 0;
51 $record->repeats = 0;
52 $record->timestart = time();
53 $record->timeduration = 0;
54 $record->timesort = 0;
55 $record->type = CALENDAR_EVENT_TYPE_STANDARD;
56 $record->courseid = 0;
57
58 foreach ($properties as $name => $value) {
59 $record->$name = $value;
60 }
61
62 $event = new \core_calendar\event($record);
63 return $event->create($record);
64}
65
66/**
67 * A test factory that will create action events.
68 */
69class action_event_test_factory implements event_factory_interface {
70
71 private $callback;
72
73 /**
74 * A test factory that will create action events. The factory accepts a callback
75 * that will be used to determine if the event should be returned or not.
76 *
77 * The callback will be given the event and should return true if the event
78 * should be returned and false otherwise.
79 */
80 public function __construct($callback = null) {
81 $this->callback = $callback;
82 }
83
84 public function create_instance(\stdClass $record) {
85 $module = null;
86 $subscription = null;
87
88 if ($record->instance && $record->modulename) {
89 $modulename = $record->modulename;
90 $module = new std_proxy($record->instance, function($id) use ($modulename) {
91 return get_coursemodule_from_instance($modulename, $id);
92 },
93 (object)[
94 'modname' => $modulename,
95 'instance' => $record->instance
96 ]);
97 }
98
99 if ($record->subscriptionid) {
100 $subscription = new std_proxy($record->subscriptionid, function($id) {
101 return (object)['id' => $id];
102 });
103 }
104
105 $event = new event(
106 $record->id,
107 $record->name,
108 new event_description($record->description, $record->format),
109 new std_proxy($record->courseid, function($id) {
110 $course = new \stdClass();
111 $course->id = $id;
112 return $course;
113 }),
114 new std_proxy($record->groupid, function($id) {
115 $group = new \stdClass();
116 $group->id = $id;
117 return $group;
118 }),
119 new std_proxy($record->userid, function($id) {
120 $user = new \stdClass();
121 $user->id = $id;
122 return $user;
123 }),
124 new repeat_event_collection($record->id, $this),
125 $module,
126 $record->eventtype,
127 new event_times(
128 (new \DateTimeImmutable())->setTimestamp($record->timestart),
129 (new \DateTimeImmutable())->setTimestamp($record->timestart + $record->timeduration),
130 (new \DateTimeImmutable())->setTimestamp($record->timesort ? $record->timesort : $record->timestart),
131 (new \DateTimeImmutable())->setTimestamp($record->timemodified)
132 ),
133 !empty($record->visible),
134 $subscription
135 );
136
137 $action = new action(
138 'Test action',
139 new \moodle_url('/'),
140 1
141 );
142
143 $actionevent = new action_event($event, $action);
144
145 if ($callback = $this->callback) {
146 return $callback($actionevent) ? $actionevent : false;
147 } else {
148 return $actionevent;
149 }
150 }
151}