From 7d1cd36e8919d6825fddf252624a4150a8ffcdac Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Sat, 29 Jul 2023 15:16:27 +0800 Subject: [PATCH] MDL-78597 mod_lti: add full field support to tool_types generator This method is used by behat only, where only a single arg is received. The method lti_add_type() expects two params: type and config, but will only ever receive one. This means not all the fields can be set when creating an lti type. This change: - Removes the superfluous param which the method won't receive - Improves the logic for handling type and type config data, making it match what happens when creating types via mforms. - Adds relative URL support to the baseurl field, allowing behat features to create types using local tool fixtures. - Sets sensible default for missing config data, allowing the created tool type to be used in launches in places like behat. --- mod/lti/tests/generator/lib.php | 42 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/mod/lti/tests/generator/lib.php b/mod/lti/tests/generator/lib.php index bfded14fd74..1a521ca7936 100644 --- a/mod/lti/tests/generator/lib.php +++ b/mod/lti/tests/generator/lib.php @@ -88,17 +88,49 @@ class mod_lti_generator extends testing_module_generator { lti_add_tool_proxy((object) $config); } + /** + * Split type creation data into 'type' and 'config' components, based on input array key prefixes. + * + * The $data array contains both the type data and config data that will be passed to lti_add_type(). This must be split into + * two params (type, config) based on the array key prefixes ({@see lti_add_type()} for how the two params are handled): + * - NO prefix: denotes 'type' data. + * - 'lti_' prefix: denotes 'config' data. + * - 'ltiservice_' prefix: denotes 'config' data, specifically config for service plugins. + * + * @param array $data array of type and config data containing prefixed keys. + * @return array containing separated type and config data. E.g. ['type' = [...], 'config' => [...]] + */ + protected function get_type_and_config_from_data(array $data): array { + // Grab any non-prefixed fields; these are the type fields. The rest is considered config. + $type = array_filter( + $data, + fn($val, $key) => !str_contains($key, 'lti_') && !str_contains($key, 'ltiservice_'), + ARRAY_FILTER_USE_BOTH + ); + $config = array_diff_key($data, $type); + + return ['type' => $type, 'config' => $config]; + } + /** * Create a tool type. * - * @param array $type - * @param array|null $config + * @param array $data */ - public function create_tool_types(array $type, ?array $config = null) { - if (!isset($type['baseurl'])) { + public function create_tool_types(array $data) { + if (!isset($data['baseurl'])) { throw new coding_exception('Must specify baseurl when creating a LTI tool type.'); } - lti_add_type((object) $type, (object) $config); + $data['baseurl'] = (new moodle_url($data['baseurl']))->out(false); // Permits relative URLs in behat features. + + // Sensible defaults permitting the tool type to be used in a launch. + $data['lti_acceptgrades'] = $data['lti_acceptgrades'] ?? LTI_SETTING_ALWAYS; + $data['lti_sendname'] = $data['lti_sendname'] ?? LTI_SETTING_ALWAYS; + $data['lti_sendemailaddr'] = $data['lti_sendname'] ?? LTI_SETTING_ALWAYS; + + ['type' => $type, 'config' => $config] = $this->get_type_and_config_from_data($data); + + lti_add_type(type: (object) $type, config: (object) $config); } /**