From d9e182b5c62478aee31da3dc410b90e1cee7fadb Mon Sep 17 00:00:00 2001 From: ishatalkin Date: Fri, 24 Mar 2023 15:10:39 +0300 Subject: [PATCH] MDL-76557 enrol_lti: sync_grades accepts status codes 200,201,202,204 --- .../ltiadvantage/task/sync_tool_grades.php | 3 +- .../task/sync_tool_grades_test.php | 85 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/enrol/lti/classes/local/ltiadvantage/task/sync_tool_grades.php b/enrol/lti/classes/local/ltiadvantage/task/sync_tool_grades.php index 1ad0211c3d3..96e26afe1cc 100644 --- a/enrol/lti/classes/local/ltiadvantage/task/sync_tool_grades.php +++ b/enrol/lti/classes/local/ltiadvantage/task/sync_tool_grades.php @@ -185,7 +185,8 @@ class sync_tool_grades extends adhoc_task { continue; } - if ($response['status'] == 200) { + $successresponses = [200, 201, 202, 204]; + if (in_array($response['status'], $successresponses)) { $user->set_lastgrade(grade_floatval($grade)); $syncedusergrades[$user->get_id()] = $user; mtrace("Success - The grade '$floatgrade' $mtracecontent was sent."); 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 cd4a41e0e66..368716a397c 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 @@ -148,6 +148,91 @@ class sync_tool_grades_test extends \lti_advantage_testcase { } } + /** + * Test the sync grades task works correct when platform responses with 200 status code. + * + * @covers ::execute + */ + public function test_grade_sync_positive_case_200() { + $this->test_grade_sync_positive_case('200'); + } + + /** + * Test the sync grades task works correct when platform responses with 201 status code. + * + * @covers ::execute + */ + public function test_grade_sync_positive_case_201() { + $this->test_grade_sync_positive_case('201'); + } + + /** + * Test the sync grades task works correct when platform responses with 202 status code. + * + * @covers ::execute + */ + public function test_grade_sync_positive_case_202() { + $this->test_grade_sync_positive_case('202'); + } + + /** + * Test the sync grades task works correct when platform responses with 204 status code. + * + * @covers ::execute + */ + public function test_grade_sync_positive_case_204() { + $this->test_grade_sync_positive_case('204'); + } + + /** + * Test the sync grades task works correct when platform responses with given status code. + * + * @covers ::execute + * @param string $statuscode the response status code with which the job should work correct + */ + protected function test_grade_sync_positive_case($statuscode) { + $this->resetAfterTest(); + + [$course, $resource] = $this->create_test_environment(); + $launchservice = $this->get_tool_launch_service(); + $task = $this->get_task_with_mocked_grade_service($statuscode); + + // 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]); + $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'], false, + 'http://purl.imsglobal.org/vocab/lis/v2/membership#Learner'); + + $student1mocklaunch = $this->get_mock_launch($resource, $studentusers[0]); + $student1user = $this->getDataGenerator()->create_user(); + [$student1id] = $launchservice->user_launches_tool($student1user, $student1mocklaunch); + + // Grade student1. + $expectedstudent1grade = $this->set_user_grade_for_resource($student1id, 65, $resource); + + // Sync and verify that only student1's grade is sent. + ob_start(); + $task->set_custom_data($resource); + $task->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.", + "Completed - Synced grades for tool '$resource->id' in the course '$course->id'. ". + "Processed 2 users; sent 1 grades." + ]; + foreach ($expectedtraces as $expectedtrace) { + $this->assertStringContainsString($expectedtrace, $ob); + } + } + /** * Test the sync grades task during several runs and for a series of grade changes. *