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