Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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