Merge branch 'MDL-60958-int-fix-1' of github.com:ryanwyllie/moodle
[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 30
18a96325
RW
31use core_calendar\local\event\entities\action_event;
32use core_calendar\local\event\entities\event;
33use core_calendar\local\event\entities\repeat_event_collection;
bd01fd15 34use core_calendar\local\event\proxies\std_proxy;
0085b0ea 35use core_calendar\local\event\proxies\coursecat_proxy;
47a71017 36use core_calendar\local\event\proxies\cm_info_proxy;
18a96325
RW
37use core_calendar\local\event\value_objects\action;
38use core_calendar\local\event\value_objects\event_description;
39use core_calendar\local\event\value_objects\event_times;
42e76c3f 40use core_calendar\local\event\factories\event_factory_interface;
18a96325
RW
41
42/**
43 * Create a calendar event with the given properties.
44 *
45 * @param array $properties The properties to set on the event
e1cd93ce 46 * @return \calendar_event
18a96325
RW
47 */
48function create_event($properties) {
49 $record = new \stdClass();
50 $record->name = 'event name';
51 $record->eventtype = 'global';
52 $record->repeat = 0;
53 $record->repeats = 0;
54 $record->timestart = time();
55 $record->timeduration = 0;
56 $record->timesort = 0;
57 $record->type = CALENDAR_EVENT_TYPE_STANDARD;
58 $record->courseid = 0;
44ae0838 59 $record->categoryid = 0;
18a96325
RW
60
61 foreach ($properties as $name => $value) {
62 $record->$name = $value;
63 }
64
e1cd93ce 65 $event = new \calendar_event($record);
18a96325
RW
66 return $event->create($record);
67}
68
69/**
70 * A test factory that will create action events.
20592f5f
JP
71 *
72 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
18a96325
RW
74 */
75class action_event_test_factory implements event_factory_interface {
76
20592f5f
JP
77 /**
78 * @var callable $callback.
79 */
18a96325
RW
80 private $callback;
81
82 /**
83 * A test factory that will create action events. The factory accepts a callback
84 * that will be used to determine if the event should be returned or not.
85 *
86 * The callback will be given the event and should return true if the event
87 * should be returned and false otherwise.
20592f5f
JP
88 *
89 * @param callable $callback The callback.
18a96325
RW
90 */
91 public function __construct($callback = null) {
92 $this->callback = $callback;
93 }
94
95 public function create_instance(\stdClass $record) {
96 $module = null;
97 $subscription = null;
98
99 if ($record->instance && $record->modulename) {
47a71017 100 $module = new cm_info_proxy($record->instance, $record->modulename, $record->courseid);
18a96325
RW
101 }
102
103 if ($record->subscriptionid) {
104 $subscription = new std_proxy($record->subscriptionid, function($id) {
105 return (object)['id' => $id];
106 });
107 }
108
109 $event = new event(
110 $record->id,
111 $record->name,
112 new event_description($record->description, $record->format),
0085b0ea 113 new coursecat_proxy($record->categoryid),
18a96325
RW
114 new std_proxy($record->courseid, function($id) {
115 $course = new \stdClass();
116 $course->id = $id;
117 return $course;
118 }),
119 new std_proxy($record->groupid, function($id) {
120 $group = new \stdClass();
121 $group->id = $id;
122 return $group;
123 }),
124 new std_proxy($record->userid, function($id) {
125 $user = new \stdClass();
126 $user->id = $id;
127 return $user;
128 }),
4e0ac12e 129 !empty($record->repeatid) ? new repeat_event_collection($record, $this) : null,
18a96325
RW
130 $module,
131 $record->eventtype,
132 new event_times(
133 (new \DateTimeImmutable())->setTimestamp($record->timestart),
134 (new \DateTimeImmutable())->setTimestamp($record->timestart + $record->timeduration),
135 (new \DateTimeImmutable())->setTimestamp($record->timesort ? $record->timesort : $record->timestart),
136 (new \DateTimeImmutable())->setTimestamp($record->timemodified)
137 ),
138 !empty($record->visible),
139 $subscription
140 );
141
142 $action = new action(
143 'Test action',
144 new \moodle_url('/'),
c91b4c02
MN
145 1,
146 true
18a96325
RW
147 );
148
149 $actionevent = new action_event($event, $action);
150
151 if ($callback = $this->callback) {
152 return $callback($actionevent) ? $actionevent : false;
153 } else {
154 return $actionevent;
155 }
156 }
157}