]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15666 Added database and forum caller unit tests
authornicolasconnault <nicolasconnault>
Tue, 9 Sep 2008 09:17:49 +0000 (09:17 +0000)
committernicolasconnault <nicolasconnault>
Tue, 9 Sep 2008 09:17:49 +0000 (09:17 +0000)
admin/generator.php
mod/data/simpletest/test_data_portfolio_callers.php [new file with mode: 0644]
mod/forum/simpletest/test_forum_portfolio_callers.php [new file with mode: 0644]

index 9c487b9a963eb2628fab7e5bdaf13a444f4ec1ac..71f427ee62fcf51d7471e4dfd1d77d901750813a 100755 (executable)
@@ -10,6 +10,7 @@ class generator {
     public $modules_to_ignore = array('hotpot', 'lams', 'journal', 'scorm', 'exercise', 'dialogue');
     public $modules_list = array('forum' => 'forum',
                                   'assignment' => 'assignment',
+                                  'data' => 'data',
                                   'glossary' => 'glossary',
                                   'quiz' => 'quiz',
                                   'comments' => 'comments',
@@ -144,6 +145,12 @@ class generator {
              array('short'=>'pd', 'long' => 'posts_per_discussion',
                    'help' => 'The number of posts to generate for each forum discussion. Default=15',
                    'type'=>'NUMBER', 'default' => 15),
+             array('short'=>'fd', 'long' => 'fields_per_database',
+                   'help' => 'The number of fields to generate for each database. Default=4',
+                   'type'=>'NUMBER', 'default' => 4),
+             array('short'=>'drs', 'long' => 'database_records_per_student',
+                   'help' => 'The number of records to generate for each student/database tuple. Default=1',
+                   'type'=>'NUMBER', 'default' => 1),
             );
 
         foreach ($arguments as $args_array) {
@@ -461,6 +468,10 @@ class generator {
                                 $module->schedule = 1;
                                 $module->chattime = 60 * 60 * 4;
                                 break;
+                            case 'data':
+                                $module->intro = $description;
+                                $module->name = 'test';
+                                break;
                             case 'choice':
                                 $module->text = $content;
                                 $module->option = array('Good choice', 'Bad choice', 'No choice');
@@ -886,6 +897,7 @@ class generator {
 
     public function generate_module_content($course_users, $courses, $modules) {
         global $USER, $DB, $CFG;
+        $result = null;
 
         $entries_count = 0;
         if ($this->get('entries_per_glossary') && !empty($modules['glossary'])) {
@@ -909,9 +921,54 @@ class generator {
             if ($entries_count > 0 && !$this->get('quiet')) {
                 echo "$entries_count glossary definitions have been generated.{$this->eolchar}";
             }
-            return true;
+            $result = true;
         }
-        return null;
+
+        $fields_count = 0;
+        if ($this->get('fields_per_database') && $this->get('database_records_per_student')) {
+            $database_field_types = array('checkbox',
+                                          'date',
+                                          'file',
+                                          'latlong',
+                                          'menu',
+                                          'multimenu',
+                                          'number',
+                                          'picture',
+                                          'radiobutton',
+                                          'text',
+                                          'textarea',
+                                          'url');
+
+            $fields = array();
+            for ($i = 0; $i < $this->get('fields_per_database'); $i++) {
+                $type = $database_field_types[array_rand($database_field_types)];
+                require_once($CFG->dirroot.'/mod/data/field/'.$type.'/field.class.php');
+                $newfield = 'data_field_'.$type;
+                $newfield = $newfield(0, $data, true);
+                $fields[] = $newfield;
+            }
+
+            foreach ($modules['data'] as $data) {
+
+                // Generate fields for each database (same fields for all, no arguing)
+                for ($i = 0; $i < $this->get('fields_per_database'); $i++) {
+
+                }
+
+                // Generate database records for each student, if needed
+                for ($i = 0; $i < $this->get('database_records_per_student'); $i++) {
+
+                }
+            }
+            if ($fields_count > 0 && !$this->get('quiet')) {
+                $datacount = count($modules['data']);
+                echo "$fields_count database fields have been generated for each of the "
+                   . "$datacount generated databases.{$this->eolchar}";
+            }
+            $result = true;
+        }
+
+        return $result;
     }
 
     /**
diff --git a/mod/data/simpletest/test_data_portfolio_callers.php b/mod/data/simpletest/test_data_portfolio_callers.php
new file mode 100644 (file)
index 0000000..5c8898a
--- /dev/null
@@ -0,0 +1,56 @@
+<?php // $Id$
+require_once($CFG->libdir.'/simpletest/testportfoliolib.php');
+require_once($CFG->dirroot.'/mod/data/lib.php');
+require_once($CFG->dirroot.'/admin/generator.php');
+
+Mock::generate('data_portfolio_caller', 'mock_caller');
+Mock::generate('portfolio_exporter', 'mock_exporter');
+
+class testDataPortfolioCallers extends portfoliolib_test {
+    public $module_type = 'data';
+    public $modules = array();
+    public $entries = array();
+    public $caller;
+
+    public function setUp() {
+        global $DB, $USER;
+
+        parent::setUp();
+
+        $settings = array('quiet' => 1,
+                          'database_prefix' => 'tst_',
+                          'pre_cleanup' => 0,
+                          'modules_list' => array($this->module_type),
+                          'number_of_students' => 5,
+                          'students_per_course' => 5,
+                          'number_of_sections' => 1,
+                          'number_of_modules' => 1,
+                          'questions_per_course' => 0);
+
+        generator_generate_data($settings);
+
+        $this->modules = $DB->get_records($this->module_type);
+        $first_module = reset($this->modules);
+        $cm = get_coursemodule_from_instance($this->module_type, $first_module->id);
+
+        // Callback args required: id, record, delimiter_name, exporttype
+
+        $callbackargs = array('assignmentid' => $cm->id, 'userid' => $USER->id);
+        $this->caller = new assignment_portfolio_caller($callbackargs);
+        $this->caller->set('exporter', new mock_exporter());
+        $user = $DB->get_record('user', array('id' => $first_submission->userid));
+        $this->caller->set('user', $user);
+    }
+
+    public function tearDown() {
+        parent::tearDown();
+    }
+
+    public function test_caller_sha1() {
+        $sha1 = $this->caller->get_sha1();
+        $this->caller->prepare_package();
+        $this->assertEqual($sha1, $this->caller->get_sha1());
+    }
+
+}
+?>
diff --git a/mod/forum/simpletest/test_forum_portfolio_callers.php b/mod/forum/simpletest/test_forum_portfolio_callers.php
new file mode 100644 (file)
index 0000000..eeb1838
--- /dev/null
@@ -0,0 +1,64 @@
+<?php // $Id$
+require_once($CFG->libdir.'/simpletest/testportfoliolib.php');
+require_once($CFG->dirroot.'/mod/forum/lib.php');
+require_once($CFG->dirroot.'/admin/generator.php');
+
+Mock::generate('forum_portfolio_caller', 'mock_caller');
+Mock::generate('portfolio_exporter', 'mock_exporter');
+
+class testForumPortfolioCallers extends portfoliolib_test {
+    public $module_type = 'forum';
+    public $modules = array();
+    public $entries = array();
+    public $caller;
+
+    public function setUp() {
+        global $DB, $USER;
+
+        parent::setUp();
+
+        $settings = array('quiet' => 1,
+                          'verbose' => 0,
+                          'database_prefix' => 'tst_',
+                          'pre_cleanup' => 0,
+                          'post_cleanup' => 0,
+                          'modules_list' => array($this->module_type),
+                          'discussions_per_forum' => 5,
+                          'posts_per_discussion' => 10,
+                          'number_of_students' => 5,
+                          'students_per_course' => 5,
+                          'number_of_sections' => 1,
+                          'number_of_modules' => 1,
+                          'questions_per_course' => 0);
+
+        generator_generate_data($settings);
+
+        $this->modules = $DB->get_records($this->module_type);
+        $first_module = reset($this->modules);
+        $cm = get_coursemodule_from_instance($this->module_type, $first_module->id);
+
+        $discussions = $DB->get_records('forum_discussions', array('forum' => $first_module->id));
+        $first_discussion = reset($discussions);
+
+        $posts = $DB->get_records('forum_posts', array('discussion' => $first_discussion->id));
+        $first_post = reset($posts);
+
+        $callbackargs = array('postid' => $first_post->id, 'discussionid' => $first_discussion->id);
+        $this->caller = new forum_portfolio_caller($callbackargs);
+        $this->caller->set('exporter', new mock_exporter());
+        $user = $DB->get_record('user', array('id' => $first_post->userid));
+        $this->caller->set('user', $user);
+    }
+
+    public function tearDown() {
+        parent::tearDown();
+    }
+
+    public function test_caller_sha1() {
+        $sha1 = $this->caller->get_sha1();
+        $this->caller->prepare_package();
+        $this->assertEqual($sha1, $this->caller->get_sha1());
+    }
+
+}
+?>