Merge branch 'MDL-38582' of git://github.com/timhunt/moodle
[moodle.git] / lib / testing / generator / data_generator.php
index f6b774b..129dfa5 100644 (file)
@@ -41,16 +41,6 @@ class testing_data_generator {
     protected $groupcount = 0;
     protected $groupingcount = 0;
 
-    /**
-     * @var int keep track of how many forum discussions have been created.
-     */
-    protected $forumdiscussioncount = 0;
-
-    /**
-     * @var int keep track of how many forum posts have been created.
-     */
-    protected $forumpostcount = 0;
-
     /** @var array list of plugin generators */
     protected $generators = array();
 
@@ -92,8 +82,6 @@ EOD;
         $this->categorycount = 0;
         $this->coursecount = 0;
         $this->scalecount = 0;
-        $this->forumdiscussioncount = 0;
-        $this->forumpostcount = 0;
 
         foreach ($this->generators as $generator) {
             $generator->reset();
@@ -243,11 +231,11 @@ EOD;
      * Create a test course category
      * @param array|stdClass $record
      * @param array $options
-     * @return stdClass course category record
+     * @return coursecat course category record
      */
     public function create_category($record=null, array $options=null) {
         global $DB, $CFG;
-        require_once("$CFG->dirroot/course/lib.php");
+        require_once("$CFG->libdir/coursecatlib.php");
 
         $this->categorycount++;
         $i = $this->categorycount;
@@ -258,43 +246,15 @@ EOD;
             $record['name'] = 'Course category '.$i;
         }
 
-        if (!isset($record['idnumber'])) {
-            $record['idnumber'] = '';
-        }
-
         if (!isset($record['description'])) {
             $record['description'] = "Test course category $i\n$this->loremipsum";
         }
 
-        if (!isset($record['descriptionformat'])) {
-            $record['descriptionformat'] = FORMAT_MOODLE;
-        }
-
-        if (!isset($record['parent'])) {
-            $record['parent'] = 0;
-        }
-
-        if (empty($record['parent'])) {
-            $parent = new stdClass();
-            $parent->path = '';
-            $parent->depth = 0;
-        } else {
-            $parent = $DB->get_record('course_categories', array('id'=>$record['parent']), '*', MUST_EXIST);
+        if (!isset($record['idnumber'])) {
+            $record['idnumber'] = '';
         }
-        $record['depth'] = $parent->depth+1;
-
-        $record['sortorder'] = 0;
-        $record['timemodified'] = time();
-        $record['timecreated'] = $record['timemodified'];
 
-        $catid = $DB->insert_record('course_categories', $record);
-        $path = $parent->path . '/' . $catid;
-        $DB->set_field('course_categories', 'path', $path, array('id'=>$catid));
-        context_coursecat::instance($catid);
-
-        fix_course_sortorder();
-
-        return $DB->get_record('course_categories', array('id'=>$catid), '*', MUST_EXIST);
+        return coursecat::create($record);
     }
 
     /**
@@ -700,136 +660,6 @@ EOD;
 
         return true;
     }
-
-    /**
-     * Function to create a dummy discussion.
-     *
-     * @param array|stdClass $record
-     * @return stdClass the discussion object
-     */
-    public function create_forum_discussion($record = null) {
-        global $DB;
-
-        // Increment the forum discussion count.
-        $this->forumdiscussioncount++;
-
-        $record = (array) $record;
-
-        if (!isset($record['course'])) {
-            throw new coding_exception('course must be present in phpunit_util::create_forum_discussion() $record');
-        }
-
-        if (!isset($record['forum'])) {
-            throw new coding_exception('forum must be present in phpunit_util::create_forum_discussion() $record');
-        }
-
-        if (!isset($record['userid'])) {
-            throw new coding_exception('userid must be present in phpunit_util::create_forum_discussion() $record');
-        }
-
-        if (!isset($record['name'])) {
-            $record['name'] = "Discussion " . $this->forumdiscussioncount;
-        }
-
-        if (!isset($record['subject'])) {
-            $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
-        }
-
-        if (!isset($record['message'])) {
-            $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
-        }
-
-        if (!isset($record['messageformat'])) {
-            $record['messageformat'] = editors_get_preferred_format();
-        }
-
-        if (!isset($record['messagetrust'])) {
-            $record['messagetrust'] = "";
-        }
-
-        if (!isset($record['assessed'])) {
-            $record['assessed'] = '1';
-        }
-
-        if (!isset($record['groupid'])) {
-            $record['groupid'] = "-1";
-        }
-
-        if (!isset($record['timestart'])) {
-            $record['timestart'] = "0";
-        }
-
-        if (!isset($record['timeend'])) {
-            $record['timeend'] = "0";
-        }
-
-        if (!isset($record['mailnow'])) {
-            $record['mailnow'] = "0";
-        }
-
-        $record = (object) $record;
-
-        // Add the discussion.
-        $record->id = forum_add_discussion($record, null, null, $record->userid);
-
-        return $record;
-    }
-
-    /**
-     * Function to create a dummy post.
-     *
-     * @param array|stdClass $record
-     * @return stdClass the post object
-     */
-    public function create_forum_post($record = null) {
-        global $DB;
-
-        // Increment the forum post count.
-        $this->forumpostcount++;
-
-        // Variable to store time.
-        $time = time() + $this->forumpostcount;
-
-        $record = (array) $record;
-
-        if (!isset($record['discussion'])) {
-            throw new coding_exception('discussion must be present in phpunit_util::create_forum_post() $record');
-        }
-
-        if (!isset($record['userid'])) {
-            throw new coding_exception('userid must be present in phpunit_util::create_forum_post() $record');
-        }
-
-        if (!isset($record['parent'])) {
-            $record['parent'] = 0;
-        }
-
-        if (!isset($record['subject'])) {
-            $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
-        }
-
-        if (!isset($record['message'])) {
-            $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
-        }
-
-        if (!isset($record['created'])) {
-            $record['created'] = $time;
-        }
-
-        if (!isset($record['modified'])) {
-            $record['modified'] = $time;
-        }
-
-        $record = (object) $record;
-
-        // Add the post.
-        $record->id = $DB->insert_record('forum_posts', $record);
-
-        // Update the last post.
-        forum_discussion_update_last_post($record->discussion);
-
-        return $record;
-    }
 }
 
 /**