Merge branch 'wip-MDL-31750-master' of git://github.com/abgreeve/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 9 Jan 2013 00:31:50 +0000 (01:31 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 9 Jan 2013 00:31:50 +0000 (01:31 +0100)
Conflicts:
course/tests/courselib_test.php

1  2 
course/category.php
course/edit.php
course/edit_form.php
course/lib.php
course/tests/courselib_test.php
lang/en/error.php

Simple merge
diff --cc course/edit.php
Simple merge
Simple merge
diff --cc course/lib.php
Simple merge
@@@ -315,124 -315,100 +315,220 @@@ class courselib_testcase extends advanc
          $this->assertFalse(empty($modinfo->sections[3]));
      }
  
 +    public function test_module_visibility() {
 +        $this->setAdminUser();
 +        $this->resetAfterTest(true);
 +
 +        // Create course and modules.
 +        $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
 +        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 +        $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id));
 +        $modules = compact('forum', 'assign');
 +
 +        // Hiding the modules.
 +        foreach ($modules as $mod) {
 +            set_coursemodule_visible($mod->cmid, 0);
 +            $this->check_module_visibility($mod, 0, 0);
 +        }
 +
 +        // Showing the modules.
 +        foreach ($modules as $mod) {
 +            set_coursemodule_visible($mod->cmid, 1);
 +            $this->check_module_visibility($mod, 1, 1);
 +        }
 +    }
 +
 +    public function test_section_visibility() {
 +        $this->setAdminUser();
 +        $this->resetAfterTest(true);
 +
 +        // Create course.
 +        $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
 +
 +        // Testing an empty section.
 +        $sectionnumber = 1;
 +        set_section_visible($course->id, $sectionnumber, 0);
 +        $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
 +        $this->assertEquals($section_info->visible, 0);
 +        set_section_visible($course->id, $sectionnumber, 1);
 +        $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
 +        $this->assertEquals($section_info->visible, 1);
 +
 +        // Testing a section with visible modules.
 +        $sectionnumber = 2;
 +        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
 +                array('section' => $sectionnumber));
 +        $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
 +                'course' => $course->id), array('section' => $sectionnumber));
 +        $modules = compact('forum', 'assign');
 +        set_section_visible($course->id, $sectionnumber, 0);
 +        $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
 +        $this->assertEquals($section_info->visible, 0);
 +        foreach ($modules as $mod) {
 +            $this->check_module_visibility($mod, 0, 1);
 +        }
 +        set_section_visible($course->id, $sectionnumber, 1);
 +        $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
 +        $this->assertEquals($section_info->visible, 1);
 +        foreach ($modules as $mod) {
 +            $this->check_module_visibility($mod, 1, 1);
 +        }
 +
 +        // Testing a section with hidden modules, which should stay hidden.
 +        $sectionnumber = 3;
 +        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
 +                array('section' => $sectionnumber));
 +        $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(),
 +                'course' => $course->id), array('section' => $sectionnumber));
 +        $modules = compact('forum', 'assign');
 +        foreach ($modules as $mod) {
 +            set_coursemodule_visible($mod->cmid, 0);
 +            $this->check_module_visibility($mod, 0, 0);
 +        }
 +        set_section_visible($course->id, $sectionnumber, 0);
 +        $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
 +        $this->assertEquals($section_info->visible, 0);
 +        foreach ($modules as $mod) {
 +            $this->check_module_visibility($mod, 0, 0);
 +        }
 +        set_section_visible($course->id, $sectionnumber, 1);
 +        $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
 +        $this->assertEquals($section_info->visible, 1);
 +        foreach ($modules as $mod) {
 +            $this->check_module_visibility($mod, 0, 0);
 +        }
 +    }
 +
 +    /**
 +     * Helper function to assert that a module has correctly been made visible, or hidden.
 +     *
 +     * @param stdClass $mod module information
 +     * @param int $visibility the current state of the module
 +     * @param int $visibleold the current state of the visibleold property
 +     * @return void
 +     */
 +    public function check_module_visibility($mod, $visibility, $visibleold) {
 +        global $DB;
 +        $cm = get_fast_modinfo($mod->course)->get_cm($mod->cmid);
 +        $this->assertEquals($visibility, $cm->visible);
 +        $this->assertEquals($visibleold, $cm->visibleold);
 +
 +        // Check the module grade items.
 +        $grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $cm->modname,
 +                'iteminstance' => $cm->instance, 'courseid' => $cm->course));
 +        if ($grade_items) {
 +            foreach ($grade_items as $grade_item) {
 +                if ($visibility) {
 +                    $this->assertFalse($grade_item->is_hidden(), "$cm->modname grade_item not visible");
 +                } else {
 +                    $this->assertTrue($grade_item->is_hidden(), "$cm->modname grade_item not hidden");
 +                }
 +            }
 +        }
 +
 +        // Check the events visibility.
 +        if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $cm->modname))) {
 +            foreach ($events as $event) {
 +                $calevent = new calendar_event($event);
 +                $this->assertEquals($visibility, $calevent->visible, "$cm->modname calendar_event visibility");
 +            }
 +        }
 +    }
 +
+     /**
+      * These tests check for moving courses around categories with different permissions enabled and disabled.
+      * (@see course/lib.php can_move_courses_to_category())
+      */
+     function test_move_course_to_category_check() {
+         $this->resetAfterTest(true);
+         // Create a user.
+         $user = $this->getDataGenerator()->create_user();
+         // Log in as the user.
+         $this->setUser($user);
+         $catstructure[] = array(
+                     'fromcategory' => array('catcap' => 'moodle/category:manage', 'allow' => CAP_ALLOW),
+                     'tocategory' => array('catcap' => 'moodle/category:manage', 'allow' => CAP_ALLOW),
+                     'courses' => array(
+                         array('coursecap' => array('moodle/course:create')),
+                         array('coursecap' => array('moodle/course:delete')),
+                         array('coursecap' => array('moodle/course:delete', 'moodle/course:create'))
+                     ),
+                     'movecourse' => array(
+                         array('course' => 0, 'result' => false),
+                         array('course' => 1, 'result' => false),
+                         array('course' => 2, 'result' => true),
+                         array('course' => array(0,1), 'result' => false),
+                         array('course' => array(0,1,2), 'result' => false),
+                     )
+             );
+         // Create a system context to ascertain the correct role id.
+         $systemcontext = context_system::instance();
+         $roleid = 4; // role id of student?
+         // This is to ensure that we get the correct role id.
+         $roles = role_get_names($systemcontext);
+         foreach ($roles as $role) {
+             if ($role->shortname == 'student') {
+                 $roleid = $role->id;
+             }
+         }
+         // Create category.
+         foreach ($catstructure as $key => $value) {
+             // Create to catgory.
+             $cat = $this->getDataGenerator()->create_category(array('name' => 'FromTestCat '.$key));
+             $catstructure[$key]['tocategory']['catid'] = $cat->id;
+             $catstructure[$key]['tocategory']['catcontext'] = context_coursecat::instance($cat->id);
+             $catcap = $catstructure[$key]['tocategory']['catcap'];
+             $catallow = $catstructure[$key]['tocategory']['allow'];
+             assign_capability($catcap, $catallow, $roleid, $catstructure[$key]['tocategory']['catcontext'], true);
+             // Create from category.
+             $fromcat = $this->getDataGenerator()->create_category(array('name' => 'ToTestCat '.$key));
+             $catstructure[$key]['fromcategory']['catid'] = $fromcat->id;
+             $catstructure[$key]['fromcategory']['catcontext'] = context_coursecat::instance($fromcat->id);
+             $catcap = $catstructure[$key]['fromcategory']['catcap'];
+             $catallow = $catstructure[$key]['fromcategory']['allow'];
+             assign_capability($catcap, $catallow, $roleid, $catstructure[$key]['fromcategory']['catcontext'], true);
+             // Create course in from category.
+             foreach($value['courses'] as $coursekey => $coursedata) {
+                 $coursed = array('category' => $fromcat->id,
+                                     'fullname' => 'Coursefullname '.$fromcat->id.$coursekey,
+                                     'shortname' => 'Courseshortname'.$fromcat->id.$coursekey);
+                 $course = $this->getDataGenerator()->create_course($coursed);
+                 $catstructure[$key]['courses'][$coursekey]['id'] = $course->id;
+                 $coursecontext = context_course::instance($course->id);
+                 foreach ($coursedata['coursecap'] as $coursecap) {
+                     assign_capability($coursecap, CAP_ALLOW, $roleid, $coursecontext, true);
+                 }
+             }
+             // Assign course creator to user on category.
+             role_assign($roleid, $user->id, $catstructure[$key]['tocategory']['catcontext']);
+             role_assign($roleid, $user->id, $catstructure[$key]['fromcategory']['catcontext']);
+         }
+         // We loop through the catstructure array and test moving courses to different categories with different permissions.
+         foreach ($catstructure as $key => $data) {
+             foreach ($data['movecourse'] as $move) {
+                 if (is_array($move['course'])) {
+                     $courseids = array();
+                     foreach ($move['course'] as $courseindex) {
+                         $courseids[] = $data['courses'][$courseindex]['id'];
+                     }
+                 } else {
+                     $courseids = $data['courses'][$move['course']]['id'];
+                 }
+                 $canmove = can_move_courses_to_category($courseids, $data['tocategory']['catid']);
+                 $this->assertEquals($canmove, $move['result']);
+             }
+         }
+     }
  }
Simple merge