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 creating a repeat collection for a parent that doesn't
48 * exist throws an exception.
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();
54 $this->expectException('\core_calendar\local\event\exceptions\no_repeat_parent_exception');
55 $collection = new repeat_event_collection($parentid, null, $factory);
59 * Test that an empty collection is valid.
61 public function test_empty_collection() {
62 $this->resetAfterTest(true);
63 $this->setAdminUser();
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.
71 $parentid = $event->id
;
72 $factory = new core_calendar_repeat_event_collection_event_test_factory();
74 // Event collection with no repeats.
75 $collection = new repeat_event_collection($parentid, null, $factory);
77 $this->assertEquals($parentid, $collection->get_id());
78 $this->assertEquals(0, $collection->get_num());
79 $this->assertNull($collection->getIterator()->next());
83 * Test that a collection with values behaves correctly.
85 public function test_values_collection() {
86 $this->resetAfterTest(true);
87 $this->setAdminUser();
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.
96 $parentid = $event->id
;
99 for ($i = 1; $i < 4; $i++
) {
100 $record = $this->create_event([
101 'name' => sprintf('repeat %d', $i),
102 'repeatid' => $parentid
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;
110 // Event collection with no repeats.
111 $collection = new repeat_event_collection($parentid, null, $factory);
113 $this->assertEquals($parentid, $collection->get_id());
114 $this->assertEquals(count($repeats), $collection->get_num());
116 foreach ($collection as $index => $event) {
117 $name = $event->get_name();
118 $this->assertEquals($repeats[$name]->name
, $name);
123 * Helper function to create calendar events using the old code.
125 * @param array $properties A list of calendar event properties to set
126 * @return calendar_event
128 protected function create_event($properties = []) {
129 $record = new \
stdClass();
130 $record->name
= 'event name';
131 $record->eventtype
= 'global';
133 $record->repeats
= 0;
134 $record->timestart
= time();
135 $record->timeduration
= 0;
136 $record->timesort
= 0;
138 $record->courseid
= 0;
139 $record->categoryid
= 0;
141 foreach ($properties as $name => $value) {
142 $record->$name = $value;
145 $event = new calendar_event($record);
146 return $event->create($record, false);
151 * Test event factory.
153 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
154 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
156 class core_calendar_repeat_event_collection_event_test_factory
implements event_factory_interface
{
158 public function create_instance(\stdClass
$dbrow) {
159 $identity = function($id) {
165 new event_description($dbrow->description
, $dbrow->format
),
166 new coursecat_proxy($dbrow->categoryid
),
167 new std_proxy($dbrow->courseid
, $identity),
168 new std_proxy($dbrow->groupid
, $identity),
169 new std_proxy($dbrow->userid
, $identity),
170 new repeat_event_collection($dbrow->id
, null, $this),
171 new std_proxy($dbrow->instance
, $identity),
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
)
179 !empty($dbrow->visible
),
180 new std_proxy($dbrow->subscriptionid
, $identity)