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