Skip to content

Commit

Permalink
MDL-15666 Generating test data on-the-fly using the generator!
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasconnault committed Aug 29, 2008
1 parent d9be210 commit 75d226d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 66 deletions.
139 changes: 74 additions & 65 deletions admin/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,67 @@ public function generate_users() {
return $users;
}

public function generate_data() {
set_time_limit($this->get('time_limit'));

// Process tiny data set
$tiny = $this->get('tiny');
if (!empty($tiny)) {
$this->verbose("Generating a tiny data set: 1 student in 1 course with 1 module in 1 section...");
$this->set('number_of_courses',1);
$this->set('number_of_students',1);
$this->set('number_of_modules',1);
$this->set('number_of_sections',1);
$this->set('assignment_grades',false);
$this->set('quiz_grades',false);
$this->set('students_per_course',1);
$this->set('questions_per_course',1);
$this->set('questions_per_quiz',1);
}

if ($this->get('pre_cleanup')) {
$this->verbose("Deleting previous test data...");

$this->data_cleanup();

if (!$this->get('quiet')) {
echo "Previous test data has been deleted.{$this->eolchar}";
}
}


if (!$this->get('no_data')) {
$users = $this->generate_users();
$courses = $this->generate_courses();
$modules = $this->generate_modules($courses);
$questions = $this->generate_questions($courses, $modules);
$course_users = $this->generate_role_assignments($users, $courses);
$this->generate_forum_posts($course_users, $modules);
$this->generate_grades($course_users, $courses, $modules);
$this->generate_module_content($course_users, $courses, $modules);
}

if ($this->get('post_cleanup')) {
if (!$this->get('quiet')) {
echo "Removing generated data..." . $this->eolchar;
}
$this->data_cleanup();
if (!$this->get('quiet')) {
echo "Generated data has been deleted." . $this->eolchar;
}
}

/**
* FINISHING SCRIPT
*/
$stoptimer = time()+microtime();
$timer = round($stoptimer-$this->starttimer,4);
if (!$this->get('quiet')) {
echo "End of script! ($timer seconds taken){$this->eolchar}";
}

}

public function generate_courses() {
global $DB;

Expand Down Expand Up @@ -254,7 +315,16 @@ public function generate_modules($courses) {

$this->verbose("Generating " . $this->get('number_of_sections')." sections with "
.$this->get('number_of_modules')." modules in each section, for each course...");
$modules = $DB->get_records('modules');

list($modules_list_sql, $modules_params) =
$DB->get_in_or_equal($this->get('modules_list'), SQL_PARAMS_NAMED, 'param0000', true);

list($modules_ignored_sql, $ignore_params) =
$DB->get_in_or_equal($this->modules_to_ignore, SQL_PARAMS_NAMED, 'param2000', false);

$wheresql = "name $modules_list_sql AND name $modules_ignored_sql";
$modules = $DB->get_records_select('modules', $wheresql,
array_merge($modules_params, $ignore_params));

foreach ($modules as $key => $module) {
$module->count = 0;
Expand Down Expand Up @@ -697,7 +767,7 @@ public function generate_grades($course_users, $courses, $modules) {
/**
* ASSIGNMENT GRADES GENERATION
*/
if ($this->get('assignment_grades')) {
if ($this->get('assignment_grades') && isset($modules['assignment'])) {
$grades_count = 0;
foreach ($course_users as $userid => $courses) {
foreach ($modules['assignment'] as $assignment) {
Expand Down Expand Up @@ -725,7 +795,7 @@ public function generate_grades($course_users, $courses, $modules) {
/**
* QUIZ GRADES GENERATION
*/
if ($this->get('quiz_grades')) {
if ($this->get('quiz_grades') && isset($modules['quiz'])) {
$grades_count = 0;
foreach ($course_users as $userid => $courses) {
foreach ($modules['quiz'] as $quiz) {
Expand Down Expand Up @@ -890,67 +960,6 @@ function data_cleanup() {
}
}

public function generate_data() {
set_time_limit($this->get('time_limit'));

// Process tiny data set
$tiny = $this->get('tiny');
if (!empty($tiny)) {
$this->verbose("Generating a tiny data set: 1 student in 1 course with 1 module in 1 section...");
$this->set('number_of_courses',1);
$this->set('number_of_students',1);
$this->set('number_of_modules',1);
$this->set('number_of_sections',1);
$this->set('assignment_grades',false);
$this->set('quiz_grades',false);
$this->set('students_per_course',1);
$this->set('questions_per_course',1);
$this->set('questions_per_quiz',1);
}

if ($this->get('pre_cleanup')) {
$this->verbose("Deleting previous test data...");

$this->data_cleanup();

if (!$this->get('quiet')) {
echo "Previous test data has been deleted.{$this->eolchar}";
}
}


if (!$this->get('no_data')) {
$users = $this->generate_users();
$courses = $this->generate_courses();
$modules = $this->generate_modules($courses);
$questions = $this->generate_questions($courses, $modules);
$course_users = $this->generate_role_assignments($users, $courses);
$this->generate_forum_posts($course_users, $modules);
$this->generate_grades($course_users, $courses, $modules);
$this->generate_module_content($course_users, $courses, $modules);
}

if ($this->get('post_cleanup')) {
if (!$this->get('quiet')) {
echo "Removing generated data..." . $this->eolchar;
}
$this->data_cleanup();
if (!$this->get('quiet')) {
echo "Generated data has been deleted." . $this->eolchar;
}
}

/**
* FINISHING SCRIPT
*/
$stoptimer = time()+microtime();
$timer = round($stoptimer-$this->starttimer,4);
if (!$this->get('quiet')) {
echo "End of script! ($timer seconds taken){$this->eolchar}";
}

}

public function get($setting) {
if (isset($this->settings[$setting])) {
return $this->settings[$setting]->value;
Expand Down Expand Up @@ -1153,7 +1162,7 @@ function definition() {
$options = null;
$htmloptions = null;

$label = ucfirst(str_replace('-', ' ', $setting->long));
$label = ucfirst(str_replace('_', ' ', $setting->long));
if (!empty($setting->type) && $setting->type == 'mod1,mod2...') {
$type = 'select';
$options = $generator->modules_list;
Expand Down
11 changes: 11 additions & 0 deletions lib/simpletest/testportfoliolib.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,16 @@ class portfoliolib_test extends UnitTestCase {
public $caller;
public $plugin;
public $exporter;
public $original_db;

function setUp() {
global $DB, $CFG;
$this->original_db = clone($DB);

$class = get_class($DB);
$DB = new $class();
$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, true, 'tst_');

$u = new StdClass;
$u->id = 100000000000;
$this->plugin = new mock_plugin();
Expand All @@ -132,6 +140,9 @@ function tearDown() {

$settings = array('no_data' => 1, 'post_cleanup' => 1, 'database_prefix' => 'tst_', 'quiet' => 1);
generator_generate_data($settings);

// Restore original DB
$DB = $this->original_db;
}

function test_construct_dupe_instance() {
Expand Down
16 changes: 15 additions & 1 deletion portfolio/type/boxnet/simpletest/testportfoliopluginboxnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,32 @@

class testPortfolioPluginBoxnet extends portfoliolib_test {
public function setUp() {
global $DB;

parent::setUp();
$this->plugin = &new mock_boxnetplugin($this);
$this->plugin->boxclient = new mock_boxclient();
$settings = array('tiny' => 1, 'quiet' => 1, 'database_prefix' => 'tst_', 'pre_cleanup' => 1);

$settings = array('tiny' => 1, 'quiet' => 1, 'database_prefix' => 'tst_', 'pre_cleanup' => 1,
'modules_list' => array('glossary'), 'entries_per_glossary' => 20,
'number_of_students' => 5, 'students_per_course' => 5, 'number_of_sections' => 1,
'number_of_modules' => 1, 'questions_per_course' => 0);
generator_generate_data($settings);
}

public function tearDown() {
parent::tearDown();
}

public function test_caller_glossary() {
global $DB;
$glossaries = $DB->get_records('glossary');
print_object($glossaries);
}

public function test_something() {
global $DB;

$ticket = md5(rand(0,873907));
$authtoken = 'ezfoeompplpug3ofii4nud0d8tvg96e0';

Expand Down

0 comments on commit 75d226d

Please sign in to comment.