Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [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   * Overview grade report functions unit tests
  19   *
  20   * @package    gradereport_overview
  21   * @category   external
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  31  
  32  /**
  33   * Overview grade report functions unit tests
  34   *
  35   * @package    gradereport_overview
  36   * @category   external
  37   * @copyright  2015 Juan Leyva <juan@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class gradereport_overview_externallib_testcase extends externallib_advanced_testcase {
  41  
  42      /**
  43       * Set up for every test
  44       */
  45      public function setUp(): void {
  46          global $DB;
  47          $this->resetAfterTest(true);
  48  
  49          $s1grade1 = 80;
  50          $s1grade2 = 40;
  51          $s2grade = 60;
  52  
  53          $this->course1 = $this->getDataGenerator()->create_course();
  54          $this->course2 = $this->getDataGenerator()->create_course();
  55  
  56          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  57          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  58          $this->student1 = $this->getDataGenerator()->create_user();
  59          $this->teacher = $this->getDataGenerator()->create_user();
  60          $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course1->id, $teacherrole->id);
  61          $this->getDataGenerator()->enrol_user($this->student1->id, $this->course1->id, $studentrole->id);
  62          $this->getDataGenerator()->enrol_user($this->student1->id, $this->course2->id, $studentrole->id);
  63  
  64          $this->student2 = $this->getDataGenerator()->create_user();
  65          $this->getDataGenerator()->enrol_user($this->student2->id, $this->course1->id, $studentrole->id);
  66          $this->getDataGenerator()->enrol_user($this->student2->id, $this->course2->id, $studentrole->id);
  67  
  68          $assignment1 = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $this->course1->id));
  69          $assignment2 = $this->getDataGenerator()->create_module('assign', array('name' => "Test assign", 'course' => $this->course2->id));
  70          $modcontext1 = get_coursemodule_from_instance('assign', $assignment1->id, $this->course1->id);
  71          $modcontext2 = get_coursemodule_from_instance('assign', $assignment2->id, $this->course2->id);
  72          $assignment1->cmidnumber = $modcontext1->id;
  73          $assignment2->cmidnumber = $modcontext2->id;
  74  
  75          $this->student1grade1 = array('userid' => $this->student1->id, 'rawgrade' => $s1grade1);
  76          $this->student1grade2 = array('userid' => $this->student1->id, 'rawgrade' => $s1grade2);
  77          $this->student2grade = array('userid' => $this->student2->id, 'rawgrade' => $s2grade);
  78          $studentgrades = array($this->student1->id => $this->student1grade1, $this->student2->id => $this->student2grade);
  79          assign_grade_item_update($assignment1, $studentgrades);
  80          $studentgrades = array($this->student1->id => $this->student1grade2);
  81          assign_grade_item_update($assignment2, $studentgrades);
  82  
  83          grade_get_setting($this->course1->id, 'report_overview_showrank', 1);
  84      }
  85  
  86      /**
  87       * Test get_course_grades function case student
  88       */
  89      public function test_get_course_grades_student() {
  90  
  91          // A user can see his own grades in both courses.
  92          $this->setUser($this->student1);
  93          $studentgrades = gradereport_overview_external::get_course_grades();
  94          $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades);
  95  
  96          $this->assertCount(0, $studentgrades['warnings']);
  97          $this->assertCount(2, $studentgrades['grades']);
  98          foreach ($studentgrades['grades'] as $grade) {
  99              if ($grade['courseid'] == $this->course1->id) {
 100                  $this->assertEquals(80.00, $grade['grade']);
 101                  $this->assertEquals(80.0000, $grade['rawgrade']);
 102                  $this->assertEquals(1, $grade['rank']);
 103              } else {
 104                  $this->assertEquals(40.00, $grade['grade']);
 105                  $this->assertEquals(40.0000, $grade['rawgrade']);
 106                  $this->assertArrayNotHasKey('rank', $grade);
 107              }
 108          }
 109  
 110          // Second student, no grade in one course.
 111          $this->setUser($this->student2);
 112          $studentgrades = gradereport_overview_external::get_course_grades();
 113          $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades);
 114  
 115          $this->assertCount(0, $studentgrades['warnings']);
 116          $this->assertCount(2, $studentgrades['grades']);
 117          foreach ($studentgrades['grades'] as $grade) {
 118              if ($grade['courseid'] == $this->course1->id) {
 119                  $this->assertEquals(60.00, $grade['grade']);
 120                  $this->assertEquals(60.0000, $grade['rawgrade']);
 121                  $this->assertEquals(2, $grade['rank']);
 122              } else {
 123                  $this->assertEquals('-', $grade['grade']);
 124                  $this->assertEmpty($grade['rawgrade']);
 125                  $this->assertArrayNotHasKey('rank', $grade);
 126              }
 127          }
 128      }
 129  
 130      /**
 131       * Test get_course_grades function case admin
 132       */
 133      public function test_get_course_grades_admin() {
 134  
 135          // A admin must see all student grades.
 136          $this->setAdminUser();
 137  
 138          $studentgrades = gradereport_overview_external::get_course_grades($this->student1->id);
 139          $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades);
 140          $this->assertCount(0, $studentgrades['warnings']);
 141          $this->assertCount(2, $studentgrades['grades']);
 142          foreach ($studentgrades['grades'] as $grade) {
 143              if ($grade['courseid'] == $this->course1->id) {
 144                  $this->assertEquals(80.00, $grade['grade']);
 145                  $this->assertEquals(80.0000, $grade['rawgrade']);
 146              } else {
 147                  $this->assertEquals(40.00, $grade['grade']);
 148                  $this->assertEquals(40.0000, $grade['rawgrade']);
 149              }
 150          }
 151  
 152          $studentgrades = gradereport_overview_external::get_course_grades($this->student2->id);
 153          $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades);
 154          $this->assertCount(0, $studentgrades['warnings']);
 155          $this->assertCount(2, $studentgrades['grades']);
 156  
 157          // Admins don't see grades.
 158          $studentgrades = gradereport_overview_external::get_course_grades();
 159          $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades);
 160          $this->assertCount(0, $studentgrades['warnings']);
 161          $this->assertCount(0, $studentgrades['grades']);
 162      }
 163  
 164      /**
 165       * Test get_course_grades function case teacher
 166       */
 167      public function test_get_course_grades_teacher() {
 168          // Teachers don't see grades.
 169          $this->setUser($this->teacher);
 170  
 171          $studentgrades = gradereport_overview_external::get_course_grades();
 172          $studentgrades = external_api::clean_returnvalue(gradereport_overview_external::get_course_grades_returns(), $studentgrades);
 173          $this->assertCount(0, $studentgrades['warnings']);
 174          $this->assertCount(0, $studentgrades['grades']);
 175      }
 176  
 177      /**
 178       * Test get_course_grades function case incorrect permissions
 179       */
 180      public function test_get_course_grades_permissions() {
 181          // Student can't see other student grades.
 182          $this->setUser($this->student2);
 183  
 184          $this->expectException('required_capability_exception');
 185          $studentgrade = gradereport_overview_external::get_course_grades($this->student1->id);
 186      }
 187  
 188      /**
 189       * Test view_grade_report function
 190       */
 191      public function test_view_grade_report() {
 192          global $USER;
 193  
 194          // Redirect events to the sink, so we can recover them later.
 195          $sink = $this->redirectEvents();
 196  
 197          $this->setUser($this->student1);
 198          $result = gradereport_overview_external::view_grade_report($this->course1->id);
 199          $result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result);
 200          $events = $sink->get_events();
 201          $this->assertCount(1, $events);
 202          $event = reset($events);
 203  
 204          // Check the event details are correct.
 205          $this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event);
 206          $this->assertEquals(context_course::instance($this->course1->id), $event->get_context());
 207          $this->assertEquals($USER->id, $event->get_data()['relateduserid']);
 208  
 209          $this->setUser($this->teacher);
 210          $result = gradereport_overview_external::view_grade_report($this->course1->id, $this->student1->id);
 211          $result = external_api::clean_returnvalue(gradereport_overview_external::view_grade_report_returns(), $result);
 212          $events = $sink->get_events();
 213          $event = reset($events);
 214          $sink->close();
 215  
 216          // Check the event details are correct.
 217          $this->assertInstanceOf('\gradereport_overview\event\grade_report_viewed', $event);
 218          $this->assertEquals(context_course::instance($this->course1->id), $event->get_context());
 219          $this->assertEquals($this->student1->id, $event->get_data()['relateduserid']);
 220      }
 221  
 222      /**
 223       * Test view_grade_report_permissions function
 224       */
 225      public function test_view_grade_report_permissions() {
 226          $this->setUser($this->student2);
 227  
 228          $this->expectException('moodle_exception');
 229          $studentgrade = gradereport_overview_external::view_grade_report($this->course1->id, $this->student1->id);
 230      }
 231  }