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