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