From 8f59d0b9376d31719216783e42fe00681cd6320b Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 2 May 2022 12:13:49 +0800 Subject: [PATCH] MDL-74576 enrol_lti: allow unsupported scopes in ags_info Instead of hard blocking launches with extension scopes, just ignore the unsupported scopes and continue. --- .../local/ltiadvantage/entity/ags_info.php | 15 +++++------ .../ltiadvantage/entity/ags_info_test.php | 27 ++++++++++++++----- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/enrol/lti/classes/local/ltiadvantage/entity/ags_info.php b/enrol/lti/classes/local/ltiadvantage/entity/ags_info.php index 929406f5b2a..bd900ad4499 100644 --- a/enrol/lti/classes/local/ltiadvantage/entity/ags_info.php +++ b/enrol/lti/classes/local/ltiadvantage/entity/ags_info.php @@ -86,7 +86,7 @@ class ags_info { * @throws \coding_exception if any of the scopes is invalid. */ private function validate_scopes(array $scopes): void { - $validscopes = [ + $supportedscopes = [ self::SCOPES_LINEITEM_READONLY, self::SCOPES_LINEITEM_MANAGE, self::SCOPES_RESULT_READONLY, @@ -96,17 +96,14 @@ class ags_info { if (!is_string($scope)) { throw new \coding_exception('Scope must be a string value'); } - $key = array_search($scope, $validscopes); - if ($key === false) { - throw new \coding_exception("Scope '{$scope}' is invalid."); - } - if ($key == 0) { + $key = array_search($scope, $supportedscopes); + if ($key === 0) { $this->lineitemscopes[] = self::SCOPES_LINEITEM_READONLY; - } else if ($key == 1) { + } else if ($key === 1) { $this->lineitemscopes[] = self::SCOPES_LINEITEM_MANAGE; - } else if ($key == 2) { + } else if ($key === 2) { $this->resultscope = self::SCOPES_RESULT_READONLY; - } else if ($key == 3) { + } else if ($key === 3) { $this->scorescope = self::SCOPES_SCORES_POST; } } diff --git a/enrol/lti/tests/local/ltiadvantage/entity/ags_info_test.php b/enrol/lti/tests/local/ltiadvantage/entity/ags_info_test.php index 689e816b0d4..fd1827b567c 100644 --- a/enrol/lti/tests/local/ltiadvantage/entity/ags_info_test.php +++ b/enrol/lti/tests/local/ltiadvantage/entity/ags_info_test.php @@ -42,7 +42,12 @@ class ags_info_test extends \advanced_testcase { $agsinfo = ags_info::create(...array_values($args)); $this->assertEquals($args['lineitemsurl'], $agsinfo->get_lineitemsurl()); $this->assertEquals($args['lineitemurl'], $agsinfo->get_lineitemurl()); - $this->assertEquals($args['scopes'], $agsinfo->get_scopes()); + if (isset($expectations['scopes'])) { + $this->assertEquals($expectations['scopes'], $agsinfo->get_scopes()); + } else { + $this->assertEquals($args['scopes'], $agsinfo->get_scopes()); + } + $this->assertEquals($expectations['lineitemscope'], $agsinfo->get_lineitemscope()); $this->assertEquals($expectations['scorescope'], $agsinfo->get_scorescope()); $this->assertEquals($expectations['resultscope'], $agsinfo->get_resultscope()); @@ -239,7 +244,7 @@ class ags_info_test extends \advanced_testcase { 'exceptionmessage' => 'Scope must be a string value' ] ], - 'Both lineitems and lineitem URL provided with invalid scopes' => [ + 'Both lineitems and lineitem URL provided with unsupported scopes' => [ 'args' => [ 'lineitemsurl' => new \moodle_url('https://platform.example.org/10/lineitems'), 'lineitemurl' => new \moodle_url('https://platform.example.org/10/lineitems/4/lineitem'), @@ -248,13 +253,23 @@ class ags_info_test extends \advanced_testcase { 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly', 'https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly', 'https://purl.imsglobal.org/spec/lti-ags/scope/score', - 'https://example.com/invalid/scope' + 'https://example.com/unsupported/scope' ] ], 'expectations' => [ - 'valid' => false, - 'exception' => \coding_exception::class, - 'exceptionmessage' => "Scope 'https://example.com/invalid/scope' is invalid." + 'valid' => true, + 'scopes' => [ + 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem', + 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly', + 'https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly', + 'https://purl.imsglobal.org/spec/lti-ags/scope/score', + ], + 'lineitemscope' => [ + 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem', + 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly' + ], + 'resultscope' => 'https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly', + 'scorescope' => 'https://purl.imsglobal.org/spec/lti-ags/scope/score' ] ], 'Both lineitems and lineitem URL provided with invalid scope types' => [