Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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_user;
  18  
  19  /**
  20   * Unit tests for user roles editable class.
  21   *
  22   * @package    core_user
  23   * @copyright  2017 Damyon Wiese
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class userroleseditable_test extends \advanced_testcase {
  27      /**
  28       * Test user roles editable.
  29       */
  30      public function test_update() {
  31          global $DB;
  32  
  33          $this->resetAfterTest();
  34  
  35          // Create user and modify user profile.
  36          $user1 = $this->getDataGenerator()->create_user();
  37          $user2 = $this->getDataGenerator()->create_user();
  38  
  39          $course1 = $this->getDataGenerator()->create_course();
  40          $coursecontext = \context_course::instance($course1->id);
  41          $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
  42          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  43          $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
  44          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
  45          role_assign($teacherrole->id, $user1->id, $coursecontext->id);
  46          role_assign($teacherrole->id, $user2->id, $coursecontext->id);
  47  
  48          $this->setAdminUser();
  49          accesslib_clear_all_caches_for_unit_testing();
  50  
  51          // Use the userroleseditable api to remove all roles from user1 and give user2 student and teacher.
  52          $itemid = $course1->id . ':' . $user1->id;
  53          $newvalue = json_encode([]);
  54  
  55          $result = \core_user\output\user_roles_editable::update($itemid, $newvalue);
  56          $this->assertTrue($result instanceof \core_user\output\user_roles_editable);
  57  
  58          $currentroles = get_user_roles_in_course($user1->id, $course1->id);
  59  
  60          $this->assertEmpty($currentroles);
  61  
  62          $this->setAdminUser();
  63          accesslib_clear_all_caches_for_unit_testing();
  64  
  65          $itemid = $course1->id . ':' . $user2->id;
  66          $newvalue = json_encode([$teacherrole->id, $studentrole->id]);
  67  
  68          $result = \core_user\output\user_roles_editable::update($itemid, $newvalue);
  69          $this->assertTrue($result instanceof \core_user\output\user_roles_editable);
  70          $currentroles = get_user_roles_in_course($user2->id, $course1->id);
  71  
  72          $this->assertStringContainsString('Non-editing teacher', $currentroles);
  73          $this->assertStringContainsString('Student', $currentroles);
  74  
  75      }
  76  
  77  }