MDL-58101 block_myoverview: don't display link if inactionable
[moodle.git] / calendar / tests / event_factory_test.php
CommitLineData
5ca71c2d
CB
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 * Event factory test.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use core_calendar\local\event\factories\event_factory;
28use core_calendar\local\interfaces\event_interface;
29
30/**
31 * Event factory testcase.
32 *
33 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class core_calendar_event_factory_testcase extends advanced_testcase {
37 /**
38 * Test event class getters.
39 *
40 * @dataProvider create_instance_testcases()
2a5cce61
CB
41 * @param \stdClass $dbrow Row from the event table.
42 * @param callable $actioncallbackapplier Action callback applier.
43 * @param callable $visibilitycallbackapplier Visibility callback applier.
44 * @param string $expectedclass Class the factory is expected to produce.
45 * @param mixed $expectedattributevalue Expected value of the modified attribute.
5ca71c2d
CB
46 */
47 public function test_create_instance(
48 $dbrow,
2a5cce61
CB
49 callable $actioncallbackapplier,
50 callable $visibilitycallbackapplier,
51 $expectedclass,
5ca71c2d
CB
52 $expectedattributevalue
53 ) {
54 $this->resetAfterTest(true);
55 $this->setAdminUser();
56 $event = $this->create_event();
57
2a5cce61 58 $factory = new event_factory($actioncallbackapplier, $visibilitycallbackapplier);
5ca71c2d
CB
59 $dbrow->id = $event->id;
60 $instance = $factory->create_instance($dbrow);
61
2a5cce61
CB
62 if ($expectedclass) {
63 $this->assertInstanceOf($expectedclass, $instance);
64 }
65
66 if (is_null($expectedclass)) {
67 $this->assertNull($instance);
68 }
69
70 if ($expectedattributevalue) {
71 $this->assertEquals($instance->testattribute, $expectedattributevalue);
72 }
73 }
74
75 /**
76 * Test invalid callback exception.
77 *
78 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
79 */
80 public function test_invalid_action_callback() {
81 $this->resetAfterTest(true);
82 $this->setAdminUser();
83 $event = $this->create_event();
84 $factory = new event_factory(
85 function () {
86 return 'hello';
87 },
88 function () {
89 return true;
90 }
91 );
92
93 $factory->create_instance(
94 (object)[
95 'id' => $event->id,
96 'name' => 'test',
97 'description' => 'Test description',
98 'format' => 2,
99 'courseid' => 1,
100 'groupid' => 1,
101 'userid' => 1,
102 'repeatid' => 1,
103 'modulename' => 'assign',
104 'instance' => 1,
105 'eventtype' => 'due',
106 'timestart' => 123456789,
107 'timeduration' => 12,
108 'timemodified' => 123456789,
109 'timesort' => 123456789,
110 'visible' => 1,
111 'subscriptionid' => 1
112 ]
113 );
5ca71c2d
CB
114 }
115
116 /**
117 * Test invalid callback exception.
118 *
119 * @expectedException \core_calendar\local\event\exceptions\invalid_callback_exception
120 */
2a5cce61 121 public function test_invalid_visibility_callback() {
5ca71c2d
CB
122 $this->resetAfterTest(true);
123 $this->setAdminUser();
124 $event = $this->create_event();
2a5cce61
CB
125 $factory = new event_factory(
126 function ($event) {
127 return $event;
128 },
129 function () {
130 return 'asdf';
131 }
132 );
5ca71c2d
CB
133
134 $factory->create_instance(
135 (object)[
136 'id' => $event->id,
137 'name' => 'test',
138 'description' => 'Test description',
139 'format' => 2,
140 'courseid' => 1,
141 'groupid' => 1,
142 'userid' => 1,
143 'repeatid' => 1,
144 'modulename' => 'assign',
145 'instance' => 1,
146 'eventtype' => 'due',
147 'timestart' => 123456789,
148 'timeduration' => 12,
149 'timemodified' => 123456789,
150 'timesort' => 123456789,
151 'visible' => 1,
152 'subscriptionid' => 1
153 ]
154 );
155 }
156
157 public function create_instance_testcases() {
158 return [
2a5cce61 159 'Sample event record with event exposed' => [
5ca71c2d
CB
160 'dbrow' => (object)[
161 'name' => 'Test event',
162 'description' => 'Hello',
163 'format' => 1,
164 'courseid' => 1,
165 'groupid' => 1,
166 'userid' => 1,
167 'repeatid' => null,
168 'modulename' => 'Test module',
169 'instance' => 1,
170 'eventtype' => 'Due',
171 'timestart' => 123456789,
172 'timeduration' => 123456789,
173 'timemodified' => 123456789,
174 'timesort' => 123456789,
175 'visible' => true,
176 'subscriptionid' => 1
177 ],
2a5cce61 178 'actioncallbackapplier' => function(event_interface $event) {
5ca71c2d
CB
179 $event->testattribute = 'Hello';
180 return $event;
181 },
2a5cce61
CB
182 'visibilitycallbackapplier' => function(event_interface $event) {
183 return true;
184 },
185 event_interface::class,
5ca71c2d 186 'Hello'
2a5cce61
CB
187 ],
188 'Sample event record with event hidden' => [
189 'dbrow' => (object)[
190 'name' => 'Test event',
191 'description' => 'Hello',
192 'format' => 1,
193 'courseid' => 1,
194 'groupid' => 1,
195 'userid' => 1,
196 'repeatid' => null,
197 'modulename' => 'Test module',
198 'instance' => 1,
199 'eventtype' => 'Due',
200 'timestart' => 123456789,
201 'timeduration' => 123456789,
202 'timemodified' => 123456789,
203 'timesort' => 123456789,
204 'visible' => true,
205 'subscriptionid' => 1
206 ],
207 'actioncallbackapplier' => function(event_interface $event) {
208 $event->testattribute = 'Hello';
209 return $event;
210 },
211 'visibilitycallbackapplier' => function(event_interface $event) {
212 return false;
213 },
214 null,
215 null
5ca71c2d
CB
216 ]
217 ];
218 }
219
220 /**
221 * Helper function to create calendar events using the old code.
222 *
223 * @param array $properties A list of calendar event properties to set
224 * @return event
225 */
226 protected function create_event($properties = []) {
227 $record = new \stdClass();
228 $record->name = 'event name';
229 $record->eventtype = 'global';
230 $record->timestart = time();
231 $record->timeduration = 0;
232 $record->timesort = 0;
233 $record->type = 1;
234 $record->courseid = 0;
235
236 foreach ($properties as $name => $value) {
237 $record->$name = $value;
238 }
239
240 $event = new \core_calendar\event($record);
241 return $event->create($record, false);
242 }
243}