From c84ee67864e9f0ebad1e6afc2ac568cd979e8ecc Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Mon, 19 Nov 2018 10:08:48 +0800 Subject: [PATCH] MDL-64063 core_calendar: Unit test for suspended users. --- calendar/tests/event_vault_test.php | 31 +++++++++++++++++++++++++++++ calendar/tests/externallib_test.php | 28 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/calendar/tests/event_vault_test.php b/calendar/tests/event_vault_test.php index e4a1b21236e..5d90f93b3d8 100644 --- a/calendar/tests/event_vault_test.php +++ b/calendar/tests/event_vault_test.php @@ -557,6 +557,37 @@ class core_calendar_event_vault_testcase extends advanced_testcase { $this->assertEquals('Assignment 1 due date', $usersevents['For user in no groups'][0]->get_name()); } + /** + * Test that if a user is suspended that events related to that course are not shown. + * User 1 is suspended. User 2 is active. + */ + public function test_get_action_events_by_timesort_with_suspended_user() { + $this->resetAfterTest(); + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $course = $this->getDataGenerator()->create_course(); + $this->setAdminuser(); + $lesson = $this->getDataGenerator()->create_module('lesson', [ + 'name' => 'Lesson 1', + 'course' => $course->id, + 'available' => time(), + 'deadline' => (time() + (60 * 60 * 24 * 5)) + ] + ); + $this->getDataGenerator()->enrol_user($user1->id, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED); + $this->getDataGenerator()->enrol_user($user2->id, $course->id); + + $factory = new action_event_test_factory(); + $strategy = new raw_event_retrieval_strategy(); + $vault = new event_vault($factory, $strategy); + + $user1events = $vault->get_action_events_by_timesort($user1, null, null, null, 20, true); + $this->assertEmpty($user1events); + $user2events = $vault->get_action_events_by_timesort($user2, null, null, null, 20, true); + $this->assertCount(1, $user2events); + $this->assertEquals('Lesson 1 closes', $user2events[0]->get_name()); + } + /** * Test that get_action_events_by_course returns events after the * provided timesort value. diff --git a/calendar/tests/externallib_test.php b/calendar/tests/externallib_test.php index ed623d0cd80..9edbc69594a 100644 --- a/calendar/tests/externallib_test.php +++ b/calendar/tests/externallib_test.php @@ -937,6 +937,34 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { $this->assertNull($result['lastid']); } + /** + * Check that it is possible to restrict the calendar events to events where the user is not suspended in the course. + */ + public function test_get_calendar_action_events_by_timesort_suspended_course() { + $this->resetAfterTest(); + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $course = $this->getDataGenerator()->create_course(); + $this->setAdminUser(); + $lesson = $this->getDataGenerator()->create_module('lesson', [ + 'name' => 'Lesson 1', + 'course' => $course->id, + 'available' => time(), + 'deadline' => (time() + (60 * 60 * 24 * 5)) + ] + ); + $this->getDataGenerator()->enrol_user($user1->id, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED); + $this->getDataGenerator()->enrol_user($user2->id, $course->id); + + $this->setUser($user1); + $result = core_calendar_external::get_calendar_action_events_by_timesort(0, null, 0, 20, true); + $this->assertEmpty($result->events); + $this->setUser($user2); + $result = core_calendar_external::get_calendar_action_events_by_timesort(0, null, 0, 20, true); + $this->assertCount(1, $result->events); + $this->assertEquals('Lesson 1 closes', $result->events[0]->name); + } + /** * Requesting calendar events from a given course and time should return all * events with a sort time at or after the requested time. All events prior -- 2.43.0