示例#1
0
 /**
  * Test get_enrolled_users
  */
 public function test_enrol_users()
 {
     global $USER, $CFG;
     $this->resetAfterTest(true);
     $course = self::getDataGenerator()->create_course();
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     // Set the required capabilities by the external function.
     $context = context_course::instance($course->id);
     $roleid = $this->assignUserCapability('enrol/manual:enrol', $context->id);
     $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
     // Add manager role to $USER.
     // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
     role_assign(1, $USER->id, context_system::instance()->id);
     // Call the external function.
     enrol_manual_external::enrol_users(array(array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course->id), array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course->id)));
     // Check we retrieve the good total number of enrolled users.
     require_once $CFG->dirroot . '/enrol/externallib.php';
     $enrolledusers = core_enrol_external::get_enrolled_users($course->id);
     $this->assertEquals(2, count($enrolledusers));
     // Call without required capability.
     $this->unassignUserCapability('enrol/manual:enrol', $context->id, $roleid);
     $this->setExpectedException('moodle_exception');
     $categories = enrol_manual_external::enrol_users($course->id);
 }
示例#2
0
 /**
  * Returns description of method result value.
  *
  * @return nul
  * @since Moodle 2.0
  * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
  * @see enrol_manual_external::enrol_users_returns()
  */
 public static function manual_enrol_users_returns()
 {
     return enrol_manual_external::enrol_users_returns();
 }
 /**
  * Test get_enrolled_users
  */
 public function test_enrol_users()
 {
     global $DB;
     $this->resetAfterTest(true);
     $user = self::getDataGenerator()->create_user();
     $this->setUser($user);
     $course1 = self::getDataGenerator()->create_course();
     $course2 = self::getDataGenerator()->create_course();
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     $context1 = context_course::instance($course1->id);
     $context2 = context_course::instance($course2->id);
     $instance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     $instance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     // Set the required capabilities by the external function.
     $roleid = $this->assignUserCapability('enrol/manual:enrol', $context1->id);
     $this->assignUserCapability('moodle/course:view', $context1->id, $roleid);
     $this->assignUserCapability('moodle/role:assign', $context1->id, $roleid);
     $this->assignUserCapability('enrol/manual:enrol', $context2->id, $roleid);
     $this->assignUserCapability('moodle/course:view', $context2->id, $roleid);
     $this->assignUserCapability('moodle/role:assign', $context2->id, $roleid);
     allow_assign($roleid, 3);
     // Call the external function.
     enrol_manual_external::enrol_users(array(array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id), array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course1->id)));
     $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
     $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
     $this->assertTrue(is_enrolled($context1, $user1));
     $this->assertTrue(is_enrolled($context1, $user2));
     // Call without required capability.
     $DB->delete_records('user_enrolments');
     $this->unassignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
     try {
         enrol_manual_external::enrol_users(array(array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id)));
         $this->fail('Exception expected if not having capability to enrol');
     } catch (moodle_exception $e) {
         $this->assertInstanceOf('required_capability_exception', $e);
         $this->assertSame('nopermissions', $e->errorcode);
     }
     $this->assignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
     $this->assertEquals(0, $DB->count_records('user_enrolments'));
     // Call with forbidden role.
     try {
         enrol_manual_external::enrol_users(array(array('roleid' => 1, 'userid' => $user1->id, 'courseid' => $course1->id)));
         $this->fail('Exception expected if not allowed to assign role.');
     } catch (moodle_exception $e) {
         $this->assertSame('wsusercannotassign', $e->errorcode);
     }
     $this->assertEquals(0, $DB->count_records('user_enrolments'));
     // Call for course without manual instance.
     $DB->delete_records('user_enrolments');
     $DB->delete_records('enrol', array('courseid' => $course2->id));
     try {
         enrol_manual_external::enrol_users(array(array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id), array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course2->id)));
         $this->fail('Exception expected if course does not have manual instance');
     } catch (moodle_exception $e) {
         $this->assertSame('wsnoinstance', $e->errorcode);
     }
 }
示例#4
0
 /**
  * Test for unenrol if user does not exist.
  * @throws coding_exception
  */
 public function test_unenrol_user_error_not_exist()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/enrollib.php';
     $this->resetAfterTest(true);
     // The user who perform the action.
     $user = $this->getDataGenerator()->create_user();
     $this->setUser($user);
     // Log this user in.
     $enrol = enrol_get_plugin('manual');
     // Create a course.
     $course = self::getDataGenerator()->create_course();
     $coursecontext = context_course::instance($course->id);
     $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
     // Set the capability for the user.
     $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext);
     $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
     $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid);
     $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid);
     // Create a student and enrol them into the course.
     $student = $this->getDataGenerator()->create_user();
     $enrol->enrol_user($enrolinstance, $student->id);
     $this->assertTrue(is_enrolled($coursecontext, $student));
     try {
         enrol_manual_external::unenrol_users(array(array('userid' => $student->id + 1, 'courseid' => $course->id)));
         $this->fail('Exception expected: invalid student id');
     } catch (Exception $ex) {
         $this->assertTrue($ex instanceof invalid_parameter_exception);
     }
     $DB->delete_records('enrol', array('id' => $enrolinstance->id));
     try {
         enrol_manual_external::unenrol_users(array(array('userid' => $student->id + 1, 'courseid' => $course->id)));
         $this->fail('Exception expected: invalid student id');
     } catch (Exception $ex) {
         $this->assertTrue($ex instanceof moodle_exception);
     }
 }