From 4f5d62730f1d2acfa1e2aa078a04b98ce67d054e Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Wed, 28 Jun 2023 12:24:30 +0800 Subject: [PATCH] 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. --- .../task/sync_tool_grades_test.php | 81 ++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/enrol/lti/tests/local/ltiadvantage/task/sync_tool_grades_test.php b/enrol/lti/tests/local/ltiadvantage/task/sync_tool_grades_test.php index b1fab113e00..cd4a41e0e66 100644 --- a/enrol/lti/tests/local/ltiadvantage/task/sync_tool_grades_test.php +++ b/enrol/lti/tests/local/ltiadvantage/task/sync_tool_grades_test.php @@ -749,11 +749,11 @@ class sync_tool_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(); @@ -824,4 +824,81 @@ class sync_tool_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_tool_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->set_custom_data($resource); + $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); + } + } }