From 3ce936fe6d6c6807c0c43480323160e91904c35c Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 19 Oct 2021 12:31:30 +0200 Subject: [PATCH] MDL-72846 testing: Add default block generators --- lib/testing/generator/data_generator.php | 38 ++++++++++-- .../generator/default_block_generator.php | 59 +++++++++++++++++++ lib/testing/generator/lib.php | 2 +- lib/testing/tests/generator_test.php | 5 ++ 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 lib/testing/generator/default_block_generator.php diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 1eae6af1926..7948bd15873 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -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."); + } + } diff --git a/lib/testing/generator/default_block_generator.php b/lib/testing/generator/default_block_generator.php new file mode 100644 index 00000000000..b86eb1a4413 --- /dev/null +++ b/lib/testing/generator/default_block_generator.php @@ -0,0 +1,59 @@ +. + +/** + * 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; + } + +} diff --git a/lib/testing/generator/lib.php b/lib/testing/generator/lib.php index 7e2b19099ce..620d6008eba 100644 --- a/lib/testing/generator/lib.php +++ b/lib/testing/generator/lib.php @@ -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'); - diff --git a/lib/testing/tests/generator_test.php b/lib/testing/tests/generator_test.php index e1b2cfa72ae..a48e4c6cd90 100644 --- a/lib/testing/tests/generator_test.php +++ b/lib/testing/tests/generator_test.php @@ -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. */