MDL-72846 testing: Add default block generators

This commit is contained in:
Noel De Martin
2021-12-02 14:56:20 +01:00
parent 448a2aa60c
commit 74a6ebbd7b
4 changed files with 97 additions and 7 deletions
+32 -6
View File
@@ -121,19 +121,20 @@ EOD;
$dir = core_component::get_component_directory($component);
$lib = $dir . '/tests/generator/lib.php';
if (!$dir || !is_readable($lib)) {
throw new coding_exception("Component {$component} does not support " .
"generators yet. Missing tests/generator/lib.php.");
$this->generators[$component] = $this->get_default_plugin_generator($component);
return $this->generators[$component];
}
include_once($lib);
$classname = $component . '_generator';
if (!class_exists($classname)) {
throw new coding_exception("Component {$component} does not support " .
"data generators yet. Class {$classname} not found.");
if (class_exists($classname)) {
$this->generators[$component] = new $classname($this);
} else {
$this->generators[$component] = $this->get_default_plugin_generator($component, $classname);
}
$this->generators[$component] = new $classname($this);
return $this->generators[$component];
}
@@ -1230,4 +1231,29 @@ EOD;
return $DB->get_record('user_lastaccess', ['id' => $recordid], '*', MUST_EXIST);
}
/**
* Gets a default generator for a given component.
*
* @param string $component The component name, e.g. 'mod_forum' or 'core_question'.
* @param string $classname The name of the class missing from the generators file.
* @return component_generator_base The generator.
*/
protected function get_default_plugin_generator(string $component, ?string $classname = null) {
[$type, $plugin] = core_component::normalize_component($component);
switch ($type) {
case 'block':
return new default_block_generator($this, $plugin);
}
if (is_null($classname)) {
throw new coding_exception("Component {$component} does not support " .
"generators yet. Missing tests/generator/lib.php.");
}
throw new coding_exception("Component {$component} does not support " .
"data generators yet. Class {$classname} not found.");
}
}