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.
   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   * Tests for the the Forum gradeitem.
  19   *
  20   * @package    mod_forum
  21   * @copyright Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tests\mod_forum\grades;
  26  
  27  use core_grades\component_gradeitem;
  28  use mod_forum\grades\forum_gradeitem as gradeitem;
  29  use mod_forum\local\entities\forum as forum_entity;
  30  use gradingform_controller;
  31  use mod_forum\grades\forum_gradeitem;
  32  
  33  require_once (__DIR__ . '/generator_trait.php');
  34  
  35  /**
  36   * Tests for the the Forum gradeitem.
  37   *
  38   * @package    mod_forum
  39   * @copyright Andrew Nicols <andrew@nicols.co.uk>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class forum_gradeitem_test extends \advanced_testcase {
  43      use \mod_forum_tests_generator_trait;
  44  
  45      /**
  46       * Test fetching of a grade for a user when the grade has been created.
  47       */
  48      public function test_get_grade_for_user_exists(): void {
  49          $forum = $this->get_forum_instance([
  50              'grade_forum' => 0,
  51          ]);
  52          $course = $forum->get_course_record();
  53          [$student] = $this->helper_create_users($course, 1);
  54          [$grader] = $this->helper_create_users($course, 1, 'editingteacher');
  55  
  56          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  57  
  58          // Create the grade record.
  59          $grade = $gradeitem->create_empty_grade($student, $grader);
  60  
  61          $this->assertIsObject($grade);
  62          $this->assertEquals($student->id, $grade->userid);
  63      }
  64  
  65      /**
  66       * Test fetching of a grade for a user when the grade has been created.
  67       */
  68      public function test_user_has_grade(): void {
  69          $forum = $this->get_forum_instance([
  70              'grade_forum' => 100,
  71          ]);
  72          $course = $forum->get_course_record();
  73          [$student] = $this->helper_create_users($course, 1);
  74          [$grader] = $this->helper_create_users($course, 1, 'editingteacher');
  75  
  76          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  77  
  78          $hasgrade = $gradeitem->user_has_grade($student);
  79          $this->assertEquals(false, $hasgrade);
  80          // Create the grade record.
  81          $gradeitem->create_empty_grade($student, $grader);
  82  
  83          $hasgrade = $gradeitem->user_has_grade($student);
  84          $this->assertEquals(false, $hasgrade);
  85  
  86          // Store a new value.
  87          $gradeitem->store_grade_from_formdata($student, $grader, (object) ['grade' => 97]);
  88          $hasgrade = $gradeitem->user_has_grade($student);
  89          $this->assertEquals(true, $hasgrade);
  90      }
  91  
  92      /**
  93       * Ensure that it is possible to get, and update, a grade for a user when simple direct grading is in use.
  94       */
  95      public function test_get_and_store_grade_for_user_with_simple_direct_grade(): void {
  96          $forum = $this->get_forum_instance([
  97              'grade_forum' => 100,
  98          ]);
  99          $course = $forum->get_course_record();
 100          [$student] = $this->helper_create_users($course, 1);
 101          [$grader] = $this->helper_create_users($course, 1, 'editingteacher');
 102  
 103          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 104  
 105          // Create the grade record.
 106          $grade = $gradeitem->create_empty_grade($student, $grader);
 107  
 108          $this->assertIsObject($grade);
 109          $this->assertEquals($student->id, $grade->userid);
 110  
 111          // Store a new value.
 112          $gradeitem->store_grade_from_formdata($student, $grader, (object) ['grade' => 97]);
 113      }
 114  
 115      /**
 116       * Ensure that it is possible to get, and update, a grade for a user when a rubric is in use.
 117       */
 118      public function test_get_and_store_grade_for_user_with_rubric(): void {
 119          global $DB;
 120  
 121          $this->resetAfterTest();
 122  
 123          $generator = \testing_util::get_data_generator();
 124          $gradinggenerator = $generator->get_plugin_generator('core_grading');
 125          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 126  
 127          $forum = $this->get_forum_instance([
 128              'grade_forum' => 100,
 129          ]);
 130          $course = $forum->get_course_record();
 131          $context = $forum->get_context();
 132          [$student] = $this->helper_create_users($course, 1);
 133          [$grader] = $this->helper_create_users($course, 1, 'editingteacher');
 134          [$editor] = $this->helper_create_users($course, 1, 'editingteacher');
 135  
 136          // Note: This must be run as a user because it messes with file uploads and drafts.
 137          $this->setUser($editor);
 138  
 139          $controller = $rubricgenerator->get_test_rubric($context, 'mod_forum', 'forum');
 140  
 141          // Create the forum_gradeitem object that we'll be testing.
 142          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 143  
 144          // The current grade should be null initially.
 145          $this->assertCount(0, $DB->get_records('forum_grades'));
 146          $grade = $gradeitem->get_grade_for_user($student, $grader);
 147          $instance = $gradeitem->get_advanced_grading_instance($grader, $grade);
 148  
 149          $this->assertIsObject($grade);
 150          $this->assertEquals($student->id, $grade->userid);
 151          $this->assertEquals($forum->get_id(), $grade->forum);
 152  
 153          $rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
 154          $data = $rubricgenerator->get_submitted_form_data($controller, $grade->id, [
 155              'Spelling is important' => [
 156                  'score' => 2,
 157                  'remark' => 'Abracadabra',
 158              ],
 159              'Pictures' => [
 160                  'score' => 1,
 161                  'remark' => 'More than one picture',
 162              ],
 163          ]);
 164  
 165          // Store a new value.
 166          $gradeitem->store_grade_from_formdata($student, $grader, (object) [
 167              'instanceid' => $instance->get_id(),
 168              'advancedgrading' => $data,
 169          ]);
 170      }
 171  
 172      /**
 173       * Get a forum instance.
 174       *
 175       * @param array $config
 176       * @return forum_entity
 177       */
 178      protected function get_forum_instance(array $config = []): forum_entity {
 179          $this->resetAfterTest();
 180  
 181          $datagenerator = $this->getDataGenerator();
 182          $course = $datagenerator->create_course();
 183          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
 184  
 185          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 186          $vault = $vaultfactory->get_forum_vault();
 187  
 188          return $vault->get_from_id((int) $forum->id);
 189      }
 190  }