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