MDL-78599 enrol_lti: test covering decoupled, course context grade syncs

This covers the case where a course is published and the launch data
doesn't include the 'lineitem' property of the ags claim, meaning the
tool can manage its own line items.
This commit is contained in:
Jake Dallimore 2023-06-28 12:34:29 +08:00
parent eb1fab720a
commit 07b0f65aac
No known key found for this signature in database

View File

@ -789,11 +789,11 @@ class sync_grades_test extends \lti_advantage_testcase {
}
/**
* Test the sync when only the lineitems URL is provided and when line item creation/query is expected.
* Test the sync for an activity context when only the lineitems URL is provided and when line item creation/query is expected.
*
* @covers ::execute
*/
public function test_sync_grades_none_or_many_lineitems() {
public function test_sync_grades_none_or_many_lineitems_activity_context() {
$this->resetAfterTest();
[$course, $resource] = $this->create_test_environment();
@ -863,4 +863,80 @@ class sync_grades_test extends \lti_advantage_testcase {
$this->assertStringContainsString($expectedtrace, $ob);
}
}
/**
* Test the sync for a course context when only the lineitems URL is provided and when line item creation/query is expected.
*
* @covers ::execute
*/
public function test_sync_grades_none_or_many_lineitems_course_context() {
$this->resetAfterTest();
[$course, $tool1, $tool2, $resource] = $this->create_test_environment();
$launchservice = $this->get_tool_launch_service();
// The launches omit the 'lineitem' claim, meaning the item may have none (or many) line items.
$agsclaim = [
"scope" => [
"https://purl.imsglobal.org/spec/lti-ags/scope/score",
"https://purl.imsglobal.org/spec/lti-ags/scope/lineitem",
],
"lineitems" => "https://platform.example.com/10/lineitems"
];
// Launch the resource for an instructor which will create the domain objects needed for service calls.
$teachermocklaunch = $this->get_mock_launch($resource, $this->get_mock_launch_users_with_ids(['1'], false)[0], null,
$agsclaim);
$instructoruser = $this->getDataGenerator()->create_user();
[$teacherid, $resource] = $launchservice->user_launches_tool($instructoruser, $teachermocklaunch);
// Launch the resource for a few more users, creating those enrolments and allowing grading to take place.
$studentusers = $this->get_mock_launch_users_with_ids(['2', '3'], false,
'http://purl.imsglobal.org/vocab/lis/v2/membership#Learner');
$student1mocklaunch = $this->get_mock_launch($resource, $studentusers[0], null, $agsclaim);
$student2mocklaunch = $this->get_mock_launch($resource, $studentusers[1], null, $agsclaim);
$student1user = $this->getDataGenerator()->create_user();
$student2user = $this->getDataGenerator()->create_user();
[$student1id] = $launchservice->user_launches_tool($student1user, $student1mocklaunch);
[$student2id] = $launchservice->user_launches_tool($student2user, $student2mocklaunch);
// Grade student1 only.
$expectedstudent1grade = $this->set_user_grade_for_resource($student1id, 65, $resource);
// Mock task, asserting that line item creation takes place via a mock grade service object.
$mockgradeservice = $this->createMock(LtiAssignmentsGradesService::class);
$mockgradeservice->method('putGrade')->willReturnCallback(function() {
return ['headers' => ['httpstatus' => "HTTP/2 200 OK"], 'body' => '', 'status' => 200];
});
$mockgradeservice->expects($this->once())
->method('findOrCreateLineitem');
$mockgradeservice->expects($this->once())
->method('putGrade')
->with($this->isInstanceOf(LtiGrade::class), $this->isInstanceOf(LtiLineitem::class));
$mocktask = $this->getMockBuilder(sync_grades::class)
->onlyMethods(['get_ags'])
->getMock();
$mocktask->method('get_ags')->willReturn($mockgradeservice);
// Sync and verify that only student1's grade is sent.
ob_start();
$mocktask->execute();
$ob = ob_get_contents();
ob_end_clean();
$expectedtraces = [
"Starting - LTI Advantage grade sync for shared resource '$resource->id' in course '$course->id'.",
"Skipping - Invalid grade for the user '$teacherid', for the resource '$resource->id' and the course ".
"'$course->id'.",
"Success - The grade '$expectedstudent1grade' for the user '$student1id', for the resource ".
"'$resource->id' and the course '$course->id' was sent.",
"Skipping - Invalid grade for the user '$student2id', for the resource '$resource->id' and the course ".
"'$course->id'.",
"Completed - Synced grades for tool '$resource->id' in the course '$course->id'. ".
"Processed 3 users; sent 1 grades."
];
foreach ($expectedtraces as $expectedtrace) {
$this->assertStringContainsString($expectedtrace, $ob);
}
}
}