Permalink
Browse files

MDL-34967 PHPunit test: enrol/externallib.php and enrol/manual/extern…

…allib.php
  • Loading branch information...
1 parent 569f1ad commit 566596abd75e3c61a8f74bae77cf9297c5c552b2 @mouneyrac mouneyrac committed Sep 4, 2012
Showing with 297 additions and 0 deletions.
  1. +73 −0 enrol/manual/tests/externallib_test.php
  2. +224 −0 enrol/tests/externallib_test.php
@@ -0,0 +1,73 @@
+<?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/>.
+
+/**
+ * Enrol manual external PHPunit tests
+ *
+ * @package enrol_manual
+ * @category external
+ * @copyright 2012 Jerome Mouneyrac
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.4
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+
+require_once($CFG->dirroot . '/webservice/tests/helpers.php');
+require_once($CFG->dirroot . '/enrol/manual/externallib.php');
+
+class enrol_manual_external_testcase extends externallib_advanced_testcase {
+
+ /**
+ * 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);
+ }
+}
@@ -0,0 +1,224 @@
+<?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/>.
+
+/**
+ * Enrol/Role external PHPunit tests
+ *
+ * @package core_enrol
+ * @category external
+ * @copyright 2012 Jerome Mouneyrac
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.4
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+
+require_once($CFG->dirroot . '/webservice/tests/helpers.php');
+require_once($CFG->dirroot . '/enrol/externallib.php');
+
+/**
+ * Enrol external PHPunit tests
+ *
+ * @package core_enrol
+ * @category external
+ * @copyright 2012 Jerome Mouneyrac
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.4
+ */
+class core_enrol_external_testcase extends externallib_advanced_testcase {
+
+ /**
+ * Test get_enrolled_users
+ */
+ public function test_get_enrolled_users() {
+ global $USER;
+
+ $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('moodle/course:viewparticipants', $context->id);
+ $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);
+
+ // Call the external function.
+ $enrolledusers = core_enrol_external::get_enrolled_users($course->id);
+
+ // Check we retrieve the good total number of enrolled users.
+ $this->assertEquals(3, count($enrolledusers));
+
+ // Call without required capability.
+ $this->unassignUserCapability('moodle/course:viewparticipants', $context->id, $roleid);
+ $this->setExpectedException('moodle_exception');
+ $categories = core_enrol_external::get_enrolled_users($course->id);
+ }
+
+ /**
+ * Test get_users_courses
+ */
+ public function test_get_users_courses() {
+ global $USER;
+
+ $this->resetAfterTest(true);
+
+ $course1 = self::getDataGenerator()->create_course();
+ $course2 = self::getDataGenerator()->create_course();
+ $courses = array($course1, $course2);
+
+ // 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);
+ }
+
+ // Call the external function.
+ $enrolledincourses = core_enrol_external::get_users_courses($USER->id);
+
+ // Check we retrieve the good total number of enrolled users.
+ $this->assertEquals(2, count($enrolledincourses));
+ }
+}
+
+/**
+ * Role external PHPunit tests
+ *
+ * @package core_enrol
+ * @category external
+ * @copyright 2012 Jerome Mouneyrac
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since Moodle 2.4
+ */
+class core_role_external_testcase extends externallib_advanced_testcase {
+
+ /**
+ * Tests set up
+ */
+ protected function setUp() {
+ global $CFG;
+ require_once($CFG->dirroot . '/enrol/externallib.php');
+ }
+
+ /**
+ * Test assign_roles
+ */
+ public function test_assign_roles() {
+ global $USER;
+
+ $this->resetAfterTest(true);
+
+ $course = self::getDataGenerator()->create_course();
+
+ // Set the required capabilities by the external function.
+ $context = context_course::instance($course->id);
+ $roleid = $this->assignUserCapability('moodle/role:assign', $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);
+
+ // Check the teacher role has not been assigned to $USER.
+ $users = get_role_users(3, $context);
+ $this->assertEquals(count($users), 0);
+
+ // Call the external function. Assign teacher role to $USER.
+ core_role_external::assign_roles(array(
+ array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
+
+ // Check the role has been assigned.
+ $users = get_role_users(3, $context);
+ $this->assertEquals(count($users), 1);
+
+ // Call without required capability.
+ $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
+ $this->setExpectedException('moodle_exception');
+ $categories = core_role_external::assign_roles(
+ array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
+ }
+
+ /**
+ * Test unassign_roles
+ */
+ public function test_unassign_roles() {
+ global $USER;
+
+ $this->resetAfterTest(true);
+
+ $course = self::getDataGenerator()->create_course();
+
+ // Set the required capabilities by the external function.
+ $context = context_course::instance($course->id);
+ $roleid = $this->assignUserCapability('moodle/role:assign', $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);
+
+ // Add teacher role to $USER on course context.
+ role_assign(3, $USER->id, $context->id);
+
+ // Check the teacher role has been assigned to $USER on course context.
+ $users = get_role_users(3, $context);
+ $this->assertEquals(count($users), 1);
+
+ // Call the external function. Assign teacher role to $USER.
+ core_role_external::unassign_roles(array(
+ array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
+
+ // Check the role has been unassigned on course context.
+ $users = get_role_users(3, $context);
+ $this->assertEquals(count($users), 0);
+
+ // Call without required capability.
+ $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
+ $this->setExpectedException('moodle_exception');
+ $categories = core_role_external::unassign_roles(
+ array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
+ }
+}

0 comments on commit 566596a

Please sign in to comment.