/** * 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' => 'leap'), '*', MUST_EXIST); $instance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'leap'), '*', MUST_EXIST); // Set the required capabilities by the external function. $roleid = $this->assignUserCapability('enrol/leap:enrol', $context1->id); $this->assignUserCapability('moodle/course:view', $context1->id, $roleid); $this->assignUserCapability('moodle/role:assign', $context1->id, $roleid); $this->assignUserCapability('enrol/leap: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_leap_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/leap:enrol', $context1->id, $roleid); try { enrol_leap_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/leap:enrol', $context1->id, $roleid); $this->assertEquals(0, $DB->count_records('user_enrolments')); // Call with forbidden role. try { enrol_leap_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 leap instance. $DB->delete_records('user_enrolments'); $DB->delete_records('enrol', array('courseid' => $course2->id)); try { enrol_leap_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 leap instance'); } catch (moodle_exception $e) { $this->assertSame('wsnoinstance', $e->errorcode); } }
/** * 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_leap_external::enrol_users_returns() */ public static function leap_enrol_users_returns() { return enrol_leap_external::enrol_users_returns(); }