]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15666 Generating test data on-the-fly using the generator!
authornicolasconnault <nicolasconnault>
Fri, 29 Aug 2008 14:46:55 +0000 (14:46 +0000)
committernicolasconnault <nicolasconnault>
Fri, 29 Aug 2008 14:46:55 +0000 (14:46 +0000)
admin/generator.php
lib/simpletest/testportfoliolib.php
portfolio/type/boxnet/simpletest/testportfoliopluginboxnet.php

index ea8f1f4fd82ea9a0336141cf3b9bdb805bd3b31e..1b513bb3264419fa47f3079d9986d6daffc82a63 100755 (executable)
@@ -205,6 +205,67 @@ class generator {
         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;
 
@@ -254,7 +315,16 @@ class generator {
 
         $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;
@@ -697,7 +767,7 @@ class generator {
         /**
          * 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) {
@@ -725,7 +795,7 @@ class generator {
         /**
          * 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) {
@@ -890,67 +960,6 @@ class generator {
         }
     }
 
-    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;
@@ -1153,7 +1162,7 @@ class generator_form extends moodleform {
             $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;
index 117657f05d29c5cf7e57e94c1e84febbf96dc8ac..0500f1d2fabbb08e0580194d74d031e0392c0ec5 100755 (executable)
@@ -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();
@@ -132,6 +140,9 @@ class portfoliolib_test extends UnitTestCase {
 
         $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() {
index 89a1cda2a4e3f6c7a5928bd4ff82ee85f888cfbc..e396bbebc2d3dfca8de5c39bc5f98e459e3f28e7 100644 (file)
@@ -8,10 +8,16 @@ Mock::generatePartial('portfolio_plugin_boxnet', 'mock_boxnetplugin', array('ens
 
 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);
     }
 
@@ -19,7 +25,15 @@ class testPortfolioPluginBoxnet extends portfoliolib_test {
         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';