MDL-32323 improve phpunit data generators

This commit is contained in:
Petr Skoda 2012-04-06 12:16:18 +02:00
parent 93c4ee56c7
commit 3c4c51891c
6 changed files with 561 additions and 97 deletions

View file

@ -25,17 +25,24 @@
defined('MOODLE_INTERNAL') || die();
/**
* Data generator for unit tests
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class phpunit_data_generator {
protected $usercounter = 0;
protected $categorycount = 0;
protected $coursecount = 0;
protected $blockcount = 0;
protected $modulecount = 0;
protected $scalecount = 0;
/** @var array list of plugin generators */
protected $generators = array();
/**
* To be called from data reset code only,
* do not use in tests.
@ -45,9 +52,32 @@ class phpunit_data_generator {
$this->usercounter = 0;
$this->categorycount = 0;
$this->coursecount = 0;
$this->blockcount = 0;
$this->modulecount = 0;
$this->scalecount = 0;
foreach($this->generators as $generator) {
$generator->reset();
}
}
public function get_plugin_generator($component) {
list($type, $plugin) = normalize_component($component);
if ($type !== 'mod' and $type !== 'block') {
throw new coding_exception("Plugin type $type does not support generators yet");
}
$dir = get_plugin_directory($type, $plugin);
if (!isset($this->generators[$type.'_'.$plugin])) {
$lib = "$dir/tests/generator/lib.php";
if (!include_once($lib)) {
throw new coding_exception("Plugin $component does not support data generator, missing tests/generator/lib");
}
$classname = $type.'_'.$plugin.'_generator';
$this->generators[$type.'_'.$plugin] = new $classname($this);
}
return $this->generators[$type.'_'.$plugin];
}
/**
@ -171,7 +201,7 @@ class phpunit_data_generator {
$record['descriptionformat'] = 0;
}
if ($record['parent'] == 0) {
if (empty($record['parent'])) {
$parent = new stdClass();
$parent->path = '';
$parent->depth = 0;
@ -313,41 +343,8 @@ class phpunit_data_generator {
* @return stdClass block instance record
*/
public function create_block($blockname, $record=null, array $options=null) {
global $DB;
$this->blockcount++;
$i = $this->blockcount;
$record = (array)$record;
$record['blockname'] = $blockname;
//TODO: use block callbacks
if (!isset($record['parentcontextid'])) {
$record['parentcontextid'] = context_system::instance()->id;
}
if (!isset($record['showinsubcontexts'])) {
$record['showinsubcontexts'] = 1;
}
if (!isset($record['pagetypepattern'])) {
$record['pagetypepattern'] = '';
}
if (!isset($record['subpagepattern'])) {
$record['subpagepattern'] = '';
}
if (!isset($record['defaultweight'])) {
$record['defaultweight'] = '';
}
$biid = $DB->insert_record('block_instances', $record);
context_block::instance($biid);
return $DB->get_record('block_instances', array('id'=>$biid), '*', MUST_EXIST);
$generator = $this->get_plugin_generator('block_'.$blockname);
return $generator->create_instance($record, $options);
}
/**
@ -358,63 +355,8 @@ class phpunit_data_generator {
* @return stdClass activity record
*/
public function create_module($modulename, $record=null, array $options=null) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
$this->modulecount++;
$i = $this->modulecount;
$record = (array)$record;
$options = (array)$options;
if (!isset($record['name'])) {
$record['name'] = get_string('pluginname', $modulename).' '.$i;
}
if (!isset($record['intro'])) {
$record['intro'] = 'Test module '.$i;
}
if (!isset($record['introformat'])) {
$record['introformat'] = FORMAT_MOODLE;
}
if (!isset($options['section'])) {
$options['section'] = 1;
}
//TODO: use module callbacks
if ($modulename === 'page') {
if (!isset($record['content'])) {
$record['content'] = 'Test page content';
}
if (!isset($record['contentformat'])) {
$record['contentformat'] = FORMAT_MOODLE;
}
} else {
error('TODO: only mod_page is supported in data generator for now');
}
$id = $DB->insert_record($modulename, $record);
$cm = new stdClass();
$cm->course = $record['course'];
$cm->module = $DB->get_field('modules', 'id', array('name'=>$modulename));
$cm->section = $options['section'];
$cm->instance = $id;
$cm->id = $DB->insert_record('course_modules', $cm);
$cm->coursemodule = $cm->id;
add_mod_to_section($cm);
context_module::instance($cm->id);
$instance = $DB->get_record($modulename, array('id'=>$id), '*', MUST_EXIST);
$instance->cmid = $cm->id;
return $instance;
$generator = $this->get_plugin_generator('mod_'.$modulename);
return $generator->create_instance($record, $options);
}
/**
@ -468,3 +410,191 @@ class phpunit_data_generator {
return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);
}
}
/**
* Module generator base class.
*
* Extend in mod/xxxx/tests/generator/lib.php as class mod_xxxx_generator.
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class phpunit_module_generator {
/** @var phpunit_data_generator@var */
protected $datagenerator;
/** @var number of created instances */
protected $instancecount = 0;
public function __construct(phpunit_data_generator $datagenerator) {
$this->datagenerator = $datagenerator;
}
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->instancecount = 0;
}
/**
* Returns module name
* @return string name of module that this class describes
* @throws coding_exception if class invalid
*/
public function get_modulename() {
$matches = null;
if (!preg_match('/^mod_([a-z0-9]+)_generator$/', get_class($this), $matches)) {
throw new coding_exception('Invalid module generator class name: '.get_class($this));
}
if (empty($matches[1])) {
throw new coding_exception('Invalid module generator class name: '.get_class($this));
}
return $matches[1];
}
/**
* Create course module and link it to course
* @param stdClass $instance
* @param array $options: section, visible
* @return stdClass $cm instance
*/
protected function create_course_module(stdClass $instance, array $options) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
$modulename = $this->get_modulename();
$cm = new stdClass();
$cm->course = $instance->course;
$cm->module = $DB->get_field('modules', 'id', array('name'=>$modulename));
$cm->instance = $instance->id;
$cm->section = isset($options['section']) ? $options['section'] : 0;
$cm->idnumber = isset($options['idnumber']) ? $options['idnumber'] : 0;
$cm->added = time();
$columns = $DB->get_columns('course_modules');
foreach ($options as $key=>$value) {
if ($key === 'id' or !isset($columns[$key])) {
continue;
}
if (property_exists($cm, $key)) {
continue;
}
$cm->$key = $value;
}
$cm->id = $DB->insert_record('course_modules', $cm);
$cm->coursemodule = $cm->id;
add_mod_to_section($cm);
$cm = get_coursemodule_from_id($modulename, $cm->id, $cm->course, true, MUST_EXIST);
context_module::instance($cm->id);
return $cm;
}
/**
* Create a test module
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record
*/
abstract public function create_instance($record = null, array $options = null);
}
/**
* Block generator base class.
*
* Extend in blocks/xxxx/tests/generator/lib.php as class block_xxxx_generator.
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class phpunit_block_generator {
/** @var phpunit_data_generator@var */
protected $datagenerator;
/** @var number of created instances */
protected $instancecount = 0;
public function __construct(phpunit_data_generator $datagenerator) {
$this->datagenerator = $datagenerator;
}
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->instancecount = 0;
}
/**
* Returns block name
* @return string name of block that this class describes
* @throws coding_exception if class invalid
*/
public function get_blockname() {
$matches = null;
if (!preg_match('/^block_([a-z0-9_]+)_generator$/', get_class($this), $matches)) {
throw new coding_exception('Invalid block generator class name: '.get_class($this));
}
if (empty($matches[1])) {
throw new coding_exception('Invalid block generator class name: '.get_class($this));
}
return $matches[1];
}
/**
* Fill in record defaults
* @param stdClass $record
* @return stdClass
*/
protected function prepare_record(stdClass $record) {
$record->blockname = $this->get_blockname();
if (!isset($record->parentcontextid)) {
$record->parentcontextid = context_system::instance()->id;
}
if (!isset($record->showinsubcontexts)) {
$record->showinsubcontexts = 1;
}
if (!isset($record->pagetypepattern)) {
$record->pagetypepattern = '';
}
if (!isset($record->subpagepattern)) {
$record->subpagepattern = null;
}
if (!isset($record->defaultregion)) {
$record->defaultregion = '';
}
if (!isset($record->defaultweight)) {
$record->defaultweight = '';
}
if (!isset($record->configdata)) {
$record->configdata = null;
}
return $record;
}
/**
* Create a test block
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record
*/
abstract public function create_instance($record = null, array $options = null);
}