Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  declare(strict_types = 1);
  18  
  19  namespace core_grades\grades\grader\gradingpanel\point\external;
  20  
  21  use advanced_testcase;
  22  use coding_exception;
  23  use core_grades\component_gradeitem;
  24  use core_external\external_api;
  25  use mod_forum\local\entities\forum as forum_entity;
  26  use moodle_exception;
  27  
  28  /**
  29   * Unit tests for core_grades\component_gradeitems;
  30   *
  31   * @package   core_grades
  32   * @category  test
  33   * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class fetch_test extends advanced_testcase {
  37  
  38      /**
  39       * Ensure that an execute with an invalid component is rejected.
  40       */
  41      public function test_execute_invalid_component(): void {
  42          $this->resetAfterTest();
  43          $user = $this->getDataGenerator()->create_user();
  44          $this->setUser($user);
  45  
  46          $this->expectException(coding_exception::class);
  47          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component");
  48          fetch::execute('mod_invalid', 1, 'foo', 2);
  49      }
  50  
  51      /**
  52       * Ensure that an execute with an invalid itemname on a valid component is rejected.
  53       */
  54      public function test_execute_invalid_itemname(): void {
  55          $this->resetAfterTest();
  56          $user = $this->getDataGenerator()->create_user();
  57          $this->setUser($user);
  58  
  59          $this->expectException(coding_exception::class);
  60          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component");
  61          fetch::execute('mod_forum', 1, 'foo', 2);
  62      }
  63  
  64      /**
  65       * Ensure that an execute against a different grading method is rejected.
  66       */
  67      public function test_execute_incorrect_type(): void {
  68          $this->resetAfterTest();
  69  
  70          $forum = $this->get_forum_instance([
  71              // Negative numbers mean a scale.
  72              'grade_forum' => -1,
  73          ]);
  74          $course = $forum->get_course_record();
  75          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  76          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  77          $this->setUser($teacher);
  78  
  79          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  80  
  81          $this->expectException(moodle_exception::class);
  82          $this->expectExceptionMessage("not configured for direct grading");
  83          fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
  84      }
  85  
  86      /**
  87       * Ensure that an execute against the correct grading method returns the current state of the user.
  88       */
  89      public function test_execute_fetch_empty(): void {
  90          $this->resetAfterTest();
  91  
  92          $forum = $this->get_forum_instance([
  93              // Negative numbers mean a scale.
  94              'grade_forum' => 5,
  95          ]);
  96          $course = $forum->get_course_record();
  97          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  98          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  99          $this->setUser($teacher);
 100  
 101          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 102  
 103          $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
 104          $result = external_api::clean_returnvalue(fetch::execute_returns(), $result);
 105  
 106          $this->assertIsArray($result);
 107          $this->assertArrayHasKey('templatename', $result);
 108  
 109          $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']);
 110  
 111          $this->assertArrayHasKey('warnings', $result);
 112          $this->assertIsArray($result['warnings']);
 113          $this->assertEmpty($result['warnings']);
 114  
 115          // Test the grade array items.
 116          $this->assertArrayHasKey('grade', $result);
 117          $this->assertIsArray($result['grade']);
 118  
 119          $this->assertArrayHasKey('grade', $result['grade']);
 120          $this->assertEmpty($result['grade']['grade']);
 121  
 122          $this->assertIsInt($result['grade']['timecreated']);
 123          $this->assertArrayHasKey('timemodified', $result['grade']);
 124          $this->assertIsInt($result['grade']['timemodified']);
 125  
 126          $this->assertArrayHasKey('usergrade', $result['grade']);
 127          $this->assertEquals('- / 5.00', $result['grade']['usergrade']);
 128  
 129          $this->assertArrayHasKey('maxgrade', $result['grade']);
 130          $this->assertIsInt($result['grade']['maxgrade']);
 131          $this->assertEquals(5, $result['grade']['maxgrade']);
 132  
 133          $this->assertArrayHasKey('gradedby', $result['grade']);
 134          $this->assertEquals(null, $result['grade']['gradedby']);
 135      }
 136  
 137      /**
 138       * Ensure that an execute against the correct grading method returns the current state of the user.
 139       */
 140      public function test_execute_fetch_graded(): void {
 141          $this->resetAfterTest();
 142  
 143          $forum = $this->get_forum_instance([
 144              // Negative numbers mean a scale.
 145              'grade_forum' => 5,
 146          ]);
 147          $course = $forum->get_course_record();
 148          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 149          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 150  
 151          $this->execute_and_assert_fetch($forum, $teacher, $teacher, $student);
 152      }
 153  
 154      /**
 155       * Class mates should not get other's grades.
 156       */
 157      public function test_execute_fetch_does_not_return_data_to_other_students(): void {
 158          $this->resetAfterTest();
 159  
 160          $forum = $this->get_forum_instance([
 161              // Negative numbers mean a scale.
 162              'grade_forum' => 5,
 163          ]);
 164          $course = $forum->get_course_record();
 165          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 166          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 167          $evilstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');
 168  
 169          $this->expectException(\required_capability_exception::class);
 170          $this->execute_and_assert_fetch($forum, $evilstudent, $teacher, $student);
 171      }
 172  
 173      /**
 174       * Grades can be returned to graded user.
 175       */
 176      public function test_execute_fetch_return_data_to_graded_user(): void {
 177          $this->resetAfterTest();
 178  
 179          $forum = $this->get_forum_instance([
 180              // Negative numbers mean a scale.
 181              'grade_forum' => 5,
 182          ]);
 183          $course = $forum->get_course_record();
 184          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 185          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 186  
 187          $this->execute_and_assert_fetch($forum, $student, $teacher, $student);
 188      }
 189  
 190      /**
 191       * Executes the fetch method with the given users and returns the result.
 192       */
 193      private function execute_and_assert_fetch ($forum, $fetcheruser, $grader, $gradeduser) {
 194          $this->setUser($grader);
 195  
 196          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 197          $gradeitem->store_grade_from_formdata($gradeduser, $grader, (object) [
 198              'grade' => 4,
 199          ]);
 200  
 201          $this->setUser($fetcheruser);
 202  
 203          $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id);
 204          $result = external_api::clean_returnvalue(fetch::execute_returns(), $result);
 205  
 206          $this->assertIsArray($result);
 207          $this->assertArrayHasKey('templatename', $result);
 208  
 209          $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']);
 210  
 211          $this->assertArrayHasKey('warnings', $result);
 212          $this->assertIsArray($result['warnings']);
 213          $this->assertEmpty($result['warnings']);
 214  
 215          // Test the grade array items.
 216          $this->assertArrayHasKey('grade', $result);
 217          $this->assertIsArray($result['grade']);
 218  
 219          $this->assertArrayHasKey('grade', $result['grade']);
 220          $this->assertIsFloat($result['grade']['grade']);
 221          $this->assertEquals(grade_floatval(unformat_float(4)), $result['grade']['grade']);
 222  
 223          $this->assertIsInt($result['grade']['timecreated']);
 224          $this->assertArrayHasKey('timemodified', $result['grade']);
 225          $this->assertIsInt($result['grade']['timemodified']);
 226  
 227          $this->assertArrayHasKey('usergrade', $result['grade']);
 228          $this->assertEquals('4.00 / 5.00', $result['grade']['usergrade']);
 229  
 230          $this->assertArrayHasKey('maxgrade', $result['grade']);
 231          $this->assertIsInt($result['grade']['maxgrade']);
 232          $this->assertEquals(5, $result['grade']['maxgrade']);
 233  
 234          $this->assertArrayHasKey('gradedby', $result['grade']);
 235          $this->assertEquals(fullname($grader), $result['grade']['gradedby']);
 236  
 237          return $result;
 238      }
 239  
 240      /**
 241       * Get a forum instance.
 242       *
 243       * @param array $config
 244       * @return forum_entity
 245       */
 246      protected function get_forum_instance(array $config = []): forum_entity {
 247          $this->resetAfterTest();
 248  
 249          $datagenerator = $this->getDataGenerator();
 250          $course = $datagenerator->create_course();
 251          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
 252  
 253          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 254          $vault = $vaultfactory->get_forum_vault();
 255  
 256          return $vault->get_from_id((int) $forum->id);
 257      }
 258  }