MDL-37624 calendar: Added location support
[moodle.git] / calendar / tests / repeat_event_collection_test.php
CommitLineData
e46efe3a
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 * Repeat event collection tests.
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
d7bc2c6f
MN
27global $CFG;
28
29require_once($CFG->dirroot . '/calendar/lib.php');
30
e46efe3a
CB
31use core_calendar\local\event\entities\event;
32use core_calendar\local\event\entities\repeat_event_collection;
0085b0ea 33use core_calendar\local\event\proxies\coursecat_proxy;
e46efe3a
CB
34use core_calendar\local\event\proxies\std_proxy;
35use core_calendar\local\event\value_objects\event_description;
36use core_calendar\local\event\value_objects\event_times;
42e76c3f 37use core_calendar\local\event\factories\event_factory_interface;
e46efe3a
CB
38
39/**
40 * Repeat event collection tests.
41 *
42 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class core_calendar_repeat_event_collection_testcase extends advanced_testcase {
46 /**
28852998
RW
47 * Test that the collection id is set to the parent id if the repeat id
48 * is falsey.
e46efe3a 49 */
28852998 50 public function test_parent_id_no_repeat_id() {
e46efe3a 51 $this->resetAfterTest(true);
28852998
RW
52 $dbrow = (object) [
53 'id' => 123122131,
54 'repeatid' => null
55 ];
e46efe3a 56 $factory = new core_calendar_repeat_event_collection_event_test_factory();
28852998
RW
57 $collection = new repeat_event_collection($dbrow, $factory);
58
59 $this->assertEquals($dbrow->id, $collection->get_id());
60 }
61
62 /**
63 * Test that the repeat id is set to the parent id if the repeat id
64 * is not falsey (even if the parent id is provided).
65 */
66 public function test_parent_id_and_repeat_id() {
67 $this->resetAfterTest(true);
68 $dbrow = (object) [
69 'id' => 123122131,
70 'repeatid' => 5647839
71 ];
72 $factory = new core_calendar_repeat_event_collection_event_test_factory();
73 $collection = new repeat_event_collection($dbrow, $factory);
74
75 $this->assertEquals($dbrow->repeatid, $collection->get_id());
e46efe3a
CB
76 }
77
78 /**
79 * Test that an empty collection is valid.
80 */
81 public function test_empty_collection() {
82 $this->resetAfterTest(true);
83 $this->setAdminUser();
84
85 $event = $this->create_event([
86 // This causes the code to set the repeat id on this record
87 // but not create any repeat event records.
88 'repeat' => 1,
89 'repeats' => 0
90 ]);
28852998
RW
91 $dbrow = (object) [
92 'id' => $event->id,
93 'repeatid' => null
94 ];
e46efe3a
CB
95 $factory = new core_calendar_repeat_event_collection_event_test_factory();
96
97 // Event collection with no repeats.
28852998 98 $collection = new repeat_event_collection($dbrow, $factory);
e46efe3a 99
28852998 100 $this->assertEquals($event->id, $collection->get_id());
e46efe3a
CB
101 $this->assertEquals(0, $collection->get_num());
102 $this->assertNull($collection->getIterator()->next());
103 }
104
105 /**
106 * Test that a collection with values behaves correctly.
107 */
108 public function test_values_collection() {
109 $this->resetAfterTest(true);
110 $this->setAdminUser();
111
112 $factory = new core_calendar_repeat_event_collection_event_test_factory();
113 $event = $this->create_event([
114 // This causes the code to set the repeat id on this record
115 // but not create any repeat event records.
116 'repeat' => 1,
117 'repeats' => 0
118 ]);
119 $parentid = $event->id;
28852998
RW
120 $dbrow = (object) [
121 'id' => $parentid,
122 'repeatid' => null
123 ];
e46efe3a
CB
124 $repeats = [];
125
126 for ($i = 1; $i < 4; $i++) {
127 $record = $this->create_event([
128 'name' => sprintf('repeat %d', $i),
129 'repeatid' => $parentid
130 ]);
131
132 // Index by name so that we don't have to rely on sorting
133 // when doing the comparison later.
134 $repeats[$record->name] = $record;
135 }
136
137 // Event collection with no repeats.
28852998 138 $collection = new repeat_event_collection($dbrow, $factory);
e46efe3a
CB
139
140 $this->assertEquals($parentid, $collection->get_id());
141 $this->assertEquals(count($repeats), $collection->get_num());
142
143 foreach ($collection as $index => $event) {
144 $name = $event->get_name();
145 $this->assertEquals($repeats[$name]->name, $name);
146 }
147 }
148
149 /**
150 * Helper function to create calendar events using the old code.
151 *
152 * @param array $properties A list of calendar event properties to set
e1cd93ce 153 * @return calendar_event
e46efe3a
CB
154 */
155 protected function create_event($properties = []) {
156 $record = new \stdClass();
157 $record->name = 'event name';
158 $record->eventtype = 'global';
159 $record->repeat = 0;
160 $record->repeats = 0;
161 $record->timestart = time();
162 $record->timeduration = 0;
163 $record->timesort = 0;
164 $record->type = 1;
165 $record->courseid = 0;
44ae0838 166 $record->categoryid = 0;
e46efe3a
CB
167
168 foreach ($properties as $name => $value) {
169 $record->$name = $value;
170 }
171
e1cd93ce 172 $event = new calendar_event($record);
e46efe3a
CB
173 return $event->create($record, false);
174 }
175}
176
177/**
178 * Test event factory.
179 *
180 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
181 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
182 */
183class core_calendar_repeat_event_collection_event_test_factory implements event_factory_interface {
20592f5f 184
5ca71c2d 185 public function create_instance(\stdClass $dbrow) {
e46efe3a
CB
186 $identity = function($id) {
187 return $id;
188 };
189 return new event(
5ca71c2d
CB
190 $dbrow->id,
191 $dbrow->name,
192 new event_description($dbrow->description, $dbrow->format),
0085b0ea 193 new coursecat_proxy($dbrow->categoryid),
5ca71c2d
CB
194 new std_proxy($dbrow->courseid, $identity),
195 new std_proxy($dbrow->groupid, $identity),
196 new std_proxy($dbrow->userid, $identity),
28852998 197 $dbrow->repeatid ? new repeat_event_collection($dbrow, $this) : null,
5ca71c2d
CB
198 new std_proxy($dbrow->instance, $identity),
199 $dbrow->type,
e46efe3a 200 new event_times(
5ca71c2d
CB
201 (new \DateTimeImmutable())->setTimestamp($dbrow->timestart),
202 (new \DateTimeImmutable())->setTimestamp($dbrow->timestart + $dbrow->timeduration),
203 (new \DateTimeImmutable())->setTimestamp($dbrow->timesort ? $dbrow->timesort : $dbrow->timestart),
204 (new \DateTimeImmutable())->setTimestamp($dbrow->timemodified)
e46efe3a 205 ),
5ca71c2d 206 !empty($dbrow->visible),
ef4e04ee
MH
207 new std_proxy($dbrow->subscriptionid, $identity),
208 $dbrow->location
e46efe3a
CB
209 );
210 }
211}