[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * This file contains the class that handles testing of the calendar events. 19 * 20 * @package core_calendar 21 * @copyright 2014 Ankit Agarwal <ankit.agrr@gmail.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 global $CFG; 27 require_once($CFG->dirroot . '/calendar/tests/externallib_test.php'); 28 29 /** 30 * This file contains the class that handles testing of the calendar events. 31 * 32 * @package core_calendar 33 * @copyright 2014 Ankit Agarwal <ankit.agrr@gmail.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class core_calendar_events_testcase extends advanced_testcase { 37 38 /** 39 * The test user. 40 */ 41 private $user; 42 43 /** 44 * The test course. 45 */ 46 private $course; 47 48 /** 49 * Test set up. 50 */ 51 protected function setUp() { 52 global $USER; 53 // The user we are going to test this on. 54 $this->setAdminUser(); 55 $this->user = $USER; 56 $this->course = self::getDataGenerator()->create_course(); 57 } 58 59 /** 60 * Tests for calendar_event_created event. 61 */ 62 public function test_calendar_event_created() { 63 64 $this->resetAfterTest(); 65 66 // Catch the events. 67 $sink = $this->redirectEvents(); 68 69 // Create a calendar event. 70 $record = new stdClass(); 71 $record->courseid = 0; 72 $time = time(); 73 $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, 74 $record); // User event. 75 76 // Capture the event. 77 $events = $sink->get_events(); 78 $sink->clear(); 79 80 // Validate the event. 81 $event = $events[0]; 82 $this->assertInstanceOf('\core\event\calendar_event_created', $event); 83 $this->assertEquals('event', $event->objecttable); 84 $this->assertEquals(0, $event->courseid); 85 $this->assertEquals($calevent->context, $event->get_context()); 86 $expectedlog = array(0, 'calendar', 'add', 'event.php?action=edit&id=' . $calevent->id , $calevent->name); 87 $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event'); 88 $this->assertEquals($other, $event->other); 89 $this->assertEventLegacyLogData($expectedlog, $event); 90 $this->assertEventContextNotUsed($event); 91 92 // Now we create a repeated course event. 93 $record = new stdClass(); 94 $record->courseid = $this->course->id; 95 $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, $time, 96 $record); 97 $events = $sink->get_events(); 98 $sink->close(); 99 100 $this->assertEquals(10, count($events)); 101 foreach ($events as $event) { 102 $this->assertInstanceOf('\core\event\calendar_event_created', $event); 103 $this->assertEquals('event', $event->objecttable); 104 $this->assertEquals($this->course->id, $event->courseid); 105 $this->assertEquals($calevent->context, $event->get_context()); 106 } 107 } 108 109 /** 110 * Tests for event validations related to calendar_event_created event. 111 */ 112 public function test_calendar_event_created_validations() { 113 $this->resetAfterTest(); 114 $context = context_user::instance($this->user->id); 115 116 // Test not setting other['repeatid']. 117 try { 118 \core\event\calendar_event_created::create(array( 119 'context' => $context, 120 'objectid' => 2, 121 'other' => array( 122 'timestart' => time(), 123 'name' => 'event' 124 ) 125 )); 126 $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without 127 other['repeatid']"); 128 } catch (coding_exception $e) { 129 $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage()); 130 } 131 132 // Test not setting other['name']. 133 try { 134 \core\event\calendar_event_created::create(array( 135 'context' => $context, 136 'objectid' => 2, 137 'other' => array( 138 'repeatid' => 0, 139 'timestart' => time(), 140 ) 141 )); 142 $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without 143 other['name']"); 144 } catch (coding_exception $e) { 145 $this->assertContains('The \'name\' value must be set in other.', $e->getMessage()); 146 } 147 148 // Test not setting other['timestart']. 149 try { 150 \core\event\calendar_event_created::create(array( 151 'context' => $context, 152 'objectid' => 2, 153 'other' => array( 154 'name' => 'event', 155 'repeatid' => 0, 156 ) 157 )); 158 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without 159 other['timestart']"); 160 } catch (coding_exception $e) { 161 $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage()); 162 } 163 } 164 165 /** 166 * Tests for calendar_event_updated event. 167 */ 168 public function test_calendar_event_updated() { 169 170 $this->resetAfterTest(); 171 172 // Create a calendar event. 173 $record = new stdClass(); 174 $record->courseid = 0; 175 $time = time(); 176 $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, 177 $record); // User event. 178 179 // Catch the events. 180 $sink = $this->redirectEvents(); 181 $prop = new stdClass(); 182 $prop->name = 'new event'; 183 $calevent->update($prop); // Update calender event. 184 // Capture the event. 185 $events = $sink->get_events(); 186 187 // Validate the event. 188 $event = $events[0]; 189 $this->assertInstanceOf('\core\event\calendar_event_updated', $event); 190 $this->assertEquals('event', $event->objecttable); 191 $this->assertEquals(0, $event->courseid); 192 $this->assertEquals($calevent->context, $event->get_context()); 193 $expectedlog = array(0, 'calendar', 'edit', 'event.php?action=edit&id=' . $calevent->id , $calevent->name); 194 $this->assertEventLegacyLogData($expectedlog, $event); 195 $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'new event'); 196 $this->assertEquals($other, $event->other); 197 $this->assertEventContextNotUsed($event); 198 199 // Now we create a repeated course event and update it. 200 $record = new stdClass(); 201 $record->courseid = $this->course->id; 202 $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(), 203 $record); 204 205 $sink->clear(); 206 $prop = new stdClass(); 207 $prop->name = 'new event'; 208 $prop->repeateditall = true; 209 $calevent->update($prop); // Update calender event. 210 $events = $sink->get_events(); 211 $sink->close(); 212 213 $this->assertEquals(10, count($events)); 214 foreach ($events as $event) { 215 $this->assertInstanceOf('\core\event\calendar_event_updated', $event); 216 $this->assertEquals('event', $event->objecttable); 217 $this->assertEquals($this->course->id, $event->courseid); 218 $this->assertEquals($calevent->context, $event->get_context()); 219 } 220 } 221 222 /** 223 * Tests for event validations related to calendar_event_created event. 224 */ 225 public function test_calendar_event_updated_validations() { 226 $this->resetAfterTest(); 227 $context = context_user::instance($this->user->id); 228 229 // Test not setting other['repeatid']. 230 try { 231 \core\event\calendar_event_updated::create(array( 232 'context' => $context, 233 'objectid' => 2, 234 'other' => array( 235 'timestart' => time(), 236 'name' => 'event' 237 ) 238 )); 239 $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without 240 other['repeatid']"); 241 } catch (coding_exception $e) { 242 $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage()); 243 } 244 245 // Test not setting other['name']. 246 try { 247 \core\event\calendar_event_updated::create(array( 248 'context' => $context, 249 'objectid' => 2, 250 'other' => array( 251 'repeatid' => 0, 252 'timestart' => time(), 253 ) 254 )); 255 $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without 256 other['name']"); 257 } catch (coding_exception $e) { 258 $this->assertContains('The \'name\' value must be set in other.', $e->getMessage()); 259 } 260 261 // Test not setting other['timestart']. 262 try { 263 \core\event\calendar_event_updated::create(array( 264 'context' => $context, 265 'objectid' => 2, 266 'other' => array( 267 'name' => 'event', 268 'repeatid' => 0, 269 ) 270 )); 271 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without 272 other['timestart']"); 273 } catch (coding_exception $e) { 274 $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage()); 275 } 276 } 277 278 /** 279 * Tests for calendar_event_deleted event. 280 */ 281 public function test_calendar_event_deleted() { 282 global $DB; 283 284 $this->resetAfterTest(); 285 286 // Create a calendar event. 287 $record = new stdClass(); 288 $record->courseid = 0; 289 $record->repeatid = 0; 290 $time = time(); 291 $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, 292 $record); // User event. 293 $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST); 294 295 // Catch the events. 296 $sink = $this->redirectEvents(); 297 $calevent->delete(false); 298 $events = $sink->get_events(); 299 300 // Validate the event. 301 $event = $events[0]; 302 $this->assertInstanceOf('\core\event\calendar_event_deleted', $event); 303 $this->assertEquals('event', $event->objecttable); 304 $this->assertEquals(0, $event->courseid); 305 $this->assertEquals($calevent->context, $event->get_context()); 306 $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event'); 307 $this->assertEquals($other, $event->other); 308 $this->assertEventContextNotUsed($event); 309 $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid)); 310 311 // Now we create a repeated course event and delete it. 312 $record = new stdClass(); 313 $record->courseid = $this->course->id; 314 $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(), 315 $record); 316 317 $sink->clear(); 318 $prop = new stdClass(); 319 $prop->name = 'new event'; 320 $prop->repeateditall = true; 321 $calevent->delete(true); 322 $events = $sink->get_events(); 323 $sink->close(); 324 325 $this->assertEquals(10, count($events)); 326 foreach ($events as $event) { 327 $this->assertInstanceOf('\core\event\calendar_event_deleted', $event); 328 $this->assertEquals('event', $event->objecttable); 329 $this->assertEquals($this->course->id, $event->courseid); 330 $this->assertEquals($calevent->context, $event->get_context()); 331 } 332 } 333 334 /** 335 * Tests for event validations related to calendar_event_deleted event. 336 */ 337 public function test_calendar_event_deleted_validations() { 338 $this->resetAfterTest(); 339 $context = context_user::instance($this->user->id); 340 341 // Test not setting other['repeatid']. 342 try { 343 \core\event\calendar_event_deleted::create(array( 344 'context' => $context, 345 'objectid' => 2, 346 'other' => array( 347 'timestart' => time(), 348 'name' => 'event' 349 ) 350 )); 351 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without 352 other['repeatid']"); 353 } catch (coding_exception $e) { 354 $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage()); 355 } 356 357 // Test not setting other['name']. 358 try { 359 \core\event\calendar_event_deleted::create(array( 360 'context' => $context, 361 'objectid' => 2, 362 'other' => array( 363 'repeatid' => 0, 364 'timestart' => time(), 365 ) 366 )); 367 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without 368 other['name']"); 369 } catch (coding_exception $e) { 370 $this->assertContains('The \'name\' value must be set in other.', $e->getMessage()); 371 } 372 373 // Test not setting other['timestart']. 374 try { 375 \core\event\calendar_event_deleted::create(array( 376 'context' => $context, 377 'objectid' => 2, 378 'other' => array( 379 'name' => 'event', 380 'repeatid' => 0, 381 ) 382 )); 383 $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without 384 other['timestart']"); 385 } catch (coding_exception $e) { 386 $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage()); 387 } 388 } 389 390 /** 391 * Tests for calendar_subscription_added event. 392 */ 393 public function test_calendar_subscription_created() { 394 global $CFG; 395 require_once($CFG->dirroot . '/calendar/lib.php'); 396 $this->resetAfterTest(true); 397 398 // Create a mock subscription. 399 $subscription = new stdClass(); 400 $subscription->eventtype = 'site'; 401 $subscription->name = 'test'; 402 $subscription->courseid = $this->course->id; 403 404 // Trigger and capture the event. 405 $sink = $this->redirectEvents(); 406 $id = calendar_add_subscription($subscription); 407 408 $events = $sink->get_events(); 409 $event = reset($events); 410 // Check that the event data is valid. 411 $this->assertInstanceOf('\core\event\calendar_subscription_created', $event); 412 $this->assertEquals($id, $event->objectid); 413 $this->assertEquals($subscription->courseid, $event->other['courseid']); 414 $this->assertEquals($subscription->eventtype, $event->other['eventtype']); 415 $this->assertDebuggingNotCalled(); 416 $sink->close(); 417 418 } 419 420 /** 421 * Tests for calendar_subscription_updated event. 422 */ 423 public function test_calendar_subscription_updated() { 424 global $CFG; 425 require_once($CFG->dirroot . '/calendar/lib.php'); 426 $this->resetAfterTest(true); 427 428 // Create a mock subscription. 429 $subscription = new stdClass(); 430 $subscription->eventtype = 'site'; 431 $subscription->name = 'test'; 432 $subscription->courseid = $this->course->id; 433 $subscription->id = calendar_add_subscription($subscription); 434 // Now edit it. 435 $subscription->name = 'awesome'; 436 437 // Trigger and capture the event. 438 $sink = $this->redirectEvents(); 439 calendar_update_subscription($subscription); 440 $events = $sink->get_events(); 441 $event = reset($events); 442 // Check that the event data is valid. 443 $this->assertInstanceOf('\core\event\calendar_subscription_updated', $event); 444 $this->assertEquals($subscription->id, $event->objectid); 445 $this->assertEquals($subscription->courseid, $event->other['courseid']); 446 $this->assertEquals($subscription->eventtype, $event->other['eventtype']); 447 $this->assertDebuggingNotCalled(); 448 $sink->close(); 449 450 } 451 452 /** 453 * Tests for calendar_subscription_deleted event. 454 */ 455 public function test_calendar_subscription_deleted() { 456 global $CFG; 457 require_once($CFG->dirroot . '/calendar/lib.php'); 458 $this->resetAfterTest(true); 459 460 // Create a mock subscription. 461 $subscription = new stdClass(); 462 $subscription->eventtype = 'site'; 463 $subscription->name = 'test'; 464 $subscription->courseid = $this->course->id; 465 $subscription->id = calendar_add_subscription($subscription); 466 467 // Trigger and capture the event. 468 $sink = $this->redirectEvents(); 469 calendar_delete_subscription($subscription); 470 $events = $sink->get_events(); 471 $event = reset($events); 472 // Check that the event data is valid. 473 $this->assertInstanceOf('\core\event\calendar_subscription_deleted', $event); 474 $this->assertEquals($subscription->id, $event->objectid); 475 $this->assertEquals($subscription->courseid, $event->other['courseid']); 476 $this->assertDebuggingNotCalled(); 477 $sink->close(); 478 479 } 480 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |