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 39 and 310]

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