Merge branch 'w47_MDL-36547_m23_sectiongen' of git://github.com/skodak/moodle into...
authorDan Poltawski <dan@moodle.com>
Wed, 21 Nov 2012 07:34:22 +0000 (15:34 +0800)
committerDan Poltawski <dan@moodle.com>
Wed, 21 Nov 2012 07:34:22 +0000 (15:34 +0800)
lib/phpunit/classes/module_generator.php
lib/phpunit/tests/generator_test.php

index e684657..1e536b5 100644 (file)
@@ -87,13 +87,12 @@ abstract class phpunit_module_generator {
         $cm->course             = $courseid;
         $cm->module             = $DB->get_field('modules', 'id', array('name'=>$modulename));
         $cm->instance           = 0;
-        $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])) {
+            if ($key === 'id' or !isset($columns[$key]) or $key === 'section') {
                 continue;
             }
             if (property_exists($cm, $key)) {
@@ -105,7 +104,11 @@ abstract class phpunit_module_generator {
         $cm->id = $DB->insert_record('course_modules', $cm);
         $cm->coursemodule = $cm->id;
 
-        add_mod_to_section($cm);
+        // add_mod_to_section() expects sectionnum, not id, crazy!
+        $cm->section = isset($options['section']) ? $options['section'] : 0;
+        $sectionid = add_mod_to_section($cm);
+        // Store the real section id.
+        $DB->set_field('course_modules', 'section', $sectionid, array('id'=>$cm->id));
 
         return $cm->id;
     }
index 3787b34..d2ea25a 100644 (file)
@@ -88,6 +88,13 @@ class core_phpunit_generator_testcase extends advanced_testcase {
 
         $page = $generator->create_module('page', array('course'=>$SITE->id));
         $this->assertNotEmpty($page);
+        $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
+        $this->assertEquals(0, $cm->sectionnum);
+
+        $page = $generator->create_module('page', array('course'=>$SITE->id), array('section'=>3));
+        $this->assertNotEmpty($page);
+        $cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
+        $this->assertEquals(3, $cm->sectionnum);
     }
 
     public function test_create_block() {