Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  defined('MOODLE_INTERNAL') || die;
  20  
  21  global $CFG;
  22  
  23  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  24  
  25  /**
  26   * Unit tests for the core_grades\external\get_feedback webservice.
  27   *
  28   * @package    core_grades
  29   * @category   external
  30   * @copyright  2023 Kevin Percy <kevin.percy@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   * @since      Moodle 4.2
  33   */
  34  class get_feedback_test extends \externallib_advanced_testcase {
  35  
  36      /**
  37       * Test get_feedback.
  38       *
  39       * @covers ::get_feedback
  40       * @dataProvider get_feedback_provider
  41       * @param string|null $feedback The feedback text added for the grade item.
  42       * @param array $expected The expected feedback data.
  43       * @return void
  44       */
  45      public function test_get_feedback(?string $feedback, array $expected) {
  46  
  47          $this->resetAfterTest(true);
  48          $course = $this->getDataGenerator()->create_course();
  49          $user = $this->getDataGenerator()->create_user(['firstname' => 'John', 'lastname' => 'Doe',
  50              'email' => 'johndoe@example.com']);
  51          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  52          $gradeitem = $this->getDataGenerator()->create_grade_item(['itemname' => 'Grade item 1',
  53              'courseid' => $course->id]);
  54  
  55          $gradegradedata = [
  56              'itemid' => $gradeitem->id,
  57              'userid' => $user->id,
  58          ];
  59  
  60          if ($feedback) {
  61              $gradegradedata['feedback'] = $feedback;
  62          }
  63  
  64          $this->getDataGenerator()->create_grade_grade($gradegradedata);
  65          $this->setAdminUser();
  66  
  67          $feedbackdata = get_feedback::execute($course->id, $user->id, $gradeitem->id);
  68  
  69          $this->assertEquals($expected['feedbacktext'], $feedbackdata['feedbacktext']);
  70          $this->assertEquals($expected['title'], $feedbackdata['title']);
  71          $this->assertEquals($expected['fullname'], $feedbackdata['fullname']);
  72          $this->assertEquals($expected['additionalfield'], $feedbackdata['additionalfield']);
  73      }
  74  
  75      /**
  76       * Data provider for test_get_feedback().
  77       *
  78       * @return array
  79       */
  80      public function get_feedback_provider(): array {
  81          return [
  82              'Return when feedback is set.' => [
  83                  'Test feedback',
  84                  [
  85                      'feedbacktext' => 'Test feedback',
  86                      'title' => 'Grade item 1',
  87                      'fullname' => 'John Doe',
  88                      'additionalfield' => 'johndoe@example.com'
  89                  ]
  90              ],
  91              'Return when feedback is not set.' => [
  92                  null,
  93                  [
  94                      'feedbacktext' => null,
  95                      'title' => 'Grade item 1',
  96                      'fullname' => 'John Doe',
  97                      'additionalfield' => 'johndoe@example.com'
  98                  ]
  99              ]
 100          ];
 101      }
 102  
 103      /**
 104       * Test get_feedback with invalid requests.
 105       *
 106       * @covers ::get_feedback
 107       * @dataProvider get_feedback_invalid_request_provider
 108       * @param string $loggeduserrole The role of the logged user.
 109       * @param bool $feedbacknotincourse Whether to request a feedback for a grade item which is not a part of the course.
 110       * @param array $expectedexception The expected exception.
 111       * @return void
 112       */
 113      public function test_get_feedback_invalid_request(string $loggeduserrole, bool $feedbacknotincourse,
 114              array $expectedexception = []) {
 115  
 116          $this->resetAfterTest(true);
 117          // Create a course with a user and a grade item.
 118          $course = $this->getDataGenerator()->create_course();
 119          $user = $this->getDataGenerator()->create_user();
 120          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 121          $gradeitem = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id]);
 122          // Add feedback for the grade item in course.
 123          $gradegradedata = [
 124              'itemid' => $gradeitem->id,
 125              'userid' => $user->id,
 126              'feedback' => 'Test feedback',
 127          ];
 128  
 129          $this->getDataGenerator()->create_grade_grade($gradegradedata);
 130          // Set the current user as specified.
 131          if ($loggeduserrole === 'user') {
 132              $this->setUser($user);
 133          } else if ($loggeduserrole === 'guest') {
 134              $this->setGuestUser();
 135          } else {
 136              $this->setAdminUser();
 137          }
 138  
 139          if ($feedbacknotincourse) { // Create a new course which will be later used in the feedback request call.
 140              $course = $this->getDataGenerator()->create_course();
 141          }
 142  
 143          $this->expectException($expectedexception['exceptionclass']);
 144  
 145          if (!empty($expectedexception['exceptionmessage'])) {
 146              $this->expectExceptionMessage($expectedexception['exceptionmessage']);
 147          }
 148  
 149          get_feedback::execute($course->id, $user->id, $gradeitem->id);
 150      }
 151  
 152      /**
 153       * Data provider for test_get_feedback_invalid_request().
 154       *
 155       * @return array
 156       */
 157      public function get_feedback_invalid_request_provider(): array {
 158          return [
 159              'Logged user does not have permissions to view feedback.' => [
 160                  'user',
 161                  false,
 162                  ['exceptionclass' => \required_capability_exception::class]
 163              ],
 164              'Guest user cannot view feedback.' => [
 165                  'guest',
 166                  false,
 167                  ['exceptionclass' => \require_login_exception::class]
 168              ],
 169              'Request feedback for a grade item which is not a part of the course.' => [
 170                  'admin',
 171                  true,
 172                  [
 173                      'exceptionclass' => \invalid_parameter_exception::class,
 174                      'exceptionmessage' => 'Course ID and item ID mismatch',
 175                  ]
 176              ]
 177          ];
 178      }
 179  }