2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Repeat event collection tests.
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();
29 require_once($CFG->dirroot
. '/calendar/lib.php');
31 use core_calendar\local\event\entities\event
;
32 use core_calendar\local\event\entities\repeat_event_collection
;
33 use core_calendar\local\event\proxies\coursecat_proxy
;
34 use core_calendar\local\event\proxies\std_proxy
;
35 use core_calendar\local\event\value_objects\event_description
;
36 use core_calendar\local\event\value_objects\event_times
;
37 use core_calendar\local\event\factories\event_factory_interface
;
40 * Repeat event collection tests.
42 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class core_calendar_repeat_event_collection_testcase
extends advanced_testcase
{
47 * Test that the collection id is set to the parent id if the repeat id
50 public function test_parent_id_no_repeat_id() {
51 $this->resetAfterTest(true);
56 $factory = new core_calendar_repeat_event_collection_event_test_factory();
57 $collection = new repeat_event_collection($dbrow, $factory);
59 $this->assertEquals($dbrow->id
, $collection->get_id());
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).
66 public function test_parent_id_and_repeat_id() {
67 $this->resetAfterTest(true);
72 $factory = new core_calendar_repeat_event_collection_event_test_factory();
73 $collection = new repeat_event_collection($dbrow, $factory);
75 $this->assertEquals($dbrow->repeatid
, $collection->get_id());
79 * Test that an empty collection is valid.
81 public function test_empty_collection() {
82 $this->resetAfterTest(true);
83 $this->setAdminUser();
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.
95 $factory = new core_calendar_repeat_event_collection_event_test_factory();
97 // Event collection with no repeats.
98 $collection = new repeat_event_collection($dbrow, $factory);
100 $this->assertEquals($event->id
, $collection->get_id());
101 $this->assertEquals(0, $collection->get_num());
102 $this->assertNull($collection->getIterator()->next());
106 * Test that a collection with values behaves correctly.
108 public function test_values_collection() {
109 $this->resetAfterTest(true);
110 $this->setAdminUser();
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.
119 $parentid = $event->id
;
126 for ($i = 1; $i < 4; $i++
) {
127 $record = $this->create_event([
128 'name' => sprintf('repeat %d', $i),
129 'repeatid' => $parentid
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;
137 // Event collection with no repeats.
138 $collection = new repeat_event_collection($dbrow, $factory);
140 $this->assertEquals($parentid, $collection->get_id());
141 $this->assertEquals(count($repeats), $collection->get_num());
143 foreach ($collection as $index => $event) {
144 $name = $event->get_name();
145 $this->assertEquals($repeats[$name]->name
, $name);
150 * Helper function to create calendar events using the old code.
152 * @param array $properties A list of calendar event properties to set
153 * @return calendar_event
155 protected function create_event($properties = []) {
156 $record = new \
stdClass();
157 $record->name
= 'event name';
158 $record->eventtype
= 'global';
160 $record->repeats
= 0;
161 $record->timestart
= time();
162 $record->timeduration
= 0;
163 $record->timesort
= 0;
165 $record->courseid
= 0;
166 $record->categoryid
= 0;
168 foreach ($properties as $name => $value) {
169 $record->$name = $value;
172 $event = new calendar_event($record);
173 return $event->create($record, false);
178 * Test event factory.
180 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
181 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
183 class core_calendar_repeat_event_collection_event_test_factory
implements event_factory_interface
{
185 public function create_instance(\stdClass
$dbrow) {
186 $identity = function($id) {
192 new event_description($dbrow->description
, $dbrow->format
),
193 new coursecat_proxy($dbrow->categoryid
),
194 new std_proxy($dbrow->courseid
, $identity),
195 new std_proxy($dbrow->groupid
, $identity),
196 new std_proxy($dbrow->userid
, $identity),
197 $dbrow->repeatid ?
new repeat_event_collection($dbrow, $this) : null,
198 new std_proxy($dbrow->instance
, $identity),
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
)
206 !empty($dbrow->visible
),
207 new std_proxy($dbrow->subscriptionid
, $identity),