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