Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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(null, $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          $formattedvalue = grade_floatval(unformat_float(4));
 221  
 222          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 223  
 224          $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum',
 225                  (int) $student->id, false, http_build_query($formdata));
 226          $result = external_api::clean_returnvalue(store::execute_returns(), $result);
 227  
 228          // The result should still be empty.
 229          $this->assertIsArray($result);
 230          $this->assertArrayHasKey('templatename', $result);
 231  
 232          $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']);
 233  
 234          $this->assertArrayHasKey('warnings', $result);
 235          $this->assertIsArray($result['warnings']);
 236          $this->assertEmpty($result['warnings']);
 237  
 238          // Test the grade array items.
 239          $this->assertArrayHasKey('grade', $result);
 240          $this->assertIsArray($result['grade']);
 241  
 242          $this->assertArrayHasKey('grade', $result['grade']);
 243          $this->assertEquals($formattedvalue, $result['grade']['grade']);
 244  
 245          $this->assertIsInt($result['grade']['timecreated']);
 246          $this->assertArrayHasKey('timemodified', $result['grade']);
 247          $this->assertIsInt($result['grade']['timemodified']);
 248  
 249          $this->assertArrayHasKey('usergrade', $result['grade']);
 250          $this->assertEquals($formattedvalue, $result['grade']['usergrade']);
 251  
 252          $this->assertArrayHasKey('maxgrade', $result['grade']);
 253          $this->assertIsInt($result['grade']['maxgrade']);
 254          $this->assertEquals(5, $result['grade']['maxgrade']);
 255  
 256          $this->assertArrayHasKey('gradedby', $result['grade']);
 257          $this->assertEquals(fullname($teacher), $result['grade']['gradedby']);
 258  
 259          // Compare against the grade stored in the database.
 260          $storedgradeitem = grade_item::fetch([
 261              'courseid' => $forum->get_course_id(),
 262              'itemtype' => 'mod',
 263              'itemmodule' => 'forum',
 264              'iteminstance' => $forum->get_id(),
 265              'itemnumber' => $gradeitem->get_grade_itemid(),
 266          ]);
 267          $storedgrade = grade_grade::fetch([
 268              'userid' => $student->id,
 269              'itemid' => $storedgradeitem->id,
 270          ]);
 271  
 272          $this->assertEquals($formattedvalue, $storedgrade->rawgrade);
 273      }
 274  
 275      /**
 276       * Ensure that an out-of-range value is rejected.
 277       *
 278       * @dataProvider execute_out_of_range_provider
 279       * @param int $maxvalue The max value of the forum
 280       * @param int $suppliedvalue The value that was submitted
 281       */
 282      public function test_execute_store_out_of__range(int $maxvalue, int $suppliedvalue): void {
 283          $this->resetAfterTest();
 284  
 285          $forum = $this->get_forum_instance([
 286              // Negative numbers mean a scale.
 287              'grade_forum' => $maxvalue,
 288          ]);
 289          $course = $forum->get_course_record();
 290          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 291          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 292          $this->setUser($teacher);
 293  
 294          $formdata = [
 295              'grade' => $suppliedvalue,
 296          ];
 297  
 298          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 299  
 300          $this->expectException(moodle_exception::class);
 301          $this->expectExceptionMessage("Invalid grade '{$suppliedvalue}' provided. Grades must be between 0 and {$maxvalue}.");
 302          store::execute('mod_forum', (int) $forum->get_context()->id, 'forum',
 303                  (int) $student->id, false, http_build_query($formdata));
 304      }
 305  
 306      /**
 307       * Data provider for out of range tests.
 308       *
 309       * @return array
 310       */
 311      public function execute_out_of_range_provider(): array {
 312          return [
 313              'above' => [
 314                  'max' => 100,
 315                  'supplied' => 101,
 316              ],
 317              'above just' => [
 318                  'max' => 100,
 319                  'supplied' => 101.001,
 320              ],
 321              'below' => [
 322                  'max' => 100,
 323                  'supplied' => -100,
 324              ],
 325              '-1' => [
 326                  'max' => 100,
 327                  'supplied' => -1,
 328              ],
 329          ];
 330      }
 331  
 332  
 333      /**
 334       * Get a forum instance.
 335       *
 336       * @param array $config
 337       * @return forum_entity
 338       */
 339      protected function get_forum_instance(array $config = []): forum_entity {
 340          $this->resetAfterTest();
 341  
 342          $datagenerator = $this->getDataGenerator();
 343          $course = $datagenerator->create_course();
 344          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
 345  
 346          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 347          $vault = $vaultfactory->get_forum_vault();
 348  
 349          return $vault->get_from_id((int) $forum->id);
 350      }
 351  }