MDL-57503 calendat: test external function get action events by courses

Part of MDL-55611 epic.
This commit is contained in:
Ryan Wyllie 2017-03-01 05:56:18 +00:00 committed by Damyon Wiese
parent 9b8e505715
commit 1aeb58faad

View File

@ -557,7 +557,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events on or after the given time then an empty result set should * If there are no events on or after the given time then an empty result set should
* be returned. * be returned.
*/ */
function test_get_calendar_action_events_by_timesort_after_time() { public function test_get_calendar_action_events_by_timesort_after_time() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
@ -617,7 +617,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events before the given time then an empty result set should be * If there are no events before the given time then an empty result set should be
* returned. * returned.
*/ */
function test_get_calendar_action_events_by_timesort_before_time() { public function test_get_calendar_action_events_by_timesort_before_time() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
@ -676,7 +676,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events in the given time range then an empty result set should be * If there are no events in the given time range then an empty result set should be
* returned. * returned.
*/ */
function test_get_calendar_action_events_by_timesort_time_range() { public function test_get_calendar_action_events_by_timesort_time_range() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
@ -736,7 +736,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events in the given time range then an empty result set should be * If there are no events in the given time range then an empty result set should be
* returned. * returned.
*/ */
function test_get_calendar_action_events_by_timesort_time_limit_offset() { public function test_get_calendar_action_events_by_timesort_time_limit_offset() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
@ -807,7 +807,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events on or after the given time then an empty result set should * If there are no events on or after the given time then an empty result set should
* be returned. * be returned.
*/ */
function test_get_calendar_action_events_by_course_after_time() { public function test_get_calendar_action_events_by_course_after_time() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course(); $course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course();
@ -871,7 +871,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events before the given time then an empty result set should be * If there are no events before the given time then an empty result set should be
* returned. * returned.
*/ */
function test_get_calendar_action_events_by_course_before_time() { public function test_get_calendar_action_events_by_course_before_time() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course(); $course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course();
@ -935,7 +935,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events in the given time range then an empty result set should be * If there are no events in the given time range then an empty result set should be
* returned. * returned.
*/ */
function test_get_calendar_action_events_by_course_time_range() { public function test_get_calendar_action_events_by_course_time_range() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course(); $course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course();
@ -1000,7 +1000,7 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
* If there are no events in the given time range then an empty result set should be * If there are no events in the given time range then an empty result set should be
* returned. * returned.
*/ */
function test_get_calendar_action_events_by_course_time_limit_offset() { public function test_get_calendar_action_events_by_course_time_limit_offset() {
$user = $this->getDataGenerator()->create_user(); $user = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course(); $course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course();
@ -1067,4 +1067,138 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
$this->assertEmpty($result); $this->assertEmpty($result);
} }
/**
* Test that get_action_events_by_courses will return a list of events for each
* course you provided as long as the user is enrolled in the course.
*/
public function test_get_action_events_by_courses() {
$user = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $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]);
$instance3 = $generator->create_instance(['course' => $course3->id]);
$records = [];
$mapresult = function($result) {
$groupedbycourse = [];
foreach ($result['groupedbycourse'] as $group) {
$events = $group['events'];
$courseid = $group['courseid'];
$groupedbycourse[$courseid] = $events;
}
return $groupedbycourse;
};
$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 < 10; $i++) {
if ($i < 3) {
$courseid = $course1->id;
$instance = $instance1->id;
} else if ($i < 6) {
$courseid = $course2->id;
$instance = $instance2->id;
} else {
$courseid = $course3->id;
$instance = $instance3->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_courses([], 1);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_courses_returns(),
$result
);
$result = $result['groupedbycourse'];
$this->assertEmpty($result);
$result = core_calendar_external::get_calendar_action_events_by_courses([$course1->id], 3);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_courses_returns(),
$result
);
$groupedbycourse = $mapresult($result);
$this->assertEmpty($groupedbycourse[$course1->id]);
$result = core_calendar_external::get_calendar_action_events_by_courses([$course1->id], 1);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_courses_returns(),
$result
);
$groupedbycourse = $mapresult($result);
$this->assertCount(2, $groupedbycourse[$course1->id]);
$this->assertEquals('Event 1', $groupedbycourse[$course1->id][0]['name']);
$this->assertEquals('Event 2', $groupedbycourse[$course1->id][1]['name']);
$result = core_calendar_external::get_calendar_action_events_by_courses(
[$course1->id, $course2->id], 1);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_courses_returns(),
$result
);
$groupedbycourse = $mapresult($result);
$this->assertCount(2, $groupedbycourse[$course1->id]);
$this->assertEquals('Event 1', $groupedbycourse[$course1->id][0]['name']);
$this->assertEquals('Event 2', $groupedbycourse[$course1->id][1]['name']);
$this->assertCount(3, $groupedbycourse[$course2->id]);
$this->assertEquals('Event 3', $groupedbycourse[$course2->id][0]['name']);
$this->assertEquals('Event 4', $groupedbycourse[$course2->id][1]['name']);
$this->assertEquals('Event 5', $groupedbycourse[$course2->id][2]['name']);
$result = core_calendar_external::get_calendar_action_events_by_courses(
[$course1->id, $course2->id], 2, 4);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_courses_returns(),
$result
);
$groupedbycourse = $mapresult($result);
$this->assertCount(2, $groupedbycourse);
$this->assertCount(1, $groupedbycourse[$course1->id]);
$this->assertEquals('Event 2', $groupedbycourse[$course1->id][0]['name']);
$this->assertCount(2, $groupedbycourse[$course2->id]);
$this->assertEquals('Event 3', $groupedbycourse[$course2->id][0]['name']);
$this->assertEquals('Event 4', $groupedbycourse[$course2->id][1]['name']);
$result = core_calendar_external::get_calendar_action_events_by_courses(
[$course1->id, $course2->id], 1, null, 1);
$result = external_api::clean_returnvalue(
core_calendar_external::get_calendar_action_events_by_courses_returns(),
$result
);
$groupedbycourse = $mapresult($result);
$this->assertCount(2, $groupedbycourse);
$this->assertCount(1, $groupedbycourse[$course1->id]);
$this->assertEquals('Event 1', $groupedbycourse[$course1->id][0]['name']);
$this->assertCount(1, $groupedbycourse[$course2->id]);
$this->assertEquals('Event 3', $groupedbycourse[$course2->id][0]['name']);
}
} }