Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [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_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   gradingform_rubric
  32   * @category  test
  33   * @copyright 2019 Mathew May <mathew.solutions>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class store_test extends advanced_testcase {
  37      /**
  38       * Ensure that an execute with an invalid component is rejected.
  39       */
  40      public function test_execute_invalid_component(): void {
  41          $this->resetAfterTest();
  42          $user = $this->getDataGenerator()->create_user();
  43          $this->setUser($user);
  44  
  45          $this->expectException(coding_exception::class);
  46          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component");
  47          store::execute('mod_invalid', 1, 'foo', 2, false, 'formdata');
  48      }
  49  
  50      /**
  51       * Ensure that an execute with an invalid itemname on a valid component is rejected.
  52       */
  53      public function test_execute_invalid_itemname(): void {
  54          $this->resetAfterTest();
  55          $user = $this->getDataGenerator()->create_user();
  56          $this->setUser($user);
  57  
  58          $this->expectException(coding_exception::class);
  59          $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component");
  60          store::execute('mod_forum', 1, 'foo', 2, false, 'formdata');
  61      }
  62  
  63      /**
  64       * Ensure that an execute against a different grading method is rejected.
  65       */
  66      public function test_execute_incorrect_type(): void {
  67          $this->resetAfterTest();
  68  
  69          $forum = $this->get_forum_instance([
  70              'grade_forum' => 5,
  71          ]);
  72          $course = $forum->get_course_record();
  73          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  74          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  75          $this->setUser($teacher);
  76  
  77          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  78  
  79          $this->expectException(moodle_exception::class);
  80          store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, 'formdata');
  81      }
  82  
  83      /**
  84       * Ensure that an execute against a different grading method is rejected.
  85       */
  86      public function test_execute_disabled(): void {
  87          $this->resetAfterTest();
  88  
  89          $forum = $this->get_forum_instance();
  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("Grading is not enabled");
  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 the correct grading method returns the current state of the user.
 104       */
 105      public function test_execute_store_graded(): void {
 106          $this->resetAfterTest();
 107          $generator = \testing_util::get_data_generator();
 108          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 109  
 110          [
 111              'forum' => $forum,
 112              'controller' => $controller,
 113              'definition' => $definition,
 114              'student' => $student,
 115              'teacher' => $teacher,
 116          ] = $this->get_test_data();
 117  
 118          $this->setUser($teacher);
 119  
 120          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 121          $grade = $gradeitem->get_grade_for_user($student, $teacher);
 122          $instance = $gradeitem->get_advanced_grading_instance($teacher, $grade);
 123  
 124          $submissiondata = $rubricgenerator->get_test_form_data($controller, (int) $student->id,
 125              0, 'Too many mistakes. Please try again.',
 126              2, 'Great number of pictures. Well done.'
 127          );
 128  
 129          $formdata = http_build_query((object) [
 130              'instanceid' => $instance->get_id(),
 131              'advancedgrading' => $submissiondata,
 132          ], '', '&');
 133  
 134          $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, $formdata);
 135          $result = external_api::clean_returnvalue(store::execute_returns(), $result);
 136  
 137          $this->assertIsArray($result);
 138          $this->assertArrayHasKey('templatename', $result);
 139  
 140          $this->assertEquals('gradingform_rubric/grades/grader/gradingpanel', $result['templatename']);
 141  
 142          $this->assertArrayHasKey('warnings', $result);
 143          $this->assertIsArray($result['warnings']);
 144          $this->assertEmpty($result['warnings']);
 145  
 146          // Test the grade array items.
 147          $this->assertArrayHasKey('grade', $result);
 148          $this->assertIsArray($result['grade']);
 149          $this->assertIsInt($result['grade']['timecreated']);
 150  
 151          $this->assertArrayHasKey('timemodified', $result['grade']);
 152          $this->assertIsInt($result['grade']['timemodified']);
 153  
 154          $this->assertArrayHasKey('usergrade', $result['grade']);
 155          $this->assertEquals('1.00 / 2.00', $result['grade']['usergrade']);
 156  
 157          $this->assertArrayHasKey('maxgrade', $result['grade']);
 158          $this->assertIsInt($result['grade']['maxgrade']);
 159          $this->assertEquals(2, $result['grade']['maxgrade']);
 160  
 161          $this->assertArrayHasKey('gradedby', $result['grade']);
 162          $this->assertEquals(fullname($teacher), $result['grade']['gradedby']);
 163  
 164          $this->assertArrayHasKey('criteria', $result['grade']);
 165          $criteria = $result['grade']['criteria'];
 166          $this->assertCount(count($definition->rubric_criteria), $criteria);
 167          foreach ($criteria as $criterion) {
 168              $this->assertArrayHasKey('id', $criterion);
 169              $criterionid = $criterion['id'];
 170              $sourcecriterion = $definition->rubric_criteria[$criterionid];
 171  
 172              $this->assertArrayHasKey('description', $criterion);
 173              $this->assertEquals($sourcecriterion['description'], $criterion['description']);
 174  
 175              $this->assertArrayHasKey('remark', $criterion);
 176  
 177              $this->assertArrayHasKey('levels', $criterion);
 178  
 179              $levels = $criterion['levels'];
 180              foreach ($levels as $level) {
 181                  $levelid = $level['id'];
 182                  if (!isset($levelid)) {
 183                      continue;
 184                  }
 185                  $sourcelevel = $sourcecriterion['levels'][$levelid];
 186  
 187                  $this->assertArrayHasKey('criterionid', $level);
 188                  $this->assertEquals($criterionid, $level['criterionid']);
 189  
 190                  $this->assertArrayHasKey('checked', $level);
 191  
 192                  $this->assertArrayHasKey('definition', $level);
 193                  $this->assertEquals($sourcelevel['definition'], $level['definition']);
 194  
 195                  $this->assertArrayHasKey('score', $level);
 196                  $this->assertEquals($sourcelevel['score'], $level['score']);
 197              }
 198  
 199          }
 200  
 201          $this->assertEquals(1, $criteria[0]['levels'][1]['checked']);
 202          $this->assertEquals('Too many mistakes. Please try again.', $criteria[0]['remark']);
 203          $this->assertEquals(1, $criteria[1]['levels'][3]['checked']);
 204          $this->assertEquals('Great number of pictures. Well done.', $criteria[1]['remark']);
 205      }
 206  
 207      /**
 208       * Get a forum instance.
 209       *
 210       * @param array $config
 211       * @return forum_entity
 212       */
 213      protected function get_forum_instance(array $config = []): forum_entity {
 214          $this->resetAfterTest();
 215  
 216          $datagenerator = $this->getDataGenerator();
 217          $course = $datagenerator->create_course();
 218          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
 219  
 220          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 221          $vault = $vaultfactory->get_forum_vault();
 222  
 223          return $vault->get_from_id((int) $forum->id);
 224      }
 225  
 226      /**
 227       * Get test data for forums graded using a rubric.
 228       *
 229       * @return array
 230       */
 231      protected function get_test_data(): array {
 232          global $DB;
 233  
 234          $this->resetAfterTest();
 235  
 236          $generator = \testing_util::get_data_generator();
 237          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 238  
 239          $forum = $this->get_forum_instance();
 240          $course = $forum->get_course_record();
 241          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 242          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 243  
 244          $this->setUser($teacher);
 245          $controller = $rubricgenerator->get_test_rubric($forum->get_context(), 'forum', 'forum');
 246          $definition = $controller->get_definition();
 247  
 248          $DB->set_field('forum', 'grade_forum', count($definition->rubric_criteria), ['id' => $forum->get_id()]);
 249          return [
 250              'forum' => $forum,
 251              'controller' => $controller,
 252              'definition' => $definition,
 253              'student' => $student,
 254              'teacher' => $teacher,
 255          ];
 256      }
 257  }