MDL-72846 testing: Add default block generators

This commit is contained in:
Noel De Martin 2021-10-19 12:31:30 +02:00
parent 18b2af60f5
commit 3ce936fe6d
4 changed files with 97 additions and 7 deletions

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];
}
@ -1347,4 +1348,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.");
}
}

View File

@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Default block generator class.
*
* @package core
* @category test
* @copyright 2021 Moodle Pty Ltd. (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Default block generator class to be used when a specific one is not supported.
*
* @package core
* @category test
* @copyright 2021 Moodle Pty Ltd. (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class default_block_generator extends testing_block_generator {
/**
* @var string
*/
private $blockname;
/**
* Constructor.
* @param testing_data_generator $datagenerator
* @param string $blockname
*/
public function __construct(testing_data_generator $datagenerator, string $blockname) {
parent::__construct($datagenerator);
$this->blockname = $blockname;
}
/**
* {@inheritdoc}
*/
public function get_blockname() {
return $this->blockname;
}
}

View File

@ -29,5 +29,5 @@ require_once(__DIR__.'/data_generator.php');
require_once(__DIR__.'/component_generator_base.php');
require_once(__DIR__.'/module_generator.php');
require_once(__DIR__.'/block_generator.php');
require_once(__DIR__.'/default_block_generator.php');
require_once(__DIR__.'/repository_generator.php');

View File

@ -47,6 +47,11 @@ class core_test_generator_testcase extends advanced_testcase {
$this->assertInstanceOf('mod_quiz_generator', $generator);
}
public function test_get_default_generator() {
$generator = $this->getDataGenerator()->get_plugin_generator('block_somethingthatdoesnotexist');
$this->assertInstanceOf('default_block_generator', $generator);
}
/**
* Test plugin generator, with no component directory.
*/