Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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_guide\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_guide
  32   * @category  test
  33   * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  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          $this->expectExceptionMessage("not configured for advanced grading with a marking guide");
  81          store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, 'formdata');
  82      }
  83  
  84      /**
  85       * Ensure that an execute against a different grading method is rejected.
  86       */
  87      public function test_execute_disabled(): void {
  88          $this->resetAfterTest();
  89  
  90          $forum = $this->get_forum_instance();
  91          $course = $forum->get_course_record();
  92          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  93          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  94          $this->setUser($teacher);
  95  
  96          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  97  
  98          $this->expectException(moodle_exception::class);
  99          $this->expectExceptionMessage("Grading is not enabled");
 100          store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, 'formdata');
 101      }
 102  
 103      /**
 104       * Ensure that an execute against the correct grading method returns the current state of the user.
 105       */
 106      public function test_execute_store_graded(): void {
 107          $this->resetAfterTest();
 108          $generator = \testing_util::get_data_generator();
 109          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 110  
 111          [
 112              'forum' => $forum,
 113              'controller' => $controller,
 114              'definition' => $definition,
 115              'student' => $student,
 116              'teacher' => $teacher,
 117          ] = $this->get_test_data();
 118  
 119          $this->setUser($teacher);
 120  
 121          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 122          $grade = $gradeitem->get_grade_for_user($student, $teacher);
 123          $instance = $gradeitem->get_advanced_grading_instance($teacher, $grade);
 124  
 125          $submissiondata = $guidegenerator->get_test_form_data($controller, (int) $student->id,
 126              10, 'Propper good speling',
 127              0, 'ASCII art is not a picture'
 128          );
 129  
 130          $formdata = http_build_query((object) [
 131              'instanceid' => $instance->get_id(),
 132              'advancedgrading' => $submissiondata,
 133          ], '', '&');
 134  
 135          $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, false, $formdata);
 136          $result = external_api::clean_returnvalue(store::execute_returns(), $result);
 137  
 138          $this->assertIsArray($result);
 139          $this->assertArrayHasKey('templatename', $result);
 140  
 141          $this->assertEquals('gradingform_guide/grades/grader/gradingpanel', $result['templatename']);
 142  
 143          $this->assertArrayHasKey('warnings', $result);
 144          $this->assertIsArray($result['warnings']);
 145          $this->assertEmpty($result['warnings']);
 146  
 147          // Test the grade array items.
 148          $this->assertArrayHasKey('grade', $result);
 149          $this->assertIsArray($result['grade']);
 150          $this->assertIsInt($result['grade']['timecreated']);
 151  
 152          $this->assertArrayHasKey('timemodified', $result['grade']);
 153          $this->assertIsInt($result['grade']['timemodified']);
 154  
 155          $this->assertArrayHasKey('usergrade', $result['grade']);
 156          $this->assertEquals('0.50 / 2.00', $result['grade']['usergrade']);
 157  
 158          $this->assertArrayHasKey('maxgrade', $result['grade']);
 159          $this->assertIsInt($result['grade']['maxgrade']);
 160          $this->assertEquals(2, $result['grade']['maxgrade']);
 161  
 162          $this->assertArrayHasKey('gradedby', $result['grade']);
 163          $this->assertEquals(fullname($teacher), $result['grade']['gradedby']);
 164  
 165          $this->assertArrayHasKey('criterion', $result['grade']);
 166          $criteria = $result['grade']['criterion'];
 167          $this->assertCount(count($definition->guide_criteria), $criteria);
 168          foreach ($criteria as $criterion) {
 169              $this->assertArrayHasKey('id', $criterion);
 170              $criterionid = $criterion['id'];
 171              $sourcecriterion = $definition->guide_criteria[$criterionid];
 172  
 173              $this->assertArrayHasKey('name', $criterion);
 174              $this->assertEquals($sourcecriterion['shortname'], $criterion['name']);
 175  
 176              $this->assertArrayHasKey('maxscore', $criterion);
 177              $this->assertEquals($sourcecriterion['maxscore'], $criterion['maxscore']);
 178  
 179              $this->assertArrayHasKey('description', $criterion);
 180              $this->assertEquals($sourcecriterion['description'], $criterion['description']);
 181  
 182              $this->assertArrayHasKey('descriptionmarkers', $criterion);
 183              $this->assertEquals($sourcecriterion['descriptionmarkers'], $criterion['descriptionmarkers']);
 184  
 185              $this->assertArrayHasKey('score', $criterion);
 186              $this->assertArrayHasKey('remark', $criterion);
 187          }
 188  
 189          $this->assertEquals(10, $criteria[0]['score']);
 190          $this->assertEquals('Propper good speling', $criteria[0]['remark']);
 191          $this->assertEquals(0, $criteria[1]['score']);
 192          $this->assertEquals('ASCII art is not a picture', $criteria[1]['remark']);
 193      }
 194  
 195      /**
 196       * Get a forum instance.
 197       *
 198       * @param array $config
 199       * @return forum_entity
 200       */
 201      protected function get_forum_instance(array $config = []): forum_entity {
 202          $this->resetAfterTest();
 203  
 204          $datagenerator = $this->getDataGenerator();
 205          $course = $datagenerator->create_course();
 206          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
 207  
 208          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 209          $vault = $vaultfactory->get_forum_vault();
 210  
 211          return $vault->get_from_id((int) $forum->id);
 212      }
 213  
 214      /**
 215       * Get test data for forums graded using a marking guide.
 216       *
 217       * @return array
 218       */
 219      protected function get_test_data(): array {
 220          global $DB;
 221  
 222          $this->resetAfterTest();
 223  
 224          $generator = \testing_util::get_data_generator();
 225          $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
 226  
 227          $forum = $this->get_forum_instance();
 228          $course = $forum->get_course_record();
 229          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 230          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 231  
 232          $this->setUser($teacher);
 233          $controller = $guidegenerator->get_test_guide($forum->get_context(), 'forum', 'forum');
 234          $definition = $controller->get_definition();
 235  
 236          $DB->set_field('forum', 'grade_forum', count($definition->guide_criteria), ['id' => $forum->get_id()]);
 237          return [
 238              'forum' => $forum,
 239              'controller' => $controller,
 240              'definition' => $definition,
 241              'student' => $student,
 242              'teacher' => $teacher,
 243          ];
 244      }
 245  }