Skip to content

Commit

Permalink
MDL-59811 enrol: Unit tests for get_user_enrolment_actions()
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 9, 2017
1 parent d8e9a23 commit 18000c6
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 0 deletions.
52 changes: 52 additions & 0 deletions enrol/cohort/tests/cohortlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,56 @@ public function test_enrol_cohort_create_new_group() {
$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);
}
}
86 changes: 86 additions & 0 deletions enrol/database/tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
45 changes: 45 additions & 0 deletions enrol/flatfile/tests/flatfile_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,49 @@ public function test_flatfile_sync_task() {

$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);
}
}
49 changes: 49 additions & 0 deletions enrol/lti/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,53 @@ public function test_delete_instance() {
$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);
}
}
47 changes: 47 additions & 0 deletions enrol/manual/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,51 @@ public function test_send_expiry_notifications() {
$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);
}
}
52 changes: 52 additions & 0 deletions enrol/meta/tests/plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,4 +870,56 @@ public function test_timeend() {
$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);
}
}
Loading

0 comments on commit 18000c6

Please sign in to comment.