mirror of
https://github.com/moodle/moodle.git
synced 2025-01-29 19:50:14 +01:00
MDL-37410 External PHPunit test: used ->getDataGenerator()->enrol_user instead to manually enrol the user. + improve the unassignUserCapability to remove a capability assigned by getDataGenerator->enrol_users
This commit is contained in:
parent
6319737865
commit
909f27acf4
@ -50,18 +50,9 @@ class core_enrol_external_testcase extends externallib_advanced_testcase {
|
||||
$this->assignUserCapability('moodle/user:viewdetails', $context->id, $roleid);
|
||||
|
||||
// Enrol the users in the course.
|
||||
// We use the manual plugin.
|
||||
$enrol = enrol_get_plugin('manual');
|
||||
$enrolinstances = enrol_get_instances($course->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
$instance = $courseenrolinstance;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$enrol->enrol_user($instance, $user1->id, $roleid);
|
||||
$enrol->enrol_user($instance, $user2->id, $roleid);
|
||||
$enrol->enrol_user($instance, $USER->id, $roleid);
|
||||
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
|
||||
|
||||
// Call the external function.
|
||||
$enrolledusers = core_enrol_external::get_enrolled_users($course->id);
|
||||
@ -92,21 +83,13 @@ class core_enrol_external_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Enrol $USER in the courses.
|
||||
// We use the manual plugin.
|
||||
$enrol = enrol_get_plugin('manual');
|
||||
$roleid = null;
|
||||
foreach ($courses as $course) {
|
||||
$context = context_course::instance($course->id);
|
||||
$roleid = $this->assignUserCapability('moodle/course:viewparticipants',
|
||||
$context->id, $roleid);
|
||||
|
||||
$enrolinstances = enrol_get_instances($course->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
$instance = $courseenrolinstance;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$enrol->enrol_user($instance, $USER->id, $roleid);
|
||||
$this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
|
||||
}
|
||||
|
||||
// Call the external function.
|
||||
|
@ -76,17 +76,10 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
|
||||
array('forum1' => $forum1->id, 'forum2' => $forum2->id)));
|
||||
|
||||
// Enrol the user in two courses.
|
||||
// Enrol them in the first course.
|
||||
// DataGenerator->enrol_user automatically sets a role for the user with the permission mod/form:viewdiscussion.
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course1->id, null, 'manual');
|
||||
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
|
||||
$enrol = enrol_get_plugin('manual');
|
||||
$enrolinstances = enrol_get_instances($course1->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
$instance1 = $courseenrolinstance;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$enrol->enrol_user($instance1, $user->id);
|
||||
// Now enrol into the second course.
|
||||
$enrolinstances = enrol_get_instances($course2->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
@ -96,11 +89,6 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
$enrol->enrol_user($instance2, $user->id);
|
||||
|
||||
// Assign capabilities to view forums for forum 1.
|
||||
$cm1 = get_coursemodule_from_id('forum', $forum1->id, 0, false, MUST_EXIST);
|
||||
$context1 = context_module::instance($cm1->id);
|
||||
$roleid1 = $this->assignUserCapability('mod/forum:viewdiscussion', $context1->id);
|
||||
|
||||
// Assign capabilities to view forums for forum 2.
|
||||
$cm2 = get_coursemodule_from_id('forum', $forum2->id, 0, false, MUST_EXIST);
|
||||
$context2 = context_module::instance($cm2->id);
|
||||
@ -140,9 +128,9 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
|
||||
// Call without required capability, ensure exception thrown.
|
||||
$this->unassignUserCapability('mod/forum:viewdiscussion', $context1->id, $roleid1);
|
||||
$this->unassignUserCapability('mod/forum:viewdiscussion', null, null, $course1->id);
|
||||
try {
|
||||
mod_forum_external::get_forums_by_courses(array($course1->id));
|
||||
$forums = mod_forum_external::get_forums_by_courses(array($course1->id));
|
||||
$this->fail('Exception expected due to missing capability.');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertEquals('nopermissions', $e->errorcode);
|
||||
@ -252,16 +240,12 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Enrol the user in the first course.
|
||||
$enrol = enrol_get_plugin('manual');
|
||||
$enrolinstances = enrol_get_instances($course1->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
$instance1 = $courseenrolinstance;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$enrol->enrol_user($instance1, $user1->id);
|
||||
// Following line enrol and assign default role id to the user.
|
||||
// So the user automatically gets mod/forum:viewdiscussion on all forums of the course.
|
||||
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
|
||||
|
||||
// Now enrol into the second course.
|
||||
// We don't use the dataGenerator as we need to get the $instance2 to unenrol later.
|
||||
$enrolinstances = enrol_get_instances($course2->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
@ -271,12 +255,6 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
$enrol->enrol_user($instance2, $user1->id);
|
||||
|
||||
// Assign capabilities to view discussions for forum 1.
|
||||
// Need to keep track of this context and role as we use it later in testing.
|
||||
$cm = get_coursemodule_from_id('forum', $forum1->id, 0, false, MUST_EXIST);
|
||||
$context1 = context_module::instance($cm->id);
|
||||
$roleid1 = $this->assignUserCapability('mod/forum:viewdiscussion', $context1->id);
|
||||
|
||||
// Assign capabilities to view discussions for forum 2.
|
||||
$cm = get_coursemodule_from_id('forum', $forum2->id, 0, false, MUST_EXIST);
|
||||
$context = context_module::instance($cm->id);
|
||||
@ -387,7 +365,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
|
||||
// Call without required view discussion capability.
|
||||
$this->unassignUserCapability('mod/forum:viewdiscussion', $context1->id, $roleid1);
|
||||
$this->unassignUserCapability('mod/forum:viewdiscussion', null, null, $course1->id);
|
||||
try {
|
||||
mod_forum_external::get_forum_discussions(array($forum1->id));
|
||||
$this->fail('Exception expected due to missing capability.');
|
||||
|
@ -82,18 +82,9 @@ class core_user_external_testcase extends externallib_advanced_testcase {
|
||||
$roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
|
||||
|
||||
// Enrol the users in the course.
|
||||
// We use the manual plugin.
|
||||
$enrol = enrol_get_plugin('manual');
|
||||
$enrolinstances = enrol_get_instances($course->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
$instance = $courseenrolinstance;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$enrol->enrol_user($instance, $user1->id, $roleid);
|
||||
$enrol->enrol_user($instance, $user2->id, $roleid);
|
||||
$enrol->enrol_user($instance, $USER->id, $roleid);
|
||||
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
|
||||
|
||||
// call as admin and receive all possible fields.
|
||||
$this->setAdminUser();
|
||||
@ -235,18 +226,9 @@ class core_user_external_testcase extends externallib_advanced_testcase {
|
||||
$roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
|
||||
|
||||
// Enrol the users in the course.
|
||||
// We use the manual plugin.
|
||||
$enrol = enrol_get_plugin('manual');
|
||||
$enrolinstances = enrol_get_instances($course->id, true);
|
||||
foreach ($enrolinstances as $courseenrolinstance) {
|
||||
if ($courseenrolinstance->enrol == "manual") {
|
||||
$instance = $courseenrolinstance;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$enrol->enrol_user($instance, $user1->id, $roleid);
|
||||
$enrol->enrol_user($instance, $user2->id, $roleid);
|
||||
$enrol->enrol_user($instance, $USER->id, $roleid);
|
||||
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
|
||||
$this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
|
||||
|
||||
// Call the external function.
|
||||
$enrolledusers = core_user_external::get_course_user_profiles(array(
|
||||
|
@ -65,14 +65,33 @@ abstract class externallib_advanced_testcase extends advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Unassign a capability to $USER
|
||||
* Unassign a capability to $USER.
|
||||
*
|
||||
* @param string $capability capability name
|
||||
* @param int $contextid
|
||||
* @param int $roleid
|
||||
* @param string $capability capability name.
|
||||
* @param int $contextid set the context id if you used assignUserCapability.
|
||||
* @param int $roleid set the role id if you used assignUserCapability.
|
||||
* @param int $courseid set the course id if you used getDataGenerator->enrol_users.
|
||||
* @param string $enrol set the enrol plugin name if you used getDataGenerator->enrol_users with a different plugin than 'manual'.
|
||||
*/
|
||||
public static function unassignUserCapability($capability, $contextid, $roleid) {
|
||||
global $USER;
|
||||
public static function unassignUserCapability($capability, $contextid = null, $roleid = null, $courseid = null, $enrol = 'manual') {
|
||||
global $DB;
|
||||
|
||||
if (!empty($courseid)) {
|
||||
// Retrieve the role id.
|
||||
$instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
|
||||
if (count($instances) != 1) {
|
||||
throw new coding_exception('No found enrol instance for courseid: ' . $courseid . ' and enrol: ' . $enrol);
|
||||
}
|
||||
$instance = reset($instances);
|
||||
|
||||
if (is_null($roleid) and $instance->roleid) {
|
||||
$roleid = $instance->roleid;
|
||||
}
|
||||
} else {
|
||||
if (empty($contextid) or empty($roleid)) {
|
||||
throw new coding_exception('unassignUserCapaibility requires contextid/roleid or courseid');
|
||||
}
|
||||
}
|
||||
|
||||
unassign_capability($capability, $roleid, $contextid);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user