Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 39 and 402]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core_enrol;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  
  23  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  24  require_once($CFG->dirroot . '/enrol/externallib.php');
  25  
  26  /**
  27   * Role external PHPunit tests
  28   *
  29   * @package    core_enrol
  30   * @category   test
  31   * @copyright  2012 Jerome Mouneyrac
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @since Moodle 2.4
  34   */
  35  class role_external_test extends \externallib_advanced_testcase {
  36  
  37      /**
  38       * Tests set up
  39       */
  40      protected function setUp(): void {
  41          global $CFG;
  42          require_once($CFG->dirroot . '/enrol/externallib.php');
  43      }
  44  
  45      /**
  46       * Test assign_roles
  47       */
  48      public function test_assign_roles() {
  49          global $USER;
  50  
  51          $this->resetAfterTest(true);
  52  
  53          $course = self::getDataGenerator()->create_course();
  54  
  55          // Set the required capabilities by the external function.
  56          $context = \context_course::instance($course->id);
  57          $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
  58          $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
  59  
  60          // Add manager role to $USER.
  61          // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
  62          role_assign(1, $USER->id, \context_system::instance()->id);
  63  
  64          // Check the teacher role has not been assigned to $USER.
  65          $users = get_role_users(3, $context);
  66          $this->assertEquals(count($users), 0);
  67  
  68          // Call the external function. Assign teacher role to $USER with contextid.
  69          \core_role_external::assign_roles(array(
  70              array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
  71  
  72          // Check the role has been assigned.
  73          $users = get_role_users(3, $context);
  74          $this->assertEquals(count($users), 1);
  75  
  76          // Unassign role.
  77          role_unassign(3, $USER->id, $context->id);
  78          $users = get_role_users(3, $context);
  79          $this->assertEquals(count($users), 0);
  80  
  81          // Call the external function. Assign teacher role to $USER.
  82          \core_role_external::assign_roles(array(
  83              array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id)));
  84          $users = get_role_users(3, $context);
  85          $this->assertEquals(count($users), 1);
  86  
  87          // Call without required capability.
  88          $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
  89          $this->expectException('moodle_exception');
  90          $categories = \core_role_external::assign_roles(
  91              array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
  92      }
  93  
  94      /**
  95       * Test unassign_roles
  96       */
  97      public function test_unassign_roles() {
  98          global $USER;
  99  
 100          $this->resetAfterTest(true);
 101  
 102          $course = self::getDataGenerator()->create_course();
 103  
 104          // Set the required capabilities by the external function.
 105          $context = \context_course::instance($course->id);
 106          $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
 107          $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
 108  
 109          // Add manager role to $USER.
 110          // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
 111          role_assign(1, $USER->id, \context_system::instance()->id);
 112  
 113          // Add teacher role to $USER on course context.
 114          role_assign(3, $USER->id, $context->id);
 115  
 116          // Check the teacher role has been assigned to $USER on course context.
 117          $users = get_role_users(3, $context);
 118          $this->assertEquals(count($users), 1);
 119  
 120          // Call the external function. Unassign teacher role using contextid.
 121          \core_role_external::unassign_roles(array(
 122              array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
 123  
 124          // Check the role has been unassigned on course context.
 125          $users = get_role_users(3, $context);
 126          $this->assertEquals(count($users), 0);
 127  
 128          // Add teacher role to $USER on course context.
 129          role_assign(3, $USER->id, $context->id);
 130          $users = get_role_users(3, $context);
 131          $this->assertEquals(count($users), 1);
 132  
 133          // Call the external function. Unassign teacher role using context level and instanceid.
 134          \core_role_external::unassign_roles(array(
 135              array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id)));
 136  
 137          // Check the role has been unassigned on course context.
 138          $users = get_role_users(3, $context);
 139          $this->assertEquals(count($users), 0);
 140  
 141          // Call without required capability.
 142          $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
 143          $this->expectException('moodle_exception');
 144          $categories = \core_role_external::unassign_roles(
 145              array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
 146      }
 147  }