MDL-57503 calendar: test external function get action events by course
authorRyan Wyllie <ryan@moodle.com>
Tue, 28 Feb 2017 07:07:24 +0000 (07:07 +0000)
committerDamyon Wiese <damyon@moodle.com>
Mon, 3 Apr 2017 03:36:34 +0000 (11:36 +0800)
Part of MDL-55611 epic.

calendar/tests/externallib_test.php

index 63d018b..37af94d 100644 (file)
@@ -798,4 +798,273 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
         $this->assertNull($result['firstid']);
         $this->assertNull($result['lastid']);
     }
+
+    /**
+     * 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
+     * to that time should not be return.
+     *
+     * If there are no events on or after the given time then an empty result set should
+     * be returned.
+     */
+    function test_get_calendar_action_events_by_course_after_time() {
+        $user = $this->getDataGenerator()->create_user();
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
+        $instance1 = $generator->create_instance(['course' => $course1->id]);
+        $instance2 = $generator->create_instance(['course' => $course2->id]);
+        $records = [];
+
+        $this->getDataGenerator()->enrol_user($user->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user->id, $course2->id);
+        $this->resetAfterTest(true);
+        $this->setUser($user);
+
+        for ($i = 1; $i < 19; $i++) {
+            $courseid = ($i < 9) ? $course1->id : $course2->id;
+            $instance = ($i < 9) ? $instance1->id : $instance2->id;
+            $records[] = $this->create_calendar_event(
+                sprintf('Event %d', $i),
+                $user->id,
+                'user',
+                0,
+                1,
+                [
+                    'type' => CALENDAR_EVENT_TYPE_ACTION,
+                    'courseid' => $courseid,
+                    'timesort' => $i,
+                    'modulename' => 'assign',
+                    'instance' => $instance,
+                ]
+            );
+        }
+
+        $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 5);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertCount(4, $result);
+        $this->assertEquals('Event 5', $result[0]['name']);
+        $this->assertEquals('Event 6', $result[1]['name']);
+        $this->assertEquals('Event 7', $result[2]['name']);
+        $this->assertEquals('Event 8', $result[3]['name']);
+
+        $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 9);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertEmpty($result);
+    }
+
+    /**
+     * Requesting calendar events for a course and before a given time should return
+     * all events with a sort time at or before the requested time (inclusive). All
+     * events after that time should not be returned.
+     *
+     * If there are no events before the given time then an empty result set should be
+     * returned.
+     */
+    function test_get_calendar_action_events_by_course_before_time() {
+        $user = $this->getDataGenerator()->create_user();
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
+        $instance1 = $generator->create_instance(['course' => $course1->id]);
+        $instance2 = $generator->create_instance(['course' => $course2->id]);
+        $records = [];
+
+        $this->getDataGenerator()->enrol_user($user->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user->id, $course2->id);
+        $this->resetAfterTest(true);
+        $this->setUser($user);
+
+        for ($i = 1; $i < 19; $i++) {
+            $courseid = ($i < 9) ? $course1->id : $course2->id;
+            $instance = ($i < 9) ? $instance1->id : $instance2->id;
+            $records[] = $this->create_calendar_event(
+                sprintf('Event %d', $i),
+                $user->id,
+                'user',
+                0,
+                1,
+                [
+                    'type' => CALENDAR_EVENT_TYPE_ACTION,
+                    'courseid' => $courseid,
+                    'timesort' => $i + 1,
+                    'modulename' => 'assign',
+                    'instance' => $instance,
+                ]
+            );
+        }
+
+        $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, null, 5);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertCount(4, $result);
+        $this->assertEquals('Event 1', $result[0]['name']);
+        $this->assertEquals('Event 2', $result[1]['name']);
+        $this->assertEquals('Event 3', $result[2]['name']);
+        $this->assertEquals('Event 4', $result[3]['name']);
+
+        $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, null, 1);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertEmpty($result);
+    }
+
+    /**
+     * Requesting calendar events for a course and within a given time range should
+     * return all events with a sort time between the lower and upper time bound
+     * (inclusive).
+     *
+     * If there are no events in the given time range then an empty result set should be
+     * returned.
+     */
+    function test_get_calendar_action_events_by_course_time_range() {
+        $user = $this->getDataGenerator()->create_user();
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
+        $instance1 = $generator->create_instance(['course' => $course1->id]);
+        $instance2 = $generator->create_instance(['course' => $course2->id]);
+        $records = [];
+
+        $this->getDataGenerator()->enrol_user($user->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user->id, $course2->id);
+        $this->resetAfterTest(true);
+        $this->setUser($user);
+
+        for ($i = 1; $i < 19; $i++) {
+            $courseid = ($i < 9) ? $course1->id : $course2->id;
+            $instance = ($i < 9) ? $instance1->id : $instance2->id;
+            $records[] = $this->create_calendar_event(
+                sprintf('Event %d', $i),
+                $user->id,
+                'user',
+                0,
+                1,
+                [
+                    'type' => CALENDAR_EVENT_TYPE_ACTION,
+                    'courseid' => $courseid,
+                    'timesort' => $i,
+                    'modulename' => 'assign',
+                    'instance' => $instance,
+                ]
+            );
+        }
+
+        $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 3, 6);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertCount(4, $result);
+        $this->assertEquals('Event 3', $result[0]['name']);
+        $this->assertEquals('Event 4', $result[1]['name']);
+        $this->assertEquals('Event 5', $result[2]['name']);
+        $this->assertEquals('Event 6', $result[3]['name']);
+
+        $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 10, 15);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertEmpty($result);
+    }
+
+    /**
+     * Requesting calendar events for a course and within a given time range and a limit
+     * and offset should return the number of events up to the given limit value that have
+     * a sort time between the lower and uppper time bound (inclusive) where the result
+     * set is shifted by the offset value.
+     *
+     * If there are no events in the given time range then an empty result set should be
+     * returned.
+     */
+    function test_get_calendar_action_events_by_course_time_limit_offset() {
+        $user = $this->getDataGenerator()->create_user();
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
+        $instance1 = $generator->create_instance(['course' => $course1->id]);
+        $instance2 = $generator->create_instance(['course' => $course2->id]);
+        $records = [];
+
+        $this->getDataGenerator()->enrol_user($user->id, $course1->id);
+        $this->getDataGenerator()->enrol_user($user->id, $course2->id);
+        $this->resetAfterTest(true);
+        $this->setUser($user);
+
+        for ($i = 1; $i < 19; $i++) {
+            $courseid = ($i < 9) ? $course1->id : $course2->id;
+            $instance = ($i < 9) ? $instance1->id : $instance2->id;
+            $records[] = $this->create_calendar_event(
+                sprintf('Event %d', $i),
+                $user->id,
+                'user',
+                0,
+                1,
+                [
+                    'type' => CALENDAR_EVENT_TYPE_ACTION,
+                    'courseid' => $courseid,
+                    'timesort' => $i,
+                    'modulename' => 'assign',
+                    'instance' => $instance,
+                ]
+            );
+        }
+
+        $result = core_calendar_external::get_calendar_action_events_by_course(
+            $course1->id, 2, 7, $records[2]->id, 2);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertCount(2, $result);
+        $this->assertEquals('Event 4', $result[0]['name']);
+        $this->assertEquals('Event 5', $result[1]['name']);
+
+        $result = core_calendar_external::get_calendar_action_events_by_course(
+            $course1->id, 2, 7, $records[4]->id, 2);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertCount(2, $result);
+        $this->assertEquals('Event 6', $result[0]['name']);
+        $this->assertEquals('Event 7', $result[1]['name']);
+
+        $result = core_calendar_external::get_calendar_action_events_by_course(
+            $course1->id, 2, 7, $records[6]->id, 2);
+        $result = external_api::clean_returnvalue(
+            core_calendar_external::get_calendar_action_events_by_course_returns(),
+            $result
+        );
+        $result = $result['events'];
+
+        $this->assertEmpty($result);
+    }
 }