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 401 and 402] [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 core_grades\external;
  18  
  19  use external_api;
  20  
  21  defined('MOODLE_INTERNAL') || die;
  22  
  23  global $CFG;
  24  
  25  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  26  
  27  /**
  28   * Unit tests for the core_grades\external\get_gradable_users.
  29   *
  30   * @package    core_grades
  31   * @category   external
  32   * @copyright  2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
  33   * @covers     \core_grades\external\get_gradable_users
  34   */
  35  class get_gradable_users_test extends \externallib_advanced_testcase {
  36  
  37      /**
  38       * Test the behaviour of get_gradable_users.
  39       *
  40       * @dataProvider execute_data
  41       * @param bool $onlyactiveenrol if we should only return active enrolments
  42       * @param bool $grouprestricted if we should only return users within a group
  43       * @param array $expected expected users
  44       */
  45      public function test_execute(bool $onlyactiveenrol, bool $grouprestricted, array $expected): void {
  46          global $DB;
  47  
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50          $generator = $this->getDataGenerator();
  51          $course = $generator->create_course();
  52  
  53          // Create and enrol test users.
  54          $student1 = $generator->create_user(['username' => 'student1', 'firstname' => 'Apple', 'lastname' => 'Apricot']);
  55          $student2 = $generator->create_user(['username' => 'student2', 'firstname' => 'Banana', 'lastname' => 'Blueberry']);
  56          $student3 = $generator->create_user(['username' => 'student3', 'firstname' => 'Cherry', 'lastname' => 'Cranberry']);
  57          $student4 = $generator->create_user(['username' => 'student4', 'firstname' => 'Durian', 'lastname' => 'Dracontomelon']);
  58          $student5 = $generator->create_user(['username' => 'student5', 'firstname' => 'Eggplant', 'lastname' => 'Ensete']);
  59  
  60          $role = $DB->get_record('role', ['shortname' => 'student'], '*', MUST_EXIST);
  61          $generator->enrol_user($student1->id, $course->id, $role->id);
  62          $generator->enrol_user($student2->id, $course->id, $role->id);
  63          $generator->enrol_user($student3->id, $course->id, $role->id);
  64          $generator->enrol_user($student4->id, $course->id, $role->id);
  65          $generator->enrol_user($student5->id, $course->id, $role->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
  66  
  67          $group1 = $generator->create_group(['courseid' => $course->id]);
  68          $group2 = $generator->create_group(['courseid' => $course->id]);
  69  
  70          $generator->create_group_member(['userid' => $student1->id, 'groupid' => $group1->id]);
  71          $generator->create_group_member(['userid' => $student1->id, 'groupid' => $group2->id]);
  72          $generator->create_group_member(['userid' => $student2->id, 'groupid' => $group2->id]);
  73          $generator->create_group_member(['userid' => $student3->id, 'groupid' => $group2->id]);
  74  
  75          $DB->set_field('course', 'groupmode', SEPARATEGROUPS, ['id' => $course->id]);
  76  
  77          $teacher = $generator->create_user(['username' => 'teacher1']);
  78          $role = $DB->get_record('role', ['shortname' => 'editingteacher'], '*', MUST_EXIST);
  79          $generator->enrol_user($teacher->id, $course->id, $role->id);
  80  
  81          $generator->create_module('assign', ['course' => $course->id]);
  82  
  83          $groupid = $grouprestricted ? $group2->id : 0;
  84          $result = get_gradable_users::execute($course->id, $groupid, $onlyactiveenrol);
  85          $result = external_api::clean_returnvalue(get_gradable_users::execute_returns(), $result);
  86  
  87          $mapped = array_map(function($user) {
  88              return [
  89                  'fullname' => $user['fullname'],
  90                  'firstname' => $user['firstname'],
  91                  'lastname' => $user['lastname'],
  92                  'profileimageurl' => $user['profileimageurl'],
  93              ];
  94          }, array_values($result['users']));
  95          $this->assertEquals($expected, $mapped);
  96      }
  97  
  98      /**
  99       * Data provider for test_execute.
 100       *
 101       * @return array
 102       */
 103      public function execute_data(): array {
 104          return [
 105              'All users' => [
 106                  false,
 107                  false,
 108                  [
 109                      [
 110                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 111                          'firstname' => 'Apple',
 112                          'lastname' => 'Apricot',
 113                          'fullname' => 'Apple Apricot',
 114                      ],
 115                      [
 116                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 117                          'firstname' => 'Banana',
 118                          'lastname' => 'Blueberry',
 119                          'fullname' => 'Banana Blueberry',
 120                      ],
 121                      [
 122                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 123                          'firstname' => 'Cherry',
 124                          'lastname' => 'Cranberry',
 125                          'fullname' => 'Cherry Cranberry',
 126                      ],
 127                      [
 128                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 129                          'firstname' => 'Durian',
 130                          'lastname' => 'Dracontomelon',
 131                          'fullname' => 'Durian Dracontomelon',
 132                      ],
 133                      [
 134                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 135                          'firstname' => 'Eggplant',
 136                          'lastname' => 'Ensete',
 137                          'fullname' => 'Eggplant Ensete',
 138                      ],
 139                  ],
 140              ],
 141              'Only active enrolment' => [
 142                  true,
 143                  false,
 144                  [
 145                      [
 146                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 147                          'firstname' => 'Apple',
 148                          'lastname' => 'Apricot',
 149                          'fullname' => 'Apple Apricot',
 150                      ],
 151                      [
 152                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 153                          'firstname' => 'Banana',
 154                          'lastname' => 'Blueberry',
 155                          'fullname' => 'Banana Blueberry',
 156                      ],
 157                      [
 158                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 159                          'firstname' => 'Cherry',
 160                          'lastname' => 'Cranberry',
 161                          'fullname' => 'Cherry Cranberry',
 162                      ],
 163                      [
 164                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 165                          'firstname' => 'Durian',
 166                          'lastname' => 'Dracontomelon',
 167                          'fullname' => 'Durian Dracontomelon',
 168                      ],
 169                  ],
 170              ],
 171              'Group restricted' => [
 172                  false,
 173                  true,
 174                  [
 175                      [
 176                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 177                          'firstname' => 'Apple',
 178                          'lastname' => 'Apricot',
 179                          'fullname' => 'Apple Apricot',
 180                      ],
 181                      [
 182                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 183                          'firstname' => 'Banana',
 184                          'lastname' => 'Blueberry',
 185                          'fullname' => 'Banana Blueberry',
 186                      ],
 187                      [
 188                          'profileimageurl' => 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f1',
 189                          'firstname' => 'Cherry',
 190                          'lastname' => 'Cranberry',
 191                          'fullname' => 'Cherry Cranberry',
 192                      ],
 193                  ],
 194              ],
 195          ];
 196      }
 197  }