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 gradingform_rubric\grades\grader\gradingpanel\external;
  20  
  21  use advanced_testcase;
  22  use coding_exception;
  23  use core_grades\component_gradeitem;
  24  use core_grades\component_gradeitems;
  25  use external_api;
  26  use mod_forum\local\entities\forum as forum_entity;
  27  use moodle_exception;
  28  
  29  /**
  30   * Unit tests for core_grades\component_gradeitems;
  31   *
  32   * @package   gradingform_rubric
  33   * @category  test
  34   * @copyright 2019 Mathew May <mathew.solutions>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class fetch_test extends advanced_testcase {
  38  
  39      public static function setupBeforeClass(): void {
  40          global $CFG;
  41          require_once("{$CFG->libdir}/externallib.php");
  42      }
  43  
  44      /**
  45       * Ensure that an execute with an invalid component is rejected.
  46       */
  47      public function test_execute_invalid_component(): void {
  48          $this->resetAfterTest();
  49          $user = $this->getDataGenerator()->create_user();
  50          $this->setUser($user);
  51  
  52          $this->expectException(coding_exception::class);
  53          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component");
  54          fetch::execute('mod_invalid', 1, 'foo', 2);
  55      }
  56  
  57      /**
  58       * Ensure that an execute with an invalid itemname on a valid component is rejected.
  59       */
  60      public function test_execute_invalid_itemname(): void {
  61          $this->resetAfterTest();
  62          $user = $this->getDataGenerator()->create_user();
  63          $this->setUser($user);
  64  
  65          $this->expectException(coding_exception::class);
  66          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component");
  67          fetch::execute('mod_forum', 1, 'foo', 2);
  68      }
  69  
  70      /**
  71       * Ensure that an execute against a different grading method is rejected.
  72       */
  73      public function test_execute_incorrect_type(): void {
  74          $this->resetAfterTest();
  75  
  76          $forum = $this->get_forum_instance([
  77              // Negative numbers mean a scale.
  78              'grade_forum' => 5,
  79          ]);
  80          $course = $forum->get_course_record();
  81          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  82          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  83          $this->setUser($teacher);
  84  
  85          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  86  
  87          $this->expectException(moodle_exception::class);
  88          $this->expectExceptionMessage("not configured for advanced grading with a rubric");
  89          fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
  90      }
  91  
  92      /**
  93       * Ensure that an execute against the correct grading method returns the current state of the user.
  94       */
  95      public function test_execute_fetch_empty(): void {
  96          $this->resetAfterTest();
  97  
  98          [
  99              'forum' => $forum,
 100              'controller' => $controller,
 101              'definition' => $definition,
 102              'student' => $student,
 103              'teacher' => $teacher,
 104          ] = $this->get_test_data();
 105  
 106          $this->setUser($teacher);
 107  
 108          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 109  
 110          $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
 111          $result = external_api::clean_returnvalue(fetch::execute_returns(), $result);
 112  
 113          $this->assertIsArray($result);
 114          $this->assertArrayHasKey('templatename', $result);
 115  
 116          $this->assertEquals('gradingform_rubric/grades/grader/gradingpanel', $result['templatename']);
 117  
 118          $this->assertArrayHasKey('warnings', $result);
 119          $this->assertIsArray($result['warnings']);
 120          $this->assertEmpty($result['warnings']);
 121  
 122          // Test the grade array items.
 123          $this->assertArrayHasKey('grade', $result);
 124          $this->assertIsArray($result['grade']);
 125          $this->assertIsInt($result['grade']['timecreated']);
 126  
 127          $this->assertArrayHasKey('timemodified', $result['grade']);
 128          $this->assertIsInt($result['grade']['timemodified']);
 129  
 130          $this->assertArrayHasKey('usergrade', $result['grade']);
 131          $this->assertEquals('- / 100.00', $result['grade']['usergrade']);
 132  
 133          $this->assertArrayHasKey('maxgrade', $result['grade']);
 134          $this->assertIsInt($result['grade']['maxgrade']);
 135          $this->assertEquals(100, $result['grade']['maxgrade']);
 136  
 137          $this->assertArrayHasKey('gradedby', $result['grade']);
 138          $this->assertEquals(null, $result['grade']['gradedby']);
 139  
 140          $this->assertArrayHasKey('criteria', $result['grade']);
 141          $criteria = $result['grade']['criteria'];
 142          $this->assertCount(count($definition->rubric_criteria), $criteria);
 143          foreach ($criteria as $criterion) {
 144              $this->assertArrayHasKey('id', $criterion);
 145              $criterionid = $criterion['id'];
 146              $sourcecriterion = $definition->rubric_criteria[$criterionid];
 147  
 148              $this->assertArrayHasKey('description', $criterion);
 149              $this->assertEquals($sourcecriterion['description'], $criterion['description']);
 150  
 151              $this->assertArrayHasKey('levels', $criterion);
 152  
 153              $levels = $criterion['levels'];
 154              foreach ($levels as $level) {
 155                  $levelid = $level['id'];
 156                  if (!isset($levelid)) {
 157                      continue;
 158                  }
 159                  $sourcelevel = $sourcecriterion['levels'][$levelid];
 160  
 161                  $this->assertArrayHasKey('criterionid', $level);
 162                  $this->assertEquals($criterionid, $level['criterionid']);
 163  
 164                  $this->assertArrayHasKey('checked', $level);
 165  
 166                  $this->assertArrayHasKey('definition', $level);
 167                  $this->assertEquals($sourcelevel['definition'], $level['definition']);
 168  
 169                  $this->assertArrayHasKey('score', $level);
 170                  $this->assertEquals($sourcelevel['score'], $level['score']);
 171              }
 172          }
 173      }
 174  
 175      /**
 176       * Ensure that an execute against the correct grading method returns the current state of the user.
 177       */
 178      public function test_execute_fetch_graded(): void {
 179          $this->resetAfterTest();
 180  
 181          [
 182              'forum' => $forum,
 183              'controller' => $controller,
 184              'definition' => $definition,
 185              'student' => $student,
 186              'teacher' => $teacher,
 187          ] = $this->get_test_data();
 188  
 189          $this->execute_and_assert_fetch($forum, $controller, $definition, $teacher, $teacher, $student);
 190      }
 191  
 192      /**
 193       * Class mates should not get other's grades.
 194       */
 195      public function test_execute_fetch_does_not_return_data_to_other_students(): void {
 196          $this->resetAfterTest();
 197  
 198          [
 199              'forum' => $forum,
 200              'controller' => $controller,
 201              'definition' => $definition,
 202              'student' => $student,
 203              'teacher' => $teacher,
 204              'course' => $course,
 205          ] = $this->get_test_data();
 206  
 207          $evilstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');
 208  
 209          $this->expectException(\required_capability_exception::class);
 210          $this->execute_and_assert_fetch($forum, $controller, $definition, $evilstudent, $teacher, $student);
 211      }
 212  
 213      /**
 214       * Grades can be returned to graded user.
 215       */
 216      public function test_execute_fetch_return_data_to_graded_user(): void {
 217          $this->resetAfterTest();
 218  
 219          [
 220              'forum' => $forum,
 221              'controller' => $controller,
 222              'definition' => $definition,
 223              'student' => $student,
 224              'teacher' => $teacher,
 225          ] = $this->get_test_data();
 226  
 227          $this->execute_and_assert_fetch($forum, $controller, $definition, $student, $teacher, $student);
 228      }
 229  
 230      /**
 231       * Executes and performs all the assertions of the fetch method with the given parameters.
 232       */
 233      private function execute_and_assert_fetch ($forum, $controller, $definition, $fetcheruser, $grader, $gradeduser) {
 234          $generator = \testing_util::get_data_generator();
 235          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 236  
 237          $this->setUser($grader);
 238  
 239          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 240          $grade = $gradeitem->get_grade_for_user($gradeduser, $grader);
 241          $instance = $gradeitem->get_advanced_grading_instance($grader, $grade);
 242  
 243          $submissiondata = $rubricgenerator->get_test_form_data($controller, (int) $gradeduser->id,
 244              0, 'Too many mistakes. Please try again.',
 245              2, 'Great number of pictures. Well done.'
 246          );
 247  
 248          $gradeitem->store_grade_from_formdata($gradeduser, $grader, (object) [
 249              'instanceid' => $instance->get_id(),
 250              'advancedgrading' => $submissiondata,
 251          ]);
 252  
 253          $this->setUser($fetcheruser);
 254  
 255          $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id);
 256          $result = external_api::clean_returnvalue(fetch::execute_returns(), $result);
 257  
 258          $this->assertIsArray($result);
 259          $this->assertArrayHasKey('templatename', $result);
 260  
 261          $this->assertEquals('gradingform_rubric/grades/grader/gradingpanel', $result['templatename']);
 262  
 263          $this->assertArrayHasKey('warnings', $result);
 264          $this->assertIsArray($result['warnings']);
 265          $this->assertEmpty($result['warnings']);
 266  
 267          // Test the grade array items.
 268          $this->assertArrayHasKey('grade', $result);
 269          $this->assertIsArray($result['grade']);
 270          $this->assertIsInt($result['grade']['timecreated']);
 271  
 272          $this->assertArrayHasKey('timemodified', $result['grade']);
 273          $this->assertIsInt($result['grade']['timemodified']);
 274  
 275          $this->assertArrayHasKey('usergrade', $result['grade']);
 276          $this->assertEquals('50.00 / 100.00', $result['grade']['usergrade']);
 277  
 278          $this->assertArrayHasKey('maxgrade', $result['grade']);
 279          $this->assertIsInt($result['grade']['maxgrade']);
 280          $this->assertEquals(100, $result['grade']['maxgrade']);
 281  
 282          $this->assertArrayHasKey('gradedby', $result['grade']);
 283          $this->assertEquals(fullname($grader), $result['grade']['gradedby']);
 284  
 285          $this->assertArrayHasKey('criteria', $result['grade']);
 286          $criteria = $result['grade']['criteria'];
 287          $this->assertCount(count($definition->rubric_criteria), $criteria);
 288          foreach ($criteria as $criterion) {
 289              $this->assertArrayHasKey('id', $criterion);
 290              $criterionid = $criterion['id'];
 291              $sourcecriterion = $definition->rubric_criteria[$criterionid];
 292  
 293              $this->assertArrayHasKey('description', $criterion);
 294              $this->assertEquals($sourcecriterion['description'], $criterion['description']);
 295  
 296              $this->assertArrayHasKey('remark', $criterion);
 297  
 298              $this->assertArrayHasKey('levels', $criterion);
 299  
 300              $levels = $criterion['levels'];
 301              foreach ($levels as $level) {
 302                  $levelid = $level['id'];
 303                  if (!isset($levelid)) {
 304                      continue;
 305                  }
 306                  $sourcelevel = $sourcecriterion['levels'][$levelid];
 307  
 308                  $this->assertArrayHasKey('criterionid', $level);
 309                  $this->assertEquals($criterionid, $level['criterionid']);
 310  
 311                  $this->assertArrayHasKey('checked', $level);
 312  
 313                  $this->assertArrayHasKey('definition', $level);
 314                  $this->assertEquals($sourcelevel['definition'], $level['definition']);
 315  
 316                  $this->assertArrayHasKey('score', $level);
 317                  $this->assertEquals($sourcelevel['score'], $level['score']);
 318              }
 319  
 320          }
 321  
 322          $this->assertEquals(1, $criteria[0]['levels'][1]['checked']);
 323          $this->assertEquals('Too many mistakes. Please try again.', $criteria[0]['remark']);
 324          $this->assertEquals(1, $criteria[1]['levels'][3]['checked']);
 325          $this->assertEquals('Great number of pictures. Well done.', $criteria[1]['remark']);
 326      }
 327  
 328      /**
 329       * Get a forum instance.
 330       *
 331       * @param array $config
 332       * @return forum_entity
 333       */
 334      protected function get_forum_instance(array $config = []): forum_entity {
 335          $this->resetAfterTest();
 336  
 337          $datagenerator = $this->getDataGenerator();
 338          $course = $datagenerator->create_course();
 339          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id, 'grade_forum' => 100]));
 340  
 341          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 342          $vault = $vaultfactory->get_forum_vault();
 343  
 344          return $vault->get_from_id((int) $forum->id);
 345      }
 346  
 347      /**
 348       * Get test data for forums graded using a rubric.
 349       *
 350       * @return array
 351       */
 352      protected function get_test_data(): array {
 353          global $DB;
 354  
 355          $this->resetAfterTest();
 356  
 357          $generator = \testing_util::get_data_generator();
 358          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 359  
 360          $forum = $this->get_forum_instance();
 361          $course = $forum->get_course_record();
 362          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 363          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 364  
 365          $this->setUser($teacher);
 366  
 367          $controller = $rubricgenerator->get_test_rubric($forum->get_context(), 'forum', 'forum');
 368          $definition = $controller->get_definition();
 369  
 370          // In the situation of mod_forum this would be the id from forum_grades.
 371          $itemid = 1;
 372          $instance = $controller->create_instance($student->id, $itemid);
 373  
 374          $data = $this->get_test_form_data(
 375              $controller,
 376              $itemid,
 377              1, 'This user made several mistakes.',
 378              0, 'Please add more pictures.'
 379          );
 380  
 381          // Update this instance with data.
 382          $instance->update($data);
 383  
 384          return [
 385              'forum' => $forum,
 386              'controller' => $controller,
 387              'definition' => $definition,
 388              'student' => $student,
 389              'teacher' => $teacher,
 390              'course' => $course,
 391          ];
 392      }
 393      /**
 394       * Fetch a set of sample data.
 395       *
 396       * @param \gradingform_rubric_controller $controller
 397       * @param int $itemid
 398       * @param float $spellingscore
 399       * @param string $spellingremark
 400       * @param float $picturescore
 401       * @param string $pictureremark
 402       * @return array
 403       */
 404      protected function get_test_form_data(
 405          \gradingform_rubric_controller $controller,
 406          int $itemid,
 407          float $spellingscore,
 408          string $spellingremark,
 409          float $picturescore,
 410          string $pictureremark
 411      ): array {
 412          $generator = \testing_util::get_data_generator();
 413          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 414  
 415          return $rubricgenerator->get_test_form_data(
 416              $controller,
 417              $itemid,
 418              $spellingscore,
 419              $spellingremark,
 420              $picturescore,
 421              $pictureremark
 422          );
 423      }
 424  }