MDL-64063 core_calendar: Unit test for suspended users.
authorAdrian Greeve <abgreeve@gmail.com>
Mon, 19 Nov 2018 02:08:48 +0000 (10:08 +0800)
committerAdrian Greeve <abgreeve@gmail.com>
Wed, 21 Nov 2018 01:51:52 +0000 (09:51 +0800)
calendar/tests/event_vault_test.php
calendar/tests/externallib_test.php

index e4a1b21..5d90f93 100644 (file)
@@ -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.
index ed623d0..9edbc69 100644 (file)
@@ -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