$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);
$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]));
+ }
}
$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);
}
}
/**
* 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
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']);
}
/**