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  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 external_api;
  25  use mod_forum\local\entities\forum as forum_entity;
  26  use moodle_exception;
  27  use grade_grade;
  28  use grade_item;
  29  
  30  /**
  31   * Unit tests for core_grades\component_gradeitems;
  32   *
  33   * @package   core_grades
  34   * @category  test
  35   * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class store_test extends advanced_testcase {
  39  
  40      public static function setupBeforeClass(): void {
  41          global $CFG;
  42          require_once("{$CFG->libdir}/externallib.php");
  43      }
  44  
  45      /**
  46       * Ensure that an execute with an invalid component is rejected.
  47       */
  48      public function test_execute_invalid_component(): void {
  49          $this->resetAfterTest();
  50          $user = $this->getDataGenerator()->create_user();
  51          $this->setUser($user);
  52  
  53          $this->expectException(coding_exception::class);
  54          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component");
  55          store::execute('mod_invalid', 1, 'foo', 2, false, 'formdata');
  56      }
  57  
  58      /**
  59       * Ensure that an execute with an invalid itemname on a valid component is rejected.
  60       */
  61      public function test_execute_invalid_itemname(): void {
  62          $this->resetAfterTest();
  63          $user = $this->getDataGenerator()->create_user();
  64          $this->setUser($user);
  65  
  66          $this->expectException(coding_exception::class);
  67          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component");
  68          store::execute('mod_forum', 1, 'foo', 2, false, 'formdata');
  69      }
  70  
  71      /**
  72       * Ensure that an execute against a different grading method is rejected.
  73       */
  74      public function test_execute_incorrect_type(): void {
  75          $this->resetAfterTest();
  76  
  77          $forum = $this->get_forum_instance([
  78              // Negative numbers mean a scale.
  79              'grade_forum' => -1,
  80          ]);
  81          $course = $forum->get_course_record();
  82          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  83          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  84          $this->setUser($teacher);
  85  
  86          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  87  
  88          $this->expectException(moodle_exception::class);
  89          $this->expectExceptionMessage("not configured for direct grading");
  90          store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, 'formdata');
  91      }
  92  
  93      /**
  94       * Ensure that an execute against a different grading method is rejected.
  95       */
  96      public function test_execute_disabled(): void {
  97          $this->resetAfterTest();
  98  
  99          $forum = $this->get_forum_instance();
 100          $course = $forum->get_course_record();
 101          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 102          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 103          $this->setUser($teacher);
 104  
 105          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 106  
 107          $this->expectException(moodle_exception::class);
 108          $this->expectExceptionMessage("Grading is not enabled");
 109          store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, 'formdata');
 110      }
 111  
 112      /**
 113       * Ensure that an execute against the correct grading method returns the current state of the user.
 114       */
 115      public function test_execute_store_empty(): void {
 116          $this->resetAfterTest();
 117  
 118          $forum = $this->get_forum_instance([
 119              // Negative numbers mean a scale.
 120              'grade_forum' => 5,
 121          ]);
 122          $course = $forum->get_course_record();
 123          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 124          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 125          $this->setUser($teacher);
 126  
 127          $formdata = [
 128              'grade' => null,
 129          ];
 130  
 131          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 132  
 133          $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum',
 134                  (int) $student->id, false, http_build_query($formdata));
 135          $result = external_api::clean_returnvalue(store::execute_returns(), $result);
 136  
 137          // The result should still be empty.
 138          $this->assertIsArray($result);
 139          $this->assertArrayHasKey('templatename', $result);
 140  
 141          $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']);
 142  
 143          $this->assertArrayHasKey('grade', $result);
 144          $this->assertIsArray($result['grade']);
 145          $this->assertArrayHasKey('grade', $result['grade']);
 146          $this->assertEmpty($result['grade']['grade']);
 147          $this->assertArrayHasKey('timecreated', $result['grade']);
 148          $this->assertIsInt($result['grade']['timecreated']);
 149          $this->assertArrayHasKey('timemodified', $result['grade']);
 150          $this->assertIsInt($result['grade']['timemodified']);
 151  
 152          $this->assertArrayHasKey('warnings', $result);
 153          $this->assertIsArray($result['warnings']);
 154          $this->assertEmpty($result['warnings']);
 155  
 156          // Test the grade array items.
 157          $this->assertArrayHasKey('grade', $result);
 158          $this->assertIsArray($result['grade']);
 159  
 160          $this->assertArrayHasKey('grade', $result['grade']);
 161          $this->assertEquals(null, $result['grade']['grade']);
 162  
 163          $this->assertIsInt($result['grade']['timecreated']);
 164          $this->assertArrayHasKey('timemodified', $result['grade']);
 165          $this->assertIsInt($result['grade']['timemodified']);
 166  
 167          $this->assertArrayHasKey('usergrade', $result['grade']);
 168          $this->assertEquals('- / 5.00', $result['grade']['usergrade']);
 169  
 170          $this->assertArrayHasKey('maxgrade', $result['grade']);
 171          $this->assertIsInt($result['grade']['maxgrade']);
 172          $this->assertEquals(5, $result['grade']['maxgrade']);
 173  
 174          $this->assertArrayHasKey('gradedby', $result['grade']);
 175          $this->assertEquals(fullname($teacher), $result['grade']['gradedby']);
 176  
 177          // Compare against the grade stored in the database.
 178          $storedgradeitem = grade_item::fetch([
 179              'courseid' => $forum->get_course_id(),
 180              'itemtype' => 'mod',
 181              'itemmodule' => 'forum',
 182              'iteminstance' => $forum->get_id(),
 183              'itemnumber' => $gradeitem->get_grade_itemid(),
 184          ]);
 185          $storedgrade = grade_grade::fetch([
 186              'userid' => $student->id,
 187              'itemid' => $storedgradeitem->id,
 188          ]);
 189  
 190          $this->assertEmpty($storedgrade->rawgrade);
 191      }
 192  
 193      /**
 194       * Ensure that an execute against the correct grading method returns the current state of the user.
 195       */
 196      public function test_execute_store_graded(): void {
 197          $this->resetAfterTest();
 198  
 199          $forum = $this->get_forum_instance([
 200              // Negative numbers mean a scale.
 201              'grade_forum' => 5,
 202          ]);
 203          $course = $forum->get_course_record();
 204          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 205          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 206          $this->setUser($teacher);
 207  
 208          $formdata = [
 209              'grade' => 4,
 210          ];
 211  
 212          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 213  
 214          $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum',
 215                  (int) $student->id, false, http_build_query($formdata));
 216          $result = external_api::clean_returnvalue(store::execute_returns(), $result);
 217  
 218          // The result should still be empty.
 219          $this->assertIsArray($result);
 220          $this->assertArrayHasKey('templatename', $result);
 221  
 222          $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']);
 223  
 224          $this->assertArrayHasKey('warnings', $result);
 225          $this->assertIsArray($result['warnings']);
 226          $this->assertEmpty($result['warnings']);
 227  
 228          // Test the grade array items.
 229          $this->assertArrayHasKey('grade', $result);
 230          $this->assertIsArray($result['grade']);
 231  
 232          $this->assertArrayHasKey('grade', $result['grade']);
 233          $this->assertEquals(grade_floatval(unformat_float(4)), $result['grade']['grade']);
 234  
 235          $this->assertIsInt($result['grade']['timecreated']);
 236          $this->assertArrayHasKey('timemodified', $result['grade']);
 237          $this->assertIsInt($result['grade']['timemodified']);
 238  
 239          $this->assertArrayHasKey('usergrade', $result['grade']);
 240          $this->assertEquals('4.00 / 5.00', $result['grade']['usergrade']);
 241  
 242          $this->assertArrayHasKey('maxgrade', $result['grade']);
 243          $this->assertIsInt($result['grade']['maxgrade']);
 244          $this->assertEquals(5, $result['grade']['maxgrade']);
 245  
 246          $this->assertArrayHasKey('gradedby', $result['grade']);
 247          $this->assertEquals(fullname($teacher), $result['grade']['gradedby']);
 248  
 249          // Compare against the grade stored in the database.
 250          $storedgradeitem = grade_item::fetch([
 251              'courseid' => $forum->get_course_id(),
 252              'itemtype' => 'mod',
 253              'itemmodule' => 'forum',
 254              'iteminstance' => $forum->get_id(),
 255              'itemnumber' => $gradeitem->get_grade_itemid(),
 256          ]);
 257          $storedgrade = grade_grade::fetch([
 258              'userid' => $student->id,
 259              'itemid' => $storedgradeitem->id,
 260          ]);
 261  
 262          $this->assertEquals(grade_floatval(unformat_float(4)), $storedgrade->rawgrade);
 263      }
 264  
 265      /**
 266       * Ensure that an out-of-range value is rejected.
 267       *
 268       * @dataProvider execute_out_of_range_provider
 269       * @param int $maxvalue The max value of the forum
 270       * @param int $suppliedvalue The value that was submitted
 271       */
 272      public function test_execute_store_out_of__range(int $maxvalue, float $suppliedvalue): void {
 273          $this->resetAfterTest();
 274  
 275          $forum = $this->get_forum_instance([
 276              // Negative numbers mean a scale.
 277              'grade_forum' => $maxvalue,
 278          ]);
 279          $course = $forum->get_course_record();
 280          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 281          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 282          $this->setUser($teacher);
 283  
 284          $formdata = [
 285              'grade' => $suppliedvalue,
 286          ];
 287  
 288          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 289  
 290          $this->expectException(moodle_exception::class);
 291          $this->expectExceptionMessage("Invalid grade '{$suppliedvalue}' provided. Grades must be between 0 and {$maxvalue}.");
 292          store::execute('mod_forum', (int) $forum->get_context()->id, 'forum',
 293                  (int) $student->id, false, http_build_query($formdata));
 294      }
 295  
 296      /**
 297       * Data provider for out of range tests.
 298       *
 299       * @return array
 300       */
 301      public function execute_out_of_range_provider(): array {
 302          return [
 303              'above' => [
 304                  'max' => 100,
 305                  'supplied' => 101,
 306              ],
 307              'above just' => [
 308                  'max' => 100,
 309                  'supplied' => 101.001,
 310              ],
 311              'below' => [
 312                  'max' => 100,
 313                  'supplied' => -100,
 314              ],
 315              '-1' => [
 316                  'max' => 100,
 317                  'supplied' => -1,
 318              ],
 319          ];
 320      }
 321  
 322  
 323      /**
 324       * Get a forum instance.
 325       *
 326       * @param array $config
 327       * @return forum_entity
 328       */
 329      protected function get_forum_instance(array $config = []): forum_entity {
 330          $this->resetAfterTest();
 331  
 332          $datagenerator = $this->getDataGenerator();
 333          $course = $datagenerator->create_course();
 334          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
 335  
 336          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 337          $vault = $vaultfactory->get_forum_vault();
 338  
 339          return $vault->get_from_id((int) $forum->id);
 340      }
 341  }