diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php
new file mode 100644
index 00000000000..cf0822be267
--- /dev/null
+++ b/course/tests/courselib_test.php
@@ -0,0 +1,99 @@
+.
+
+/**
+ * Course related unit tests
+ *
+ * @package core
+ * @category phpunit
+ * @copyright 2012 Petr Skoda {@link http://skodak.org}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+
+class courselib_testcase extends advanced_testcase {
+
+ public function test_reorder_sections() {
+ global $DB;
+ $this->resetAfterTest(true);
+
+ $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
+ $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
+ $oldsections = array();
+ $sections = array();
+ foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
+ $oldsections[$section->section] = $section->id;
+ $sections[$section->id] = $section->section;
+ }
+ ksort($oldsections);
+
+ $neworder = reorder_sections($sections, 2, 4);
+ $neworder = array_keys($neworder);
+ $this->assertEquals($oldsections[0], $neworder[0]);
+ $this->assertEquals($oldsections[1], $neworder[1]);
+ $this->assertEquals($oldsections[2], $neworder[4]);
+ $this->assertEquals($oldsections[3], $neworder[2]);
+ $this->assertEquals($oldsections[4], $neworder[3]);
+ $this->assertEquals($oldsections[5], $neworder[5]);
+ $this->assertEquals($oldsections[6], $neworder[6]);
+
+ $neworder = reorder_sections(1, 2, 4);
+ $this->assertFalse($neworder);
+ }
+
+ public function test_move_section() {
+ global $DB;
+ $this->resetAfterTest(true);
+
+ $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
+ $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
+ $oldsections = array();
+ foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
+ $oldsections[$section->section] = $section->id;
+ }
+ ksort($oldsections);
+
+ move_section_to($course, 2, 4);
+ $sections = array();
+ foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
+ $sections[$section->section] = $section->id;
+ }
+ ksort($sections);
+
+ $this->assertEquals($oldsections[0], $sections[0]);
+ $this->assertEquals($oldsections[1], $sections[1]);
+ $this->assertEquals($oldsections[2], $sections[4]);
+ $this->assertEquals($oldsections[3], $sections[2]);
+ $this->assertEquals($oldsections[4], $sections[3]);
+ $this->assertEquals($oldsections[5], $sections[5]);
+ $this->assertEquals($oldsections[6], $sections[6]);
+ }
+
+ public function test_get_course_display_name_for_list() {
+ global $CFG;
+ $this->resetAfterTest(true);
+
+ $course = $this->getDataGenerator()->create_course(array('shortname' => 'FROG101', 'fullname' => 'Introduction to pond life'));
+
+ $CFG->courselistshortnames = 0;
+ $this->assertEquals('Introduction to pond life', get_course_display_name_for_list($course));
+
+ $CFG->courselistshortnames = 1;
+ $this->assertEquals('FROG101 Introduction to pond life', get_course_display_name_for_list($course));
+ }
+}
diff --git a/lib/phpunit/generatorlib.php b/lib/phpunit/generatorlib.php
index 45ca5598f4d..ce369a8ee1b 100644
--- a/lib/phpunit/generatorlib.php
+++ b/lib/phpunit/generatorlib.php
@@ -195,7 +195,8 @@ class phpunit_data_generator {
/**
* Create a test course
* @param array|stdClass $record
- * @param array $options
+ * @param array $options with keys:
+ * 'createsections'=>bool precreate all sections
* @return stdClass course record
*/
function create_course($record=null, array $options=null) {
@@ -246,9 +247,62 @@ class phpunit_data_generator {
$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));
+ }
+ }
+
return $course;
}
+ /**
+ * Create course section if does not exist yet
+ * @param mixed $record
+ * @param array|null $options
+ * @return stdClass
+ * @throws coding_exception
+ */
+ public function create_course_section($record = null, array $options = null) {
+ global $DB;
+
+ $record = (array)$record;
+
+ if (empty($record['course'])) {
+ throw new coding_exception('course must be present in phpunit_util::create_course_section() $record');
+ }
+
+ if (!isset($record['section'])) {
+ 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));
+ }
+
/**
* Create a test block
* @param string $blockname
diff --git a/lib/tests/phpunit_test.php b/lib/tests/phpunit_test.php
index aec55877964..3a0a5855725 100644
--- a/lib/tests/phpunit_test.php
+++ b/lib/tests/phpunit_test.php
@@ -23,7 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-
defined('MOODLE_INTERNAL') || die();
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index bd6a7a4b26d..36011c9ede8 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -35,6 +35,9 @@
blog/tests
+
+ course/tests
+