diff --git a/mod/lti/classes/external.php b/mod/lti/classes/external.php index dad4370feea..dcb6613b68a 100644 --- a/mod/lti/classes/external.php +++ b/mod/lti/classes/external.php @@ -138,22 +138,18 @@ class mod_lti_external extends external_api { * @throws moodle_exception */ public static function get_tool_proxies($orphanedonly) { - global $PAGE; $params = self::validate_parameters(self::get_tool_proxies_parameters(), array( 'orphanedonly' => $orphanedonly )); $orphanedonly = $params['orphanedonly']; - $proxies = array(); $context = context_system::instance(); self::validate_context($context); require_capability('moodle/site:config', $context); - $proxies = lti_get_tool_proxies($orphanedonly); - - return array_map('serialise_tool_proxy', $proxies); + return lti_get_tool_proxies($orphanedonly); } /** @@ -164,7 +160,7 @@ class mod_lti_external extends external_api { */ public static function get_tool_proxies_returns() { return new external_multiple_structure( - self::tool_type_return_structure() + self::tool_proxy_return_structure() ); } diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php index fe42828851c..a80f1b16a01 100644 --- a/mod/lti/locallib.php +++ b/mod/lti/locallib.php @@ -4173,9 +4173,14 @@ function serialise_tool_type(stdClass $type) { * * @param stdClass $proxy The tool proxy * + * @deprecated since Moodle 3.10 + * @todo This will be finally removed for Moodle 4.2 as part of MDL-69976. * @return array An array of values representing this type */ function serialise_tool_proxy(stdClass $proxy) { + $deprecatedtext = __FUNCTION__ . '() is deprecated. Please remove all references to this method.'; + debugging($deprecatedtext, DEBUG_DEVELOPER); + return array( 'id' => $proxy->id, 'name' => $proxy->name, diff --git a/mod/lti/tests/externallib_test.php b/mod/lti/tests/externallib_test.php index 2b088a0bd57..f56dc8330e2 100644 --- a/mod/lti/tests/externallib_test.php +++ b/mod/lti/tests/externallib_test.php @@ -88,6 +88,75 @@ class mod_lti_external_testcase extends externallib_advanced_testcase { ]; } + /** + * Generate a tool type. + * + * @param string $uniqueid Each tool type needs a different base url. Provide a unique string for every tool type created. + * @param int|null $toolproxyid Optional proxy to associate with tool type. + * @return stdClass A tool type. + */ + protected function generate_tool_type(string $uniqueid, int $toolproxyid = null) : stdClass { + // Create a tool type. + $type = new stdClass(); + $type->state = LTI_TOOL_STATE_CONFIGURED; + $type->name = "Test tool $uniqueid"; + $type->description = "Example description $uniqueid"; + $type->toolproxyid = $toolproxyid; + $type->baseurl = $this->getExternalTestFileUrl("/test$uniqueid.html"); + lti_add_type($type, new stdClass()); + return $type; + } + + /** + * Generate a tool proxy. + * + * @param string $uniqueid Each tool proxy needs a different reg url. Provide a unique string for every tool proxy created. + * @return stdClass A tool proxy. + */ + protected function generate_tool_proxy(string $uniqueid) : stdClass { + // Create a tool proxy. + $proxy = mod_lti_external::create_tool_proxy("Test proxy $uniqueid", + $this->getExternalTestFileUrl("/proxy$uniqueid.html"), array(), array()); + $proxy = (object)external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy); + return $proxy; + } + + /** + * Test get_tool_proxies. + */ + public function test_mod_lti_get_tool_proxies() { + // Create two tool proxies. One to associate with tool, and one to leave orphaned. + $this->setAdminUser(); + $proxy = $this->generate_tool_proxy("1"); + $orphanedproxy = $this->generate_tool_proxy("2"); + $this->generate_tool_type("1", $proxy->id); // Associate proxy 1 with tool type. + + // Fetch all proxies. + $proxies = mod_lti_external::get_tool_proxies(false); + $proxies = external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies); + + $this->assertCount(2, $proxies); + $this->assertEqualsCanonicalizing([(array) $proxy, (array) $orphanedproxy], $proxies); + } + + /** + * Test get_tool_proxies with orphaned proxies only. + */ + public function test_mod_lti_get_orphaned_tool_proxies() { + // Create two tool proxies. One to associate with tool, and one to leave orphaned. + $this->setAdminUser(); + $proxy = $this->generate_tool_proxy("1"); + $orphanedproxy = $this->generate_tool_proxy("2"); + $this->generate_tool_type("1", $proxy->id); // Associate proxy 1 with tool type. + + // Fetch all proxies. + $proxies = mod_lti_external::get_tool_proxies(true); + $proxies = external_api::clean_returnvalue(mod_lti_external::get_tool_proxies_returns(), $proxies); + + $this->assertCount(1, $proxies); + $this->assertEqualsCanonicalizing([(array) $orphanedproxy], $proxies); + } + /** * Test get_tool_launch_data. */ diff --git a/mod/lti/upgrade.txt b/mod/lti/upgrade.txt index a3c01f4f343..318c625ce78 100644 --- a/mod/lti/upgrade.txt +++ b/mod/lti/upgrade.txt @@ -4,6 +4,8 @@ This files describes API changes in the lti code. * Select Content supports multiple, allowing a tool to return more than one link at a time. Parameter multiple in function lti_build_content_item_selection_request() is now set to true. +* Deprecated unused function after external function, 'get_tool_proxies()', was refactored: + - serialise_tool_proxy() === 3.8 ===