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 core_grades\grades\grader\gradingpanel\scale\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   core_grades
  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 fetch_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          fetch::execute('mod_invalid', 1, 'foo', 2);
  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          fetch::execute('mod_forum', 1, 'foo', 2);
  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              // Negative numbers mean a scale.
  77              'grade_forum' => 5,
  78          ]);
  79          $course = $forum->get_course_record();
  80          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  81          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  82          $this->setUser($teacher);
  83  
  84          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
  85  
  86          $this->expectException(moodle_exception::class);
  87          $this->expectExceptionMessage("not configured for grading with scales");
  88          fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
  89      }
  90  
  91      /**
  92       * Ensure that an execute against the correct grading method returns the current state of the user.
  93       */
  94      public function test_execute_fetch_empty(): void {
  95          $this->resetAfterTest();
  96  
  97          $options = [
  98              'A',
  99              'B',
 100              'C'
 101          ];
 102          $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]);
 103  
 104          $forum = $this->get_forum_instance([
 105              // Negative numbers mean a scale.
 106              'grade_forum' => -1 * $scale->id
 107          ]);
 108          $course = $forum->get_course_record();
 109          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 110          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 111          $this->setUser($teacher);
 112  
 113          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 114  
 115          $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
 116          $result = external_api::clean_returnvalue(fetch::execute_returns(), $result);
 117  
 118          $this->assertIsArray($result);
 119          $this->assertArrayHasKey('templatename', $result);
 120  
 121          $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']);
 122  
 123          $this->assertArrayHasKey('warnings', $result);
 124          $this->assertIsArray($result['warnings']);
 125          $this->assertEmpty($result['warnings']);
 126  
 127          // Test the grade array items.
 128          $this->assertArrayHasKey('grade', $result);
 129          $this->assertIsArray($result['grade']);
 130  
 131          $this->assertIsInt($result['grade']['timecreated']);
 132          $this->assertArrayHasKey('timemodified', $result['grade']);
 133          $this->assertIsInt($result['grade']['timemodified']);
 134  
 135          $this->assertArrayHasKey('usergrade', $result['grade']);
 136          $this->assertEquals('-', $result['grade']['usergrade']);
 137  
 138          $this->assertArrayHasKey('maxgrade', $result['grade']);
 139          $this->assertIsInt($result['grade']['maxgrade']);
 140          $this->assertEquals(3, $result['grade']['maxgrade']);
 141  
 142          $this->assertArrayHasKey('gradedby', $result['grade']);
 143          $this->assertEquals(null, $result['grade']['gradedby']);
 144  
 145          $this->assertArrayHasKey('options', $result['grade']);
 146          $this->assertCount(count($options), $result['grade']['options']);
 147          rsort($options);
 148          foreach ($options as $index => $option) {
 149              $this->assertArrayHasKey($index, $result['grade']['options']);
 150  
 151              $returnedoption = $result['grade']['options'][$index];
 152              $this->assertArrayHasKey('value', $returnedoption);
 153              $this->assertEquals(3 - $index, $returnedoption['value']);
 154  
 155              $this->assertArrayHasKey('title', $returnedoption);
 156              $this->assertEquals($option, $returnedoption['title']);
 157  
 158              $this->assertArrayHasKey('selected', $returnedoption);
 159              $this->assertFalse($returnedoption['selected']);
 160          }
 161      }
 162  
 163      /**
 164       * Ensure that an execute against the correct grading method returns the current state of the user.
 165       */
 166      public function test_execute_fetch_graded(): void {
 167          $this->resetAfterTest();
 168  
 169          $options = [
 170              'A',
 171              'B',
 172              'C'
 173          ];
 174          $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]);
 175  
 176          $forum = $this->get_forum_instance([
 177              // Negative numbers mean a scale.
 178              'grade_forum' => -1 * $scale->id
 179          ]);
 180          $course = $forum->get_course_record();
 181          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 182          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 183  
 184          $this->execute_and_assert_fetch($forum, $options, $scale, $teacher, $teacher, $student);
 185      }
 186  
 187      /**
 188       * Class mates should not get other's grades.
 189       */
 190      public function test_execute_fetch_does_not_return_data_to_other_students(): void {
 191          $this->resetAfterTest();
 192  
 193          $options = [
 194              'A',
 195              'B',
 196              'C'
 197          ];
 198          $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]);
 199  
 200          $forum = $this->get_forum_instance([
 201              // Negative numbers mean a scale.
 202              'grade_forum' => -1 * $scale->id
 203          ]);
 204          $course = $forum->get_course_record();
 205          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 206          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 207          $evilstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');
 208  
 209          $this->expectException(\required_capability_exception::class);
 210          $this->execute_and_assert_fetch($forum, $options, $scale, $evilstudent, $teacher, $student);
 211      }
 212  
 213      /**
 214       * Grades can be returned to graded user.
 215       */
 216      public function test_execute_fetch_return_data_to_graded_user(): void {
 217          $this->resetAfterTest();
 218  
 219          $options = [
 220              'A',
 221              'B',
 222              'C'
 223          ];
 224          $scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]);
 225  
 226          $forum = $this->get_forum_instance([
 227              // Negative numbers mean a scale.
 228              'grade_forum' => -1 * $scale->id
 229          ]);
 230          $course = $forum->get_course_record();
 231          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
 232          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 233  
 234          $this->execute_and_assert_fetch($forum, $options, $scale, $student, $teacher, $student);
 235      }
 236  
 237      /**
 238       * Executes the fetch method with the given users and returns the result.
 239       */
 240      private function execute_and_assert_fetch ($forum, $options, $scale, $fetcheruser, $grader, $gradeduser) {
 241  
 242          $this->setUser($grader);
 243  
 244          $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
 245          $gradeitem->store_grade_from_formdata($gradeduser, $grader, (object) [
 246              'grade' => 2,
 247          ]);
 248  
 249          $this->setUser($fetcheruser);
 250  
 251          $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id);
 252          $result = external_api::clean_returnvalue(fetch::execute_returns(), $result);
 253  
 254          $this->assertIsArray($result);
 255          $this->assertArrayHasKey('templatename', $result);
 256  
 257          $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']);
 258  
 259          $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id);
 260          $result = external_api::clean_returnvalue(fetch::execute_returns(), $result);
 261  
 262          $this->assertIsArray($result);
 263          $this->assertArrayHasKey('templatename', $result);
 264  
 265          $this->assertEquals('core_grades/grades/grader/gradingpanel/scale', $result['templatename']);
 266  
 267          $this->assertArrayHasKey('warnings', $result);
 268          $this->assertIsArray($result['warnings']);
 269          $this->assertEmpty($result['warnings']);
 270  
 271          // Test the grade array items.
 272          $this->assertArrayHasKey('grade', $result);
 273          $this->assertIsArray($result['grade']);
 274  
 275          $this->assertIsInt($result['grade']['timecreated']);
 276          $this->assertArrayHasKey('timemodified', $result['grade']);
 277          $this->assertIsInt($result['grade']['timemodified']);
 278  
 279          $this->assertArrayHasKey('usergrade', $result['grade']);
 280          $this->assertEquals('B', $result['grade']['usergrade']);
 281  
 282          $this->assertArrayHasKey('maxgrade', $result['grade']);
 283          $this->assertIsInt($result['grade']['maxgrade']);
 284          $this->assertEquals(3, $result['grade']['maxgrade']);
 285  
 286          $this->assertArrayHasKey('gradedby', $result['grade']);
 287          $this->assertEquals(fullname($grader), $result['grade']['gradedby']);
 288  
 289          $this->assertArrayHasKey('options', $result['grade']);
 290          $this->assertCount(count($options), $result['grade']['options']);
 291          rsort($options);
 292          foreach ($options as $index => $option) {
 293              $this->assertArrayHasKey($index, $result['grade']['options']);
 294  
 295              $returnedoption = $result['grade']['options'][$index];
 296              $this->assertArrayHasKey('value', $returnedoption);
 297              $this->assertEquals(3 - $index, $returnedoption['value']);
 298  
 299              $this->assertArrayHasKey('title', $returnedoption);
 300              $this->assertEquals($option, $returnedoption['title']);
 301  
 302              $this->assertArrayHasKey('selected', $returnedoption);
 303          }
 304  
 305          // The grade was 2, which relates to the middle option.
 306          $this->assertFalse($result['grade']['options'][0]['selected']);
 307          $this->assertTrue($result['grade']['options'][1]['selected']);
 308          $this->assertFalse($result['grade']['options'][2]['selected']);
 309      }
 310  
 311      /**
 312       * Get a forum instance.
 313       *
 314       * @param array $config
 315       * @return forum_entity
 316       */
 317      protected function get_forum_instance(array $config = []): forum_entity {
 318          $this->resetAfterTest();
 319  
 320          $datagenerator = $this->getDataGenerator();
 321          $course = $datagenerator->create_course();
 322          $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
 323  
 324          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 325          $vault = $vaultfactory->get_forum_vault();
 326  
 327          return $vault->get_from_id((int) $forum->id);
 328      }
 329  }