Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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