MDL-35339 Course functions unit tests
authorMarina Glancy <marina@moodle.com>
Tue, 9 Oct 2012 07:54:22 +0000 (15:54 +0800)
committerMarina Glancy <marina@moodle.com>
Mon, 15 Oct 2012 06:08:14 +0000 (14:08 +0800)
backup/util/checks/tests/checks_test.php
course/tests/courselib_test.php
lib/phpunit/classes/data_generator.php

index ccee3ba..e9905c4 100644 (file)
@@ -45,7 +45,7 @@ class backup_check_testcase extends advanced_testcase {
 
         $this->resetAfterTest(true);
 
-        $course = $this->getDataGenerator()->create_course();
+        $course = $this->getDataGenerator()->create_course(array(), array('createsections' => true));
         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
         $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
 
index 87fa03d..5957966 100644 (file)
@@ -62,6 +62,41 @@ class courselib_testcase extends advanced_testcase {
         $this->assertGreaterThan(0, $blockcount);
     }
 
+    public function test_create_course_with_generator() {
+        global $DB;
+        $this->resetAfterTest(true);
+        $course = $this->getDataGenerator()->create_course();
+
+        // Ensure default section is created.
+        $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
+        $this->assertTrue($sectioncreated);
+    }
+
+    public function test_create_course_sections() {
+        global $DB;
+        $this->resetAfterTest(true);
+
+        $course = $this->getDataGenerator()->create_course(
+                array('shortname' => 'GrowingCourse',
+                    'fullname' => 'Growing Course',
+                    'numsections' => 5),
+                array('createsections' => true));
+
+        // Ensure all 6 (0-5) sections were created and modinfo/sectioninfo cache works properly
+        $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
+        $this->assertEquals(range(0, $course->numsections), $sectionscreated);
+
+        // this will do nothing, section already exists
+        $this->assertFalse(course_create_sections_if_missing($course, $course->numsections));
+
+        // this will create new section
+        $this->assertTrue(course_create_sections_if_missing($course, $course->numsections + 1));
+
+        // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
+        $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
+        $this->assertEquals(range(0, $course->numsections + 1), $sectionscreated);
+    }
+
     public function test_reorder_sections() {
         global $DB;
         $this->resetAfterTest(true);
@@ -260,4 +295,31 @@ class courselib_testcase extends advanced_testcase {
         $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder);
         $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder);
     }
+
+    public function test_move_module_in_course() {
+        $this->resetAfterTest(true);
+        // Setup fixture
+        $course = $this->getDataGenerator()->create_course(array('numsections'=>5));
+        $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id));
+
+        // reset cache inside $course because $course is not passed to create_module
+        $course->modinfo = null;
+        $course->sectioncache = null;
+
+        $cms = get_fast_modinfo($course)->get_cms();
+        $cm = reset($cms);
+
+        course_create_sections_if_missing($course, 3);
+        $section3 = get_fast_modinfo($course)->get_section_info(3);
+
+        moveto_module($cm, $section3);
+
+        // reset cache inside $course because $course is not passed to moveto_module
+        $course->modinfo = null;
+        $course->sectioncache = null;
+
+        $modinfo = get_fast_modinfo($course);
+        $this->assertTrue(empty($modinfo->sections[0]));
+        $this->assertFalse(empty($modinfo->sections[3]));
+    }
 }
index 515f062..98fecbe 100644 (file)
@@ -369,10 +369,11 @@ EOD;
 
         $course = create_course((object)$record);
         context_course::instance($course->id);
-
         if (!empty($options['createsections'])) {
-            for($i=1; $i<$record['numsections']; $i++) {
-                self::create_course_section(array('course'=>$course->id, 'section'=>$i));
+            if (isset($course->numsections)) {
+                course_create_sections_if_missing($course, range(0, $course->numsections));
+            } else {
+                course_create_sections_if_missing($course, 0);
             }
         }
 
@@ -381,7 +382,7 @@ EOD;
 
     /**
      * Create course section if does not exist yet
-     * @param mixed $record
+     * @param array|stdClass $record must contain 'course' and 'section' attributes
      * @param array|null $options
      * @return stdClass
      * @throws coding_exception
@@ -399,31 +400,9 @@ EOD;
             throw new coding_exception('section must be present in phpunit_util::create_course_section() $record');
         }
 
-        if (!isset($record['name'])) {
-            $record['name'] = '';
-        }
-
-        if (!isset($record['summary'])) {
-            $record['summary'] = '';
-        }
-
-        if (!isset($record['summaryformat'])) {
-            $record['summaryformat'] = FORMAT_MOODLE;
-        }
-
-        if ($section = $DB->get_record('course_sections', array('course'=>$record['course'], 'section'=>$record['section']))) {
-            return $section;
-        }
-
-        $section = new stdClass();
-        $section->course        = $record['course'];
-        $section->section       = $record['section'];
-        $section->name          = $record['name'];
-        $section->summary       = $record['summary'];
-        $section->summaryformat = $record['summaryformat'];
-        $id = $DB->insert_record('course_sections', $section);
-
-        return $DB->get_record('course_sections', array('id'=>$id));
+        $course = $DB->get_record('course', array('id' => $record['course']), '*', MUST_EXIST);
+        course_create_sections_if_missing($course, $record['section']);
+        return get_fast_modinfo($course)->get_section_info($record['section']);
     }
 
     /**