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.
This commit is contained in:
Jake Dallimore
2023-08-29 13:06:47 +08:00
parent b3ef113509
commit 7d1cd36e89
+37 -5
View File
@@ -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);
}
/**