Merge branch 'MDL-36754-master' of git://github.com/andrewnicols/moodle
[moodle.git] / calendar / tests / helpers.php
blob92f2e2e4ac86408a4fc3e17223c75d6d42192e5a
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 * This file contains helper classes and functions for testing.
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
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/calendar/lib.php');
31 use core_calendar\local\event\entities\action_event;
32 use core_calendar\local\event\entities\event;
33 use core_calendar\local\event\entities\repeat_event_collection;
34 use core_calendar\local\event\proxies\std_proxy;
35 use core_calendar\local\event\proxies\coursecat_proxy;
36 use core_calendar\local\event\proxies\cm_info_proxy;
37 use core_calendar\local\event\value_objects\action;
38 use core_calendar\local\event\value_objects\event_description;
39 use core_calendar\local\event\value_objects\event_times;
40 use core_calendar\local\event\factories\event_factory_interface;
42 /**
43 * Create a calendar event with the given properties.
45 * @param array $properties The properties to set on the event
46 * @return \calendar_event
48 function 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;
59 $record->categoryid = 0;
61 foreach ($properties as $name => $value) {
62 $record->$name = $value;
65 $event = new \calendar_event($record);
66 return $event->create($record);
69 /**
70 * A test factory that will create action events.
72 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
75 class action_event_test_factory implements event_factory_interface {
77 /**
78 * @var callable $callback.
80 private $callback;
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.
86 * The callback will be given the event and should return true if the event
87 * should be returned and false otherwise.
89 * @param callable $callback The callback.
91 public function __construct($callback = null) {
92 $this->callback = $callback;
95 public function create_instance(\stdClass $record) {
96 $module = null;
97 $subscription = null;
99 if ($record->instance && $record->modulename) {
100 $module = new cm_info_proxy($record->instance, $record->modulename, $record->courseid);
103 if ($record->subscriptionid) {
104 $subscription = new std_proxy($record->subscriptionid, function($id) {
105 return (object)['id' => $id];
109 $event = new event(
110 $record->id,
111 $record->name,
112 new event_description($record->description, $record->format),
113 new coursecat_proxy($record->categoryid),
114 new std_proxy($record->courseid, function($id) {
115 $course = new \stdClass();
116 $course->id = $id;
117 return $course;
119 new std_proxy($record->groupid, function($id) {
120 $group = new \stdClass();
121 $group->id = $id;
122 return $group;
124 new std_proxy($record->userid, function($id) {
125 $user = new \stdClass();
126 $user->id = $id;
127 return $user;
129 !empty($record->repeatid) ? new repeat_event_collection($record, $this) : null,
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)
138 !empty($record->visible),
139 $subscription,
140 $record->location
143 $action = new action(
144 'Test action',
145 new \moodle_url('/'),
147 true
150 $actionevent = new action_event($event, $action);
152 if ($callback = $this->callback) {
153 return $callback($actionevent) ? $actionevent : false;
154 } else {
155 return $actionevent;