. /** * 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($sections, 4, 2); $neworder = array_keys($neworder); $this->assertEquals($oldsections[0], $neworder[0]); $this->assertEquals($oldsections[1], $neworder[1]); $this->assertEquals($oldsections[2], $neworder[3]); $this->assertEquals($oldsections[3], $neworder[4]); $this->assertEquals($oldsections[4], $neworder[2]); $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_down() { 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); // Test move section down.. 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_move_section_up() { 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); // Test move section up.. move_section_to($course, 6, 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[2]); $this->assertEquals($oldsections[3], $sections[3]); $this->assertEquals($oldsections[4], $sections[5]); $this->assertEquals($oldsections[5], $sections[6]); $this->assertEquals($oldsections[6], $sections[4]); } public function test_move_section_marker() { 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)); // Set course marker to the section we are going to move.. course_set_marker($course->id, 2); // Verify that the course marker is set correctly. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(2, $course->marker); // Test move the marked section down.. move_section_to($course, 2, 4); // Verify that the coruse marker has been moved along with the section.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(4, $course->marker); // Test move the marked section up.. move_section_to($course, 4, 3); // Verify that the course marker has been moved along with the section.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(3, $course->marker); // Test moving a non-marked section above the marked section.. move_section_to($course, 4, 2); // Verify that the course marker has been moved down to accomodate.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(4, $course->marker); // Test moving a non-marked section below the marked section.. move_section_to($course, 3, 6); // Verify that the course marker has been up to accomodate.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(3, $course->marker); } 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)); } public function test_create_course_category() { global $CFG, $DB; $this->resetAfterTest(true); // Create the category $data = new stdClass(); $data->name = 'aaa'; $data->description = 'aaa'; $data->idnumber = ''; $category1 = create_course_category($data); // Initially confirm that base data was inserted correctly $this->assertEquals($data->name, $category1->name); $this->assertEquals($data->description, $category1->description); $this->assertEquals($data->idnumber, $category1->idnumber); // sortorder should be blank initially $this->assertEmpty($category1->sortorder); // Calling fix_course_sortorder() should provide a new sortorder fix_course_sortorder(); $category1 = $DB->get_record('course_categories', array('id' => $category1->id)); $this->assertGreaterThanOrEqual(1, $category1->sortorder); // Create two more categories and test the sortorder worked correctly $data->name = 'ccc'; $category2 = create_course_category($data); $this->assertEmpty($category2->sortorder); $data->name = 'bbb'; $category3 = create_course_category($data); $this->assertEmpty($category3->sortorder); // Calling fix_course_sortorder() should provide a new sortorder to give category1, // category2, category3. New course categories are ordered by id not name fix_course_sortorder(); $category1 = $DB->get_record('course_categories', array('id' => $category1->id)); $category2 = $DB->get_record('course_categories', array('id' => $category2->id)); $category3 = $DB->get_record('course_categories', array('id' => $category3->id)); $this->assertGreaterThanOrEqual($category1->sortorder, $category2->sortorder); $this->assertGreaterThanOrEqual($category2->sortorder, $category3->sortorder); $this->assertGreaterThanOrEqual($category1->sortorder, $category3->sortorder); } }