Exemplo n.º 1
0
 /**
  * Test course move after course.
  */
 public function test_course_change_sortorder_after_course()
 {
     global $DB;
     $this->resetAfterTest(true);
     $generator = $this->getDataGenerator();
     $category = $generator->create_category();
     $course3 = $generator->create_course(array('category' => $category->id));
     $course2 = $generator->create_course(array('category' => $category->id));
     $course1 = $generator->create_course(array('category' => $category->id));
     $context = $category->get_context();
     list($user, $roleid) = $this->get_user_objects($generator, $context->id);
     $caps = course_capability_assignment::allow('moodle/category:manage', $roleid, $context->id);
     $courses = $category->get_courses();
     $this->assertInternalType('array', $courses);
     $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
     $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
     $this->assertEquals(array_keys($dbcourses), array_keys($courses));
     // Test moving down.
     $this->assertTrue(course_change_sortorder_after_course($course1->id, $course3->id));
     $courses = $category->get_courses();
     $this->assertInternalType('array', $courses);
     $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
     $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
     $this->assertEquals(array_keys($dbcourses), array_keys($courses));
     // Test moving up.
     $this->assertTrue(course_change_sortorder_after_course($course1->id, $course2->id));
     $courses = $category->get_courses();
     $this->assertInternalType('array', $courses);
     $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
     $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
     $this->assertEquals(array_keys($dbcourses), array_keys($courses));
     // Test moving to the top.
     $this->assertTrue(course_change_sortorder_after_course($course1->id, 0));
     $courses = $category->get_courses();
     $this->assertInternalType('array', $courses);
     $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
     $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id');
     $this->assertEquals(array_keys($dbcourses), array_keys($courses));
 }
Exemplo n.º 2
0
 public function test_move_courses_into_category()
 {
     global $DB, $CFG;
     $this->resetAfterTest(true);
     $generator = $this->getDataGenerator();
     $cat1 = $generator->create_category();
     $cat2 = $generator->create_category();
     $sub1 = $generator->create_category(array('parent' => $cat1->id));
     $sub2 = $generator->create_category(array('parent' => $cat1->id));
     $course1 = $generator->create_course(array('category' => $cat1->id));
     $course2 = $generator->create_course(array('category' => $sub1->id));
     $course3 = $generator->create_course(array('category' => $sub1->id));
     $course4 = $generator->create_course(array('category' => $cat2->id));
     $syscontext = context_system::instance();
     list($user, $roleid) = $this->get_user_objects($generator, $syscontext->id);
     course_capability_assignment::allow(array(self::CATEGORY_MANAGE, self::CATEGORY_VIEWHIDDEN), $roleid, $syscontext->id);
     // Check they are where we think they are.
     $this->assertEquals(1, $cat1->get_courses_count());
     $this->assertEquals(1, $cat2->get_courses_count());
     $this->assertEquals(2, $sub1->get_courses_count());
     $this->assertEquals(0, $sub2->get_courses_count());
     // Move the courses in sub category 1 to sub category 2.
     $this->assertTrue(\core_course\management\helper::move_courses_into_category($sub2->id, array($course2->id, $course3->id)));
     $this->assertEquals(1, $cat1->get_courses_count());
     $this->assertEquals(1, $cat2->get_courses_count());
     $this->assertEquals(0, $sub1->get_courses_count());
     $this->assertEquals(2, $sub2->get_courses_count());
     $courses = $DB->get_records('course', array('category' => $sub2->id), 'id');
     $this->assertEquals(array((int) $course2->id, (int) $course3->id), array_keys($courses));
     // Move the courses in sub category 2 back into to sub category 1.
     $this->assertTrue(\core_course\management\helper::move_courses_into_category($sub1->id, array($course2->id, $course3->id)));
     $this->assertEquals(1, $cat1->get_courses_count());
     $this->assertEquals(1, $cat2->get_courses_count());
     $this->assertEquals(2, $sub1->get_courses_count());
     $this->assertEquals(0, $sub2->get_courses_count());
     $courses = $DB->get_records('course', array('category' => $sub1->id), 'id');
     $this->assertEquals(array((int) $course2->id, (int) $course3->id), array_keys($courses));
     // Try moving just one course.
     $this->assertTrue(\core_course\management\helper::move_courses_into_category($sub2->id, $course4->id));
     $this->assertEquals(1, $cat1->get_courses_count());
     $this->assertEquals(0, $cat2->get_courses_count());
     $this->assertEquals(2, $sub1->get_courses_count());
     $this->assertEquals(1, $sub2->get_courses_count());
     $courses = $DB->get_records('course', array('category' => $sub2->id), 'id');
     $this->assertEquals(array((int) $course4->id), array_keys($courses));
     // Current state:
     // * $cat1 => $course1
     //    * $sub1 => $course2, $course3
     //    * $sub2 => $course4
     // * $cat2 =>.
     // Prevent the user from being able to move into $sub2.
     $sub2cap = course_capability_assignment::prohibit(self::CATEGORY_MANAGE, $roleid, $sub2->get_context()->id);
     $sub2 = coursecat::get($sub2->id);
     // Suppress debugging messages for a moment.
     $olddebug = $CFG->debug;
     $CFG->debug = 0;
     // Try to move a course into sub2. This shouldn't be possible because you should always be able to undo what you've done.
     // Try moving just one course.
     try {
         \core_course\management\helper::move_courses_into_category($sub2->id, array($course2->id));
         $this->fail('Invalid move of course between categories, action can\'t be undone.');
     } catch (moodle_exception $ex) {
         $this->assertEquals(get_string('cannotmovecourses', 'error'), $ex->getMessage());
     }
     // Nothing should have changed.
     $this->assertEquals(1, $cat1->get_courses_count());
     $this->assertEquals(0, $cat2->get_courses_count());
     $this->assertEquals(2, $sub1->get_courses_count());
     $this->assertEquals(1, $sub2->get_courses_count());
     // Now try moving a course out of sub2. Again should not be possible.
     // Try to move a course into sub2. This shouldn't be possible because you should always be able to undo what you've done.
     // Try moving just one course.
     try {
         \core_course\management\helper::move_courses_into_category($cat2->id, array($course4->id));
         $this->fail('Invalid move of course between categories, action can\'t be undone.');
     } catch (moodle_exception $ex) {
         $this->assertEquals(get_string('cannotmovecourses', 'error'), $ex->getMessage());
     }
     // Nothing should have changed.
     $this->assertEquals(1, $cat1->get_courses_count());
     $this->assertEquals(0, $cat2->get_courses_count());
     $this->assertEquals(2, $sub1->get_courses_count());
     $this->assertEquals(1, $sub2->get_courses_count());
     $CFG->debug = $olddebug;
 }