Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 403]

   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 gradereport_singleview;
  18  
  19  use gradereport_singleview_screen_testable;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once (__DIR__ . '/fixtures/screen.php');
  25  require_once($CFG->libdir . '/gradelib.php');
  26  
  27  /**
  28   * Tests for screen class.
  29   *
  30   * @package    gradereport_singleview
  31   * @category   test
  32   * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class screen_test extends \advanced_testcase {
  36  
  37      /**
  38       * Test load_users method.
  39       */
  40      public function test_load_users() {
  41          global $DB;
  42  
  43          $this->setAdminUser();
  44          $this->resetAfterTest(true);
  45  
  46          $roleteacher = $DB->get_record('role', ['shortname' => 'teacher'], '*', MUST_EXIST);
  47  
  48          // Create a course, users and groups.
  49          $course = $this->getDataGenerator()->create_course();
  50          $coursecontext = \context_course::instance($course->id);
  51          $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
  52          $teacher = $this->getDataGenerator()->create_user();
  53          $user1 = $this->getDataGenerator()->create_user();
  54          $user2 = $this->getDataGenerator()->create_user();
  55          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
  56          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
  57          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
  58          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $teacher->id]);
  59          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user1->id]);
  60          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user2->id]);
  61  
  62          // Perform a regrade before creating the report.
  63          grade_regrade_final_grades($course->id);
  64          $screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
  65          $groupusers = $screentest->test_load_users();
  66          $this->assertCount(2, $groupusers);
  67  
  68          // Now, let's suspend the enrolment of a user. Should return only one user.
  69          $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
  70          $users = $screentest->test_load_users();
  71          $this->assertCount(1, $users);
  72  
  73          // Change the viewsuspendedusers capabilities and set the user preference to display suspended users.
  74          assign_capability('moodle/course:viewsuspendedusers', CAP_ALLOW, $roleteacher->id, $coursecontext, true);
  75          set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
  76          accesslib_clear_all_caches_for_unit_testing();
  77          $this->setUser($teacher);
  78          $screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
  79          $users = $screentest->test_load_users();
  80          $this->assertCount(2, $users);
  81  
  82          // Change the capability again, now the user can't see the suspended enrolments.
  83          assign_capability('moodle/course:viewsuspendedusers', CAP_PROHIBIT, $roleteacher->id, $coursecontext, true);
  84          set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
  85          accesslib_clear_all_caches_for_unit_testing();
  86          $users = $screentest->test_load_users();
  87          $this->assertCount(1, $users);
  88  
  89          // Now, activate the user enrolment again. We shall get 2 users now.
  90          $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_ACTIVE);
  91          $users = $screentest->test_load_users();
  92          $this->assertCount(2, $users);
  93      }
  94  }