. /** * This file contains the class that handles testing of the calendar events. * * @package core_calendar * @copyright 2014 Ankit Agarwal * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/calendar/tests/externallib_test.php'); /** * This file contains the class that handles testing of the calendar events. * * @package core_calendar * @copyright 2014 Ankit Agarwal * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class core_calendar_events_testcase extends advanced_testcase { /** * The test user. */ private $user; /** * The test course. */ private $course; /** * Test set up. */ protected function setUp() { global $USER; // The user we are going to test this on. $this->setAdminUser(); $this->user = $USER; $this->course = self::getDataGenerator()->create_course(); } /** * Tests for calendar_event_created event. */ public function test_calendar_event_created() { $this->resetAfterTest(); // Catch the events. $sink = $this->redirectEvents(); // Create a calendar event. $record = new stdClass(); $record->courseid = 0; $time = time(); $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, $record); // User event. // Capture the event. $events = $sink->get_events(); $sink->clear(); // Validate the event. $event = $events[0]; $this->assertInstanceOf('\core\event\calendar_event_created', $event); $this->assertEquals('event', $event->objecttable); $this->assertEquals(0, $event->courseid); $this->assertEquals($calevent->context, $event->get_context()); $expectedlog = array(0, 'calendar', 'add', 'event.php?action=edit&id=' . $calevent->id , $calevent->name); $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event'); $this->assertEquals($other, $event->other); $this->assertEventLegacyLogData($expectedlog, $event); $this->assertEventContextNotUsed($event); // Now we create a repeated course event. $record = new stdClass(); $record->courseid = $this->course->id; $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, $time, $record); $events = $sink->get_events(); $sink->close(); $this->assertEquals(10, count($events)); foreach ($events as $event) { $this->assertInstanceOf('\core\event\calendar_event_created', $event); $this->assertEquals('event', $event->objecttable); $this->assertEquals($this->course->id, $event->courseid); $this->assertEquals($calevent->context, $event->get_context()); } } /** * Tests for event validations related to calendar_event_created event. */ public function test_calendar_event_created_validations() { $this->resetAfterTest(); $context = context_user::instance($this->user->id); // Test not setting other['repeatid']. try { \core\event\calendar_event_created::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'timestart' => time(), 'name' => 'event' ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without other['repeatid']"); } catch (coding_exception $e) { $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage()); } // Test not setting other['name']. try { \core\event\calendar_event_created::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'repeatid' => 0, 'timestart' => time(), ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_created to be triggered without other['name']"); } catch (coding_exception $e) { $this->assertContains('The \'name\' value must be set in other.', $e->getMessage()); } // Test not setting other['timestart']. try { \core\event\calendar_event_created::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'name' => 'event', 'repeatid' => 0, ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without other['timestart']"); } catch (coding_exception $e) { $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage()); } } /** * Tests for calendar_event_updated event. */ public function test_calendar_event_updated() { $this->resetAfterTest(); // Create a calendar event. $record = new stdClass(); $record->courseid = 0; $time = time(); $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, $record); // User event. // Catch the events. $sink = $this->redirectEvents(); $prop = new stdClass(); $prop->name = 'new event'; $calevent->update($prop); // Update calender event. // Capture the event. $events = $sink->get_events(); // Validate the event. $event = $events[0]; $this->assertInstanceOf('\core\event\calendar_event_updated', $event); $this->assertEquals('event', $event->objecttable); $this->assertEquals(0, $event->courseid); $this->assertEquals($calevent->context, $event->get_context()); $expectedlog = array(0, 'calendar', 'edit', 'event.php?action=edit&id=' . $calevent->id , $calevent->name); $this->assertEventLegacyLogData($expectedlog, $event); $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'new event'); $this->assertEquals($other, $event->other); $this->assertEventContextNotUsed($event); // Now we create a repeated course event and update it. $record = new stdClass(); $record->courseid = $this->course->id; $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(), $record); $sink->clear(); $prop = new stdClass(); $prop->name = 'new event'; $prop->repeateditall = true; $calevent->update($prop); // Update calender event. $events = $sink->get_events(); $sink->close(); $this->assertEquals(10, count($events)); foreach ($events as $event) { $this->assertInstanceOf('\core\event\calendar_event_updated', $event); $this->assertEquals('event', $event->objecttable); $this->assertEquals($this->course->id, $event->courseid); $this->assertEquals($calevent->context, $event->get_context()); } } /** * Tests for event validations related to calendar_event_created event. */ public function test_calendar_event_updated_validations() { $this->resetAfterTest(); $context = context_user::instance($this->user->id); // Test not setting other['repeatid']. try { \core\event\calendar_event_updated::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'timestart' => time(), 'name' => 'event' ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without other['repeatid']"); } catch (coding_exception $e) { $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage()); } // Test not setting other['name']. try { \core\event\calendar_event_updated::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'repeatid' => 0, 'timestart' => time(), ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_updated to be triggered without other['name']"); } catch (coding_exception $e) { $this->assertContains('The \'name\' value must be set in other.', $e->getMessage()); } // Test not setting other['timestart']. try { \core\event\calendar_event_updated::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'name' => 'event', 'repeatid' => 0, ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without other['timestart']"); } catch (coding_exception $e) { $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage()); } } /** * Tests for calendar_event_deleted event. */ public function test_calendar_event_deleted() { global $DB; $this->resetAfterTest(); // Create a calendar event. $record = new stdClass(); $record->courseid = 0; $record->repeatid = 0; $time = time(); $calevent = core_calendar_externallib_testcase::create_calendar_event('event', $this->user->id, 'user', 0, $time, $record); // User event. $dbrecord = $DB->get_record('event', array('id' => $calevent->id), '*', MUST_EXIST); // Catch the events. $sink = $this->redirectEvents(); $calevent->delete(false); $events = $sink->get_events(); // Validate the event. $event = $events[0]; $this->assertInstanceOf('\core\event\calendar_event_deleted', $event); $this->assertEquals('event', $event->objecttable); $this->assertEquals(0, $event->courseid); $this->assertEquals($calevent->context, $event->get_context()); $other = array('repeatid' => 0, 'timestart' => $time, 'name' => 'event'); $this->assertEquals($other, $event->other); $this->assertEventContextNotUsed($event); $this->assertEquals($dbrecord, $event->get_record_snapshot('event', $event->objectid)); // Now we create a repeated course event and delete it. $record = new stdClass(); $record->courseid = $this->course->id; $calevent = core_calendar_externallib_testcase::create_calendar_event('course', $this->user->id, 'course', 10, time(), $record); $sink->clear(); $prop = new stdClass(); $prop->name = 'new event'; $prop->repeateditall = true; $calevent->delete(true); $events = $sink->get_events(); $sink->close(); $this->assertEquals(10, count($events)); foreach ($events as $event) { $this->assertInstanceOf('\core\event\calendar_event_deleted', $event); $this->assertEquals('event', $event->objecttable); $this->assertEquals($this->course->id, $event->courseid); $this->assertEquals($calevent->context, $event->get_context()); } } /** * Tests for event validations related to calendar_event_deleted event. */ public function test_calendar_event_deleted_validations() { $this->resetAfterTest(); $context = context_user::instance($this->user->id); // Test not setting other['repeatid']. try { \core\event\calendar_event_deleted::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'timestart' => time(), 'name' => 'event' ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without other['repeatid']"); } catch (coding_exception $e) { $this->assertContains('The \'repeatid\' value must be set in other.', $e->getMessage()); } // Test not setting other['name']. try { \core\event\calendar_event_deleted::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'repeatid' => 0, 'timestart' => time(), ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without other['name']"); } catch (coding_exception $e) { $this->assertContains('The \'name\' value must be set in other.', $e->getMessage()); } // Test not setting other['timestart']. try { \core\event\calendar_event_deleted::create(array( 'context' => $context, 'objectid' => 2, 'other' => array( 'name' => 'event', 'repeatid' => 0, ) )); $this->fail("Event validation should not allow \\core\\event\\calendar_event_deleted to be triggered without other['timestart']"); } catch (coding_exception $e) { $this->assertContains('The \'timestart\' value must be set in other.', $e->getMessage()); } } }