857d13e4ba83a754ba640d1242ee42c0adbb433f
[moodle.git] / course / simpletest / testcourselib.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // NOTICE OF COPYRIGHT                                                   //
6 //                                                                       //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
8 //          http://moodle.org                                            //
9 //                                                                       //
10 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
11 //                                                                       //
12 // This program is free software; you can redistribute it and/or modify  //
13 // it under the terms of the GNU General Public License as published by  //
14 // the Free Software Foundation; either version 2 of the License, or     //
15 // (at your option) any later version.                                   //
16 //                                                                       //
17 // This program is distributed in the hope that it will be useful,       //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
20 // GNU General Public License for more details:                          //
21 //                                                                       //
22 //          http://www.gnu.org/copyleft/gpl.html                         //
23 //                                                                       //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27  * Unit tests for Course lib.
28  *
29  * @author nicolasconnault@gmail.com
30  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31  * @package moodlecore
32  */
34 if (!defined('MOODLE_INTERNAL')) {
35     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
36 }
38 require_once($CFG->dirroot . '/course/lib.php');
40 class courselib_test extends UnitTestCase {
41     var $realDB;
42     public  static $includecoverage = array('course/lib.php');
44     function setUp() {
45         global $DB;
46         Mock::generate(get_class($DB), 'mockDB');
47         $this->realDB = $DB;
48         $DB = new mockDB();
49     }
51     function tearDown() {
52         global $DB;
53         $DB = $this->realDB;
54     }
56     function testMoveSection() {
57         global $DB;
58         $course = new stdClass();
59         $course->numsections = 10;
60         $course->id = 1;
61         $sections = array(20 => 0, 21 => 1, 22 => 2, 23 => 3, 24 => 4, 25 => 5);
63         $DB->setReturnValueAt(0, 'get_records_menu', $sections);
64         $DB->expectAt(0, 'set_field', array('course_sections', 'section', 0, array('id' => 20)));
65         $DB->expectAt(1, 'set_field', array('course_sections', 'section', 1, array('id' => 21)));
66         $DB->expectAt(2, 'set_field', array('course_sections', 'section', 2, array('id' => 23)));
67         $DB->expectAt(3, 'set_field', array('course_sections', 'section', 3, array('id' => 24)));
68         $DB->expectAt(4, 'set_field', array('course_sections', 'section', 4, array('id' => 22)));
69         $DB->expectAt(5, 'set_field', array('course_sections', 'section', 5, array('id' => 25)));
70         move_section_to($course, 2, 4);
72         $DB->setReturnValueAt(1, 'get_records_menu', $sections);
73         $DB->expectAt(6, 'set_field', array('course_sections', 'section', 0, array('id' => 20)));
74         $DB->expectAt(7, 'set_field', array('course_sections', 'section', 1, array('id' => 24)));
75         $DB->expectAt(8, 'set_field', array('course_sections', 'section', 2, array('id' => 21)));
76         $DB->expectAt(9, 'set_field', array('course_sections', 'section', 3, array('id' => 22)));
77         $DB->expectAt(10, 'set_field', array('course_sections', 'section', 4, array('id' => 23)));
78         $DB->expectAt(11, 'set_field', array('course_sections', 'section', 5, array('id' => 25)));
79         move_section_to($course, 4, 0);
80     }
82     function testReorderSections() {
83         $sections = array(20 => 0, 21 => 1, 22 => 2, 23 => 3, 24 => 4, 25 => 5);
84         $this->assertFalse(reorder_sections(1,3,4));
86         $newsections = reorder_sections($sections, 2, 4);
87         $newsections_flipped = array_flip($newsections);
89         $this->assertEqual(20, reset($newsections_flipped));
90         $this->assertEqual(21, next($newsections_flipped));
91         $this->assertEqual(23, next($newsections_flipped));
92         $this->assertEqual(24, next($newsections_flipped));
93         $this->assertEqual(22, next($newsections_flipped));
94         $this->assertEqual(25, next($newsections_flipped));
96         $newsections = reorder_sections($sections, 4, 0);
97         $newsections_flipped = array_flip($newsections);
99         $this->assertEqual(20, reset($newsections_flipped));
100         $this->assertEqual(24, next($newsections_flipped));
101         $this->assertEqual(21, next($newsections_flipped));
102         $this->assertEqual(22, next($newsections_flipped));
103         $this->assertEqual(23, next($newsections_flipped));
104         $this->assertEqual(25, next($newsections_flipped));
106         $newsections = reorder_sections($sections, 1, 5);
107         $newsections_flipped = array_flip($newsections);
109         $this->assertEqual(20, reset($newsections_flipped));
110         $this->assertEqual(22, next($newsections_flipped));
111         $this->assertEqual(23, next($newsections_flipped));
112         $this->assertEqual(24, next($newsections_flipped));
113         $this->assertEqual(25, next($newsections_flipped));
114         $this->assertEqual(21, next($newsections_flipped));
115     }
117     function test_get_course_display_name_for_list() {
118         global $CFG;
120         $course = (object)array('shortname' => 'FROG101',
121                 'fullname' => 'Introduction to pond life');
123         // Store config value in case other tests rely on it
124         $oldcfg = $CFG->courselistshortnames;
126         $CFG->courselistshortnames = 0;
127         $this->assertEqual('Introduction to pond life',
128                 get_course_display_name_for_list($course));
130         $CFG->courselistshortnames = 1;
131         $this->assertEqual('FROG101 Introduction to pond life',
132                 get_course_display_name_for_list($course));
134         $CFG->courselistshortnames = $oldcfg;
135     }