MDL-59811 enrol: Unit tests for get_user_enrolment_actions()

This commit is contained in:
Jun Pataleta 2017-08-23 15:18:22 +08:00
parent d8e9a23c48
commit 18000c6d60
8 changed files with 434 additions and 0 deletions

View File

@ -83,4 +83,56 @@ class enrol_cohort_lib_testcase extends advanced_testcase {
$this->assertEquals($cohort->name . ' cohort (3)', $groupinfo->name);
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'cohort';
// Only enable the cohort enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Teachers don't have enrol/cohort:unenrol capability by default. Login as admin for simplicity.
$this->setAdminUser();
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Cohort-sync has no enrol actions for active students.
$this->assertCount(0, $actions);
// Enrol actions for a suspended student.
// Suspend the student.
$ue->status = ENROL_USER_SUSPENDED;
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Cohort-sync has enrol actions for suspended students -- unenrol.
$this->assertCount(1, $actions);
}
}

View File

@ -0,0 +1,86 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Database enrolment tests.
*
* @package enrol_database
* @copyright 2017 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Database enrolment tests.
*
* @package enrol_database
* @copyright 2017 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_database_lib_testcase extends advanced_testcase {
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'database';
// Only enable the database enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Teachers don't have enrol/database:unenrol capability by default. Login as admin for simplicity.
$this->setAdminUser();
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Database enrol has 0 enrol actions for active users.
$this->assertCount(0, $actions);
// Enrol actions for a suspended student.
// Suspend the student.
$ue->status = ENROL_USER_SUSPENDED;
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Database enrol has enrol actions for suspended students -- unenrol.
$this->assertCount(1, $actions);
}
}

View File

@ -498,4 +498,49 @@ class enrol_flatfile_testcase extends advanced_testcase {
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid' => $studentrole->id)));
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'flatfile';
// Only enable the flatfile enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Teachers don't have enrol/flatfile:manage and enrol/flatfile:unenrol capabilities by default.
// Login as admin for simplicity.
$this->setAdminUser();
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Flatfile enrolment has 2 enrol actions for active users -- edit and unenrol.
$this->assertCount(2, $actions);
}
}

View File

@ -117,4 +117,53 @@ class enrol_lti_testcase extends advanced_testcase {
$this->assertFalse($DB->record_exists('enrol_lti_tools', [ 'id' => $tool->id ]));
$this->assertFalse($DB->record_exists('enrol', [ 'id' => $instance->id ]));
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $DB, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'lti';
// Only enable the lti enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
$context = context_course::instance($course->id);
$teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST);
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
// Enable this enrol plugin for the course.
$fields = ['contextid' => $context->id, 'roleinstructor' => $teacherroleid, 'rolelearner' => $studentroleid];
$plugin->add_instance($course, $fields);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Teachers don't have enrol/lti:unenrol capability by default. Login as admin for simplicity.
$this->setAdminUser();
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// LTI enrolment has 1 enrol actions for active users -- unenrol.
$this->assertCount(1, $actions);
}
}

View File

@ -493,4 +493,51 @@ class enrol_manual_lib_testcase extends advanced_testcase {
$manualplugin->send_expiry_notifications($trace);
$this->assertEquals(6, $sink->count());
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'manual';
// Only enable the manual enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a teacher.
$teacher = $generator->create_user();
// Enrol the teacher to the course.
$generator->enrol_user($teacher->id, $course->id, 'editingteacher', $pluginname);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Login as the teacher.
$this->setUser($teacher);
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Manual enrol has 2 enrol actions -- edit and unenrol.
$this->assertCount(2, $actions);
}
}

View File

@ -870,4 +870,56 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
$this->assertEquals($expectedenrolments, $enrolments);
$sink->close();
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'meta';
// Only enable the meta enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Teachers don't have enrol/meta:unenrol capability by default. Login as admin for simplicity.
$this->setAdminUser();
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Meta-link enrolment has no enrol actions for active students.
$this->assertCount(0, $actions);
// Enrol actions for a suspended student.
// Suspend the student.
$ue->status = ENROL_USER_SUSPENDED;
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Meta-link enrolment has enrol actions for suspended students -- unenrol.
$this->assertCount(1, $actions);
}
}

View File

@ -168,4 +168,60 @@ class enrol_paypal_testcase extends advanced_testcase {
$this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'paypal';
// Only enable the paypal enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
// Login as admin to see all enrol actions.
$this->setAdminUser();
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Paypal enrolment has 2 enrol actions for active users when logged in as admin: edit and unenrol.
$this->assertCount(2, $actions);
// Enrol actions when viewing as a teacher.
// Create a teacher.
$teacher = $generator->create_user();
// Enrol the teacher to the course.
$generator->enrol_user($teacher->id, $course->id, 'editingteacher', $pluginname);
// Login as the teacher.
$this->setUser($teacher);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Teachers don't have the enrol/paypal:unenrol capability by default, but have enrol/paypal:manage.
$this->assertCount(1, $actions);
}
}

View File

@ -726,4 +726,51 @@ class enrol_self_testcase extends advanced_testcase {
$contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_NOREPLY, $context);
$this->assertEquals($noreplyuser, $contact);
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions() {
global $CFG, $DB, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'self';
// Only enable the self enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Create a teacher.
$teacher = $generator->create_user();
// Enrol the teacher to the course.
$enrolresult = $generator->enrol_user($teacher->id, $course->id, 'editingteacher', $pluginname);
$this->assertTrue($enrolresult);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$enrolresult = $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
$this->assertTrue($enrolresult);
// Login as the teacher.
$this->setUser($teacher);
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Self enrol has 2 enrol actions -- edit and unenrol.
$this->assertCount(2, $actions);
}
}