From cbbca0d00f7f778697e80c42b230a82963e14072 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Fri, 4 Nov 2016 14:28:04 +0800 Subject: [PATCH] MDL-56120 Calendar: Unit tests for getting events. Added a unit test for checking that calendar_get_events will only return events for activated activities. --- calendar/tests/lib_test.php | 60 +++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/calendar/tests/lib_test.php b/calendar/tests/lib_test.php index 2eef505a4a8..1d74d01ff6f 100644 --- a/calendar/tests/lib_test.php +++ b/calendar/tests/lib_test.php @@ -111,4 +111,64 @@ class core_calendar_lib_testcase extends advanced_testcase { $this->expectOutputRegex('/Error updating calendar subscription: The given iCal URL is invalid/'); calendar_cron(); } + + /** + * Test the calendar_get_events() function only returns activity + * events that are enabled. + */ + public function test_calendar_get_events_with_disabled_module() { + global $DB; + + $course = $this->getDataGenerator()->create_course(); + $events = [[ + 'name' => 'Start of assignment', + 'description' => '', + 'format' => 1, + 'courseid' => $course->id, + 'groupid' => 0, + 'userid' => 2, + 'modulename' => 'assign', + 'instance' => 1, + 'eventtype' => 'due', + 'timestart' => time(), + 'timeduration' => 86400, + 'visible' => 1 + ], [ + 'name' => 'Start of lesson', + 'description' => '', + 'format' => 1, + 'courseid' => $course->id, + 'groupid' => 0, + 'userid' => 2, + 'modulename' => 'lesson', + 'instance' => 1, + 'eventtype' => 'end', + 'timestart' => time(), + 'timeduration' => 86400, + 'visible' => 1 + ] + ]; + + foreach ($events as $event) { + calendar_event::create($event, false); + } + + $timestart = time() - 60; + $timeend = time() + 60; + + // Get all events. + $events = calendar_get_events($timestart, $timeend, true, 0, true); + $this->assertCount(2, $events); + + // Disable the lesson module. + $modulerecord = $DB->get_record('modules', ['name' => 'lesson']); + $modulerecord->visible = 0; + $DB->update_record('modules', $modulerecord); + + // Check that we only return the assign event. + $events = calendar_get_events($timestart, $timeend, true, 0, true); + $this->assertCount(1, $events); + $event = reset($events); + $this->assertEquals('assign', $event->modulename); + } }